Skip to content

Commit

Permalink
Chore: User agent refactor and Browser tests (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored Apr 21, 2017
1 parent ac838d2 commit 059f6ac
Show file tree
Hide file tree
Showing 2 changed files with 536 additions and 12 deletions.
65 changes: 53 additions & 12 deletions src/lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,50 @@ const MIME_H264_BASELINE = 'video/mp4; codecs="avc1.42E01E"';
const MIME_H264_MAIN = 'video/mp4; codecs="avc1.4D401E"';
const MIME_H264_HIGH = 'video/mp4; codecs="avc1.64001E"';
const EXT_STANDARD_DERIVATIVES = 'OES_standard_derivatives';
const USER_AGENT = navigator.userAgent;
const EXT_LOSE_CONTEXT = 'WEBGL_lose_context';
const EVENT_WEBGL_CONEXT_LOST = 'webglcontextlost';

let userAgent = navigator.userAgent;
let name;
let gl;
let supportsWebGL;

class Browser {
/**
* Override the current user agent.
*
* @public
* @param {string} newUserAgent The new user agent to use for all browser compatibility testing.
* @return {void}
*/
static overrideUserAgent(newUserAgent) {
userAgent = newUserAgent;
// Nullify old name to be refreshed on next "getName()" call.
name = null;
}

/**
* Get the name of the current browser.
*
* @public
* @return {string} The name of the browser.
*/
static getName() {
if (name) {
return name;
}

if (USER_AGENT.indexOf('Edge/') > 0) {
if (userAgent.indexOf('Edge/') > 0) {
name = 'Edge';
} else if (USER_AGENT.indexOf('OPR/') > 0) {
} else if (userAgent.indexOf('OPR/') > 0 || userAgent.indexOf('Opera/') > 0) {
name = 'Opera';
} else if (USER_AGENT.indexOf('Chrome/') > 0) {
} else if (userAgent.indexOf('Chrome/') > 0) {
name = 'Chrome';
} else if (USER_AGENT.indexOf('Safari/') > 0) {
} else if (userAgent.indexOf('Safari/') > 0) {
name = 'Safari';
} else if (USER_AGENT.indexOf('Trident/') > 0) {
} else if (userAgent.indexOf('Trident/') > 0) {
name = 'Explorer';
} else if (USER_AGENT.indexOf('Firefox/') > 0) {
} else if (userAgent.indexOf('Firefox/') > 0) {
name = 'Firefox';
}

Expand Down Expand Up @@ -164,8 +184,10 @@ class Browser {
if (!gl) {
const canvas = document.createElement('canvas');
// Should stop 'Rats! WebGL hit a snag' error when checking WebGL support
canvas.addEventListener('webglcontextlost', (e) => {
canvas.addEventListener(EVENT_WEBGL_CONEXT_LOST, (e) => {
/* istanbul ignore next*/
e.preventDefault();
/* istanbul ignore next*/
e.stopPropagation();
});

Expand All @@ -181,6 +203,25 @@ class Browser {
return supportsWebGL;
}

/**
* Clean up the old WebGL context used by hasWebGL().
*
* @public
* @return {void}
*/
static clearGLContext() {
if (!gl) {
return;
}

const loseExt = gl.getExtension(EXT_LOSE_CONTEXT);
if (loseExt && typeof loseExt.loseContext === 'function') {
loseExt.loseContext();
}

gl = null;
}

/**
* Returns true if the browser supports full capabilities required by
* the Box3DRuntime for displaying Model Preview
Expand Down Expand Up @@ -231,7 +272,7 @@ class Browser {
*/
static isMobile() {
// Relying on the user agent to avoid desktop browsers on machines with touch screens.
return /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(navigator.userAgent);
return /iphone|ipad|ipod|android|blackberry|bb10|mini|windows\sce|palm/i.test(userAgent);
}

/**
Expand All @@ -250,7 +291,7 @@ class Browser {
* @return {boolean} true if the device is running IOS
*/
static isIOS() {
return /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
return /(iPad|iPhone|iPod)/g.test(userAgent);
}

/**
Expand All @@ -259,7 +300,7 @@ class Browser {
* @return {boolean} true if the device is running Android
*/
static isAndroid() {
return /Android/g.test(navigator.userAgent);
return /Android/g.test(userAgent);
}

/**
Expand All @@ -268,7 +309,7 @@ class Browser {
* @return {boolean} True if device is running IOS 10.3.x
*/
static isIOSWithFontIssue() {
return Browser.isIOS() && /(?:OS\s)10_3/i.test(navigator.userAgent);
return Browser.isIOS() && /(?:OS\s)10_3/i.test(userAgent);
}
}

Expand Down
Loading

0 comments on commit 059f6ac

Please sign in to comment.