browser_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright 2013 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. /**
  15. * @fileoverview Unit tests for goog.labs.userAgent.browser.
  16. */
  17. goog.provide('goog.labs.userAgent.browserTest');
  18. goog.require('goog.labs.userAgent.browser');
  19. goog.require('goog.labs.userAgent.testAgents');
  20. goog.require('goog.labs.userAgent.util');
  21. goog.require('goog.object');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.labs.userAgent.browserTest');
  24. /*
  25. * Map of browser name to checking method.
  26. * Used by assertBrowser() to verify that only one is true at a time.
  27. */
  28. var Browser = {
  29. ANDROID_BROWSER: goog.labs.userAgent.browser.isAndroidBrowser,
  30. CHROME: goog.labs.userAgent.browser.isChrome,
  31. COAST: goog.labs.userAgent.browser.isCoast,
  32. FIREFOX: goog.labs.userAgent.browser.isFirefox,
  33. OPERA: goog.labs.userAgent.browser.isOpera,
  34. IE: goog.labs.userAgent.browser.isIE,
  35. IOS_WEBVIEW: goog.labs.userAgent.browser.isIosWebview,
  36. SAFARI: goog.labs.userAgent.browser.isSafari,
  37. SILK: goog.labs.userAgent.browser.isSilk,
  38. EDGE: goog.labs.userAgent.browser.isEdge
  39. };
  40. /*
  41. * Assert that the given browser is true and the others are false.
  42. */
  43. function assertBrowser(browser) {
  44. assertTrue(
  45. 'Supplied argument "browser" not in Browser object',
  46. goog.object.containsValue(Browser, browser));
  47. // Verify that the method is true for the given browser
  48. // and false for all others.
  49. goog.object.forEach(Browser, function(f, name) {
  50. if (f == browser) {
  51. assertTrue('Value for browser ' + name, f());
  52. } else {
  53. assertFalse('Value for browser ' + name, f());
  54. }
  55. });
  56. }
  57. function setUp() {
  58. goog.labs.userAgent.util.setUserAgent(null);
  59. }
  60. function testOpera10() {
  61. goog.labs.userAgent.util.setUserAgent(
  62. goog.labs.userAgent.testAgents.OPERA_10);
  63. assertBrowser(Browser.OPERA);
  64. assertVersion('10.00');
  65. assertVersionBetween('10.00', '10.10');
  66. }
  67. function testOperaMac() {
  68. goog.labs.userAgent.util.setUserAgent(
  69. goog.labs.userAgent.testAgents.OPERA_MAC);
  70. assertBrowser(Browser.OPERA);
  71. assertVersion('11.52');
  72. assertVersionBetween('11.50', '12.00');
  73. }
  74. function testOperaLinux() {
  75. goog.labs.userAgent.util.setUserAgent(
  76. goog.labs.userAgent.testAgents.OPERA_LINUX);
  77. assertBrowser(Browser.OPERA);
  78. assertVersion('11.50');
  79. assertVersionBetween('11.00', '12.00');
  80. }
  81. function testOpera15() {
  82. goog.labs.userAgent.util.setUserAgent(
  83. goog.labs.userAgent.testAgents.OPERA_15);
  84. // Opera 15 is Chromium 28. We treat all Chromium variants as Chrome.
  85. assertBrowser(Browser.CHROME);
  86. assertVersion('28.0.1500.52');
  87. assertVersionBetween('28.00', '29.00');
  88. }
  89. function testOperaMini() {
  90. goog.labs.userAgent.util.setUserAgent(
  91. goog.labs.userAgent.testAgents.OPERA_MINI);
  92. assertBrowser(Browser.OPERA);
  93. assertVersion('11.10');
  94. assertVersionBetween('11.00', '12.00');
  95. }
  96. function testIE6() {
  97. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_6);
  98. assertBrowser(Browser.IE);
  99. assertVersion('6.0');
  100. assertVersionBetween('5.0', '7.0');
  101. }
  102. function testIE7() {
  103. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_7);
  104. assertBrowser(Browser.IE);
  105. assertVersion('7.0');
  106. }
  107. function testIE8() {
  108. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_8);
  109. assertBrowser(Browser.IE);
  110. assertVersion('8.0');
  111. assertVersionBetween('7.0', '9.0');
  112. }
  113. function testIE8Compatibility() {
  114. goog.labs.userAgent.util.setUserAgent(
  115. goog.labs.userAgent.testAgents.IE_8_COMPATIBILITY);
  116. assertBrowser(Browser.IE);
  117. assertVersion('8.0');
  118. }
  119. function testIE9() {
  120. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_9);
  121. assertBrowser(Browser.IE);
  122. assertVersion('9.0');
  123. assertVersionBetween('8.0', '10.0');
  124. }
  125. function testIE9Compatibility() {
  126. goog.labs.userAgent.util.setUserAgent(
  127. goog.labs.userAgent.testAgents.IE_9_COMPATIBILITY);
  128. assertBrowser(Browser.IE);
  129. assertVersion('9.0');
  130. }
  131. function testIE10() {
  132. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_10);
  133. assertBrowser(Browser.IE);
  134. assertVersion('10.0');
  135. assertVersionBetween('10.0', '11.0');
  136. }
  137. function testIE10Compatibility() {
  138. goog.labs.userAgent.util.setUserAgent(
  139. goog.labs.userAgent.testAgents.IE_10_COMPATIBILITY);
  140. assertBrowser(Browser.IE);
  141. assertVersion('10.0');
  142. }
  143. function testIE10Mobile() {
  144. goog.labs.userAgent.util.setUserAgent(
  145. goog.labs.userAgent.testAgents.IE_10_MOBILE);
  146. assertBrowser(Browser.IE);
  147. assertVersion('10.0');
  148. }
  149. function testIE11() {
  150. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IE_11);
  151. assertBrowser(Browser.IE);
  152. assertVersion('11.0');
  153. assertVersionBetween('10.0', '12.0');
  154. }
  155. function testIE11CompatibilityMSIE7() {
  156. goog.labs.userAgent.util.setUserAgent(
  157. goog.labs.userAgent.testAgents.IE_11_COMPATIBILITY_MSIE_7);
  158. assertBrowser(Browser.IE);
  159. assertVersion('11.0');
  160. }
  161. function testIE11CompatibilityMSIE9() {
  162. goog.labs.userAgent.util.setUserAgent(
  163. goog.labs.userAgent.testAgents.IE_11_COMPATIBILITY_MSIE_9);
  164. assertBrowser(Browser.IE);
  165. assertVersion('11.0');
  166. }
  167. function testEdge120() {
  168. goog.labs.userAgent.util.setUserAgent(
  169. goog.labs.userAgent.testAgents.EDGE_12_0);
  170. assertBrowser(Browser.EDGE);
  171. assertVersion('12.0');
  172. assertVersionBetween('11.0', '13.0');
  173. }
  174. function testEdge() {
  175. goog.labs.userAgent.util.setUserAgent(
  176. goog.labs.userAgent.testAgents.EDGE_12_9600);
  177. assertBrowser(Browser.EDGE);
  178. assertVersion('12.9600');
  179. assertVersionBetween('11.0', '13.0');
  180. }
  181. function testFirefox19() {
  182. goog.labs.userAgent.util.setUserAgent(
  183. goog.labs.userAgent.testAgents.FIREFOX_19);
  184. assertBrowser(Browser.FIREFOX);
  185. assertVersion('19.0');
  186. assertVersionBetween('18.0', '20.0');
  187. }
  188. function testFirefoxWindows() {
  189. goog.labs.userAgent.util.setUserAgent(
  190. goog.labs.userAgent.testAgents.FIREFOX_WINDOWS);
  191. assertBrowser(Browser.FIREFOX);
  192. assertVersion('14.0.1');
  193. assertVersionBetween('14.0', '15.0');
  194. }
  195. function testFirefoxLinux() {
  196. goog.labs.userAgent.util.setUserAgent(
  197. goog.labs.userAgent.testAgents.FIREFOX_LINUX);
  198. assertBrowser(Browser.FIREFOX);
  199. assertTrue(goog.labs.userAgent.browser.isFirefox());
  200. assertVersion('15.0.1');
  201. }
  202. function testChromeAndroid() {
  203. goog.labs.userAgent.util.setUserAgent(
  204. goog.labs.userAgent.testAgents.CHROME_ANDROID);
  205. assertBrowser(Browser.CHROME);
  206. assertTrue(goog.labs.userAgent.browser.isChrome());
  207. assertVersion('18.0.1025.133');
  208. assertVersionBetween('18.0', '19.0');
  209. assertVersionBetween('17.0', '18.1');
  210. }
  211. function testChromeIphone() {
  212. goog.labs.userAgent.util.setUserAgent(
  213. goog.labs.userAgent.testAgents.CHROME_IPHONE);
  214. assertBrowser(Browser.CHROME);
  215. assertTrue(goog.labs.userAgent.browser.isChrome());
  216. assertVersion('22.0.1194.0');
  217. assertVersionBetween('22.0', '23.0');
  218. assertVersionBetween('22.0', '22.10');
  219. }
  220. function testChromeIpad() {
  221. goog.labs.userAgent.util.setUserAgent(
  222. goog.labs.userAgent.testAgents.CHROME_IPAD);
  223. assertBrowser(Browser.CHROME);
  224. assertTrue(goog.labs.userAgent.browser.isChrome());
  225. assertVersion('32.0.1700.20');
  226. assertVersionBetween('32.0', '33.0');
  227. assertVersionBetween('32.0', '32.10');
  228. }
  229. function testChromeMac() {
  230. goog.labs.userAgent.util.setUserAgent(
  231. goog.labs.userAgent.testAgents.CHROME_MAC);
  232. assertBrowser(Browser.CHROME);
  233. assertTrue(goog.labs.userAgent.browser.isChrome());
  234. assertVersion('24.0.1309.0');
  235. assertVersionBetween('24.0', '25.0');
  236. assertVersionBetween('24.0', '24.10');
  237. }
  238. function testSafariIpad() {
  239. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.IPAD_6);
  240. assertBrowser(Browser.SAFARI);
  241. assertTrue(goog.labs.userAgent.browser.isSafari());
  242. assertVersion('6.0');
  243. assertVersionBetween('5.1', '7.0');
  244. }
  245. function testSafari6() {
  246. goog.labs.userAgent.util.setUserAgent(
  247. goog.labs.userAgent.testAgents.SAFARI_6);
  248. assertBrowser(Browser.SAFARI);
  249. assertTrue(goog.labs.userAgent.browser.isSafari());
  250. assertVersion('6.0');
  251. assertVersionBetween('6.0', '7.0');
  252. }
  253. function testSafariIphone() {
  254. goog.labs.userAgent.util.setUserAgent(
  255. goog.labs.userAgent.testAgents.SAFARI_IPHONE_6);
  256. assertBrowser(Browser.SAFARI);
  257. assertTrue(goog.labs.userAgent.browser.isSafari());
  258. assertVersion('6.0');
  259. assertVersionBetween('5.0', '7.0');
  260. }
  261. function testCoast() {
  262. goog.labs.userAgent.util.setUserAgent(goog.labs.userAgent.testAgents.COAST);
  263. assertBrowser(Browser.COAST);
  264. }
  265. function testWebviewIOS() {
  266. goog.labs.userAgent.util.setUserAgent(
  267. goog.labs.userAgent.testAgents.WEBVIEW_IPHONE);
  268. assertBrowser(Browser.IOS_WEBVIEW);
  269. goog.labs.userAgent.util.setUserAgent(
  270. goog.labs.userAgent.testAgents.WEBVIEW_IPAD);
  271. assertBrowser(Browser.IOS_WEBVIEW);
  272. }
  273. function testAndroidBrowser235() {
  274. goog.labs.userAgent.util.setUserAgent(
  275. goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
  276. assertBrowser(Browser.ANDROID_BROWSER);
  277. assertVersion('4.0');
  278. assertVersionBetween('3.0', '5.0');
  279. }
  280. function testAndroidBrowser403() {
  281. goog.labs.userAgent.util.setUserAgent(
  282. goog.labs.userAgent.testAgents.ANDROID_BROWSER_403);
  283. assertBrowser(Browser.ANDROID_BROWSER);
  284. assertVersion('4.0');
  285. assertVersionBetween('3.0', '5.0');
  286. }
  287. function testAndroidBrowser233() {
  288. goog.labs.userAgent.util.setUserAgent(
  289. goog.labs.userAgent.testAgents.ANDROID_BROWSER_233);
  290. assertBrowser(Browser.ANDROID_BROWSER);
  291. assertVersion('4.0');
  292. assertVersionBetween('3.0', '5.0');
  293. }
  294. function testAndroidWebView411() {
  295. goog.labs.userAgent.util.setUserAgent(
  296. goog.labs.userAgent.testAgents.ANDROID_WEB_VIEW_4_1_1);
  297. assertBrowser(Browser.ANDROID_BROWSER);
  298. assertVersion('4.0');
  299. assertVersionBetween('3.0', '5.0');
  300. }
  301. function testAndroidWebView44() {
  302. goog.labs.userAgent.util.setUserAgent(
  303. goog.labs.userAgent.testAgents.ANDROID_WEB_VIEW_4_4);
  304. assertBrowser(Browser.CHROME);
  305. assertVersion('30.0.0.0');
  306. assertVersionBetween('29.0', '31.0');
  307. }
  308. function testSilk() {
  309. goog.labs.userAgent.util.setUserAgent(
  310. goog.labs.userAgent.testAgents.KINDLE_FIRE);
  311. assertBrowser(Browser.SILK);
  312. assertVersion('2.1');
  313. }
  314. function testFirefoxOnAndroidTablet() {
  315. goog.labs.userAgent.util.setUserAgent(
  316. goog.labs.userAgent.testAgents.FIREFOX_ANDROID_TABLET);
  317. assertBrowser(Browser.FIREFOX);
  318. assertVersion('28.0');
  319. assertVersionBetween('27.0', '29.0');
  320. }
  321. function assertVersion(version) {
  322. assertEquals(version, goog.labs.userAgent.browser.getVersion());
  323. }
  324. function assertVersionBetween(lowVersion, highVersion) {
  325. assertTrue(goog.labs.userAgent.browser.isVersionOrHigher(lowVersion));
  326. assertFalse(goog.labs.userAgent.browser.isVersionOrHigher(highVersion));
  327. }