product_test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright 2008 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.userAgent.productTest');
  15. goog.setTestOnly('goog.userAgent.productTest');
  16. goog.require('goog.array');
  17. goog.require('goog.labs.userAgent.testAgents');
  18. goog.require('goog.labs.userAgent.util');
  19. goog.require('goog.testing.MockUserAgent');
  20. goog.require('goog.testing.PropertyReplacer');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. goog.require('goog.userAgent.product');
  24. goog.require('goog.userAgent.product.isVersion');
  25. goog.require('goog.userAgentTestUtil');
  26. var mockAgent;
  27. var replacer;
  28. function setUp() {
  29. mockAgent = new goog.testing.MockUserAgent();
  30. mockAgent.install();
  31. replacer = new goog.testing.PropertyReplacer();
  32. }
  33. function tearDown() {
  34. replacer.reset();
  35. mockAgent.dispose();
  36. updateUserAgentUtils();
  37. }
  38. function updateUserAgentUtils() {
  39. goog.labs.userAgent.util.setUserAgent(null);
  40. goog.userAgentTestUtil.reinitializeUserAgent();
  41. }
  42. // The set of products whose corresponding goog.userAgent.product value is set
  43. // in goog.userAgent.product.init_().
  44. var DETECTED_BROWSER_KEYS =
  45. ['FIREFOX', 'IPHONE', 'IPAD', 'ANDROID', 'CHROME', 'SAFARI'];
  46. // browserKey should be the constant name, as a string
  47. // 'FIREFOX', 'CHROME', 'ANDROID', etc.
  48. function assertIsBrowser(currentBrowser) {
  49. assertTrue(
  50. 'Current browser key into goog.userAgent.product ' +
  51. 'should be true',
  52. goog.userAgent.product[currentBrowser]);
  53. // Make sure we don't have any false positives for other browsers.
  54. goog.array.forEach(DETECTED_BROWSER_KEYS, function(browserKey) {
  55. // Ignore the iPad/Safari case, as the new code correctly
  56. // identifies the test useragent as both iPad and Safari.
  57. if (currentBrowser == 'IPAD' && browserKey == 'SAFARI') {
  58. return;
  59. }
  60. if (currentBrowser == 'IPHONE' && browserKey == 'SAFARI') {
  61. return;
  62. }
  63. if (currentBrowser == 'CHROME' && browserKey == 'IPHONE') {
  64. return;
  65. }
  66. if (currentBrowser != browserKey) {
  67. assertFalse(
  68. 'Current browser key is ' + currentBrowser +
  69. ' but different key into goog.userAgent.product is true: ' +
  70. browserKey,
  71. goog.userAgent.product[browserKey]);
  72. }
  73. });
  74. }
  75. function assertBrowserAndVersion(userAgent, browser, version) {
  76. mockAgent.setUserAgentString(userAgent);
  77. updateUserAgentUtils();
  78. assertIsBrowser(browser);
  79. assertEquals(
  80. 'User agent should have this version', version, goog.userAgent.VERSION);
  81. }
  82. /**
  83. * @param {Array<{
  84. * ua: string,
  85. * versions: Array<{
  86. * num: (string|number), truth: boolean}>}>} userAgents
  87. * @param {string} browser
  88. */
  89. function checkEachUserAgentDetected(userAgents, browser) {
  90. goog.array.forEach(userAgents, function(ua) {
  91. mockAgent.setUserAgentString(ua.ua);
  92. updateUserAgentUtils();
  93. assertIsBrowser(browser);
  94. // Check versions
  95. goog.array.forEach(ua.versions, function(v) {
  96. mockAgent.setUserAgentString(ua.ua);
  97. updateUserAgentUtils();
  98. assertEquals(
  99. 'Expected version ' + v.num + ' from ' + ua.ua + ' but got ' +
  100. goog.userAgent.product.VERSION,
  101. v.truth, goog.userAgent.product.isVersion(v.num));
  102. });
  103. });
  104. }
  105. function testInternetExplorer() {
  106. var userAgents = [
  107. {
  108. ua: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; ' +
  109. 'chromeframe; .NET CLR 1.1.4322; InfoPath.1; ' +
  110. '.NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; ' +
  111. '.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)',
  112. versions: [
  113. {num: 6, truth: true}, {num: '7.0', truth: true},
  114. {num: 7.1, truth: false}, {num: 8, truth: false}
  115. ]
  116. },
  117. {
  118. ua: 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko',
  119. versions: [
  120. {num: 10, truth: true}, {num: 11, truth: true},
  121. {num: '11.0', truth: true}, {num: '12', truth: false}
  122. ]
  123. }
  124. ];
  125. // hide any navigator.product value by putting in a navigator with no
  126. // properties.
  127. mockAgent.setNavigator({});
  128. checkEachUserAgentDetected(userAgents, 'IE');
  129. }
  130. function testEdge() {
  131. var userAgents = [{
  132. ua: goog.labs.userAgent.testAgents.EDGE_12_9600,
  133. versions: [
  134. {num: 11, truth: true}, {num: 12, truth: true},
  135. {num: '12.96', truth: true}, {num: '12.9600', truth: true},
  136. {num: '12.9601', truth: false}, {num: '12.10240', truth: false},
  137. {num: 13, truth: false}
  138. ]
  139. }];
  140. checkEachUserAgentDetected(userAgents, 'EDGE');
  141. }
  142. function testOpera() {
  143. var opera = {};
  144. var userAgents = [{
  145. ua: 'Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.01',
  146. versions: [
  147. {num: 9, truth: true}, {num: '10.1', truth: true},
  148. {num: 11, truth: false}
  149. ]
  150. }];
  151. replacer.set(goog.global, 'opera', opera);
  152. opera.version = '10.01';
  153. checkEachUserAgentDetected(userAgents, 'OPERA');
  154. }
  155. function testFirefox() {
  156. var userAgents = [
  157. {
  158. ua: 'Mozilla/6.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; ' +
  159. 'rv:2.0.0.0) Gecko/20061028 Firefox/3.0',
  160. versions: [
  161. {num: 2, truth: true}, {num: '3.0', truth: true},
  162. {num: '3.5.3', truth: false}
  163. ]
  164. },
  165. {
  166. ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; ' +
  167. 'rv:1.8.1.4) Gecko/20070515 Firefox/2.0.4',
  168. versions: [
  169. {num: 2, truth: true}, {num: '2.0.4', truth: true},
  170. {num: 3, truth: false}, {num: '3.5.3', truth: false}
  171. ]
  172. },
  173. {
  174. ua: 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0',
  175. versions: [
  176. {num: 6, truth: true}, {num: '6.0', truth: true},
  177. {num: 7, truth: false}, {num: '7.0', truth: false}
  178. ]
  179. }
  180. ];
  181. checkEachUserAgentDetected(userAgents, 'FIREFOX');
  182. // Mozilla reported to us that they plan this UA format starting
  183. // in Firefox 13.
  184. // See bug at https://bugzilla.mozilla.org/show_bug.cgi?id=588909
  185. mockAgent.setNavigator({product: 'Gecko'});
  186. assertBrowserAndVersion(
  187. 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0', 'FIREFOX',
  188. '6.0');
  189. }
  190. function testChrome() {
  191. var userAgents = [
  192. {
  193. ua: 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) ' +
  194. 'AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.0 ' +
  195. 'Safari/525.19',
  196. versions: [{num: '0.2.153', truth: true}, {num: 1, truth: false}]
  197. },
  198. {
  199. ua: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) ' +
  200. 'AppleWebKit/532.3 (KHTML, like Gecko) Chrome/4.0.223.11 ' +
  201. 'Safari/532.3',
  202. versions: [
  203. {num: 4, truth: true}, {num: '0.2.153', truth: true},
  204. {num: '4.1.223.13', truth: false}, {num: '4.0.223.10', truth: true}
  205. ]
  206. },
  207. {
  208. ua: 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B)' +
  209. 'AppleWebKit/535.19 (KHTML, like Gecko) ' +
  210. 'Chrome/18.0.1025.133 Mobile' +
  211. 'Safari/535.19',
  212. versions: [
  213. {num: 18, truth: true}, {num: '0.2.153', truth: true},
  214. {num: 29, truth: false}, {num: '18.0.1025.133', truth: true}
  215. ]
  216. },
  217. {
  218. ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) ' +
  219. 'AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.79 ' +
  220. 'Mobile/14D27 Safari/602.1',
  221. versions: [
  222. {num: '56.1.2924.79', truth: false},
  223. {num: '56.0.2924.79', truth: true}
  224. ]
  225. }
  226. ];
  227. checkEachUserAgentDetected(userAgents, 'CHROME');
  228. }
  229. function testSafari() {
  230. var userAgents = [
  231. {
  232. ua: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; de-de) ' +
  233. 'AppleWebKit/534.16+ (KHTML, like Gecko) Version/5.0.3 ' +
  234. 'Safari/533.19.4',
  235. versions: [
  236. {num: 5, truth: true}, {num: '5.0.3', truth: true},
  237. {num: '5.0.4', truth: false}, {num: '533', truth: false}
  238. ]
  239. },
  240. {
  241. ua: 'Mozilla/5.0 (Windows; U; Windows NT 6.0; pl-PL) ' +
  242. 'AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21',
  243. versions: [
  244. {num: 3, truth: true}, {num: '3.0', truth: true},
  245. {num: '3.1.2', truth: true}
  246. ]
  247. },
  248. {
  249. ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_3; en-us) ' +
  250. 'AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.20',
  251. versions: [
  252. {num: 3, truth: true}, {num: '3.1.1', truth: true},
  253. {num: '3.1.2', truth: false}, {num: '525.21', truth: false}
  254. ]
  255. },
  256. // Safari 1 and 2 do not report product version numbers in their
  257. // user-agent strings. VERSION for these browsers will be set to ''.
  258. {
  259. ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ja-jp) ' +
  260. 'AppleWebKit/418.9.1 (KHTML, like Gecko) Safari/419.3',
  261. versions: [
  262. {num: 3, truth: false}, {num: 2, truth: false}, {num: 1, truth: false},
  263. {num: 0, truth: true}, {num: '0', truth: true},
  264. {num: '', truth: true}
  265. ]
  266. }
  267. ];
  268. checkEachUserAgentDetected(userAgents, 'SAFARI');
  269. }
  270. function testIphone() {
  271. var userAgents = [
  272. {
  273. ua: 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ ' +
  274. '(KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3',
  275. versions: [
  276. {num: '3.0.1A543a', truth: true}, {num: '3.0', truth: true},
  277. {num: '3.0.1B543a', truth: false}, {num: '3.1.1A543a', truth: false},
  278. {num: '3.0.1A320c', truth: true}, {num: '3.0.3A100a', truth: false}
  279. ]
  280. },
  281. {
  282. ua: 'Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 ' +
  283. '(KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3',
  284. versions: [
  285. {num: '3.0.1A543a', truth: true}, {num: '3.0.3A100a', truth: true}
  286. ]
  287. }
  288. ];
  289. checkEachUserAgentDetected(userAgents, 'IPHONE');
  290. }
  291. function testIpad() {
  292. var userAgents = [
  293. {
  294. ua: 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) ' +
  295. 'AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 ' +
  296. 'Mobile/7B334b Safari/531.21.10',
  297. versions: [
  298. {num: '4.0.4.7B334b', truth: true}, {num: '4.0', truth: true},
  299. {num: '4.0.4.7C334b', truth: false}, {num: '4.1.7B334b', truth: false},
  300. {num: '4.0.4.7B320c', truth: true},
  301. {num: '4.0.4.8B334b', truth: false}
  302. ]
  303. },
  304. // Webview in the Facebook iOS app
  305. {
  306. ua: 'Mozilla/5.0 (iPad; CPU OS 8_1 like Mac OS X) AppleWebKit/600.1.4' +
  307. '(KHTML, like Gecko) Mobile/12B410 [FBAN/FBIOS;FBAV/16.0.0.13.22;' +
  308. 'FBBV/4697910;FBDV/iPad3,4;FBMD/iPad;FBSN/iPhone OS;FBSV/8.1;' +
  309. 'FBSS/2; FBCR/;FBID/tablet;FBLC/ja_JP;FBOP/1]',
  310. versions: [{num: '', truth: true}]
  311. }
  312. ];
  313. checkEachUserAgentDetected(userAgents, 'IPAD');
  314. }
  315. function testAndroid() {
  316. var userAgents = [
  317. {
  318. ua: 'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ ' +
  319. '(KHTML, like Gecko) Safari/419.3',
  320. versions: [{num: 0.5, truth: true}, {num: '1.0', truth: false}]
  321. },
  322. {
  323. ua: 'Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) ' +
  324. 'AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile ' +
  325. 'Safari/523.12.2',
  326. versions: [
  327. {num: 0.5, truth: true}, {num: 1, truth: true},
  328. {num: '1.0', truth: true}, {num: '3.0.12', truth: false}
  329. ]
  330. }
  331. ];
  332. checkEachUserAgentDetected(userAgents, 'ANDROID');
  333. }
  334. function testAndroidLegacyBehavior() {
  335. mockAgent.setUserAgentString(
  336. goog.labs.userAgent.testAgents.FIREFOX_ANDROID_TABLET);
  337. updateUserAgentUtils();
  338. // Historically, goog.userAgent.product.ANDROID has referred to the
  339. // Android browser, not the platform. Firefox on Android should
  340. // be false.
  341. assertFalse(goog.userAgent.product.ANDROID);
  342. }
  343. function testSafariIosLegacyBehavior() {
  344. mockAgent.setUserAgentString(goog.labs.userAgent.testAgents.SAFARI_IPHONE_6);
  345. updateUserAgentUtils();
  346. // Historically, goog.userAgent.product.SAFARI has referred to the
  347. // Safari desktop browser, not the mobile browser.
  348. assertFalse(goog.userAgent.product.SAFARI);
  349. }