aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/useragent/product_test.html
blob: 01d691b9e5ea9e06e62387c3c9d80de20780dc59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--

  @author andybons@google.com (Andrew Boneventre)
-->
<head>
<!--
This test has not yet been updated to run on IE8. See http://b/hotlist?id=36311
-->
<!--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<title>Closure Unit Tests - goog.userAgent.product</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.array');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.MockUserAgent');
  goog.require('goog.testing.PropertyReplacer');
  goog.require('goog.userAgent.product');
  goog.require('goog.userAgent.product.isVersion');
</script>
</head>
<body>
<script>
  var mockAgent;
  var replacer;

  function setUp() {
    mockAgent = new goog.testing.MockUserAgent();
    mockAgent.install();
    replacer = new goog.testing.PropertyReplacer();
  }

  function tearDown() {
    replacer.reset();
    mockAgent.dispose();
    updateUserAgentUtils();
  }

  function updateUserAgentUtils() {
    goog.userAgent.init_();

    // Unfortunately we can't isolate the useragent setting in a function
    // we can call, because things rely on it compiling to nothing when
    // one of the ASSUME flags is set, and the compiler isn't smart enough
    // to do that when the setting is done inside a function that's inlined.
    goog.userAgent.OPERA = goog.userAgent.detectedOpera_;
    goog.userAgent.IE = goog.userAgent.detectedIe_;
    goog.userAgent.GECKO = goog.userAgent.detectedGecko_;
    goog.userAgent.WEBKIT = goog.userAgent.detectedWebkit_;
    goog.userAgent.MOBILE = goog.userAgent.detectedMobile_;
    goog.userAgent.SAFARI = goog.userAgent.WEBKIT;

    goog.userAgent.VERSION = goog.userAgent.determineVersion_();

    goog.userAgent.product.init_();
    // In an ideal world, this assignment would be just a function in
    // product.js that we could call, but putting it into a function causes
    // the compiler to fail to compile product.js to nothing when one of
    // the ASSUME flags is set.
    goog.userAgent.product.OPERA = goog.userAgent.OPERA;
    goog.userAgent.product.IE = goog.userAgent.IE;
    goog.userAgent.product.FIREFOX = goog.userAgent.product.detectedFirefox_;
    goog.userAgent.product.CAMINO = goog.userAgent.product.detectedCamino_;
    goog.userAgent.product.IPHONE = goog.userAgent.product.detectedIphone_;
    goog.userAgent.product.IPAD = goog.userAgent.product.detectedIpad_;
    goog.userAgent.product.ANDROID = goog.userAgent.product.detectedAndroid_;
    goog.userAgent.product.CHROME = goog.userAgent.product.detectedChrome_;
    goog.userAgent.product.SAFARI = goog.userAgent.product.detectedSafari_;
    goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();
  }

  // The set of products whose corresponding goog.userAgent.product value is set
  // in goog.userAgent.product.init_().
  var DETECTED_BROWSER_KEYS =
      ['FIREFOX', 'CAMINO', 'IPHONE', 'IPAD', 'ANDROID', 'CHROME', 'SAFARI'];

  function assertIsBrowser(browser) {
    function createDetectedBrowserKey(browser) {
      switch (browser) {
        case 'FIREFOX': return 'detectedFirefox_';
        case 'CAMINO': return 'detectedCamino_';
        case 'IPHONE': return 'detectedIphone_';
        case 'IPAD': return 'detectedIpad_';
        case 'ANDROID': return 'detectedAndroid_';
        case 'CHROME': return 'detectedChrome_';
        case 'SAFARI': return 'detectedSafari_';
        case 'IE': return 'IE';
        case 'OPERA': return 'OPERA';
      }
      throw Error('Unknown browser: ' + browser);
    }

    var productKey = createDetectedBrowserKey(browser);
    assertTrue(goog.userAgent.product[productKey]);
    // Make sure we don't have any false positives for other browsers.
    goog.array.forEach(DETECTED_BROWSER_KEYS, function(el) {
      if (el != browser) {
        assertFalse('useragent should not match: ' + el,
            goog.userAgent.product[createDetectedBrowserKey(el)]);
      }
    });
  }

  function assertBrowserAndVersion(userAgent, browser, version) {
    mockAgent.setUserAgentString(userAgent);
    updateUserAgentUtils();
    assertIsBrowser(browser);
    assertEquals('User agent should have this version',
                 version, goog.userAgent.VERSION);
  }

  /**
   * @param {Array.<{
   *           ua: string,
   *           versions: Array.<{
   *             num: {string|number}, truth: boolean}>}>} userAgents
   * @param {string} browser
   */
  function checkEachUserAgentDetected(userAgents, browser) {
    goog.array.forEach(userAgents, function(ua) {
      mockAgent.setUserAgentString(ua.ua);
      updateUserAgentUtils();
      assertIsBrowser(browser);
      goog.array.forEach(ua.versions, function(v) {
        assertEquals(
            'Expected version ' + v.num + ' from ' + ua.ua + ' but got ' +
                goog.userAgent.product.VERSION,
            v.truth, goog.userAgent.product.isVersion(v.num));
      });
    });
  }

  function testInternetExplorer() {
    var userAgents = [
      {ua: 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6; ' +
           'chromeframe; .NET CLR 1.1.4322; InfoPath.1; ' +
           '.NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; ' +
           '.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727)',
       versions: [
         {num: 6, truth: true},
         {num: '7.0', truth: true},
         {num: 7.1, truth: false},
         {num: 8, truth: false}
       ]
      }
    ];
    // hide any navigator.product value by putting in a navigator with no
    // properties.
    mockAgent.setNavigator({});
    checkEachUserAgentDetected(userAgents, 'IE');
  }

  function testOpera() {
    var opera = {};
    var userAgents = [
      {ua: 'Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.01',
       versions: [
         {num: 9, truth: true},
         {num: '10.1', truth: true},
         {num: 11, truth: false}
       ]}
    ];
    replacer.set(goog.global, 'opera', opera);
    opera.version = '10.01';
    checkEachUserAgentDetected(userAgents, 'OPERA');
    userAgents = [
      {ua: 'Opera/9.63 (Windows NT 5.1; U; en) Presto/2.1.1',
       versions: [
         {num: 9, truth: true},
         {num: '10.1', truth: false},
         {num: '9.80', truth: false},
         {num: '9.60', truth: true}
       ]}
    ];
    opera.version = '9.63';
    checkEachUserAgentDetected(userAgents, 'OPERA');
  }

  function testFirefox() {
    var userAgents = [
     {ua: 'Mozilla/6.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; ' +
           'rv:2.0.0.0) Gecko/20061028 Firefox/3.0',
       versions: [
         {num: 2, truth: true},
         {num: '3.0', truth: true},
         {num: '3.5.3', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; ' +
           'rv:1.8.1.4) Gecko/20070515 Firefox/2.0.4',
       versions: [
         {num: 2, truth: true},
         {num: '2.0.4', truth: true},
         {num: 3, truth: false},
         {num: '3.5.3', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0',
       versions: [
         {num: 6, truth: true},
         {num: '6.0', truth: true},
         {num: 7, truth: false},
         {num: '7.0', truth: false}
       ]}
    ];

    checkEachUserAgentDetected(userAgents, 'FIREFOX');

    // Mozilla reported to us that they plan this UA format starting
    // in Firefox 13.
    // See bug at https://bugzilla.mozilla.org/show_bug.cgi?id=588909
    // and thread at http://goto.google.com/pfltz
    mockAgent.setNavigator({product: 'Gecko'});
    assertBrowserAndVersion(
        'Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/6.0 Firefox/6.0',
        'FIREFOX', '6.0');
  }

  function testCamino() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en; rv:1.8.1.11) ' +
           'Gecko/20071128 Camino/1.5.4',
       versions: [
         {num: '1.5.4', truth: true},
         {num: 1, truth: true},
         {num: '2.0', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.10) ' +
           'Gecko/20070228 Camino/1.0.4',
       versions: [
         {num: '1.5.4', truth: false},
         {num: 1, truth: true},
         {num: '2.0', truth: false}
       ]}
    ];
    mockAgent.setNavigator({vendor: 'Camino', product: 'Gecko'});
    checkEachUserAgentDetected(userAgents, 'CAMINO');
  }

  function testChrome() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) ' +
           'AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.0 ' +
           'Safari/525.19',
       versions: [
         {num: '0.2.153', truth: true},
         {num: 1, truth: false}
       ]},
      {ua: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) ' +
           'AppleWebKit/532.3 (KHTML, like Gecko) Chrome/4.0.223.11 ' +
           'Safari/532.3',
       versions: [
         {num: 4, truth: true},
         {num: '0.2.153', truth: true},
         {num: '4.1.223.13', truth: false},
         {num: '4.0.223.10', truth: true}
       ]}
    ];
    checkEachUserAgentDetected(userAgents, 'CHROME');
  }

  function testSafari() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; de-de) ' +
           'AppleWebKit/534.16+ (KHTML, like Gecko) Version/5.0.3 ' +
           'Safari/533.19.4',
       versions: [
         {num: 5, truth: true},
         {num: '5.0.3', truth: true},
         {num: '5.0.4', truth: false},
         {num: '533', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (Windows; U; Windows NT 6.0; pl-PL) ' +
           'AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21',
       versions: [
         {num: 3, truth: true},
         {num: '3.0', truth: true},
         {num: '3.1.2', truth: true}
       ]},
      {ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_3; en-us) ' +
           'AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.1 Safari/525.20',
       versions: [
         {num: 3, truth: true},
         {num: '3.1.1', truth: true},
         {num: '3.1.2', truth: false},
         {num: '525.21', truth: false}
       ]},

       // Safari 1 and 2 do not report product version numbers in their
       // user-agent strings. VERSION for these browsers will be set to ''.
       {ua: 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; ja-jp) ' +
            'AppleWebKit/418.9.1 (KHTML, like Gecko) Safari/419.3',
       versions: [
         {num: 3, truth: false},
         {num: 2, truth: false},
         {num: 1, truth: false},
         {num: 0, truth: true},
         {num: '0', truth: true},
         {num: '', truth: true}
       ]}
    ];
    checkEachUserAgentDetected(userAgents, 'SAFARI');
  }

  function testIphone() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ ' +
           '(KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3',
       versions: [
        {num: '3.0.1A543a', truth: true},
        {num: '3.0', truth: true},
        {num: '3.0.1B543a', truth: false},
        {num: '3.1.1A543a', truth: false},
        {num: '3.0.1A320c', truth: true},
        {num: '3.0.3A100a', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 ' +
           '(KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3',
       versions: [
         {num: '3.0.1A543a', truth: true},
         {num: '3.0.3A100a', truth: true}
       ]}
    ];
    checkEachUserAgentDetected(userAgents, 'IPHONE');
  }

  function testIpad() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) ' +
           'AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 ' +
           'Mobile/7B334b Safari/531.21.10',
       versions: [
        {num: '4.0.4.7B334b', truth: true},
        {num: '4.0', truth: true},
        {num: '4.0.4.7C334b', truth: false},
        {num: '4.1.7B334b', truth: false},
        {num: '4.0.4.7B320c', truth: true},
        {num: '4.0.4.8B334b', truth: false}
       ]}
    ];
    checkEachUserAgentDetected(userAgents, 'IPAD');
  }

  function testAndroid() {
    var userAgents = [
      {ua: 'Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ ' +
           '(KHTML, like Gecko) Safari/419.3',
       versions: [
         {num: 0.5, truth: true},
         {num: '1.0', truth: false}
       ]},
      {ua: 'Mozilla/5.0 (Linux; U; Android 1.0; en-us; dream) ' +
           'AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile ' +
           'Safari/523.12.2',
       versions: [
         {num: 0.5, truth: true},
         {num: 1, truth: true},
         {num: '1.0', truth: true},
         {num: '3.0.12', truth: false}
       ]}
    ];
    checkEachUserAgentDetected(userAgents, 'ANDROID');
  }
</script>
</body>
</html>