|
| 1 | +/* |
| 2 | +Copyright 2024 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { logger } from "matrix-js-sdk/src/logger"; |
| 18 | + |
| 19 | +import { checkBrowserSupport, LOCAL_STORAGE_KEY } from "../src/SupportedBrowser"; |
| 20 | +import ToastStore from "../src/stores/ToastStore"; |
| 21 | +import GenericToast from "../src/components/views/toasts/GenericToast"; |
| 22 | + |
| 23 | +jest.mock("matrix-js-sdk/src/logger"); |
| 24 | + |
| 25 | +describe("SupportedBrowser", () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.resetAllMocks(); |
| 28 | + localStorage.clear(); |
| 29 | + }); |
| 30 | + |
| 31 | + const testUserAgentFactory = |
| 32 | + (expectedWarning?: string) => |
| 33 | + async (userAgent: string): Promise<void> => { |
| 34 | + const toastSpy = jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast"); |
| 35 | + const warnLogSpy = jest.spyOn(logger, "warn"); |
| 36 | + Object.defineProperty(window, "navigator", { value: { userAgent: userAgent }, writable: true }); |
| 37 | + checkBrowserSupport(); |
| 38 | + if (expectedWarning) { |
| 39 | + expect(warnLogSpy).toHaveBeenCalledWith(expectedWarning, expect.any(String)); |
| 40 | + expect(toastSpy).toHaveBeenCalled(); |
| 41 | + } else { |
| 42 | + expect(warnLogSpy).not.toHaveBeenCalled(); |
| 43 | + expect(toastSpy).not.toHaveBeenCalled(); |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + it.each([ |
| 48 | + // Safari on iOS |
| 49 | + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1", |
| 50 | + // Firefox on iOS |
| 51 | + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/128.0 Mobile/15E148 Safari/605.1.15", |
| 52 | + // Opera on Samsung |
| 53 | + "Mozilla/5.0 (Linux; Android 10; SM-G970F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.64 Mobile Safari/537.36 OPR/76.2.4027.73374", |
| 54 | + ])("should warn for mobile browsers", testUserAgentFactory("Browser unsupported, unsupported device type")); |
| 55 | + |
| 56 | + it.each([ |
| 57 | + // Chrome on Chrome OS |
| 58 | + "Mozilla/5.0 (X11; CrOS x86_64 15633.69.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.212 Safari/537.36", |
| 59 | + // Opera on Windows |
| 60 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 OPR/113.0.0.0", |
| 61 | + // Vivaldi on Linux |
| 62 | + "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Vivaldi/6.8.3381.48", |
| 63 | + // IE11 on Windows 10 |
| 64 | + "Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko", |
| 65 | + // Firefox 115 on macOS |
| 66 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_4_5; rv:115.0) Gecko/20000101 Firefox/115.0", |
| 67 | + ])( |
| 68 | + "should warn for unsupported desktop browsers", |
| 69 | + testUserAgentFactory("Browser unsupported, unsupported user agent"), |
| 70 | + ); |
| 71 | + |
| 72 | + it.each([ |
| 73 | + // Safari 17.5 on macOS Sonoma |
| 74 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15", |
| 75 | + // Firefox 127 on macOS Sonoma |
| 76 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:127.0) Gecko/20100101 Firefox/127.0", |
| 77 | + // Edge 126 on Windows |
| 78 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/126.0.2592.113", |
| 79 | + // Edge 126 on macOS |
| 80 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/126.0.2592.113", |
| 81 | + // Firefox 128 on Windows |
| 82 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0", |
| 83 | + // Firefox 128 on Linux |
| 84 | + "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0", |
| 85 | + // Chrome 127 on Windows |
| 86 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36", |
| 87 | + ])("should not warn for supported browsers", testUserAgentFactory()); |
| 88 | + |
| 89 | + it.each([ |
| 90 | + // Element Nightly on macOS |
| 91 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2024072501 Chrome/126.0.6478.127 Electron/31.2.1 Safari/537.36", |
| 92 | + ])("should not warn for Element Desktop", testUserAgentFactory()); |
| 93 | + |
| 94 | + it.each(["AppleTV11,1/11.1"])( |
| 95 | + "should handle unknown user agent sanely", |
| 96 | + testUserAgentFactory("Browser unsupported, unknown client"), |
| 97 | + ); |
| 98 | + |
| 99 | + it("should not warn for unsupported browser if user accepted already", async () => { |
| 100 | + const toastSpy = jest.spyOn(ToastStore.sharedInstance(), "addOrReplaceToast"); |
| 101 | + const warnLogSpy = jest.spyOn(logger, "warn"); |
| 102 | + const userAgent = |
| 103 | + "Mozilla/5.0 (X11; CrOS x86_64 15633.69.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.212 Safari/537.36"; |
| 104 | + Object.defineProperty(window, "navigator", { value: { userAgent: userAgent }, writable: true }); |
| 105 | + |
| 106 | + checkBrowserSupport(); |
| 107 | + expect(warnLogSpy).toHaveBeenCalledWith("Browser unsupported, unsupported user agent", expect.any(String)); |
| 108 | + expect(toastSpy).toHaveBeenCalledWith( |
| 109 | + expect.objectContaining({ |
| 110 | + component: GenericToast, |
| 111 | + title: "Element does not support this browser", |
| 112 | + }), |
| 113 | + ); |
| 114 | + |
| 115 | + localStorage.setItem(LOCAL_STORAGE_KEY, String(true)); |
| 116 | + toastSpy.mockClear(); |
| 117 | + warnLogSpy.mockClear(); |
| 118 | + |
| 119 | + checkBrowserSupport(); |
| 120 | + expect(warnLogSpy).toHaveBeenCalledWith("Browser unsupported, but user has previously accepted"); |
| 121 | + expect(toastSpy).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments