platform_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2010 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.platformTest');
  15. goog.setTestOnly('goog.userAgent.platformTest');
  16. goog.require('goog.testing.MockUserAgent');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.userAgent');
  19. goog.require('goog.userAgent.platform');
  20. goog.require('goog.userAgentTestUtil');
  21. var mockAgent;
  22. function setUp() {
  23. mockAgent = new goog.testing.MockUserAgent();
  24. mockAgent.install();
  25. }
  26. function tearDown() {
  27. mockAgent.dispose();
  28. updateUserAgentUtils();
  29. }
  30. function updateUserAgentUtils() {
  31. goog.userAgentTestUtil.reinitializeUserAgent();
  32. }
  33. function testWindows() {
  34. mockAgent.setNavigator({platform: 'Win32'});
  35. var win98 = 'Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98; Win 9x 4.90)';
  36. var win2k = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 5.0; en-US)';
  37. var xp = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 5.1; en-US)';
  38. var vista = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
  39. var win7 = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.1; en-US)';
  40. var win81 = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  41. mockAgent.setUserAgentString(win98);
  42. updateUserAgentUtils();
  43. assertEquals('0', goog.userAgent.platform.VERSION);
  44. mockAgent.setUserAgentString(win2k);
  45. updateUserAgentUtils();
  46. assertEquals('5.0', goog.userAgent.platform.VERSION);
  47. mockAgent.setUserAgentString(xp);
  48. updateUserAgentUtils();
  49. assertEquals('5.1', goog.userAgent.platform.VERSION);
  50. mockAgent.setUserAgentString(vista);
  51. updateUserAgentUtils();
  52. assertEquals('6.0', goog.userAgent.platform.VERSION);
  53. mockAgent.setUserAgentString(win7);
  54. updateUserAgentUtils();
  55. assertEquals('6.1', goog.userAgent.platform.VERSION);
  56. mockAgent.setUserAgentString(win81);
  57. updateUserAgentUtils();
  58. assertEquals('6.3', goog.userAgent.platform.VERSION);
  59. }
  60. function testMac() {
  61. // For some reason Chrome substitutes _ for . in the OS version.
  62. var chrome = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US)' +
  63. 'AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.49 Safari/532.5';
  64. var ff = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US;' +
  65. 'rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6';
  66. mockAgent.setNavigator({platform: 'IntelMac'});
  67. mockAgent.setUserAgentString(chrome);
  68. updateUserAgentUtils();
  69. assertEquals('10.5.8', goog.userAgent.platform.VERSION);
  70. mockAgent.setUserAgentString(ff);
  71. updateUserAgentUtils();
  72. assertEquals('10.5', goog.userAgent.platform.VERSION);
  73. }
  74. function testChromeOnAndroid() {
  75. // Borrowing search's test user agent string for android.
  76. var uaString = 'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus' +
  77. ' Build/ICL53F) AppleWebKit/535.7 (KHTML, like Gecko) ' +
  78. 'Chrome/18.0.1025.133 Mobile Safari/535.7';
  79. // Need to set this lest the testing platform be used for detection.
  80. mockAgent.setNavigator({platform: 'Android'});
  81. mockAgent.setUserAgentString(uaString);
  82. updateUserAgentUtils();
  83. assertTrue(goog.userAgent.ANDROID);
  84. assertEquals('4.0.2', goog.userAgent.platform.VERSION);
  85. }
  86. function testAndroidBrowser() {
  87. var uaString = 'Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr;' +
  88. 'HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko)' +
  89. 'Version/4.0 Mobile Safari/533.1';
  90. // Need to set this lest the testing platform be used for detection.
  91. mockAgent.setNavigator({platform: 'Android'});
  92. mockAgent.setUserAgentString(uaString);
  93. updateUserAgentUtils();
  94. assertTrue(goog.userAgent.ANDROID);
  95. assertEquals('2.3.4', goog.userAgent.platform.VERSION);
  96. }
  97. function testIPhone() {
  98. // Borrowing search's test user agent string for the iPhone.
  99. var uaString = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; ' +
  100. 'en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 ' +
  101. 'Mobile/8A293 Safari/6531.22.7';
  102. // Need to set this lest the testing platform be used for detection.
  103. mockAgent.setNavigator({platform: 'iPhone'});
  104. mockAgent.setUserAgentString(uaString);
  105. updateUserAgentUtils();
  106. assertTrue(goog.userAgent.IPHONE);
  107. assertEquals('4.0', goog.userAgent.platform.VERSION);
  108. }
  109. function testIPad() {
  110. // Borrowing search's test user agent string for the iPad.
  111. var uaString = 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; ja-jp) ' +
  112. 'AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 ' +
  113. 'Safari/6533.18.5';
  114. // Need to set this lest the testing platform be used for detection.
  115. mockAgent.setNavigator({platform: 'iPad'});
  116. mockAgent.setUserAgentString(uaString);
  117. updateUserAgentUtils();
  118. assertTrue(goog.userAgent.IPAD);
  119. assertEquals('4.2.1', goog.userAgent.platform.VERSION);
  120. }
  121. function testIPod22() {
  122. // Borrowing from webserver/browser_rules for user agents
  123. var uaString = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2 like ' +
  124. 'Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Mobile/5G77a';
  125. // Need to set this lest the testing platform be used for detection.
  126. mockAgent.setNavigator({platform: 'iPod'});
  127. mockAgent.setUserAgentString(uaString);
  128. updateUserAgentUtils();
  129. assertTrue(goog.userAgent.IPOD);
  130. assertEquals('2.2', goog.userAgent.platform.VERSION);
  131. }
  132. function testIPod91() {
  133. // Borrowing from webserver/browser_rules for user agents
  134. var uaString = 'Mozilla/5.0 (iPod; CPU iPhone OS 9_1 like ' +
  135. 'Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) ' +
  136. 'CriOS/47.0.2526.70 Mobile/13B143 Safari/601.1.46,gzip(gfe)';
  137. // Need to set this lest the testing platform be used for detection.
  138. mockAgent.setNavigator({platform: 'iPod'});
  139. mockAgent.setUserAgentString(uaString);
  140. updateUserAgentUtils();
  141. assertTrue(goog.userAgent.IPOD);
  142. assertEquals('9.1', goog.userAgent.platform.VERSION);
  143. }