Return browser version in capabilities (bug 1124243)

This commit is contained in:
Mark Striemer 2015-01-21 17:40:00 -06:00
Родитель 980524a916
Коммит 7996b92f8e
3 изменённых файлов: 102 добавлений и 30 удалений

Просмотреть файл

@ -4,17 +4,39 @@ define('capabilities', ['settings'], function(settings) {
return !!m && m.matches;
}
function detectOS() {
var macInfo = /Mac OS X 10[_\.](\d+)/
.exec(navigator.userAgent);
if (macInfo) {
return {
name: 'Mac OS X',
version: [10, parseInt(macInfo[1], 10)],
};
} else {
return {};
var osInfo = {
windows: {
name: 'Windows', regex: /Windows/, slug: 'windows',
},
mac: {
name: 'Mac OS X', regex: /Mac OS X 10[_\.](\d+)/, slug: 'mac',
},
linux: {
name: 'Linux', regex: /Linux/, slug: 'linux',
},
android: {
name: 'Android', regex: /Android/, slug: 'android',
},
};
function detectOS(navigator) {
navigator = navigator || window.navigator;
var matchData;
var os;
var osNames = Object.keys(osInfo);
for (var i = 0; i < osNames.length; i++) {
os = osInfo[osNames[i]];
matchData = os.regex.exec(navigator.userAgent);
if (matchData) {
return {
name: os.name,
slug: os.slug,
// Return `undefined` not `NaN` if we don't detect version.
version: matchData[1] && parseInt(matchData[1], 10),
};
}
}
return {};
}
var static_caps = {
@ -37,6 +59,7 @@ define('capabilities', ['settings'], function(settings) {
navigator.userAgent.indexOf('Android') === -1 &&
(navigator.userAgent.indexOf('Mobile') !== -1 || navigator.userAgent.indexOf('Tablet') !== -1),
'phantom': navigator.userAgent.match(/Phantom/), // Don't use this if you can help it.
'detectOS': detectOS,
'os': detectOS(),
};
@ -89,8 +112,8 @@ define('capabilities', ['settings'], function(settings) {
// OS X requires some extra work to install apps that aren't from the
// App Store. See bug 1112275 for more info.
static_caps.osXInstallIssues = static_caps.os.name === 'Mac OS X' &&
static_caps.os.version[1] >= 9;
static_caps.osXInstallIssues = static_caps.os.slug === 'mac' &&
static_caps.os.version >= 9;
return static_caps;

Просмотреть файл

@ -6,6 +6,7 @@ var eq_ = a.eq_;
var eeq_ = a.eeq_;
var feq_ = a.feq_;
var mock = a.mock;
var capabilities = require('capabilities');
test('capabilities device_platform', function(done, fail) {
mock(
@ -112,4 +113,70 @@ test('capabilities device_type', function(done, fail) {
);
});
test('capabilities.os Mac 10.9 Firefox', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:23.0) Gecko/20100101 Firefox/23.0',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Mac OS X');
eeq_(os.version, 9);
eq_(os.slug, 'mac');
done();
});
test('capabilities.os Mac 10.10 Firefox', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:23.0) Gecko/20100101 Firefox/23.0',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Mac OS X');
eeq_(os.version, 10);
eq_(os.slug, 'mac');
done();
});
test('capabilities.os Mac 10.9 Safari', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/600.2.5 (KHTML, like Gecko) Version/7.1.2 Safari/537.85.11',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Mac OS X');
eeq_(os.version, 9);
eq_(os.slug, 'mac');
done();
});
test('capabilities.os Windows IE 9', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Windows');
eeq_(os.version, undefined);
eq_(os.slug, 'windows');
done();
});
test('capabilities.os Linux Firefox', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Linux');
eeq_(os.version, undefined);
eq_(os.slug, 'linux');
done();
});
test('capabilities.os Android Firefox', function(done, fail) {
var navigator = {
userAgent: 'Mozilla/5.0 (Android; Mobile; rv:26.0) Gecko/26.0 Firefox/26.0',
};
var os = capabilities.detectOS(navigator);
eq_(os.name, 'Android');
eeq_(os.version, undefined);
eq_(os.slug, 'android');
done();
});
})();

Просмотреть файл

@ -167,23 +167,6 @@ define('utils', ['jquery', 'l10n', 'underscore'], function($, l10n, _) {
return '';
}
var osStrings = {
'windows': 'Windows',
'mac': 'Mac',
'linux': 'Linux',
'android': 'Android'
};
function browser() {
var os = {};
var platform = '';
for (var i in osStrings) {
if (navigator.userAgent.indexOf(osStrings[i]) !== -1) {
return i;
}
}
return 'other';
}
var a = document.createElement('a');
function urlparse(url) {
@ -195,7 +178,6 @@ define('utils', ['jquery', 'l10n', 'underscore'], function($, l10n, _) {
'_pd': _pd,
'baseurl': baseurl,
'bgurl': bgurl,
'browser': browser,
'encodeURIComponent': encodeURIComponent,
'decodeURIComponent': decodeURIComponent,
'escape_': _.escape,