1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Improve browser detection code (#12456)

* Improve browser detection code

* Add unit tests for platform detection

* Add test

* Use key check instead of object presence
This commit is contained in:
David Walsh 2021-11-05 19:28:44 -05:00 committed by GitHub
parent accfd0b049
commit 2822379bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 16 deletions

View File

@ -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;
};
/**

View File

@ -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);
});
});
});