bitwarden-estensione-browser/apps/browser/src/services/browserPlatformUtils.servic...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-06-14 17:10:53 +02:00
import { DeviceType } from "@bitwarden/common/enums/deviceType";
2018-01-05 22:38:50 +01:00
2022-02-24 18:14:04 +01:00
import BrowserPlatformUtilsService from "./browserPlatformUtils.service";
2018-01-05 22:38:50 +01:00
describe("Browser Utils Service", () => {
describe("getBrowser", () => {
const originalUserAgent = navigator.userAgent;
2021-12-21 15:43:35 +01:00
2018-01-05 22:38:50 +01:00
// Reset the userAgent.
afterAll(() => {
Object.defineProperty(navigator, "userAgent", {
value: originalUserAgent,
});
});
let browserPlatformUtilsService: BrowserPlatformUtilsService;
beforeEach(() => {
(window as any).matchMedia = jest.fn().mockReturnValueOnce({});
browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null, null, null);
});
afterEach(() => {
window.matchMedia = undefined;
(window as any).chrome = undefined;
2018-01-05 22:38:50 +01:00
});
2021-12-21 15:43:35 +01:00
2018-01-05 22:38:50 +01:00
it("should detect chrome", () => {
Object.defineProperty(navigator, "userAgent", {
configurable: true,
2021-12-21 15:43:35 +01:00
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36",
2021-12-21 15:43:35 +01:00
});
(window as any).chrome = {};
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.ChromeExtension);
2021-12-21 15:43:35 +01:00
});
2018-01-05 22:38:50 +01:00
it("should detect firefox", () => {
Object.defineProperty(navigator, "userAgent", {
configurable: true,
2020-04-13 16:32:45 +02:00
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0",
2021-12-21 15:43:35 +01:00
});
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.FirefoxExtension);
2021-12-21 15:43:35 +01:00
});
2018-01-05 22:38:50 +01:00
it("should detect opera", () => {
Object.defineProperty(navigator, "userAgent", {
configurable: true,
2021-12-21 15:43:35 +01:00
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3175.3 Safari/537.36 OPR/49.0.2695.0 (Edition developer)",
2021-12-21 15:43:35 +01:00
});
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.OperaExtension);
2021-12-21 15:43:35 +01:00
});
2018-01-05 22:38:50 +01:00
it("should detect edge", () => {
2018-01-11 21:49:29 +01:00
Object.defineProperty(navigator, "userAgent", {
2018-01-05 22:38:50 +01:00
configurable: true,
2021-12-21 15:43:35 +01:00
value:
2020-09-15 16:50:45 +02:00
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43",
2021-12-21 15:43:35 +01:00
});
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.EdgeExtension);
2021-12-21 15:43:35 +01:00
});
2018-01-11 21:49:29 +01:00
it("should detect safari", () => {
Object.defineProperty(navigator, "userAgent", {
2018-04-14 16:16:05 +02:00
configurable: true,
2021-12-21 15:43:35 +01:00
value:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8",
2021-12-21 15:43:35 +01:00
});
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.SafariExtension);
2021-12-21 15:43:35 +01:00
});
2018-01-11 21:49:29 +01:00
it("should detect vivaldi", () => {
Object.defineProperty(navigator, "userAgent", {
configurable: true,
2021-12-21 15:43:35 +01:00
value:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.97 Safari/537.36 Vivaldi/1.94.1008.40",
2021-12-21 15:43:35 +01:00
});
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.VivaldiExtension);
2021-12-21 15:43:35 +01:00
});
});
2018-01-05 22:38:50 +01:00
});