Debian Codegraph @

closure-library/closure/goog/dom/vendor_test.js

raw
1 // Copyright 2012 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.dom.vendorTest');
16 goog.setTestOnly('goog.dom.vendorTest');
17
18 goog.require('goog.array');
19 goog.require('goog.dom.vendor');
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.userAgentTestUtil');
26
27 var documentMode;
28 var mockUserAgent;
29 var propertyReplacer = new goog.testing.PropertyReplacer();
30
31 function setUp() {
32 mockUserAgent = new goog.testing.MockUserAgent();
33 mockUserAgent.install();
34 }
35
36 function tearDown() {
37 goog.dispose(mockUserAgent);
38 documentMode = undefined;
39 propertyReplacer.reset();
40 }
41
42 goog.userAgent.getDocumentMode_ = function() {
43 return documentMode;
44 };
45
46
47 var UserAgents = {GECKO: 'GECKO', IE: 'IE', OPERA: 'OPERA', WEBKIT: 'WEBKIT'};
48
49
50 /**
51 * Return whether a given user agent has been detected.
52 * @param {number} agent Value in UserAgents.
53 * @return {boolean} Whether the user agent has been detected.
54 */
55 function getUserAgentDetected_(agent) {
56 switch (agent) {
57 case UserAgents.GECKO:
58 return goog.userAgent.GECKO;
59 case UserAgents.IE:
60 return goog.userAgent.IE;
61 case UserAgents.OPERA:
62 return goog.userAgent.OPERA;
63 case UserAgents.WEBKIT:
64 return goog.userAgent.WEBKIT;
65 }
66 return null;
67 }
68
69
70 /**
71 * Test browser detection for a user agent configuration.
72 * @param {Array<number>} expectedAgents Array of expected userAgents.
73 * @param {string} uaString User agent string.
74 * @param {string=} opt_product Navigator product string.
75 * @param {string=} opt_vendor Navigator vendor string.
76 */
77 function assertUserAgent(expectedAgents, uaString, opt_product, opt_vendor) {
78 var mockNavigator =
79 {'userAgent': uaString, 'product': opt_product, 'vendor': opt_vendor};
80
81 mockUserAgent.setNavigator(mockNavigator);
82 mockUserAgent.setUserAgentString(uaString);
83
84 // Force reread of navigator.userAgent;
85 goog.labs.userAgent.util.setUserAgent(null);
86 goog.userAgentTestUtil.reinitializeUserAgent();
87 for (var ua in UserAgents) {
88 var isExpected = goog.array.contains(expectedAgents, UserAgents[ua]);
89 assertEquals(isExpected, getUserAgentDetected_(UserAgents[ua]));
90 }
91 }
92
93
94 /**
95 * Tests for the vendor prefix for Webkit.
96 */
97 function testVendorPrefixWebkit() {
98 assertUserAgent([UserAgents.WEBKIT], 'WebKit');
99 assertEquals('-webkit', goog.dom.vendor.getVendorPrefix());
100 }
101
102
103 /**
104 * Tests for the vendor prefix for Mozilla/Gecko.
105 */
106 function testVendorPrefixGecko() {
107 assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
108 assertEquals('-moz', goog.dom.vendor.getVendorPrefix());
109 }
110
111
112 /**
113 * Tests for the vendor prefix for Opera.
114 */
115 function testVendorPrefixOpera() {
116 assertUserAgent([UserAgents.OPERA], 'Opera');
117 assertEquals('-o', goog.dom.vendor.getVendorPrefix());
118 }
119
120
121 /**
122 * Tests for the vendor prefix for IE.
123 */
124 function testVendorPrefixIE() {
125 assertUserAgent([UserAgents.IE], 'MSIE');
126 assertEquals('-ms', goog.dom.vendor.getVendorPrefix());
127 }
128
129
130 /**
131 * Tests for the vendor Js prefix for Webkit.
132 */
133 function testVendorJsPrefixWebkit() {
134 assertUserAgent([UserAgents.WEBKIT], 'WebKit');
135 assertEquals('Webkit', goog.dom.vendor.getVendorJsPrefix());
136 }
137
138
139 /**
140 * Tests for the vendor Js prefix for Mozilla/Gecko.
141 */
142 function testVendorJsPrefixGecko() {
143 assertUserAgent([UserAgents.GECKO], 'Gecko', 'Gecko');
144 assertEquals('Moz', goog.dom.vendor.getVendorJsPrefix());
145 }
146
147
148 /**
149 * Tests for the vendor Js prefix for Opera.
150 */
151 function testVendorJsPrefixOpera() {
152 assertUserAgent([UserAgents.OPERA], 'Opera');
153 assertEquals('O', goog.dom.vendor.getVendorJsPrefix());
154 }
155
156
157 /**
158 * Tests for the vendor Js prefix for IE.
159 */
160 function testVendorJsPrefixIE() {
161 assertUserAgent([UserAgents.IE], 'MSIE');
162 assertEquals('ms', goog.dom.vendor.getVendorJsPrefix());
163 }
164
165
166 /**
167 * Tests for the vendor Js prefix if no UA detected.
168 */
169 function testVendorJsPrefixNone() {
170 assertUserAgent([], '');
171 assertNull(goog.dom.vendor.getVendorJsPrefix());
172 }
173
174
175 /**
176 * Tests for the prefixed property name on Webkit.
177 */
178 function testPrefixedPropertyNameWebkit() {
179 assertUserAgent([UserAgents.WEBKIT], 'WebKit');
180 assertEquals(
181 'webkitFoobar', goog.dom.vendor.getPrefixedPropertyName('foobar'));
182 }
183
184
185 /**
186 * Tests for the prefixed property name on Webkit in an object.
187 */
188 function testPrefixedPropertyNameWebkitAndObject() {
189 var mockDocument = {
190 // setting a value of 0 on purpose, to ensure we only look for property
191 // names, not their values.
192 'webkitFoobar': 0
193 };
194 assertUserAgent([UserAgents.WEBKIT], 'WebKit');
195 assertEquals(
196 'webkitFoobar',
197 goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
198 }
199
200
201 /**
202 * Tests for the prefixed property name.
203 */
204 function testPrefixedPropertyName() {
205 assertUserAgent([], '');
206 assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar'));
207 }
208
209
210 /**
211 * Tests for the prefixed property name in an object.
212 */
213 function testPrefixedPropertyNameAndObject() {
214 var mockDocument = {'foobar': 0};
215 assertUserAgent([], '');
216 assertEquals(
217 'foobar',
218 goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
219 }
220
221
222 /**
223 * Tests for the prefixed property name when it doesn't exist.
224 */
225 function testPrefixedPropertyNameAndObjectIsEmpty() {
226 var mockDocument = {};
227 assertUserAgent([], '');
228 assertNull(goog.dom.vendor.getPrefixedPropertyName('foobar', mockDocument));
229 }
230
231
232 /**
233 * Test for prefixed event type.
234 */
235 function testPrefixedEventType() {
236 assertUserAgent([], '');
237 assertEquals('foobar', goog.dom.vendor.getPrefixedEventType('foobar'));
238 }
239
240
241 /**
242 * Test for browser-specific prefixed event type.
243 */
244 function testPrefixedEventTypeForBrowser() {
245 assertUserAgent([UserAgents.WEBKIT], 'WebKit');
246 assertEquals('webkitfoobar', goog.dom.vendor.getPrefixedEventType('foobar'));
247 }
248
249
250 function assertIe(uaString, expectedVersion) {
251 assertUserAgent([UserAgents.IE], uaString);
252 assertEquals(
253 'User agent ' + uaString + ' should have had version ' + expectedVersion +
254 ' but had ' + goog.userAgent.VERSION,
255 expectedVersion, goog.userAgent.VERSION);
256 }
257
258
259 function assertGecko(uaString, expectedVersion) {
260 assertUserAgent([UserAgents.GECKO], uaString, 'Gecko');
261 assertEquals(
262 'User agent ' + uaString + ' should have had version ' + expectedVersion +
263 ' but had ' + goog.userAgent.VERSION,
264 expectedVersion, goog.userAgent.VERSION);
265 }