From 6129b75d4f874a8d7b738bd4793c381df12661e0 Mon Sep 17 00:00:00 2001 From: Baris Aydinoglu Date: Tue, 7 Jan 2014 13:35:46 +0200 Subject: [PATCH] Fix: Tests failing in Firefox (Issue #14) Issue #14 fixed. The reason being that the userAgent property is readonly [1], per spec [2]. [1] http://mxr.mozilla.org/mozilla-central/source/dom/webidl/Navigator.webidl#45 [2] http://www.whatwg.org/specs/web-apps/current-work/#the-navigator-object Instead of changing navigator.userAgent of browser, the userAgent parameter of Modernizr.Detectizr.device changed at the initialization stage. --- test/tests.js | 66 ++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/test/tests.js b/test/tests.js index 08c895d..f37d732 100644 --- a/test/tests.js +++ b/test/tests.js @@ -1,25 +1,16 @@ -var userAgentsToTest = [ - { - ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36', - b: 'chrome', - bv: '31', - os: 'mac', - osv: 'os x' - }, - { - ua: 'Mozilla/5.0 (Linux; U; Android 4.0.3; de-de; Build/20120717) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30', - b: 'safari', - bv: '4', - os: 'android', - osv: '4' - } -]; - -function changeUserAgent(newUserAgent) { - window.navigator = { - userAgent: newUserAgent - } -} +var userAgentsToTest = [{ + ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36', + b: 'chrome', + bv: '31', + os: 'mac', + osv: 'os x' +}, { + ua: 'Mozilla/5.0 (Linux; U; Android 4.0.3; de-de; Build/20120717) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30', + b: 'safari', + bv: '4', + os: 'android', + osv: '4' +}]; function isUserAgentTestOk(userAgentToTest) { if (userAgentToTest.os == Modernizr.Detectizr.device.os && Modernizr.Detectizr.device.osVersion == userAgentToTest.osv && Modernizr.Detectizr.device.browser == userAgentToTest.b && Modernizr.Detectizr.device.browserVersion == userAgentToTest.bv) { @@ -28,27 +19,26 @@ function isUserAgentTestOk(userAgentToTest) { return false; } -test('is Detectizr ready', function() { +test('is Detectizr ready', function () { Modernizr.Detectizr.detect(); notEqual(Modernizr.Detectizr.device, undefined); }); -test('useragent tests', function() { +test('useragent tests', function () { for (var i = userAgentsToTest.length - 1; i >= 0; i--) { - changeUserAgent(userAgentsToTest[i].ua); - Modernizr.Detectizr.device = { - type: '', - model: '', - orientation: '', - browser: '', - browserEngine: '', - browserPlugins: [], - browserVersion: '', - os: '', - osVersion: '', - osVersionFull: '', - userAgent: (navigator.userAgent || navigator.vendor || window.opera).toLowerCase() - }; + Modernizr.Detectizr.device = { + type: '', + model: '', + orientation: '', + browser: '', + browserEngine: '', + browserPlugins: [], + browserVersion: '', + os: '', + osVersion: '', + osVersionFull: '', + userAgent: userAgentsToTest[i].ua.toLowerCase() + }; Modernizr.Detectizr.detect(); ok(isUserAgentTestOk(userAgentsToTest[i]), userAgentsToTest[i].ua); }