Update wmts-hidpi, add nicer-api-docs

This commit is contained in:
Andreas Hocevar
2014-05-06 13:02:46 -05:00
parent b3ac1afd00
commit 1e25fc5585
2239 changed files with 3726515 additions and 37010 deletions

View File

@@ -0,0 +1,189 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Closure user agent detection (Browser).
* @see <a href="http://www.useragentstring.com/">User agent strings</a>
* For more information on rendering engine, platform, or device see the other
* sub-namespaces in goog.labs.userAgent, goog.labs.userAgent.platform,
* goog.labs.userAgent.device respectively.)
*
*/
goog.provide('goog.labs.userAgent.browser');
goog.require('goog.asserts');
goog.require('goog.labs.userAgent.util');
goog.require('goog.memoize');
goog.require('goog.string');
/**
* @return {boolean} Whether the user's browser is Opera.
* @private
*/
goog.labs.userAgent.browser.matchOpera_ = goog.memoize(
goog.partial(goog.labs.userAgent.util.matchUserAgent, 'Opera'));
/**
* @return {boolean} Whether the user's browser is IE.
* @private
*/
goog.labs.userAgent.browser.matchIE_ = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('Trident') ||
goog.labs.userAgent.util.matchUserAgent('MSIE');
});
/**
* @return {boolean} Whether the user's browser is Firefox.
* @private
*/
goog.labs.userAgent.browser.matchFirefox_ = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('Firefox');
});
/**
* @return {boolean} Whether the user's browser is Safari.
* @private
*/
goog.labs.userAgent.browser.matchSafari_ = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('Safari') &&
!goog.labs.userAgent.util.matchUserAgent('Chrome') &&
!goog.labs.userAgent.util.matchUserAgent('CriOS') &&
!goog.labs.userAgent.util.matchUserAgent('Android');
});
/**
* @return {boolean} Whether the user's browser is Chrome.
* @private
*/
goog.labs.userAgent.browser.matchChrome_ = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('Chrome') ||
goog.labs.userAgent.util.matchUserAgent('CriOS');
});
/**
* @return {boolean} Whether the user's browser is the Android browser.
* @private
*/
goog.labs.userAgent.browser.matchAndroidBrowser_ = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('Android') &&
!goog.labs.userAgent.util.matchUserAgent('Chrome') &&
!goog.labs.userAgent.util.matchUserAgent('CriOS');
});
/**
* @return {boolean} Whether the user's browser is Opera.
*/
goog.labs.userAgent.browser.isOpera =
goog.memoize(goog.labs.userAgent.browser.matchOpera_);
/**
* @return {boolean} Whether the user's browser is IE.
*/
goog.labs.userAgent.browser.isIE =
goog.memoize(goog.labs.userAgent.browser.matchIE_);
/**
* @return {boolean} Whether the user's browser is Firefox.
*/
goog.labs.userAgent.browser.isFirefox =
goog.memoize(goog.labs.userAgent.browser.matchFirefox_);
/**
* @return {boolean} Whether the user's browser is Safari.
*/
goog.labs.userAgent.browser.isSafari =
goog.memoize(goog.labs.userAgent.browser.matchSafari_);
/**
* @return {boolean} Whether the user's browser is Chrome.
*/
goog.labs.userAgent.browser.isChrome =
goog.memoize(goog.labs.userAgent.browser.matchChrome_);
/**
* @return {boolean} Whether the user's browser is the Android browser.
*/
goog.labs.userAgent.browser.isAndroidBrowser =
goog.memoize(goog.labs.userAgent.browser.matchAndroidBrowser_);
/**
* @return {string} The browser version or empty string if version cannot be
* determined.
*/
goog.labs.userAgent.browser.getVersion = goog.memoize(function() {
var userAgentString = goog.labs.userAgent.util.getUserAgentString();
// Special case IE since IE's version is inside the parenthesis and without
// the '/'.
if (goog.labs.userAgent.browser.isIE()) {
return goog.labs.userAgent.browser.getIEVersion_();
}
var versionTuples =
goog.labs.userAgent.util.extractVersionTuples(userAgentString);
// tuples[2] (The first X/Y tuple after the parenthesis) contains the browser
// version number.
// TODO (vbhasin): Make this check more robust.
goog.asserts.assert(versionTuples.length > 2,
'Couldn\'t extract version tuple from user agent string');
return goog.isDef(versionTuples[2][1]) ? versionTuples[2][1] : '';
});
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the browser version is higher or the same as the
* given version.
*/
goog.labs.userAgent.browser.isVersionOrHigher = function(version) {
return goog.string.compareVersions(goog.labs.userAgent.browser.getVersion(),
version) >= 0;
};
/**
* Determines IE version. More information:
* http://msdn.microsoft.com/en-us/library/jj676915(v=vs.85).aspx
*
* @return {string}
* @private
*/
goog.labs.userAgent.browser.getIEVersion_ = goog.memoize(function() {
var gDoc = goog.global['document'];
var version;
var userAgentString = goog.labs.userAgent.util.getUserAgentString();
if (gDoc && gDoc.documentMode) {
version = gDoc.documentMode;
} else if (gDoc && gDoc.compatMode && gDoc.compatMode == 'CSS1Compat') {
version = 7;
} else {
var arr = /\b(?:MSIE|rv)\s+([^\);]+)(?:\)|;)/.exec(userAgentString);
version = arr && arr[1] ? arr[1] : '';
}
return version;
});

View File

@@ -0,0 +1,193 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.labs.userAgent.browser.
*/
goog.provide('goog.labs.userAgent.browserTest');
goog.require('goog.labs.userAgent.browser');
goog.require('goog.labs.userAgent.testAgents');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.labs.userAgent.browserTest');
var propertyReplacer = new goog.testing.PropertyReplacer();
function setUp() {
// disable memoization
propertyReplacer.set(goog.memoize, 'ENABLE_MEMOIZE', false);
}
function tearDown() {
propertyReplacer.reset();
}
function testOpera() {
setGlobalUAString(goog.labs.userAgent.testAgents.OPERA_10);
assertTrue(goog.labs.userAgent.browser.isOpera());
assertVersion('10.00');
assertVersionBetween('10.00', '10.10');
setGlobalUAString(goog.labs.userAgent.testAgents.OPERA_MAC);
assertTrue(goog.labs.userAgent.browser.isOpera());
assertVersion('11.52');
assertVersionBetween('11.50', '12.00');
setGlobalUAString(goog.labs.userAgent.testAgents.OPERA_LINUX);
assertTrue(goog.labs.userAgent.browser.isOpera());
assertVersion('11.50');
assertVersionBetween('11.00', '12.00');
}
function testIE() {
setGlobalUAString(goog.labs.userAgent.testAgents.IE_6);
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('6.0');
assertVersionBetween('5.0', '7.0');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_10);
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('10.6');
assertVersionBetween('10.0', '11.0');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_9);
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('9.0');
assertVersionBetween('8.0', '10.0');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_8);
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('8.0');
assertVersionBetween('7.0', '9.0');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_8_COMPATIBILITY);
// Test Document mode override
setDocumentMode('9');
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('9');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_9_COMPATIBILITY);
setDocumentMode('9');
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('9');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_9_COMPATIBILITY);
setDocumentMode('8');
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('8');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_11);
assertTrue(goog.labs.userAgent.browser.isIE());
assertVersion('11.0');
assertVersionBetween('10.0', '12.0');
}
function testFirefox() {
setGlobalUAString(goog.labs.userAgent.testAgents.FIREFOX_19);
assertTrue(goog.labs.userAgent.browser.isFirefox());
assertVersion('19.0');
assertVersionBetween('18.0', '20.0');
setGlobalUAString(goog.labs.userAgent.testAgents.FIREFOX_WINDOWS);
assertTrue(goog.labs.userAgent.browser.isFirefox());
assertVersion('14.0.1');
assertVersionBetween('14.0', '15.0');
setGlobalUAString(goog.labs.userAgent.testAgents.FIREFOX_LINUX);
assertTrue(goog.labs.userAgent.browser.isFirefox());
assertVersion('15.0.1');
}
function testChrome() {
setGlobalUAString(goog.labs.userAgent.testAgents.CHROME_ANDROID);
assertTrue(goog.labs.userAgent.browser.isChrome());
assertVersion('18.0.1025.133');
assertVersionBetween('18.0', '19.0');
assertVersionBetween('17.0', '18.1');
setGlobalUAString(goog.labs.userAgent.testAgents.CHROME_IPHONE);
assertTrue(goog.labs.userAgent.browser.isChrome());
assertVersion('22.0.1194.0');
assertVersionBetween('22.0', '23.0');
assertVersionBetween('22.0', '22.10');
setGlobalUAString(goog.labs.userAgent.testAgents.CHROME_MAC);
assertTrue(goog.labs.userAgent.browser.isChrome());
assertVersion('24.0.1309.0');
assertVersionBetween('24.0', '25.0');
assertVersionBetween('24.0', '24.10');
}
function testSafari() {
setGlobalUAString(goog.labs.userAgent.testAgents.IPAD_6);
assertTrue(goog.labs.userAgent.browser.isSafari());
assertVersion('6.0');
assertVersionBetween('5.1', '7.0');
setGlobalUAString(goog.labs.userAgent.testAgents.SAFARI_6);
assertTrue(goog.labs.userAgent.browser.isSafari());
assertVersion('6.0');
assertVersionBetween('6.0', '7.0');
setGlobalUAString(goog.labs.userAgent.testAgents.SAFARI_IPHONE);
assertTrue(goog.labs.userAgent.browser.isSafari());
assertVersion('5.0.2');
assertVersionBetween('5.0', '6.0');
}
function testAndroidBrowser() {
setGlobalUAString(goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
assertTrue(goog.labs.userAgent.browser.isAndroidBrowser());
assertVersion('4.0');
assertVersionBetween('3.0', '5.0');
setGlobalUAString(goog.labs.userAgent.testAgents.ANDROID_BROWSER_403);
assertTrue(goog.labs.userAgent.browser.isAndroidBrowser());
assertVersion('4.0');
assertVersionBetween('3.0', '5.0');
setGlobalUAString(goog.labs.userAgent.testAgents.ANDROID_BROWSER_233);
assertTrue(goog.labs.userAgent.browser.isAndroidBrowser());
assertVersion('4.0');
assertVersionBetween('3.0', '5.0');
}
function setGlobalUAString(uaString) {
var mockGlobal = {
'navigator': {
'userAgent': uaString
}
};
propertyReplacer.set(goog, 'global', mockGlobal);
}
function setDocumentMode(docMode) {
var mockDocument = {
'documentMode': docMode
};
propertyReplacer.set(goog.global, 'document', mockDocument);
}
function assertVersion(version) {
assertEquals(version, goog.labs.userAgent.browser.getVersion());
}
function assertVersionBetween(lowVersion, highVersion) {
assertTrue(goog.labs.userAgent.browser.isVersionOrHigher(lowVersion));
assertFalse(goog.labs.userAgent.browser.isVersionOrHigher(highVersion));
}

View File

@@ -0,0 +1,64 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Closure user device detection (based on user agent).
* @see http://en.wikipedia.org/wiki/User_agent
* For more information on browser brand, platform, or engine see the other
* sub-namespaces in goog.labs.userAgent (browser, platform, and engine).
*
*/
goog.provide('goog.labs.userAgent.device');
goog.require('goog.labs.userAgent.util');
/**
* Currently we detect the iPhone, iPod and Android mobiles (devices that have
* both Android and Mobile in the user agent string).
*
* @return {boolean} Whether the user is using a tablet.
*/
goog.labs.userAgent.device.isMobile = goog.memoize(function() {
return !goog.labs.userAgent.device.isTablet() &&
(goog.labs.userAgent.util.matchUserAgent('iPod') ||
goog.labs.userAgent.util.matchUserAgent('iPhone') ||
goog.labs.userAgent.util.matchUserAgent('Android'));
});
/**
* Currently we detect Kindle Fire, iPad, and Android tablets (devices that have
* Android but not Mobile in the user agent string).
*
* @return {boolean} Whether the user is using a tablet.
*/
goog.labs.userAgent.device.isTablet = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('iPad') ||
(goog.labs.userAgent.util.matchUserAgent('Android') &&
!goog.labs.userAgent.util.matchUserAgent('Mobile')) ||
goog.labs.userAgent.util.matchUserAgent('Silk');
});
/**
* Currently we detect Kindle Fire, iPad, and Android tablets.
*
* @return {boolean} Whether the user is using a tablet.
*/
goog.labs.userAgent.device.isDesktop = goog.memoize(function() {
return !goog.labs.userAgent.device.isMobile() &&
!goog.labs.userAgent.device.isTablet();
});

View File

@@ -0,0 +1,88 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.labs.userAgent.device.
*/
goog.provide('goog.labs.userAgent.deviceTest');
goog.require('goog.labs.userAgent.device');
goog.require('goog.labs.userAgent.testAgents');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.labs.userAgent.deviceTest');
var propertyReplacer = new goog.testing.PropertyReplacer();
function setUp() {
// disable memoization
propertyReplacer.set(goog.memoize, 'ENABLE_MEMOIZE', false);
}
function tearDown() {
propertyReplacer.reset();
}
function setGlobalUAString(uaString) {
var mockGlobal = {
'navigator': {
'userAgent': uaString
}
};
propertyReplacer.set(goog, 'global', mockGlobal);
}
function testMobile() {
assertIsMobile(goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
assertIsMobile(goog.labs.userAgent.testAgents.CHROME_ANDROID);
assertIsMobile(goog.labs.userAgent.testAgents.SAFARI_IPHONE);
}
function testTablet() {
assertIsTablet(goog.labs.userAgent.testAgents.CHROME_ANDROID_TABLET);
assertIsTablet(goog.labs.userAgent.testAgents.KINDLE_FIRE);
assertIsTablet(goog.labs.userAgent.testAgents.IPAD_6);
}
function testDesktop() {
assertIsDesktop(goog.labs.userAgent.testAgents.CHROME_25);
assertIsDesktop(goog.labs.userAgent.testAgents.OPERA_10);
assertIsDesktop(goog.labs.userAgent.testAgents.FIREFOX_19);
assertIsDesktop(goog.labs.userAgent.testAgents.IE_9);
assertIsDesktop(goog.labs.userAgent.testAgents.IE_10);
assertIsDesktop(goog.labs.userAgent.testAgents.IE_11);
}
function assertIsMobile(uaString) {
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.device.isMobile());
assertFalse(goog.labs.userAgent.device.isTablet());
assertFalse(goog.labs.userAgent.device.isDesktop());
}
function assertIsTablet(uaString) {
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.device.isTablet());
assertFalse(goog.labs.userAgent.device.isMobile());
assertFalse(goog.labs.userAgent.device.isDesktop());
}
function assertIsDesktop(uaString) {
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.device.isDesktop());
assertFalse(goog.labs.userAgent.device.isMobile());
assertFalse(goog.labs.userAgent.device.isTablet());
}

View File

@@ -0,0 +1,144 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Closure user agent detection.
* @see http://en.wikipedia.org/wiki/User_agent
* For more information on browser brand, platform, or device see the other
* sub-namespaces in goog.labs.userAgent (browser, platform, and device).
*
*/
goog.provide('goog.labs.userAgent.engine');
goog.require('goog.array');
goog.require('goog.labs.userAgent.util');
goog.require('goog.memoize');
goog.require('goog.string');
/**
* Returns the user agent string.
*
* @return {?string} The user agent string.
*/
goog.labs.userAgent.engine.getUserAgentString = goog.memoize(function() {
return goog.global['navigator'] ? goog.global['navigator'].userAgent : null;
});
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string.
* @private
*/
goog.labs.userAgent.engine.matchUserAgent_ = function(str) {
var userAgentString = goog.labs.userAgent.engine.getUserAgentString();
return Boolean(userAgentString && goog.string.contains(userAgentString, str));
};
/**
* @return {boolean} Whether the rendering engine is Presto.
*/
goog.labs.userAgent.engine.isPresto = goog.memoize(
goog.partial(goog.labs.userAgent.engine.matchUserAgent_, 'Presto'));
/**
* @return {boolean} Whether the rendering engine is Trident.
*/
goog.labs.userAgent.engine.isTrident = goog.memoize(
goog.partial(goog.labs.userAgent.engine.matchUserAgent_, 'Trident'));
/**
* @return {boolean} Whether the rendering engine is WebKit.
*/
goog.labs.userAgent.engine.isWebKit = goog.memoize(
goog.partial(goog.labs.userAgent.engine.matchUserAgent_, 'WebKit'));
/**
* @return {boolean} Whether the rendering engine is Gecko.
*/
goog.labs.userAgent.engine.isGecko = goog.memoize(
goog.partial(goog.labs.userAgent.engine.matchUserAgent_, 'Gecko'));
/**
* @return {string} The rendering engine's version or empty string if version
* can't be determined.
*/
goog.labs.userAgent.engine.getVersion = goog.memoize(function() {
var userAgentString = goog.labs.userAgent.engine.getUserAgentString();
if (userAgentString) {
var tuples = goog.labs.userAgent.util.extractVersionTuples(userAgentString);
var engineTuple = tuples[1];
if (engineTuple) {
// In Gecko, the version string is either in the browser info or the
// Firefox version. See Gecko user agent string reference:
// http://goo.gl/mULqa
if (engineTuple[0] == 'Gecko') {
return goog.labs.userAgent.engine.getVersionForKey_(tuples, 'Firefox');
}
return engineTuple[1];
}
// IE has only one version identifier, and the Trident version is
// specified in the parenthetical.
var browserTuple = tuples[0];
var info;
if (browserTuple && (info = browserTuple[2])) {
var match = /Trident\/([^\s;]+)/.exec(info);
if (match) {
return match[1];
}
}
return '';
}
});
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the rendering engine version is higher or the same
* as the given version.
*/
goog.labs.userAgent.engine.isVersionOrHigher = function(version) {
return goog.string.compareVersions(goog.labs.userAgent.engine.getVersion(),
version) >= 0;
};
/**
* @param {!Array.<string>} tuples Version tuples.
* @param {string} key The key to look for.
* @return {string} The version string of the given key, if present.
* Otherwise, the empty string.
* @private
*/
goog.labs.userAgent.engine.getVersionForKey_ = function(tuples, key) {
// TODO(nnaze): Move to util if useful elsewhere.
var pair = goog.array.find(tuples, function(pair) {
return key == pair[0];
});
return pair && pair[1] || '';
};

View File

@@ -0,0 +1,164 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.labs.userAgent.engine.
*/
goog.provide('goog.labs.userAgent.engineTest');
goog.require('goog.labs.userAgent.engine');
goog.require('goog.labs.userAgent.testAgents');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.labs.userAgent.engineTest');
var propertyReplacer = new goog.testing.PropertyReplacer();
function setUp() {
// disable memoization
propertyReplacer.set(goog.memoize, 'ENABLE_MEMOIZE', false);
}
function tearDown() {
propertyReplacer.reset();
}
function setGlobalUAString(uaString) {
var mockGlobal = {
'navigator': {
'userAgent': uaString
}
};
propertyReplacer.set(goog, 'global', mockGlobal);
}
function assertVersion(version) {
assertEquals(version, goog.labs.userAgent.engine.getVersion());
}
function assertLowAndHighVersions(lowVersion, highVersion) {
assertTrue(goog.labs.userAgent.engine.isVersionOrHigher(lowVersion));
assertFalse(goog.labs.userAgent.engine.isVersionOrHigher(highVersion));
}
function testPresto() {
setGlobalUAString(
'Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.00');
assertTrue(goog.labs.userAgent.engine.isPresto());
assertVersion('2.9.181');
assertLowAndHighVersions('2.9', '2.10');
setGlobalUAString(
'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168' +
' Version/11.52');
assertTrue(goog.labs.userAgent.engine.isPresto());
assertVersion('2.9.168');
assertLowAndHighVersions('2.9', '2.10');
setGlobalUAString(
'Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11');
assertTrue(goog.labs.userAgent.engine.isPresto());
assertVersion('2.8.131');
assertLowAndHighVersions('2.8', '2.9');
}
function testTrident() {
setGlobalUAString(
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; ' +
'WOW64; Trident/6.0)');
assertTrue(goog.labs.userAgent.engine.isTrident());
assertVersion('6.0');
assertLowAndHighVersions('6.0', '7.0');
setGlobalUAString(
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; ' +
'Trident/4.0; SLCC2; Media Center PC 6.0; InfoPath.2; MS-RTC LM 8)');
assertTrue(goog.labs.userAgent.engine.isTrident());
assertVersion('4.0');
assertLowAndHighVersions('4.0', '5.0');
setGlobalUAString(
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)');
assertTrue(goog.labs.userAgent.engine.isTrident());
assertVersion('5.0');
assertLowAndHighVersions('5.0', '6.0');
setGlobalUAString(goog.labs.userAgent.testAgents.IE_11);
assertTrue(goog.labs.userAgent.engine.isTrident());
assertVersion('7.0');
assertLowAndHighVersions('6.0', '8.0');
}
function testWebKit() {
setGlobalUAString(
'Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40)' +
'AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1');
assertTrue(goog.labs.userAgent.engine.isWebKit());
assertVersion('533.1');
assertLowAndHighVersions('533.0', '534.0');
setGlobalUAString(
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) ' +
'AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.370.0 Safari/533.4');
assertTrue(goog.labs.userAgent.engine.isWebKit());
assertVersion('533.4');
assertLowAndHighVersions('533.0', '534.0');
setGlobalUAString(
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) ' +
'AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.370.0 Safari/533.4');
assertTrue(goog.labs.userAgent.engine.isWebKit());
assertVersion('533.4');
assertLowAndHighVersions('533.0', '534.0');
setGlobalUAString(
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 ' +
'(KHTML, like Gecko) Version/5.1.3 Safari/534.53.10');
assertTrue(goog.labs.userAgent.engine.isWebKit());
assertVersion('534.55.3');
assertLowAndHighVersions('534.0', '535.0');
setGlobalUAString(
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 ' +
'(KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15');
assertTrue(goog.labs.userAgent.engine.isWebKit());
assertVersion('537.15');
assertLowAndHighVersions('537.0', '538.0');
}
function testGecko() {
setGlobalUAString(
'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2');
assertTrue(goog.labs.userAgent.engine.isGecko());
assertVersion('15.0a2');
assertLowAndHighVersions('14.0', '16.0');
// This is actually not at V15 because it is alpha 2
assertFalse(goog.labs.userAgent.engine.isVersionOrHigher('15'));
setGlobalUAString(
'Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 ' +
'Firefox/16.0.1');
assertTrue(goog.labs.userAgent.engine.isGecko());
assertVersion('16.0.1');
assertLowAndHighVersions('16.0', '17.0');
setGlobalUAString('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) ' +
'Gecko/20100101 Firefox/14.0.1');
assertTrue(goog.labs.userAgent.engine.isGecko());
assertVersion('14.0.1');
assertLowAndHighVersions('14.0', '15.0');
}

View File

@@ -0,0 +1,193 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Closure user agent platform detection.
* @see <a href="http://www.useragentstring.com/">User agent strings</a>
* For more information on browser brand, rendering engine, or device see the
* other sub-namespaces in goog.labs.userAgent (browser, engine, and device
* respectively).
*
*/
goog.provide('goog.labs.userAgent.platform');
goog.require('goog.labs.userAgent.util');
goog.require('goog.memoize');
goog.require('goog.string');
/**
* Returns the platform string.
*
* @return {string} The platform string.
*/
goog.labs.userAgent.platform.getPlatformString = goog.memoize(function() {
return goog.global['navigator'] && goog.global['navigator'].platform ?
goog.global['navigator'].platform : '';
});
/**
* Returns the appVersion string.
*
* @return {string} The appVersion string.
*/
goog.labs.userAgent.platform.getAppVersion = goog.memoize(function() {
return goog.global['navigator'] && goog.global['navigator'].appVersion ?
goog.global['navigator'].appVersion : '';
});
/**
* @param {string} str
* @return {boolean} Whether the platform contains the given string.
* @private
*/
goog.labs.userAgent.platform.matchPlatform_ = function(str) {
var platformString = goog.labs.userAgent.platform.getPlatformString();
return goog.string.contains(platformString, str);
};
/**
* @return {boolean} Whether the platform is Android.
*/
goog.labs.userAgent.platform.isAndroid = goog.memoize(
goog.partial(goog.labs.userAgent.util.matchUserAgent, 'Android'));
/**
* @return {boolean} Whether the platform is iPod.
*/
goog.labs.userAgent.platform.isIpod = goog.memoize(
goog.partial(goog.labs.userAgent.util.matchUserAgent, 'iPod'));
/**
* @return {boolean} Whether the platform is iPhone.
*/
goog.labs.userAgent.platform.isIphone = goog.memoize(function() {
return goog.labs.userAgent.util.matchUserAgent('iPhone') &&
!goog.labs.userAgent.util.matchUserAgent('iPod');
});
/**
* @return {boolean} Whether the platform is iPad.
*/
goog.labs.userAgent.platform.isIpad = goog.memoize(
goog.partial(goog.labs.userAgent.util.matchUserAgent, 'iPad'));
/**
* @return {boolean} Whether the platform is iOS.
*/
goog.labs.userAgent.platform.isIos = goog.memoize(function() {
return goog.labs.userAgent.platform.isIphone() ||
goog.labs.userAgent.platform.isIpad() ||
goog.labs.userAgent.platform.isIpod();
});
/**
* @return {boolean} Whether the platform is Mac.
*/
goog.labs.userAgent.platform.isMac = goog.memoize(
goog.partial(goog.labs.userAgent.platform.matchPlatform_, 'Mac'));
/**
* @return {boolean} Whether the platform is Linux.
*/
goog.labs.userAgent.platform.isLinux = goog.memoize(
goog.partial(goog.labs.userAgent.platform.matchPlatform_, 'Linux'));
/**
* @return {boolean} Whether the platform is Windows.
*/
goog.labs.userAgent.platform.isWindows = goog.memoize(
goog.partial(goog.labs.userAgent.platform.matchPlatform_, 'Win'));
/**
* @return {boolean} Whether the platform is X11.
*/
goog.labs.userAgent.platform.isX11 = goog.memoize(function() {
var appVersion = goog.labs.userAgent.platform.getAppVersion();
return goog.string.contains(appVersion, 'X11');
});
/**
* @return {boolean} Whether the platform is ChromeOS.
*/
goog.labs.userAgent.platform.isChromeOS = goog.memoize(
goog.partial(goog.labs.userAgent.util.matchUserAgent, 'CrOS'));
/**
* The version of the platform. We only determine the version for Windows,
* Mac, and Chrome OS. It doesn't make much sense on Linux. For Windows, we only
* look at the NT version. Non-NT-based versions (e.g. 95, 98, etc.) are given
* version 0.0.
*
* @return {string} The platform version or empty string if version cannot be
* determined.
*/
goog.labs.userAgent.platform.getVersion = function() {
var userAgentString = goog.labs.userAgent.util.getUserAgentString();
var version = '', re;
if (goog.labs.userAgent.platform.isWindows()) {
re = /Windows NT ([0-9.]+)/;
var match = re.exec(userAgentString);
if (match) {
version = match[1];
} else {
version = '0.0';
}
} else if (goog.labs.userAgent.platform.isMac()) {
re = /Mac OS X ([0-9_.]+)/;
var match = re.exec(userAgentString);
// Note: some old versions of Camino do not report an OSX version.
// Default to 10.
version = match ? match[1].replace(/_/g, '.') : '10';
} else if (goog.labs.userAgent.platform.isAndroid()) {
re = /Android\s+([^\);]+)(\)|;)/;
var match = re.exec(userAgentString);
version = match && match[1];
} else if (goog.labs.userAgent.platform.isIos()) {
re = /(?:iPhone|iPod|iPad|CPU)\s+OS\s+(\S+)/;
var match = re.exec(userAgentString);
// Report the version as x.y.z and not x_y_z
version = match && match[1].replace(/_/g, '.');
} else if (goog.labs.userAgent.platform.isChromeOS()) {
re = /(?:CrOS\s+(?:i686|x86_64)\s+([0-9.]+))/;
var match = re.exec(userAgentString);
version = match && match[1];
}
return version || '';
};
/**
* @param {string|number} version The version to check.
* @return {boolean} Whether the browser version is higher or the same as the
* given version.
*/
goog.labs.userAgent.platform.isVersionOrHigher = function(version) {
return goog.string.compareVersions(goog.labs.userAgent.platform.getVersion(),
version) >= 0;
};

View File

@@ -0,0 +1,256 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.labs.userAgent.platform.
*/
goog.provide('goog.labs.userAgent.platformTest');
goog.require('goog.labs.userAgent.platform');
goog.require('goog.labs.userAgent.testAgents');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.labs.userAgent.platformTest');
var propertyReplacer = new goog.testing.PropertyReplacer();
function setUp() {
// disable memoization
propertyReplacer.set(goog.memoize, 'ENABLE_MEMOIZE', false);
}
function tearDown() {
propertyReplacer.reset();
}
function setGlobalUAString(uaString, platform, appVersion) {
var mockGlobal = {
'navigator': {
'userAgent': uaString,
'platform': platform,
'appVersion': appVersion
}
};
propertyReplacer.set(goog, 'global', mockGlobal);
}
function testAndroid() {
var uaString = goog.labs.userAgent.testAgents.ANDROID_BROWSER_233;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isAndroid());
assertVersion('2.3.3');
assertVersionBetween('2.3.0', '2.3.5');
assertVersionBetween('2.3', '2.4');
assertVersionBetween('2', '3');
uaString = goog.labs.userAgent.testAgents.ANDROID_BROWSER_221;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isAndroid());
assertVersion('2.2.1');
assertVersionBetween('2.2.0', '2.2.5');
assertVersionBetween('2.2', '2.3');
assertVersionBetween('2', '3');
uaString = goog.labs.userAgent.testAgents.CHROME_ANDROID;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isAndroid());
assertVersion('4.0.2');
assertVersionBetween('4.0.0', '4.1.0');
assertVersionBetween('4.0', '4.1');
assertVersionBetween('4', '5');
}
function testIpod() {
var uaString = goog.labs.userAgent.testAgents.SAFARI_IPOD;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIpod());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('');
}
function testIphone() {
var uaString = goog.labs.userAgent.testAgents.SAFARI_IPHONE;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIphone());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('4.2.1');
assertVersionBetween('4', '5');
assertVersionBetween('4.2', '4.3');
uaString = goog.labs.userAgent.testAgents.IPHONE_6;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIphone());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('6.0');
assertVersionBetween('5', '7');
uaString = goog.labs.userAgent.testAgents.SAFARI_IPHONE_32;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIphone());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('3.2');
assertVersionBetween('3', '4');
}
function testIpad() {
var uaString = goog.labs.userAgent.testAgents.IPAD_4;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIpad());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('3.2');
assertVersionBetween('3', '4');
assertVersionBetween('3.1', '4');
uaString = goog.labs.userAgent.testAgents.IPAD_5;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIpad());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('5.1');
assertVersionBetween('5', '6');
uaString = goog.labs.userAgent.testAgents.IPAD_6;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isIpad());
assertTrue(goog.labs.userAgent.platform.isIos());
assertVersion('6.0');
assertVersionBetween('5', '7');
}
function testMac() {
var uaString = goog.labs.userAgent.testAgents.CHROME_MAC;
var platform = 'IntelMac';
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isMac());
assertVersion('10.8.2');
assertVersionBetween('10', '11');
assertVersionBetween('10.8', '10.9');
assertVersionBetween('10.8.1', '10.8.3');
uaString = goog.labs.userAgent.testAgents.OPERA_MAC;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isMac());
assertVersion('10.6.8');
assertVersionBetween('10', '11');
assertVersionBetween('10.6', '10.7');
assertVersionBetween('10.6.5', '10.7.0');
uaString = goog.labs.userAgent.testAgents.SAFARI_MAC;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isMac());
assertVersionBetween('10', '11');
assertVersionBetween('10.6', '10.7');
assertVersionBetween('10.6.5', '10.7.0');
uaString = goog.labs.userAgent.testAgents.FIREFOX_MAC;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isMac());
assertVersion('11.7.9');
assertVersionBetween('11', '12');
assertVersionBetween('11.7', '11.8');
assertVersionBetween('11.7.9', '11.8.0');
}
function testLinux() {
var uaString = goog.labs.userAgent.testAgents.FIREFOX_LINUX;
var platform = 'Linux';
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isLinux());
assertVersion('');
uaString = goog.labs.userAgent.testAgents.CHROME_LINUX;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isLinux());
assertVersion('');
uaString = goog.labs.userAgent.testAgents.OPERA_LINUX;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isLinux());
assertVersion('');
}
function testWindows() {
var uaString = goog.labs.userAgent.testAgents.SAFARI_WINDOWS;
var platform = 'Win32';
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isWindows());
assertVersion('6.1');
assertVersionBetween('6', '7');
uaString = goog.labs.userAgent.testAgents.IE_10;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isWindows());
assertVersion('6.1');
assertVersionBetween('6', '6.5');
uaString = goog.labs.userAgent.testAgents.CHROME_25;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isWindows());
assertVersion('5.1');
assertVersionBetween('5', '6');
uaString = goog.labs.userAgent.testAgents.FIREFOX_WINDOWS;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isWindows());
assertVersion('6.1');
assertVersionBetween('6', '7');
uaString = goog.labs.userAgent.testAgents.IE_11;
setGlobalUAString(uaString, platform);
assertTrue(goog.labs.userAgent.platform.isWindows());
assertVersion('6.3');
assertVersionBetween('6', '6.5');
}
function testX11() {
var uaString = goog.labs.userAgent.testAgents.CHROME_LINUX;
var platform = 'Linux';
var appVersion = goog.labs.userAgent.testAgents.CHROME_LINUX_APPVERVERSION;
setGlobalUAString(uaString, platform, appVersion);
assertTrue(goog.labs.userAgent.platform.isX11());
assertVersion('');
}
function testChromeOS() {
var uaString = goog.labs.userAgent.testAgents.CHROME_OS_910;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isChromeOS());
assertVersion('9.10.0');
assertVersionBetween('9', '10');
uaString = goog.labs.userAgent.testAgents.CHROME_OS;
setGlobalUAString(uaString);
assertTrue(goog.labs.userAgent.platform.isChromeOS());
assertVersion('3701.62.0');
assertVersionBetween('3701', '3702');
}
function assertVersion(version) {
assertEquals(version, goog.labs.userAgent.platform.getVersion());
}
function assertVersionBetween(lowVersion, highVersion) {
assertTrue(goog.labs.userAgent.platform.isVersionOrHigher(lowVersion));
assertFalse(goog.labs.userAgent.platform.isVersionOrHigher(highVersion));
}

View File

@@ -0,0 +1,272 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS-IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Various User-Agent strings.
* See http://go/useragentexamples and http://www.useragentstring.com/ for
* examples.
*
*/
goog.provide('goog.labs.userAgent.testAgents');
goog.setTestOnly('goog.labs.userAgent.testAgents');
goog.scope(function() {
var testAgents = goog.labs.userAgent.testAgents;
/** @const {string} */
testAgents.ANDROID_BROWSER_235 =
'Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; ' +
'HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) ' +
'Version/4.0 Mobile Safari/533.1';
/** @const {string} */
testAgents.ANDROID_BROWSER_221 =
'Mozilla/5.0 (Linux; U; Android 2.2.1; en-ca; LG-P505R Build/FRG83)' +
' AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
/** @const {string} */
testAgents.ANDROID_BROWSER_233 =
'Mozilla/5.0 (Linux; U; Android 2.3.3; en-us; HTC_DesireS_S510e' +
' Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0' +
' Mobile Safari/533.1';
/** @const {string} */
testAgents.ANDROID_BROWSER_403 =
'Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K)' +
' AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30';
/** @const {string} */
testAgents.IE_6 =
'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1;' +
'.NET CLR 2.0.50727)';
/** @const {string} */
testAgents.IE_8 =
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)';
/** @const {string} */
testAgents.IE_8_COMPATIBILITY =
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)';
/** @const {string} */
testAgents.IE_9 =
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)';
/** @const {string} */
testAgents.IE_9_COMPATIBILITY =
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0)';
/** @const {string} */
testAgents.IE_10 =
'Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0;' +
' InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;' +
' .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0';
/** @const {string} */
testAgents.IE_11 =
'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko';
/** @const {string} */
testAgents.FIREFOX_19 =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) ' +
'Gecko/20100101 Firefox/19.0';
/** @const {string} */
testAgents.FIREFOX_LINUX =
'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101' +
' Firefox/15.0.1';
/** @const {string} */
testAgents.FIREFOX_MAC =
'Mozilla/6.0 (Macintosh; I; Intel Mac OS X 11_7_9; de-LI; rv:1.9b4)' +
' Gecko/2012010317 Firefox/10.0a4';
/** @const {string} */
testAgents.FIREFOX_WINDOWS =
'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507' +
' Firefox/14.0.1';
/** @const {string} */
testAgents.SAFARI_6 =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) ' +
'AppleWebKit/536.25 (KHTML, like Gecko) ' +
'Version/6.0 Safari/536.25';
/** @const {string} */
testAgents.SAFARI_IPHONE =
'Mozilla/5.0 (iPhone; U; ru; CPU iPhone OS 4_2_1 like Mac OS X; ru)' +
' AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a' +
' Safari/6533.18.5';
/** @const {string} */
testAgents.SAFARI_IPHONE_431 =
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1 like Mac OS X; zh-tw)' +
' AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4' +
' Safari/6533.18.5';
/** @const {string} */
testAgents.SAFARI_IPHONE_32 =
'Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us)' +
' AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314' +
' Safari/531.21.10';
/** @const {string} */
testAgents.SAFARI_IPOD =
'Mozila/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1' +
' (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3';
/** @const {string} */
testAgents.SAFARI_MAC =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+' +
' (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2';
/** @const {string} */
testAgents.SAFARI_WINDOWS =
'Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25' +
' (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27';
/** @const {string} */
testAgents.OPERA_10 =
'Opera/9.80 (S60; SymbOS; Opera Mobi/447; U; en) ' +
'Presto/2.4.18 Version/10.00';
/** @const {string} */
testAgents.OPERA_LINUX =
'Opera/9.80 (X11; Linux x86_64; U; fr) Presto/2.9.168 Version/11.50';
/** @const {string} */
testAgents.OPERA_MAC =
'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168' +
' Version/11.52';
/** @const {string} */
testAgents.IPHONE_6 =
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 ' +
'like Mac OS X) AppleWebKit/536.26 ' +
'(KHTML, like Gecko) Mobile/10A5376e';
/** @const {string} */
testAgents.IPAD_4 =
'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us)' +
' AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b' +
' Safari/531.21.10';
/** @const {string} */
testAgents.IPAD_5 =
'Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X; en-us) AppleWebKit/534.46' +
' (KHTML, like Gecko) Version/5.1 Mobile/9B176 Safari/7534.48.3';
/** @const {string} */
testAgents.IPAD_6 =
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) ' +
'AppleWebKit/536.26 (KHTML, like Gecko) ' +
'Version/6.0 Mobile/10A403 Safari/8536.25';
/** @const {string} */
testAgents.CHROME_25 =
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) ' +
'AppleWebKit/535.8 (KHTML, like Gecko) ' +
'Chrome/25.0.1000.10 Safari/535.8';
/** @const {string} */
testAgents.CHROME_ANDROID =
'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) ' +
'AppleWebKit/535.7 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile ' +
'Safari/535.7';
/** @const {string} */
testAgents.CHROME_ANDROID_TABLET =
'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) ' +
'AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Safari/535.19';
/** @const {string} */
testAgents.CHROME_IPHONE =
'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X; en-us) ' +
'AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/22.0.1194.0 Mobile/11E53 ' +
'Safari/7534.48.3';
/** @const {string} */
testAgents.CHROME_LINUX =
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko)' +
' Chrome/26.0.1410.33 Safari/537.31';
/**
* We traditionally use Appversion to detect X11
* @const {string}
*/
testAgents.CHROME_LINUX_APPVERVERSION =
'5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko)' +
' Chrome/26.0.1410.33 Safari/537.31';
/** @const {string} */
testAgents.CHROME_MAC =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17' +
' (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17';
/** @const {string} */
testAgents.CHROME_OS =
'Mozilla/5.0 (X11; CrOS x86_64 3701.62.0) AppleWebKit/537.31 ' +
'(KHTML, like Gecko) Chrome/26.0.1410.40 Safari/537.31';
/** @const {string} */
testAgents.CHROME_OS_910 =
'Mozilla/5.0 (X11; U; CrOS i686 9.10.0; en-US) AppleWebKit/532.5' +
' (KHTML, like Gecko) Chrome/4.0.253.0 Safari/532.5';
/** @const {string} */
testAgents.KINDLE_FIRE =
'Mozilla/5.0 (Linux; U; Android 4.0.3; en-us; KFTT Build/IML74K)' +
' AppleWebKit/535.19 (KHTML, like Gecko) Silk/2.1 Mobile Safari/535.19' +
' Silk-Accelerated=true';
}); // goog.scope

View File

@@ -0,0 +1,89 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities used by goog.labs.userAgent tools. These functions
* should not be used outside of goog.labs.userAgent.*.
*
* @visibility {//visibility:private}
* @author nnaze@google.com (Nathan Naze)
*/
goog.provide('goog.labs.userAgent.util');
goog.require('goog.memoize');
goog.require('goog.string');
/**
* Returns the user agent string.
*
* @return {string} The user agent string.
*/
goog.labs.userAgent.util.getUserAgentString = goog.memoize(function() {
return goog.global['navigator'] ? goog.global['navigator'].userAgent : '';
});
/**
* @param {string} str
* @return {boolean} Whether the user agent contains the given string.
*/
goog.labs.userAgent.util.matchUserAgent = function(str) {
var userAgentString = goog.labs.userAgent.util.getUserAgentString();
return goog.string.contains(userAgentString, str);
};
/**
* Parses the user agent into tuples for each section.
* @param {string} userAgent
* @return {!Array.<string>} Tuples of key, version, and the contents of the
* parenthetical.
*/
goog.labs.userAgent.util.extractVersionTuples = function(userAgent) {
// Matches each section of a user agent string.
// Example UA:
// Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us)
// AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405
// This has three version tuples: Mozilla, AppleWebKit, and Mobile.
var versionRegExp = new RegExp(
// Key. Note that a key may have a space.
// (i.e. 'Mobile Safari' in 'Mobile Safari/5.0')
'(\\w[\\w ]+)' +
'/' + // slash
'([^\\s]+)' + // version (i.e. '5.0b')
'\\s*' + // whitespace
'(?:\\((.*?)\\))?', // parenthetical info. parentheses not matched.
'g');
var data = [];
var match;
// Iterate and collect the version tuples. Each iteration will be the
// next regex match.
while (match = versionRegExp.exec(userAgent)) {
data.push([
match[1], // key
match[2], // value
// || undefined as this is not undefined in IE7 and IE8
match[3] || undefined // info
]);
}
return data;
};

View File

@@ -0,0 +1,60 @@
// Copyright 2013 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file frexcept in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Unit tests for goog.labs.userAgent.engine.
*/
goog.provide('goog.labs.userAgent.utilTest');
goog.require('goog.labs.userAgent.testAgents');
goog.require('goog.labs.userAgent.util');
goog.require('goog.testing.jsunit');
goog.setTestOnly('goog.labs.userAgent.utilTest');
/**
* Tests parsing a few example UA strings.
*/
function testExtractVersionTuples() {
// Old Android
var tuples = goog.labs.userAgent.util.extractVersionTuples(
goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
assertEquals(4, tuples.length);
assertSameElements(
['Mozilla', '5.0',
'Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40'], tuples[0]);
assertSameElements(['AppleWebKit', '533.1', 'KHTML, like Gecko'], tuples[1]);
assertSameElements(['Version', '4.0', undefined], tuples[2]);
assertSameElements(['Mobile Safari', '533.1', undefined], tuples[3]);
// IE 9
tuples = goog.labs.userAgent.util.extractVersionTuples(
goog.labs.userAgent.testAgents.IE_9);
assertEquals(1, tuples.length);
assertSameElements(
['Mozilla', '5.0',
'compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'], tuples[0]);
// Opera
tuples = goog.labs.userAgent.util.extractVersionTuples(
goog.labs.userAgent.testAgents.OPERA_10);
assertEquals(3, tuples.length);
assertSameElements(['Opera', '9.80', 'S60; SymbOS; Opera Mobi/447; U; en'],
tuples[0]);
assertSameElements(['Presto', '2.4.18', undefined], tuples[1]);
assertSameElements(['Version', '10.00', undefined], tuples[2]);
}