Debian Codegraph @

closure-library/closure/goog/useragent/product_test.js

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