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

100 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-01-05 22:38:50 +01:00
import BrowserPlatformUtilsService from './browserPlatformUtils.service';
2018-01-09 20:26:20 +01:00
import { DeviceType } from 'jslib/enums';
2018-01-05 22:38:50 +01:00
describe('Browser Utils Service', () => {
describe('getBrowser', () => {
2018-04-14 17:54:59 +02:00
const originalUserAgent = navigator.userAgent;
2019-08-13 18:53:57 +02:00
const originalSafariAppExtension = (window as any).safariAppExtension;
2018-04-14 17:54:59 +02:00
const originalOpr = (window as any).opr;
2018-01-05 22:38:50 +01:00
// Reset the userAgent.
afterAll(() => {
Object.defineProperty(navigator, 'userAgent', {
2019-08-13 18:53:57 +02:00
value: originalUserAgent,
2018-04-14 17:54:59 +02:00
});
Object.defineProperty(window, 'safari', {
2019-08-13 18:53:57 +02:00
value: originalSafariAppExtension,
2018-04-14 17:54:59 +02:00
});
Object.defineProperty(window, 'opr', {
2019-08-13 18:53:57 +02:00
value: originalOpr,
2018-01-05 22:38:50 +01:00
});
});
it('should detect chrome', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
2018-01-05 22:38:50 +01:00
});
2018-10-14 04:52:49 +02:00
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.ChromeExtension);
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',
2018-01-05 22:38:50 +01:00
});
2018-10-14 04:52:49 +02:00
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.FirefoxExtension);
2018-01-05 22:38:50 +01:00
});
it('should detect opera', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
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)',
2018-01-05 22:38:50 +01:00
});
2018-04-14 17:54:59 +02:00
Object.defineProperty(window, 'opr', {
configurable: true,
2020-04-13 16:32:45 +02:00
value: {},
2018-04-14 17:54:59 +02:00
});
2018-10-14 04:52:49 +02:00
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.OperaExtension);
2018-01-05 22:38:50 +01:00
});
it('should detect edge', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
2018-01-05 22:38:50 +01:00
});
2018-10-14 04:52:49 +02:00
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.EdgeExtension);
2018-01-05 22:38:50 +01:00
});
2018-01-11 21:49:29 +01:00
it('should detect safari', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
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',
2018-01-11 21:49:29 +01:00
});
2019-08-13 18:53:57 +02:00
Object.defineProperty(window, 'safariAppExtension', {
2018-04-14 16:16:05 +02:00
configurable: true,
2019-08-13 18:53:57 +02:00
value: true,
2018-04-14 16:16:05 +02:00
});
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.SafariExtension);
2019-08-16 23:11:45 +02:00
Object.defineProperty(window, 'safariAppExtension', {
configurable: true,
value: false,
});
2018-01-11 21:49:29 +01:00
});
it('should detect vivaldi', () => {
Object.defineProperty(navigator, 'userAgent', {
configurable: true,
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',
2018-01-11 21:49:29 +01:00
});
2019-02-27 15:28:16 +01:00
const browserPlatformUtilsService = new BrowserPlatformUtilsService(null, null);
2018-07-09 15:12:41 +02:00
expect(browserPlatformUtilsService.getDevice()).toBe(DeviceType.VivaldiExtension);
2018-01-11 21:49:29 +01:00
});
2018-01-05 22:38:50 +01:00
});
});