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