From 2822379bdc8ffe9077553fab3bc76faa3d777fc1 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Fri, 5 Nov 2021 19:28:44 -0500 Subject: [PATCH] Improve browser detection code (#12456) * Improve browser detection code * Add unit tests for platform detection * Add test * Use key check instead of object presence --- app/scripts/lib/util.js | 27 ++++++++-------- app/scripts/lib/util.test.js | 63 ++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index 9467218ca..c5c35a7d5 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -56,21 +56,20 @@ const getEnvironmentType = (url = window.location.href) => * @returns {string} the platform ENUM * */ -const getPlatform = (_) => { - const ua = window.navigator.userAgent; - if (ua.search('Firefox') === -1) { - if (window && window.chrome && window.chrome.ipcRenderer) { - return PLATFORM_BRAVE; - } - if (ua.search('Edge') !== -1) { - return PLATFORM_EDGE; - } - if (ua.search('OPR') !== -1) { - return PLATFORM_OPERA; - } - return PLATFORM_CHROME; +const getPlatform = () => { + const { navigator } = window; + const { userAgent } = navigator; + + if (userAgent.includes('Firefox')) { + return PLATFORM_FIREFOX; + } else if ('brave' in navigator) { + return PLATFORM_BRAVE; + } else if (userAgent.includes('Edg/')) { + return PLATFORM_EDGE; + } else if (userAgent.includes('OPR')) { + return PLATFORM_OPERA; } - return PLATFORM_FIREFOX; + return PLATFORM_CHROME; }; /** diff --git a/app/scripts/lib/util.test.js b/app/scripts/lib/util.test.js index f21c10d5e..d25949bc4 100644 --- a/app/scripts/lib/util.test.js +++ b/app/scripts/lib/util.test.js @@ -1,13 +1,17 @@ import { strict as assert } from 'assert'; +import sinon from 'sinon'; import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils'; - import { ENVIRONMENT_TYPE_POPUP, ENVIRONMENT_TYPE_NOTIFICATION, ENVIRONMENT_TYPE_FULLSCREEN, ENVIRONMENT_TYPE_BACKGROUND, + PLATFORM_FIREFOX, + PLATFORM_OPERA, + PLATFORM_CHROME, + PLATFORM_EDGE, } from '../../../shared/constants/app'; -import { getEnvironmentType } from './util'; +import { getEnvironmentType, getPlatform } from './util'; describe('app utils', function () { describe('getEnvironmentType', function () { @@ -151,4 +155,59 @@ describe('app utils', function () { ); }); }); + + describe('getPlatform', function () { + const setBrowserSpecificWindow = (browser) => { + switch (browser) { + case 'firefox': { + sinon.stub(window, 'navigator').value({ + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0', + }); + break; + } + case 'edge': { + sinon.stub(window, 'navigator').value({ + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30', + }); + break; + } + case 'opera': { + sinon.stub(window, 'navigator').value({ + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36 OPR/80.0.4170.63', + }); + break; + } + default: { + sinon.stub(window, 'navigator').value({ + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36', + }); + break; + } + } + }; + + it('should detect Firefox', function () { + setBrowserSpecificWindow('firefox'); + assert.equal(getPlatform(), PLATFORM_FIREFOX); + }); + + it('should detect Edge', function () { + setBrowserSpecificWindow('edge'); + assert.equal(getPlatform(), PLATFORM_EDGE); + }); + + it('should detect Opera', function () { + setBrowserSpecificWindow('opera'); + assert.equal(getPlatform(), PLATFORM_OPERA); + }); + + it('should detect Chrome', function () { + setBrowserSpecificWindow('chrome'); + assert.equal(getPlatform(), PLATFORM_CHROME); + }); + }); });