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:
parent
accfd0b049
commit
2822379bdc
@ -56,21 +56,20 @@ const getEnvironmentType = (url = window.location.href) =>
|
|||||||
* @returns {string} the platform ENUM
|
* @returns {string} the platform ENUM
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
const getPlatform = (_) => {
|
const getPlatform = () => {
|
||||||
const ua = window.navigator.userAgent;
|
const { navigator } = window;
|
||||||
if (ua.search('Firefox') === -1) {
|
const { userAgent } = navigator;
|
||||||
if (window && window.chrome && window.chrome.ipcRenderer) {
|
|
||||||
return PLATFORM_BRAVE;
|
if (userAgent.includes('Firefox')) {
|
||||||
}
|
return PLATFORM_FIREFOX;
|
||||||
if (ua.search('Edge') !== -1) {
|
} else if ('brave' in navigator) {
|
||||||
return PLATFORM_EDGE;
|
return PLATFORM_BRAVE;
|
||||||
}
|
} else if (userAgent.includes('Edg/')) {
|
||||||
if (ua.search('OPR') !== -1) {
|
return PLATFORM_EDGE;
|
||||||
return PLATFORM_OPERA;
|
} else if (userAgent.includes('OPR')) {
|
||||||
}
|
return PLATFORM_OPERA;
|
||||||
return PLATFORM_CHROME;
|
|
||||||
}
|
}
|
||||||
return PLATFORM_FIREFOX;
|
return PLATFORM_CHROME;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,13 +1,17 @@
|
|||||||
import { strict as assert } from 'assert';
|
import { strict as assert } from 'assert';
|
||||||
|
import sinon from 'sinon';
|
||||||
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils';
|
import { isPrefixedFormattedHexString } from '../../../shared/modules/network.utils';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ENVIRONMENT_TYPE_POPUP,
|
ENVIRONMENT_TYPE_POPUP,
|
||||||
ENVIRONMENT_TYPE_NOTIFICATION,
|
ENVIRONMENT_TYPE_NOTIFICATION,
|
||||||
ENVIRONMENT_TYPE_FULLSCREEN,
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
||||||
ENVIRONMENT_TYPE_BACKGROUND,
|
ENVIRONMENT_TYPE_BACKGROUND,
|
||||||
|
PLATFORM_FIREFOX,
|
||||||
|
PLATFORM_OPERA,
|
||||||
|
PLATFORM_CHROME,
|
||||||
|
PLATFORM_EDGE,
|
||||||
} from '../../../shared/constants/app';
|
} from '../../../shared/constants/app';
|
||||||
import { getEnvironmentType } from './util';
|
import { getEnvironmentType, getPlatform } from './util';
|
||||||
|
|
||||||
describe('app utils', function () {
|
describe('app utils', function () {
|
||||||
describe('getEnvironmentType', 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user