Debian Codegraph @

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

raw
1 // Copyright 2006 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.userAgentTest');
16 goog.setTestOnly('goog.userAgentTest');
17
18 goog.require('goog.array');
19 goog.require('goog.labs.userAgent.platform');
20 goog.require('goog.labs.userAgent.testAgents');
21 goog.require('goog.labs.userAgent.util');
22 goog.require('goog.testing.PropertyReplacer');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.userAgent');
25 goog.require('goog.userAgentTestUtil');
26
27
28 var documentMode;
29 goog.userAgent.getDocumentMode_ = function() {
30 return documentMode;
31 };
32
33
34 var propertyReplacer = new goog.testing.PropertyReplacer();
35
36 var UserAgents =
37 {GECKO: 'GECKO', IE: 'IE', OPERA: 'OPERA', WEBKIT: 'WEBKIT', EDGE: 'EDGE'};
38
39
40 function tearDown() {
41 goog.labs.userAgent.util.setUserAgent(null);
42 documentMode = undefined;
43 propertyReplacer.reset();
44 }
45
46
47 /**
48 * Test browser detection for a user agent configuration.
49 * @param {Array<number>} expectedAgents Array of expected userAgents.
50 * @param {string} uaString User agent string.
51 * @param {string=} opt_product Navigator product string.
52 * @param {string=} opt_vendor Navigator vendor string.
53 */
54 function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
55 var mockGlobal = {
56 'navigator':
57 {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor}
58 };
59 propertyReplacer.set(goog, 'global', mockGlobal);
60
61 goog.labs.userAgent.util.setUserAgent(null);
62
63 goog.userAgentTestUtil.reinitializeUserAgent();
64 for (var ua in UserAgents) {
65 var isExpected = goog.array.contains(expectedAgents, UserAgents[ua]);
66 assertEquals(
67 isExpected,
68 goog.userAgentTestUtil.getUserAgentDetected(UserAgents[ua]));
69 }
70 }
71
72 function testOperaInit() {
73 // Check Opera Mini version strings are detected properly
74 var mockGlobal = {
75 'navigator': {'userAgent': goog.labs.userAgent.testAgents.OPERA_MINI}
76 };
77 propertyReplacer.set(goog, 'global', mockGlobal);
78 propertyReplacer.set(goog.userAgent, 'getUserAgentString', function() {
79 return goog.labs.userAgent.testAgents.OPERA_MINI;
80 });
81
82 goog.labs.userAgent.util.setUserAgent(null);
83 goog.userAgentTestUtil.reinitializeUserAgent();
84 assertTrue(goog.userAgent.OPERA);
85 assertEquals('11.10', goog.userAgent.VERSION);
86
87 // Check Opera + Blink versions are detected as Chromium
88 mockGlobal = {
89 'navigator': {'userAgent': goog.labs.userAgent.testAgents.OPERA_15},
90 };
91 propertyReplacer.set(goog, 'global', mockGlobal);
92 propertyReplacer.set(goog.userAgent, 'getUserAgentString', function() {
93 return goog.labs.userAgent.testAgents.OPERA_15;
94 });
95
96 goog.labs.userAgent.util.setUserAgent(null);
97 goog.userAgentTestUtil.reinitializeUserAgent();
98 // TODO(johnlenz): Chrome/Blink is miscategorized as Webkit
99 assertTrue(goog.userAgent.WEBKIT);
100 assertEquals('537.36', goog.userAgent.VERSION);
101 }
102
103 function testCompare() {
104 assertTrue(
105 'exact equality broken', goog.userAgent.compare('1.0', '1.0') == 0);
106 assertTrue(
107 'mutlidot equality broken',
108 goog.userAgent.compare('1.0.0.0', '1.0') == 0);
109 assertTrue('less than broken', goog.userAgent.compare('1.0.2.1', '1.1') < 0);
110 assertTrue(
111 'greater than broken', goog.userAgent.compare('1.1', '1.0.2.1') > 0);
112
113 assertTrue('b broken', goog.userAgent.compare('1.1', '1.1b') > 0);
114 assertTrue('b broken', goog.userAgent.compare('1.1b', '1.1') < 0);
115 assertTrue('b broken', goog.userAgent.compare('1.1b', '1.1b') == 0);
116
117 assertTrue('b>a broken', goog.userAgent.compare('1.1b', '1.1a') > 0);
118 assertTrue('a<b broken', goog.userAgent.compare('1.1a', '1.1b') < 0);
119 }
120
121 function testGecko() {
122 assertGecko(
123 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl-NL; rv:1.7.5)' +
124 'Gecko/20041202 Gecko/1.0',
125 '1.7.5');
126 assertGecko(
127 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.6)' +
128 'Gecko/20050512 Gecko',
129 '1.7.6');
130 assertGecko(
131 'Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.8)' +
132 'Gecko/20050609 Gecko/1.0.4',
133 '1.7.8');
134 assertGecko(
135 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.9)' +
136 'Gecko/20050711 Gecko/1.0.5',
137 '1.7.9');
138 assertGecko(
139 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10)' +
140 'Gecko/20050716 Gecko/1.0.6',
141 '1.7.10');
142 assertGecko(
143 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-GB;' +
144 'rv:1.7.10) Gecko/20050717 Gecko/1.0.6',
145 '1.7.10');
146 assertGecko(
147 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12)' +
148 'Gecko/20050915 Gecko/1.0.7',
149 '1.7.12');
150 assertGecko(
151 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US;' +
152 'rv:1.7.12) Gecko/20050915 Gecko/1.0.7',
153 '1.7.12');
154 assertGecko(
155 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b4)' +
156 'Gecko/20050908 Gecko/1.4',
157 '1.8b4');
158 assertGecko(
159 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8)' +
160 'Gecko/20051107 Gecko/1.5',
161 '1.8');
162 assertGecko(
163 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1)' +
164 'Gecko/20060111 Gecko/1.5.0.1',
165 '1.8.0.1');
166 assertGecko(
167 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.0.1)' +
168 'Gecko/20060111 Gecko/1.5.0.1',
169 '1.8.0.1');
170 assertGecko(
171 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.2)' +
172 'Gecko/20060308 Gecko/1.5.0.2',
173 '1.8.0.2');
174 assertGecko(
175 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US;' +
176 'rv:1.8.0.3) Gecko/20060426 Gecko/1.5.0.3',
177 '1.8.0.3');
178 assertGecko(
179 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.3)' +
180 'Gecko/20060426 Gecko/1.5.0.3',
181 '1.8.0.3');
182 assertGecko(
183 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.4)' +
184 'Gecko/20060508 Gecko/1.5.0.4',
185 '1.8.0.4');
186 assertGecko(
187 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4)' +
188 'Gecko/20060508 Gecko/1.5.0.4',
189 '1.8.0.4');
190 assertGecko(
191 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.0.4)' +
192 'Gecko/20060508 Gecko/1.5.0.4',
193 '1.8.0.4');
194 assertGecko(
195 'Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.8.0.6)' +
196 'Gecko/20060728 Gecko/1.5.0.6',
197 '1.8.0.6');
198 assertGecko(
199 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.6)' +
200 'Gecko/20060808 Fedora/1.5.0.6-2.fc5 Gecko/1.5.0.6 pango-text',
201 '1.8.0.6');
202 assertGecko(
203 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8)' +
204 'Gecko/20060321 Gecko/2.0a1',
205 '1.8');
206 assertGecko(
207 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0', '6.0');
208 }
209
210 function testIe() {
211 assertIe('Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)', '5.01');
212 assertIe('Mozilla/4.0 (compatible; MSIE 5.17; Mac_PowerPC)', '5.17');
213 assertIe('Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)', '5.23');
214 assertIe('Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)', '5.5');
215 assertIe('Mozilla/4.0 (compatible; MSIE 6.0; MSN 2.5; Windows 98)', '6.0');
216 assertIe('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', '6.0');
217 assertIe(
218 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; ' +
219 '.NET CLR 1.1.4322)',
220 '6.0');
221 assertIe(
222 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; ' +
223 '.NET CLR 2.0.50727)',
224 '6.0');
225 assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)', '7.0b');
226 assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Win32)', '7.0b');
227 assertIe('Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)', '7.0b');
228 assertIe(
229 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1;' +
230 'Arcor 5.005; .NET CLR 1.0.3705; .NET CLR 1.1.4322)',
231 '7.0');
232 assertIe(
233 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko', '11.0');
234 }
235
236 function testIeDocumentModeOverride() {
237 documentMode = 9;
238 assertIe(
239 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0', '9');
240 assertIe(
241 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0', '9');
242
243 documentMode = 8;
244 assertIe(
245 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0', '8.0');
246 }
247
248 function testDocumentModeInStandardsMode() {
249 goog.userAgentTestUtil.reinitializeUserAgent();
250 var expectedMode =
251 goog.userAgent.IE ? parseInt(goog.userAgent.VERSION) : undefined;
252 assertEquals(expectedMode, goog.userAgent.DOCUMENT_MODE);
253 }
254
255 function testEdge() {
256 var testAgents = goog.labs.userAgent.testAgents;
257 assertEdge(testAgents.EDGE_12_0, '12.0');
258 assertEdge(testAgents.EDGE_12_9600, '12.9600');
259 }
260
261 function testOpera() {
262 var assertOpera = function(uaString) {
263 assertUserAgent([UserAgents.OPERA], uaString);
264 };
265 assertOpera('Opera/7.23 (Windows 98; U) [en]');
266 assertOpera('Opera/8.00 (Windows NT 5.1; U; en)');
267 assertOpera('Opera/8.0 (X11; Linux i686; U; cs)');
268 assertOpera('Opera/8.02 (Windows NT 5.1; U; en)');
269 assertOpera('Opera/8.50 (Windows NT 5.1; U; en)');
270 assertOpera('Opera/8.5 (X11; Linux i686; U; cs)');
271 assertOpera('Opera/8.51 (Windows NT 5.1; U; en)');
272 assertOpera('Opera/9.0 (Windows NT 5.0; U; en)');
273 assertOpera('Opera/9.00 (Macintosh; PPC Mac OS X; U; en)');
274 assertOpera('Opera/9.00 (Windows NT 5.1; U; en)');
275 assertOpera('Opera/9.00 (Windows NT 5.2; U; en)');
276 assertOpera('Opera/9.00 (Windows NT 6.0; U; en)');
277 }
278
279 function testWebkit() {
280 var testAgents = goog.labs.userAgent.testAgents;
281 assertWebkit(testAgents.ANDROID_BROWSER_403);
282 assertWebkit(testAgents.ANDROID_BROWSER_403_ALT);
283 }
284
285 function testUnknownBrowser() {
286 assertUserAgent([], 'MyWebBrowser');
287 assertUserAgent([], undefined);
288 }
289
290 function testNoNavigator() {
291 // global object has no "navigator" property.
292 var mockGlobal = {};
293 propertyReplacer.set(goog, 'global', mockGlobal);
294 goog.labs.userAgent.util.setUserAgent(null);
295 goog.userAgentTestUtil.reinitializeUserAgent();
296
297 assertEquals(
298 'Platform should be the empty string', '', goog.userAgent.PLATFORM);
299 assertEquals(
300 'Version should be the empty string', '', goog.userAgent.VERSION);
301 }
302
303 function testLegacyChromeOsAndLinux() {
304 // As a legacy behavior, goog.userAgent.LINUX considers
305 // ChromeOS to be Linux.
306 // goog.labs.userAgent.platform.isLinux() does not.
307 goog.labs.userAgent.util.setUserAgent(
308 goog.labs.userAgent.testAgents.CHROME_OS);
309 goog.userAgentTestUtil.reinitializeUserAgent();
310 assertTrue(goog.userAgent.LINUX);
311 assertFalse(goog.labs.userAgent.platform.isLinux());
312 }
313
314 function assertIe(uaString, expectedVersion) {
315 assertUserAgent([UserAgents.IE], uaString);
316 assertEquals(
317 'User agent ' + uaString + ' should have had version ' + expectedVersion +
318 ' but had ' + goog.userAgent.VERSION,
319 expectedVersion, goog.userAgent.VERSION);
320 }
321
322 function assertEdge(uaString, expectedVersion) {
323 assertUserAgent([UserAgents.EDGE], uaString);
324 assertEquals(
325 'User agent ' + uaString + ' should have had version ' + expectedVersion +
326 ' but had ' + goog.userAgent.VERSION,
327 expectedVersion, goog.userAgent.VERSION);
328 }
329
330 function assertGecko(uaString, expectedVersion) {
331 assertUserAgent([UserAgents.GECKO], uaString, 'Gecko');
332 assertEquals(
333 'User agent ' + uaString + ' should have had version ' + expectedVersion +
334 ' but had ' + goog.userAgent.VERSION,
335 expectedVersion, goog.userAgent.VERSION);
336 }
337
338 function assertWebkit(uaString) {
339 assertUserAgent([UserAgents.WEBKIT], uaString, 'WebKit');
340 }