2022-10-12 21:35:52 +08:00
|
|
|
/*
|
2024-09-06 22:44:31 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-10-12 21:35:52 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-06 22:44:31 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-10-12 21:35:52 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-10-13 16:22:34 +08:00
|
|
|
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-07-15 04:05:23 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import { UpdateCheckStatus } from "../../../../src/BasePlatform";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
|
|
|
import dispatcher from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import * as rageshake from "../../../../src/rageshake/rageshake";
|
|
|
|
import { BreadcrumbsStore } from "../../../../src/stores/BreadcrumbsStore";
|
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import DesktopCapturerSourcePicker from "../../../../src/components/views/elements/DesktopCapturerSourcePicker";
|
2022-10-12 21:35:52 +08:00
|
|
|
import ElectronPlatform from "../../../../src/vector/platform/ElectronPlatform";
|
2023-09-26 19:28:31 +08:00
|
|
|
import { setupLanguageMock } from "../../../setup/setupLanguage";
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../src/rageshake/rageshake", () => ({
|
2022-10-13 16:22:34 +08:00
|
|
|
flush: jest.fn(),
|
|
|
|
}));
|
2022-10-12 21:35:52 +08:00
|
|
|
|
|
|
|
describe("ElectronPlatform", () => {
|
2022-10-13 16:22:34 +08:00
|
|
|
const defaultUserAgent =
|
|
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " +
|
|
|
|
"(KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36";
|
2022-10-12 21:35:52 +08:00
|
|
|
const mockElectron = {
|
|
|
|
on: jest.fn(),
|
2022-10-13 16:22:34 +08:00
|
|
|
send: jest.fn(),
|
2022-10-12 21:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchSpy = jest.spyOn(dispatcher, "dispatch");
|
|
|
|
const dispatchFireSpy = jest.spyOn(dispatcher, "fire");
|
|
|
|
const logSpy = jest.spyOn(logger, "log").mockImplementation(() => {});
|
|
|
|
|
|
|
|
const userId = "@alice:server.org";
|
|
|
|
const deviceId = "device-id";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
window.electron = mockElectron;
|
|
|
|
jest.clearAllMocks();
|
2023-04-25 16:36:17 +08:00
|
|
|
Object.defineProperty(window, "navigator", { value: { userAgent: defaultUserAgent }, writable: true });
|
2023-09-26 19:28:31 +08:00
|
|
|
setupLanguageMock();
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
2024-10-16 23:43:07 +08:00
|
|
|
const getElectronEventHandlerCall = (
|
|
|
|
eventType: string,
|
|
|
|
): [type: string, handler: (...args: any) => void] | undefined =>
|
2022-10-12 21:35:52 +08:00
|
|
|
mockElectron.on.mock.calls.find(([type]) => type === eventType);
|
|
|
|
|
|
|
|
it("flushes rageshake before quitting", () => {
|
|
|
|
new ElectronPlatform();
|
2023-04-25 16:36:17 +08:00
|
|
|
const [event, handler] = getElectronEventHandlerCall("before-quit")!;
|
2022-10-13 16:22:34 +08:00
|
|
|
// correct event bound
|
|
|
|
expect(event).toBeTruthy();
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2022-10-13 16:22:34 +08:00
|
|
|
handler();
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(logSpy).toHaveBeenCalled();
|
|
|
|
expect(rageshake.flush).toHaveBeenCalled();
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("dispatches view settings action on preferences event", () => {
|
|
|
|
new ElectronPlatform();
|
2023-04-25 16:36:17 +08:00
|
|
|
const [event, handler] = getElectronEventHandlerCall("preferences")!;
|
2022-10-13 16:22:34 +08:00
|
|
|
// correct event bound
|
|
|
|
expect(event).toBeTruthy();
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2022-10-13 16:22:34 +08:00
|
|
|
handler();
|
2022-10-12 21:35:52 +08:00
|
|
|
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(dispatchFireSpy).toHaveBeenCalledWith(Action.ViewUserSettings);
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
2023-07-15 04:05:23 +08:00
|
|
|
it("creates a modal on openDesktopCapturerSourcePicker", async () => {
|
|
|
|
const plat = new ElectronPlatform();
|
|
|
|
Modal.createDialog = jest.fn();
|
|
|
|
|
|
|
|
// @ts-ignore mock
|
|
|
|
mocked(Modal.createDialog).mockReturnValue({
|
|
|
|
finished: new Promise((r) => r(["source"])),
|
|
|
|
});
|
|
|
|
|
|
|
|
let res: () => void;
|
|
|
|
const waitForIPCSend = new Promise<void>((r) => {
|
|
|
|
res = r;
|
|
|
|
});
|
|
|
|
// @ts-ignore mock
|
|
|
|
jest.spyOn(plat.ipc, "call").mockImplementation(() => {
|
|
|
|
res();
|
|
|
|
});
|
|
|
|
|
|
|
|
const [event, handler] = getElectronEventHandlerCall("openDesktopCapturerSourcePicker")!;
|
|
|
|
handler();
|
|
|
|
|
|
|
|
await waitForIPCSend;
|
|
|
|
|
|
|
|
expect(event).toBeTruthy();
|
|
|
|
expect(Modal.createDialog).toHaveBeenCalledWith(DesktopCapturerSourcePicker);
|
|
|
|
// @ts-ignore mock
|
|
|
|
expect(plat.ipc.call).toHaveBeenCalledWith("callDisplayMediaCallback", "source");
|
|
|
|
});
|
|
|
|
|
2022-10-12 21:35:52 +08:00
|
|
|
describe("updates", () => {
|
|
|
|
it("dispatches on check updates action", () => {
|
|
|
|
new ElectronPlatform();
|
2023-04-25 16:36:17 +08:00
|
|
|
const [event, handler] = getElectronEventHandlerCall("check_updates")!;
|
2022-10-12 21:35:52 +08:00
|
|
|
// correct event bound
|
|
|
|
expect(event).toBeTruthy();
|
2022-10-13 16:22:34 +08:00
|
|
|
|
2022-10-12 21:35:52 +08:00
|
|
|
handler({}, true);
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.CheckUpdates,
|
2022-10-13 16:22:34 +08:00
|
|
|
status: UpdateCheckStatus.Downloading,
|
|
|
|
});
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("dispatches on check updates action when update not available", () => {
|
|
|
|
new ElectronPlatform();
|
2023-04-25 16:36:17 +08:00
|
|
|
const [, handler] = getElectronEventHandlerCall("check_updates")!;
|
2022-10-13 16:22:34 +08:00
|
|
|
|
2022-10-12 21:35:52 +08:00
|
|
|
handler({}, false);
|
|
|
|
expect(dispatchSpy).toHaveBeenCalledWith({
|
|
|
|
action: Action.CheckUpdates,
|
2022-10-13 16:22:34 +08:00
|
|
|
status: UpdateCheckStatus.NotAvailable,
|
|
|
|
});
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("starts update check", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
platform.startUpdateCheck();
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith("check_updates");
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("installs update", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
platform.installUpdate();
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith("install_update");
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns human readable name", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.getHumanReadableName()).toEqual("Electron Platform");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("getDefaultDeviceDisplayName", () => {
|
|
|
|
it.each([
|
2022-12-09 20:28:29 +08:00
|
|
|
[
|
2022-10-13 16:22:34 +08:00
|
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " +
|
|
|
|
"(KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36",
|
2022-10-12 21:35:52 +08:00
|
|
|
"Element Desktop: macOS",
|
|
|
|
],
|
|
|
|
[
|
2022-10-13 16:22:34 +08:00
|
|
|
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " +
|
|
|
|
"electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36",
|
2022-10-12 21:35:52 +08:00
|
|
|
"Element Desktop: Windows",
|
|
|
|
],
|
|
|
|
["Mozilla/5.0 (X11; Linux i686; rv:21.0) Gecko/20100101 Firefox/21.0", "Element Desktop: Linux"],
|
|
|
|
["Mozilla/5.0 (X11; FreeBSD i686; rv:21.0) Gecko/20100101 Firefox/21.0", "Element Desktop: FreeBSD"],
|
|
|
|
["Mozilla/5.0 (X11; OpenBSD i686; rv:21.0) Gecko/20100101 Firefox/21.0", "Element Desktop: OpenBSD"],
|
|
|
|
["Mozilla/5.0 (X11; SunOS i686; rv:21.0) Gecko/20100101 Firefox/21.0", "Element Desktop: SunOS"],
|
|
|
|
["custom user agent", "Element Desktop: Unknown"],
|
2022-10-13 16:22:34 +08:00
|
|
|
])("%s = %s", (userAgent, result) => {
|
2023-04-25 16:36:17 +08:00
|
|
|
Object.defineProperty(window, "navigator", { value: { userAgent }, writable: true });
|
2022-10-12 21:35:52 +08:00
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.getDefaultDeviceDisplayName()).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns true for needsUrlTooltips", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.needsUrlTooltips()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should override browser shortcuts", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.overrideBrowserShortcuts()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows overriding native context menus", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.allowOverridingNativeContextMenus()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("indicates support for desktop capturer", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.supportsDesktopCapturer()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("indicates no support for jitsi screensharing", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.supportsJitsiScreensharing()).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("notifications", () => {
|
|
|
|
it("indicates support for notifications", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.supportsNotifications()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("may send notifications", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.maySendNotifications()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("pretends to request notification permission", async () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
const result = await platform.requestNotificationPermission();
|
|
|
|
expect(result).toEqual("granted");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates a loud notification", async () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
platform.loudNotification(new MatrixEvent(), new Room("!room:server", {} as any, userId));
|
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith("loudNotification");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets notification count when count is changing", async () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
platform.setNotificationCount(0);
|
|
|
|
// not called because matches internal notificaiton count
|
|
|
|
expect(mockElectron.send).not.toHaveBeenCalledWith("setBadgeCount", 0);
|
|
|
|
platform.setNotificationCount(1);
|
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith("setBadgeCount", 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("spellcheck", () => {
|
|
|
|
it("indicates support for spellcheck settings", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
expect(platform.supportsSpellCheckSettings()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("gets available spellcheck languages", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
mockElectron.send.mockClear();
|
|
|
|
platform.getAvailableSpellCheckLanguages();
|
|
|
|
|
|
|
|
const [channel, { name }] = mockElectron.send.mock.calls[0];
|
|
|
|
expect(channel).toEqual("ipcCall");
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(name).toEqual("getAvailableSpellCheckLanguages");
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("pickle key", () => {
|
|
|
|
it("makes correct ipc call to get pickle key", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
mockElectron.send.mockClear();
|
|
|
|
platform.getPickleKey(userId, deviceId);
|
|
|
|
|
|
|
|
const [, { name, args }] = mockElectron.send.mock.calls[0];
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(name).toEqual("getPickleKey");
|
|
|
|
expect(args).toEqual([userId, deviceId]);
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("makes correct ipc call to create pickle key", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
mockElectron.send.mockClear();
|
|
|
|
platform.createPickleKey(userId, deviceId);
|
|
|
|
|
|
|
|
const [, { name, args }] = mockElectron.send.mock.calls[0];
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(name).toEqual("createPickleKey");
|
|
|
|
expect(args).toEqual([userId, deviceId]);
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("makes correct ipc call to destroy pickle key", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
mockElectron.send.mockClear();
|
|
|
|
platform.destroyPickleKey(userId, deviceId);
|
|
|
|
|
|
|
|
const [, { name, args }] = mockElectron.send.mock.calls[0];
|
2022-10-13 16:22:34 +08:00
|
|
|
expect(name).toEqual("destroyPickleKey");
|
|
|
|
expect(args).toEqual([userId, deviceId]);
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("versions", () => {
|
|
|
|
it("calls install update", () => {
|
|
|
|
const platform = new ElectronPlatform();
|
|
|
|
platform.installUpdate();
|
|
|
|
|
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith("install_update");
|
|
|
|
});
|
|
|
|
});
|
2023-03-22 17:22:51 +08:00
|
|
|
|
|
|
|
describe("breacrumbs", () => {
|
|
|
|
it("should send breadcrumb updates over the IPC", () => {
|
|
|
|
const spy = jest.spyOn(BreadcrumbsStore.instance, "on");
|
|
|
|
new ElectronPlatform();
|
|
|
|
const cb = spy.mock.calls[0][1];
|
|
|
|
cb();
|
|
|
|
|
|
|
|
expect(mockElectron.send).toHaveBeenCalledWith(
|
|
|
|
"ipcCall",
|
|
|
|
expect.objectContaining({
|
|
|
|
name: "breadcrumbs",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-10-12 21:35:52 +08:00
|
|
|
});
|