useragent_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.userAgentTest');
  15. goog.setTestOnly('goog.userAgentTest');
  16. goog.require('goog.array');
  17. goog.require('goog.labs.userAgent.platform');
  18. goog.require('goog.labs.userAgent.testAgents');
  19. goog.require('goog.labs.userAgent.util');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. goog.require('goog.userAgentTestUtil');
  24. var documentMode;
  25. goog.userAgent.getDocumentMode_ = function() {
  26. return documentMode;
  27. };
  28. var propertyReplacer = new goog.testing.PropertyReplacer();
  29. var UserAgents =
  30. {GECKO: 'GECKO', IE: 'IE', OPERA: 'OPERA', WEBKIT: 'WEBKIT', EDGE: 'EDGE'};
  31. function tearDown() {
  32. goog.labs.userAgent.util.setUserAgent(null);
  33. documentMode = undefined;
  34. propertyReplacer.reset();
  35. }
  36. /**
  37. * Test browser detection for a user agent configuration.
  38. * @param {Array<number>} expectedAgents Array of expected userAgents.
  39. * @param {string} uaString User agent string.
  40. * @param {string=} opt_product Navigator product string.
  41. * @param {string=} opt_vendor Navigator vendor string.
  42. */
  43. function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
  44. var mockGlobal = {
  45. 'navigator':
  46. {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor}
  47. };
  48. propertyReplacer.set(goog, 'global', mockGlobal);
  49. goog.labs.userAgent.util.setUserAgent(null);
  50. goog.userAgentTestUtil.reinitializeUserAgent();
  51. for (var ua in UserAgents) {
  52. var isExpected = goog.array.contains(expectedAgents, UserAgents[ua]);
  53. assertEquals(
  54. isExpected,
  55. goog.userAgentTestUtil.getUserAgentDetected(UserAgents[ua]));
  56. }
  57. }
  58. function testOperaInit() {
  59. // Check Opera Mini version strings are detected properly
  60. var mockGlobal = {
  61. 'navigator': {'userAgent': goog.labs.userAgent.testAgents.OPERA_MINI}
  62. };
  63. propertyReplacer.set(goog, 'global', mockGlobal);
  64. propertyReplacer.set(goog.userAgent, 'getUserAgentString', function() {
  65. return goog.labs.userAgent.testAgents.OPERA_MINI;
  66. });
  67. goog.labs.userAgent.util.setUserAgent(null);
  68. goog.userAgentTestUtil.reinitializeUserAgent();
  69. assertTrue(goog.userAgent.OPERA);
  70. assertEquals('11.10', goog.userAgent.VERSION);
  71. // Check Opera + Blink versions are detected as Chromium
  72. mockGlobal = {
  73. 'navigator': {'userAgent': goog.labs.userAgent.testAgents.OPERA_15},
  74. };
  75. propertyReplacer.set(goog, 'global', mockGlobal);
  76. propertyReplacer.set(goog.userAgent, 'getUserAgentString', function() {
  77. return goog.labs.userAgent.testAgents.OPERA_15;
  78. });
  79. goog.labs.userAgent.util.setUserAgent(null);
  80. goog.userAgentTestUtil.reinitializeUserAgent();
  81. // TODO(johnlenz): Chrome/Blink is miscategorized as Webkit
  82. assertTrue(goog.userAgent.WEBKIT);
  83. assertEquals('537.36', goog.userAgent.VERSION);
  84. }
  85. function testCompare() {
  86. assertTrue(
  87. 'exact equality broken', goog.userAgent.compare('1.0', '1.0') == 0);
  88. assertTrue(
  89. 'mutlidot equality broken',
  90. goog.userAgent.compare('1.0.0.0', '1.0') == 0);
  91. assertTrue('less than broken', goog.userAgent.compare('1.0.2.1', '1.1') < 0);
  92. assertTrue(
  93. 'greater than broken', goog.userAgent.compare('1.1', '1.0.2.1') > 0);
  94. assertTrue('b broken', goog.userAgent.compare('1.1', '1.1b') > 0);
  95. assertTrue('b broken', goog.userAgent.compare('1.1b', '1.1') < 0);
  96. assertTrue('b broken', goog.userAgent.compare('1.1b', '1.1b') == 0);
  97. assertTrue('b>a broken', goog.userAgent.compare('1.1b', '1.1a') > 0);
  98. assertTrue('a<b broken', goog.userAgent.compare('1.1a', '1.1b') < 0);
  99. }
  100. function testGecko() {
  101. assertGecko(
  102. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl-NL; rv:1.7.5)' +
  103. 'Gecko/20041202 Gecko/1.0',
  104. '1.7.5');
  105. assertGecko(
  106. 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.6)' +
  107. 'Gecko/20050512 Gecko',
  108. '1.7.6');
  109. assertGecko(
  110. 'Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.8)' +
  111. 'Gecko/20050609 Gecko/1.0.4',
  112. '1.7.8');
  113. assertGecko(
  114. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.9)' +
  115. 'Gecko/20050711 Gecko/1.0.5',
  116. '1.7.9');
  117. assertGecko(
  118. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10)' +
  119. 'Gecko/20050716 Gecko/1.0.6',
  120. '1.7.10');
  121. assertGecko(
  122. 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-GB;' +
  123. 'rv:1.7.10) Gecko/20050717 Gecko/1.0.6',
  124. '1.7.10');
  125. assertGecko(
  126. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12)' +
  127. 'Gecko/20050915 Gecko/1.0.7',
  128. '1.7.12');
  129. assertGecko(
  130. 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US;' +
  131. 'rv:1.7.12) Gecko/20050915 Gecko/1.0.7',
  132. '1.7.12');
  133. assertGecko(
  134. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b4)' +
  135. 'Gecko/20050908 Gecko/1.4',
  136. '1.8b4');
  137. assertGecko(
  138. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8)' +
  139. 'Gecko/20051107 Gecko/1.5',
  140. '1.8');
  141. assertGecko(
  142. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1)' +
  143. 'Gecko/20060111 Gecko/1.5.0.1',
  144. '1.8.0.1');
  145. assertGecko(
  146. 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.0.1)' +
  147. 'Gecko/20060111 Gecko/1.5.0.1',
  148. '1.8.0.1');
  149. assertGecko(
  150. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.2)' +
  151. 'Gecko/20060308 Gecko/1.5.0.2',
  152. '1.8.0.2');
  153. assertGecko(
  154. 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US;' +
  155. 'rv:1.8.0.3) Gecko/20060426 Gecko/1.5.0.3',
  156. '1.8.0.3');
  157. assertGecko(
  158. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.3)' +
  159. 'Gecko/20060426 Gecko/1.5.0.3',
  160. '1.8.0.3');
  161. assertGecko(
  162. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4)' +
  163. 'Gecko/20060508 Gecko/1.5.0.4',
  164. '1.8.0.4');
  165. assertGecko(
  166. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4)' +
  167. 'Gecko/20060508 Gecko/1.5.0.4',
  168. '1.8.0.4');
  169. assertGecko(
  170. 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.0.4)' +
  171. 'Gecko/20060508 Gecko/1.5.0.4',
  172. '1.8.0.4');
  173. assertGecko(
  174. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.0.6)' +
  175. 'Gecko/20060728 Gecko/1.5.0.6',
  176. '1.8.0.6');
  177. assertGecko(
  178. 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.6)' +
  179. 'Gecko/20060808 Fedora/1.5.0.6-2.fc5 Gecko/1.5.0.6 pango-text',
  180. '1.8.0.6');
  181. assertGecko(
  182. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8)' +
  183. 'Gecko/20060321 Gecko/2.0a1',
  184. '1.8');
  185. assertGecko(
  186. 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0', '6.0');
  187. }
  188. function testIe() {
  189. assertIe('Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)', '5.01');
  190. assertIe('Mozilla/4.0 (compatible; MSIE 5.17; Mac_PowerPC)', '5.17');
  191. assertIe('Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)', '5.23');
  192. assertIe('Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)', '5.5');
  193. assertIe('Mozilla/4.0 (compatible; MSIE 6.0; MSN 2.5; Windows 98)', '6.0');
  194. assertIe('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', '6.0');
  195. assertIe(
  196. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; ' +
  197. '.NET CLR 1.1.4322)',
  198. '6.0');
  199. assertIe(
  200. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; ' +
  201. '.NET CLR 2.0.50727)',
  202. '6.0');
  203. assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)', '7.0b');
  204. assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Win32)', '7.0b');
  205. assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)', '7.0b');
  206. assertIe(
  207. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;' +
  208. 'Arcor 5.005; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
  209. '7.0');
  210. assertIe(
  211. 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', '11.0');
  212. }
  213. function testIeDocumentModeOverride() {
  214. documentMode = 9;
  215. assertIe(
  216. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0', '9');
  217. assertIe(
  218. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0', '9');
  219. documentMode = 8;
  220. assertIe(
  221. 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0', '8.0');
  222. }
  223. function testDocumentModeInStandardsMode() {
  224. goog.userAgentTestUtil.reinitializeUserAgent();
  225. var expectedMode =
  226. goog.userAgent.IE ? parseInt(goog.userAgent.VERSION) : undefined;
  227. assertEquals(expectedMode, goog.userAgent.DOCUMENT_MODE);
  228. }
  229. function testEdge() {
  230. var testAgents = goog.labs.userAgent.testAgents;
  231. assertEdge(testAgents.EDGE_12_0, '12.0');
  232. assertEdge(testAgents.EDGE_12_9600, '12.9600');
  233. }
  234. function testOpera() {
  235. var assertOpera = function(uaString) {
  236. assertUserAgent([UserAgents.OPERA], uaString);
  237. };
  238. assertOpera('Opera/7.23 (Windows 98; U) [en]');
  239. assertOpera('Opera/8.00 (Windows NT 5.1; U; en)');
  240. assertOpera('Opera/8.0 (X11; Linux i686; U; cs)');
  241. assertOpera('Opera/8.02 (Windows NT 5.1; U; en)');
  242. assertOpera('Opera/8.50 (Windows NT 5.1; U; en)');
  243. assertOpera('Opera/8.5 (X11; Linux i686; U; cs)');
  244. assertOpera('Opera/8.51 (Windows NT 5.1; U; en)');
  245. assertOpera('Opera/9.0 (Windows NT 5.0; U; en)');
  246. assertOpera('Opera/9.00 (Macintosh; PPC Mac OS X; U; en)');
  247. assertOpera('Opera/9.00 (Windows NT 5.1; U; en)');
  248. assertOpera('Opera/9.00 (Windows NT 5.2; U; en)');
  249. assertOpera('Opera/9.00 (Windows NT 6.0; U; en)');
  250. }
  251. function testWebkit() {
  252. var testAgents = goog.labs.userAgent.testAgents;
  253. assertWebkit(testAgents.ANDROID_BROWSER_403);
  254. assertWebkit(testAgents.ANDROID_BROWSER_403_ALT);
  255. }
  256. function testUnknownBrowser() {
  257. assertUserAgent([], 'MyWebBrowser');
  258. assertUserAgent([], undefined);
  259. }
  260. function testNoNavigator() {
  261. // global object has no "navigator" property.
  262. var mockGlobal = {};
  263. propertyReplacer.set(goog, 'global', mockGlobal);
  264. goog.labs.userAgent.util.setUserAgent(null);
  265. goog.userAgentTestUtil.reinitializeUserAgent();
  266. assertEquals(
  267. 'Platform should be the empty string', '', goog.userAgent.PLATFORM);
  268. assertEquals(
  269. 'Version should be the empty string', '', goog.userAgent.VERSION);
  270. }
  271. function testLegacyChromeOsAndLinux() {
  272. // As a legacy behavior, goog.userAgent.LINUX considers
  273. // ChromeOS to be Linux.
  274. // goog.labs.userAgent.platform.isLinux() does not.
  275. goog.labs.userAgent.util.setUserAgent(
  276. goog.labs.userAgent.testAgents.CHROME_OS);
  277. goog.userAgentTestUtil.reinitializeUserAgent();
  278. assertTrue(goog.userAgent.LINUX);
  279. assertFalse(goog.labs.userAgent.platform.isLinux());
  280. }
  281. function assertIe(uaString, expectedVersion) {
  282. assertUserAgent([UserAgents.IE], uaString);
  283. assertEquals(
  284. 'User agent ' + uaString + ' should have had version ' + expectedVersion +
  285. ' but had ' + goog.userAgent.VERSION,
  286. expectedVersion, goog.userAgent.VERSION);
  287. }
  288. function assertEdge(uaString, expectedVersion) {
  289. assertUserAgent([UserAgents.EDGE], uaString);
  290. assertEquals(
  291. 'User agent ' + uaString + ' should have had version ' + expectedVersion +
  292. ' but had ' + goog.userAgent.VERSION,
  293. expectedVersion, goog.userAgent.VERSION);
  294. }
  295. function assertGecko(uaString, expectedVersion) {
  296. assertUserAgent([UserAgents.GECKO], uaString, 'Gecko');
  297. assertEquals(
  298. 'User agent ' + uaString + ' should have had version ' + expectedVersion +
  299. ' but had ' + goog.userAgent.VERSION,
  300. expectedVersion, goog.userAgent.VERSION);
  301. }
  302. function assertWebkit(uaString) {
  303. assertUserAgent([UserAgents.WEBKIT], uaString, 'WebKit');
  304. }