Debian Codegraph @

closure-library/closure/goog/labs/useragent/util_test.js

raw
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 frexcept 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 /**
16 * @fileoverview Unit tests for goog.labs.userAgent.engine.
17 */
18
19 goog.provide('goog.labs.userAgent.utilTest');
20
21 goog.require('goog.functions');
22 goog.require('goog.labs.userAgent.testAgents');
23 goog.require('goog.labs.userAgent.util');
24 goog.require('goog.testing.PropertyReplacer');
25 goog.require('goog.testing.jsunit');
26
27 goog.setTestOnly('goog.labs.userAgent.utilTest');
28
29
30 var stubs = new goog.testing.PropertyReplacer();
31
32 function tearDown() {
33 stubs.reset();
34 }
35
36
37 /**
38 * Tests parsing a few example UA strings.
39 */
40 function testExtractVersionTuples() {
41 // Old Android
42 var tuples = goog.labs.userAgent.util.extractVersionTuples(
43 goog.labs.userAgent.testAgents.ANDROID_BROWSER_235);
44
45 assertEquals(4, tuples.length);
46 assertSameElements(
47 [
48 'Mozilla', '5.0',
49 'Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40'
50 ],
51 tuples[0]);
52 assertSameElements(['AppleWebKit', '533.1', 'KHTML, like Gecko'], tuples[1]);
53 assertSameElements(['Version', '4.0', undefined], tuples[2]);
54 assertSameElements(['Mobile Safari', '533.1', undefined], tuples[3]);
55
56 // IE 9
57 tuples = goog.labs.userAgent.util.extractVersionTuples(
58 goog.labs.userAgent.testAgents.IE_9);
59 assertEquals(1, tuples.length);
60 assertSameElements(
61 ['Mozilla', '5.0', 'compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'],
62 tuples[0]);
63
64 // Opera
65 tuples = goog.labs.userAgent.util.extractVersionTuples(
66 goog.labs.userAgent.testAgents.OPERA_10);
67 assertEquals(3, tuples.length);
68 assertSameElements(
69 ['Opera', '9.80', 'S60; SymbOS; Opera Mobi/447; U; en'], tuples[0]);
70 assertSameElements(['Presto', '2.4.18', undefined], tuples[1]);
71 assertSameElements(['Version', '10.00', undefined], tuples[2]);
72 }
73
74 function testSetUserAgent() {
75 var ua = 'Five Finger Death Punch';
76 goog.labs.userAgent.util.setUserAgent(ua);
77 assertEquals(ua, goog.labs.userAgent.util.getUserAgent());
78 assertTrue(goog.labs.userAgent.util.matchUserAgent('Punch'));
79 assertFalse(goog.labs.userAgent.util.matchUserAgent('punch'));
80 assertFalse(goog.labs.userAgent.util.matchUserAgent('Mozilla'));
81 }
82
83 function testSetUserAgentIgnoreCase() {
84 var ua = 'Five Finger Death Punch';
85 goog.labs.userAgent.util.setUserAgent(ua);
86 assertEquals(ua, goog.labs.userAgent.util.getUserAgent());
87 assertTrue(goog.labs.userAgent.util.matchUserAgentIgnoreCase('Punch'));
88 assertTrue(goog.labs.userAgent.util.matchUserAgentIgnoreCase('punch'));
89 assertFalse(goog.labs.userAgent.util.matchUserAgentIgnoreCase('Mozilla'));
90 }
91
92 function testNoNavigator() {
93 stubs.set(
94 goog.labs.userAgent.util, 'getNavigator_',
95 goog.functions.constant(undefined));
96 assertEquals('', goog.labs.userAgent.util.getNativeUserAgentString_());
97 }
98
99 function testNavigatorWithNoUserAgent() {
100 stubs.set(
101 goog.labs.userAgent.util, 'getNavigator_',
102 goog.functions.constant(undefined));
103 assertEquals('', goog.labs.userAgent.util.getNativeUserAgentString_());
104 }
105
106 function testNavigatorWithUserAgent() {
107 stubs.set(
108 goog.labs.userAgent.util, 'getNavigator_',
109 goog.functions.constant({'userAgent': 'moose'}));
110 assertEquals('moose', goog.labs.userAgent.util.getNativeUserAgentString_());
111 }