2022-09-29 16:23:02 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-10-10 16:18:38 +02:00
|
|
|
import { mocked, MockedObject } from "jest-mock";
|
|
|
|
import { ClientEvent, MatrixClient } from "matrix-js-sdk/src/client";
|
2022-10-06 16:27:12 +02:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2022-09-29 16:23:02 +02:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2022-10-10 16:18:38 +02:00
|
|
|
import { SyncState } from "matrix-js-sdk/src/sync";
|
2022-10-25 18:53:31 +02:00
|
|
|
import { waitFor } from "@testing-library/react";
|
2022-09-29 16:23:02 +02:00
|
|
|
|
2022-10-06 16:27:12 +02:00
|
|
|
import BasePlatform from "../src/BasePlatform";
|
|
|
|
import { ElementCall } from "../src/models/Call";
|
2022-09-29 16:23:02 +02:00
|
|
|
import Notifier from "../src/Notifier";
|
2022-10-06 16:27:12 +02:00
|
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
|
|
|
import ToastStore from "../src/stores/ToastStore";
|
2022-10-10 16:18:38 +02:00
|
|
|
import {
|
|
|
|
createLocalNotificationSettingsIfNeeded,
|
|
|
|
getLocalNotificationAccountDataEventType,
|
|
|
|
} from "../src/utils/notifications";
|
2022-10-25 18:53:31 +02:00
|
|
|
import { getMockClientWithEventEmitter, mkEvent, mockClientMethodsUser, mockPlatformPeg } from "./test-utils";
|
2022-10-06 16:27:12 +02:00
|
|
|
import { IncomingCallToast } from "../src/toasts/IncomingCallToast";
|
2022-10-25 18:53:31 +02:00
|
|
|
import { SdkContextClass } from "../src/contexts/SDKContext";
|
|
|
|
import UserActivity from "../src/UserActivity";
|
|
|
|
import Modal from "../src/Modal";
|
|
|
|
import { mkThread } from "./test-utils/threads";
|
|
|
|
import dis from "../src/dispatcher/dispatcher";
|
|
|
|
import { ThreadPayload } from "../src/dispatcher/payloads/ThreadPayload";
|
|
|
|
import { Action } from "../src/dispatcher/actions";
|
2022-09-29 16:23:02 +02:00
|
|
|
|
2022-10-10 16:18:38 +02:00
|
|
|
jest.mock("../src/utils/notifications", () => ({
|
|
|
|
// @ts-ignore
|
|
|
|
...jest.requireActual("../src/utils/notifications"),
|
|
|
|
createLocalNotificationSettingsIfNeeded: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-09-29 16:23:02 +02:00
|
|
|
describe("Notifier", () => {
|
|
|
|
const roomId = "!room1:server";
|
|
|
|
const testEvent = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
});
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
let MockPlatform: MockedObject<BasePlatform>;
|
|
|
|
let mockClient: MockedObject<MatrixClient>;
|
2022-10-25 18:53:31 +02:00
|
|
|
let testRoom: Room;
|
2022-10-06 16:27:12 +02:00
|
|
|
let accountDataEventKey: string;
|
|
|
|
let accountDataStore = {};
|
2022-09-29 16:23:02 +02:00
|
|
|
|
2022-10-25 18:53:31 +02:00
|
|
|
let mockSettings: Record<string, boolean> = {};
|
|
|
|
|
2022-10-13 09:07:48 +02:00
|
|
|
const userId = "@bob:example.org";
|
|
|
|
|
2022-09-29 16:23:02 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
accountDataStore = {};
|
2022-10-06 16:27:12 +02:00
|
|
|
mockClient = getMockClientWithEventEmitter({
|
2022-10-13 09:07:48 +02:00
|
|
|
...mockClientMethodsUser(userId),
|
2022-10-06 16:27:12 +02:00
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
getAccountData: jest.fn().mockImplementation(eventType => accountDataStore[eventType]),
|
|
|
|
setAccountData: jest.fn().mockImplementation((eventType, content) => {
|
2022-10-13 09:07:48 +02:00
|
|
|
accountDataStore[eventType] = content ? new MatrixEvent({
|
2022-10-06 16:27:12 +02:00
|
|
|
type: eventType,
|
|
|
|
content,
|
2022-10-13 09:07:48 +02:00
|
|
|
}) : undefined;
|
2022-10-06 16:27:12 +02:00
|
|
|
}),
|
|
|
|
decryptEventIfNeeded: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getPushActionsForEvent: jest.fn(),
|
|
|
|
});
|
2022-10-13 09:07:48 +02:00
|
|
|
|
|
|
|
mockClient.pushRules = {
|
|
|
|
global: undefined,
|
|
|
|
};
|
2022-10-06 16:27:12 +02:00
|
|
|
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId);
|
|
|
|
|
2022-10-25 18:53:31 +02:00
|
|
|
testRoom = new Room(roomId, mockClient, mockClient.getUserId());
|
2022-10-06 16:27:12 +02:00
|
|
|
|
2022-09-29 16:23:02 +02:00
|
|
|
MockPlatform = mockPlatformPeg({
|
|
|
|
supportsNotifications: jest.fn().mockReturnValue(true),
|
|
|
|
maySendNotifications: jest.fn().mockReturnValue(true),
|
|
|
|
displayNotification: jest.fn(),
|
2022-10-13 09:07:48 +02:00
|
|
|
loudNotification: jest.fn(),
|
2022-09-29 16:23:02 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
Notifier.isBodyEnabled = jest.fn().mockReturnValue(true);
|
2022-10-06 16:27:12 +02:00
|
|
|
|
2022-10-25 18:53:31 +02:00
|
|
|
mockClient.getRoom.mockImplementation(id => {
|
|
|
|
return id === roomId ? testRoom : new Room(id, mockClient, mockClient.getUserId());
|
|
|
|
});
|
2022-09-29 16:23:02 +02:00
|
|
|
});
|
|
|
|
|
2022-10-13 09:07:48 +02:00
|
|
|
describe('triggering notification from events', () => {
|
|
|
|
let hasStartedNotiferBefore = false;
|
|
|
|
|
|
|
|
const event = new MatrixEvent({
|
|
|
|
sender: '@alice:server.org',
|
|
|
|
type: 'm.room.message',
|
|
|
|
room_id: '!room:server.org',
|
|
|
|
content: {
|
|
|
|
body: 'hey',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// notifier defines some listener functions in start
|
|
|
|
// and references them in stop
|
|
|
|
// so blows up if stopped before it was started
|
|
|
|
if (hasStartedNotiferBefore) {
|
|
|
|
Notifier.stop();
|
|
|
|
}
|
|
|
|
Notifier.start();
|
|
|
|
hasStartedNotiferBefore = true;
|
|
|
|
mockClient.getRoom.mockReturnValue(testRoom);
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-10-25 18:53:31 +02:00
|
|
|
mockSettings = {
|
|
|
|
'notificationsEnabled': true,
|
|
|
|
'audioNotificationsEnabled': true,
|
|
|
|
};
|
|
|
|
|
2022-10-13 09:07:48 +02:00
|
|
|
// enable notifications by default
|
2022-10-25 18:53:31 +02:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReset().mockImplementation(
|
|
|
|
settingName => mockSettings[settingName] ?? false,
|
2022-10-13 09:07:48 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Notifier.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create notifications before syncing has started', () => {
|
|
|
|
mockClient!.emit(ClientEvent.Event, event);
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create notifications for own event', () => {
|
|
|
|
const ownEvent = new MatrixEvent({ sender: userId });
|
|
|
|
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing);
|
|
|
|
mockClient!.emit(ClientEvent.Event, ownEvent);
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create notifications when event does not have notify push action', () => {
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: false,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing);
|
|
|
|
mockClient!.emit(ClientEvent.Event, event);
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).not.toHaveBeenCalled();
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates desktop notification when enabled', () => {
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing);
|
|
|
|
mockClient!.emit(ClientEvent.Event, event);
|
|
|
|
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledWith(
|
|
|
|
testRoom.name,
|
|
|
|
'hey',
|
|
|
|
null,
|
|
|
|
testRoom,
|
|
|
|
event,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a loud notification when enabled', () => {
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing);
|
|
|
|
mockClient!.emit(ClientEvent.Event, event);
|
|
|
|
|
|
|
|
expect(MockPlatform.loudNotification).toHaveBeenCalledWith(
|
|
|
|
event, testRoom,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create loud notification when event does not have sound tweak in push actions', () => {
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
mockClient!.emit(ClientEvent.Sync, SyncState.Syncing);
|
|
|
|
mockClient!.emit(ClientEvent.Event, event);
|
|
|
|
|
|
|
|
// desktop notification created
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalled();
|
|
|
|
// without noisy
|
|
|
|
expect(MockPlatform.loudNotification).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-29 16:23:02 +02:00
|
|
|
describe("_displayPopupNotification", () => {
|
|
|
|
it.each([
|
2022-10-13 09:07:48 +02:00
|
|
|
{ event: { is_silenced: true }, count: 0 },
|
|
|
|
{ event: { is_silenced: false }, count: 1 },
|
|
|
|
{ event: undefined, count: 1 },
|
|
|
|
])("does not dispatch when notifications are silenced", ({ event, count }) => {
|
|
|
|
mockClient.setAccountData(accountDataEventKey, event);
|
2022-09-29 16:23:02 +02:00
|
|
|
Notifier._displayPopupNotification(testEvent, testRoom);
|
|
|
|
expect(MockPlatform.displayNotification).toHaveBeenCalledTimes(count);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("_playAudioNotification", () => {
|
|
|
|
it.each([
|
2022-10-13 09:07:48 +02:00
|
|
|
{ event: { is_silenced: true }, count: 0 },
|
|
|
|
{ event: { is_silenced: false }, count: 1 },
|
|
|
|
{ event: undefined, count: 1 },
|
|
|
|
])("does not dispatch when notifications are silenced", ({ event, count }) => {
|
2022-09-29 16:23:02 +02:00
|
|
|
// It's not ideal to only look at whether this function has been called
|
|
|
|
// but avoids starting to look into DOM stuff
|
|
|
|
Notifier.getSoundForRoom = jest.fn();
|
|
|
|
|
2022-10-13 09:07:48 +02:00
|
|
|
mockClient.setAccountData(accountDataEventKey, event);
|
2022-09-29 16:23:02 +02:00
|
|
|
Notifier._playAudioNotification(testEvent, testRoom);
|
|
|
|
expect(Notifier.getSoundForRoom).toHaveBeenCalledTimes(count);
|
|
|
|
});
|
|
|
|
});
|
2022-10-06 16:27:12 +02:00
|
|
|
|
|
|
|
describe("group call notifications", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(true);
|
|
|
|
jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast");
|
|
|
|
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {},
|
|
|
|
});
|
|
|
|
|
2022-10-10 16:18:38 +02:00
|
|
|
Notifier.onSyncStateChange(SyncState.Syncing);
|
2022-10-06 16:27:12 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
const callOnEvent = (type?: string) => {
|
2022-10-25 18:53:31 +02:00
|
|
|
const callEvent = mkEvent({
|
|
|
|
type: type ?? ElementCall.CALL_EVENT_TYPE.name,
|
|
|
|
user: "@alice:foo",
|
|
|
|
room: roomId,
|
|
|
|
content: {},
|
|
|
|
event: true,
|
|
|
|
});
|
2022-10-06 16:27:12 +02:00
|
|
|
Notifier.onEvent(callEvent);
|
|
|
|
return callEvent;
|
|
|
|
};
|
|
|
|
|
|
|
|
const setGroupCallsEnabled = (val: boolean) => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
|
|
|
|
if (name === "feature_group_calls") return val;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
it("should show toast when group calls are supported", () => {
|
|
|
|
setGroupCallsEnabled(true);
|
|
|
|
|
|
|
|
const callEvent = callOnEvent();
|
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).toHaveBeenCalledWith(expect.objectContaining({
|
|
|
|
key: `call_${callEvent.getStateKey()}`,
|
|
|
|
priority: 100,
|
|
|
|
component: IncomingCallToast,
|
|
|
|
bodyClassName: "mx_IncomingCallToast",
|
|
|
|
props: { callEvent },
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not show toast when group calls are not supported", () => {
|
|
|
|
setGroupCallsEnabled(false);
|
|
|
|
|
|
|
|
callOnEvent();
|
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not show toast when calling with non-group call event", () => {
|
|
|
|
setGroupCallsEnabled(true);
|
|
|
|
|
|
|
|
callOnEvent("event_type");
|
|
|
|
|
|
|
|
expect(ToastStore.sharedInstance().addOrReplaceToast).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-10 16:18:38 +02:00
|
|
|
|
|
|
|
describe('local notification settings', () => {
|
|
|
|
const createLocalNotificationSettingsIfNeededMock = mocked(createLocalNotificationSettingsIfNeeded);
|
|
|
|
let hasStartedNotiferBefore = false;
|
|
|
|
beforeEach(() => {
|
|
|
|
// notifier defines some listener functions in start
|
|
|
|
// and references them in stop
|
|
|
|
// so blows up if stopped before it was started
|
|
|
|
if (hasStartedNotiferBefore) {
|
|
|
|
Notifier.stop();
|
|
|
|
}
|
|
|
|
Notifier.start();
|
|
|
|
hasStartedNotiferBefore = true;
|
|
|
|
createLocalNotificationSettingsIfNeededMock.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Notifier.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create local notifications event after a sync error', () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Error, SyncState.Syncing);
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create local notifications event after sync stops', () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Stopped, SyncState.Syncing);
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not create local notifications event after a cached sync', () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing, {
|
|
|
|
fromCache: true,
|
|
|
|
});
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates local notifications event after a non-cached sync', () => {
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Syncing, SyncState.Syncing, {});
|
|
|
|
expect(createLocalNotificationSettingsIfNeededMock).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2022-10-25 18:53:31 +02:00
|
|
|
|
|
|
|
describe('_evaluateEvent', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SdkContextClass.instance.roomViewStore, "getRoomId")
|
|
|
|
.mockReturnValue(testRoom.roomId);
|
|
|
|
|
|
|
|
jest.spyOn(UserActivity.sharedInstance(), "userActiveRecently")
|
|
|
|
.mockReturnValue(true);
|
|
|
|
|
|
|
|
jest.spyOn(Modal, "hasDialogs").mockReturnValue(false);
|
|
|
|
|
|
|
|
jest.spyOn(Notifier, "_displayPopupNotification").mockReset();
|
|
|
|
jest.spyOn(Notifier, "isEnabled").mockReturnValue(true);
|
|
|
|
|
|
|
|
mockClient.getPushActionsForEvent.mockReturnValue({
|
|
|
|
notify: true,
|
|
|
|
tweaks: {
|
|
|
|
sound: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show a pop-up", () => {
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(0);
|
|
|
|
Notifier._evaluateEvent(testEvent);
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
const eventFromOtherRoom = mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: "m.room.message",
|
|
|
|
user: "@user1:server",
|
|
|
|
room: "!otherroom:example.org",
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
|
|
|
|
Notifier._evaluateEvent(eventFromOtherRoom);
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should a pop-up for thread event", async () => {
|
|
|
|
const { events, rootEvent } = mkThread({
|
|
|
|
room: testRoom,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: "@bob:example.org",
|
|
|
|
participantUserIds: ["@bob:example.org"],
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
Notifier._evaluateEvent(rootEvent);
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
Notifier._evaluateEvent(events[1]);
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
dis.dispatch<ThreadPayload>({
|
|
|
|
action: Action.ViewThread,
|
|
|
|
thread_id: rootEvent.getId(),
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(SdkContextClass.instance.roomViewStore.getThreadId()).toBe(rootEvent.getId()),
|
|
|
|
);
|
|
|
|
|
|
|
|
Notifier._evaluateEvent(events[1]);
|
|
|
|
expect(Notifier._displayPopupNotification).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
2022-09-29 16:23:02 +02:00
|
|
|
});
|