Skip to content

Commit

Permalink
Fix: Tests failing in Firefox (Issue #14)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
barisaydinoglu committed Jan 7, 2014
1 parent f88d479 commit 6129b75
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit 6129b75

Please sign in to comment.