Merge "Define 'MW_UPDATER' when running update.php"
[mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.client.test.js
blob4c7c302222dcf768a6e485b81388cb7fd829644d
1 ( function ( $ ) {
3         QUnit.module( 'jquery.client', QUnit.newMwEnvironment() );
5         var uacount = 0,
6                 // Object keyed by userAgent. Value is an array (human-readable name, client-profile object, navigator.platform value)
7                 // Info based on results from http://toolserver.org/~krinkle/testswarm/job/174/
8                 uas = {
9                         // Internet Explorer 6
10                         // Internet Explorer 7
11                         'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)': {
12                                 title: 'Internet Explorer 7',
13                                 platform: 'Win32',
14                                 profile: {
15                                         name: 'msie',
16                                         layout: 'trident',
17                                         layoutVersion: 'unknown',
18                                         platform: 'win',
19                                         version: '7.0',
20                                         versionBase: '7',
21                                         versionNumber: 7
22                                 },
23                                 wikiEditor: {
24                                         ltr: true,
25                                         rtl: false
26                                 }
27                         },
28                         // Internet Explorer 8
29                         // Internet Explorer 9
30                         // Internet Explorer 10
31                         'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)': {
32                                 title: 'Internet Explorer 10',
33                                 platform: 'Win32',
34                                 profile: {
35                                         name: 'msie',
36                                         layout: 'trident',
37                                         layoutVersion: 6,
38                                         platform: 'win',
39                                         version: '10.0',
40                                         versionBase: '10',
41                                         versionNumber: 10
42                                 },
43                                 wikiEditor: {
44                                         ltr: true,
45                                         rtl: true
46                                 }
47                         },
48                         // Internet Explorer 11
49                         'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko': {
50                                 title: 'Internet Explorer 11',
51                                 platform: 'Win32',
52                                 profile: {
53                                         name: 'msie',
54                                         layout: 'trident',
55                                         layoutVersion: 7,
56                                         platform: 'win',
57                                         version: '11.0',
58                                         versionBase: '11',
59                                         versionNumber: 11
60                                 },
61                                 wikiEditor: {
62                                         ltr: true,
63                                         rtl: true
64                                 }
65                         },
66                         // Internet Explorer 11 - Windows 8.1 x64 Modern UI
67                         'Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; rv:11.0) like Gecko': {
68                                 title: 'Internet Explorer 11',
69                                 platform: 'Win64',
70                                 profile: {
71                                         name: 'msie',
72                                         layout: 'trident',
73                                         layoutVersion: 7,
74                                         platform: 'win',
75                                         version: '11.0',
76                                         versionBase: '11',
77                                         versionNumber: 11
78                                 },
79                                 wikiEditor: {
80                                         ltr: true,
81                                         rtl: true
82                                 }
83                         },
84                         // Internet Explorer 11 - Windows 8.1 x64 desktop UI
85                         'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko': {
86                                 title: 'Internet Explorer 11',
87                                 platform: 'WOW64',
88                                 profile: {
89                                         name: 'msie',
90                                         layout: 'trident',
91                                         layoutVersion: 7,
92                                         platform: 'win',
93                                         version: '11.0',
94                                         versionBase: '11',
95                                         versionNumber: 11
96                                 },
97                                 wikiEditor: {
98                                         ltr: true,
99                                         rtl: true
100                                 }
101                         },
102                         // Firefox 2
103                         // Firefox 3.5
104                         'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.19) Gecko/20110420 Firefox/3.5.19': {
105                                 title: 'Firefox 3.5',
106                                 platform: 'MacIntel',
107                                 profile: {
108                                         name: 'firefox',
109                                         layout: 'gecko',
110                                         layoutVersion: 20110420,
111                                         platform: 'mac',
112                                         version: '3.5.19',
113                                         versionBase: '3',
114                                         versionNumber: 3.5
115                                 },
116                                 wikiEditor: {
117                                         ltr: true,
118                                         rtl: true
119                                 }
120                         },
121                         // Firefox 3.6
122                         'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17': {
123                                 title: 'Firefox 3.6',
124                                 platform: 'Linux i686',
125                                 profile: {
126                                         name: 'firefox',
127                                         layout: 'gecko',
128                                         layoutVersion: 20110422,
129                                         platform: 'linux',
130                                         version: '3.6.17',
131                                         versionBase: '3',
132                                         versionNumber: 3.6
133                                 },
134                                 wikiEditor: {
135                                         ltr: true,
136                                         rtl: true
137                                 }
138                         },
139                         // Firefox 4
140                         'Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1': {
141                                 title: 'Firefox 4',
142                                 platform: 'Win32',
143                                 profile: {
144                                         name: 'firefox',
145                                         layout: 'gecko',
146                                         layoutVersion: 20100101,
147                                         platform: 'win',
148                                         version: '4.0.1',
149                                         versionBase: '4',
150                                         versionNumber: 4
151                                 },
152                                 wikiEditor: {
153                                         ltr: true,
154                                         rtl: true
155                                 }
156                         },
157                         // Firefox 10 nightly build
158                         'Mozilla/5.0 (X11; Linux x86_64; rv:10.0a1) Gecko/20111103 Firefox/10.0a1': {
159                                 title: 'Firefox 10 nightly',
160                                 platform: 'Linux',
161                                 profile: {
162                                         name: 'firefox',
163                                         layout: 'gecko',
164                                         layoutVersion: 20111103,
165                                         platform: 'linux',
166                                         version: '10.0a1',
167                                         versionBase: '10',
168                                         versionNumber: 10
169                                 },
170                                 wikiEditor: {
171                                         ltr: true,
172                                         rtl: true
173                                 }
174                         },
175                         // Iceweasel 10.0.6
176                         'Mozilla/5.0 (X11; Linux i686; rv:10.0.6) Gecko/20100101 Iceweasel/10.0.6': {
177                                 title: 'Iceweasel 10.0.6',
178                                 platform: 'Linux',
179                                 profile: {
180                                         name: 'iceweasel',
181                                         layout: 'gecko',
182                                         layoutVersion: 20100101,
183                                         platform: 'linux',
184                                         version: '10.0.6',
185                                         versionBase: '10',
186                                         versionNumber: 10
187                                 },
188                                 wikiEditor: {
189                                         ltr: true,
190                                         rtl: true
191                                 }
192                         },
193                         // Iceweasel 15.0.1
194                         'Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1 Iceweasel/15.0.1': {
195                                 title: 'Iceweasel 15.0.1',
196                                 platform: 'Linux',
197                                 profile: {
198                                         name: 'iceweasel',
199                                         layout: 'gecko',
200                                         layoutVersion: 20100101,
201                                         platform: 'linux',
202                                         version: '15.0.1',
203                                         versionBase: '15',
204                                         versionNumber: 15
205                                 },
206                                 wikiEditor: {
207                                         ltr: true,
208                                         rtl: true
209                                 }
210                         },
211                         // Firefox 5
212                         // Safari 3
213                         // Safari 4
214                         'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; nl-nl) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7': {
215                                 title: 'Safari 4',
216                                 platform: 'MacIntel',
217                                 profile: {
218                                         name: 'safari',
219                                         layout: 'webkit',
220                                         layoutVersion: 531,
221                                         platform: 'mac',
222                                         version: '4.0.5',
223                                         versionBase: '4',
224                                         versionNumber: 4
225                                 },
226                                 wikiEditor: {
227                                         ltr: true,
228                                         rtl: true
229                                 }
230                         },
231                         'Mozilla/5.0 (Windows; U; Windows NT 6.0; cs-CZ) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7': {
232                                 title: 'Safari 4',
233                                 platform: 'Win32',
234                                 profile: {
235                                         name: 'safari',
236                                         layout: 'webkit',
237                                         layoutVersion: 533,
238                                         platform: 'win',
239                                         version: '4.0.5',
240                                         versionBase: '4',
241                                         versionNumber: 4
242                                 },
243                                 wikiEditor: {
244                                         ltr: true,
245                                         rtl: true
246                                 }
247                         },
248                         // Safari 5
249                         // Safari 6
250                         'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.29.13 (KHTML, like Gecko) Version/6.0.4 Safari/536.29.13': {
251                                 title: 'Safari 6',
252                                 platform: 'MacIntel',
253                                 profile: {
254                                         name: 'safari',
255                                         layout: 'webkit',
256                                         layoutVersion: 536,
257                                         platform: 'mac',
258                                         version: '6.0.4',
259                                         versionBase: '6',
260                                         versionNumber: 6
261                                 },
262                                 wikiEditor: {
263                                         ltr: true,
264                                         rtl: true
265                                 }
266                         },
267                         // Safari 6.0.5+ (doesn't have the comma in "KHTML, like Gecko")
268                         'Mozilla/5.0 (Macintosh; Intel Mac OS X 1084) AppleWebKit/536.30.1 (KHTML like Gecko) Version/6.0.5 Safari/536.30.1': {
269                                 title: 'Safari 6',
270                                 platform: 'MacIntel',
271                                 profile: {
272                                         name: 'safari',
273                                         layout: 'webkit',
274                                         layoutVersion: 536,
275                                         platform: 'mac',
276                                         version: '6.0.5',
277                                         versionBase: '6',
278                                         versionNumber: 6
279                                 },
280                                 wikiEditor: {
281                                         ltr: true,
282                                         rtl: true
283                                 }
284                         },
285                         // Opera 10+
286                         'Opera/9.80 (Windows NT 5.1)': {
287                                 title: 'Opera 10+ (exact version unspecified)',
288                                 platform: 'Win32',
289                                 profile: {
290                                         name: 'opera',
291                                         layout: 'presto',
292                                         layoutVersion: 'unknown',
293                                         platform: 'win',
294                                         version: '10',
295                                         versionBase: '10',
296                                         versionNumber: 10
297                                 },
298                                 wikiEditor: {
299                                         ltr: true,
300                                         rtl: true
301                                 }
302                         },
303                         // Opera 12
304                         'Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.11': {
305                                 title: 'Opera 12',
306                                 platform: 'Win32',
307                                 profile: {
308                                         name: 'opera',
309                                         layout: 'presto',
310                                         layoutVersion: 'unknown',
311                                         platform: 'win',
312                                         version: '12.11',
313                                         versionBase: '12',
314                                         versionNumber: 12.11
315                                 },
316                                 wikiEditor: {
317                                         ltr: true,
318                                         rtl: true
319                                 }
320                         },
321                         // Opera 15 (WebKit-based)
322                         'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.130': {
323                                 title: 'Opera 15',
324                                 platform: 'Win32',
325                                 profile: {
326                                         name: 'opera',
327                                         layout: 'webkit',
328                                         layoutVersion: 537,
329                                         platform: 'win',
330                                         version: '15.0.1147.130',
331                                         versionBase: '15',
332                                         versionNumber: 15
333                                 },
334                                 wikiEditor: {
335                                         ltr: true,
336                                         rtl: true
337                                 }
338                         },
339                         // Chrome 5
340                         // Chrome 6
341                         // Chrome 7
342                         // Chrome 8
343                         // Chrome 9
344                         // Chrome 10
345                         // Chrome 11
346                         // Chrome 12
347                         'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30': {
348                                 title: 'Chrome 12',
349                                 platform: 'MacIntel',
350                                 profile: {
351                                         name: 'chrome',
352                                         layout: 'webkit',
353                                         layoutVersion: 534,
354                                         platform: 'mac',
355                                         version: '12.0.742.112',
356                                         versionBase: '12',
357                                         versionNumber: 12
358                                 },
359                                 wikiEditor: {
360                                         ltr: true,
361                                         rtl: true
362                                 }
363                         },
364                         'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30': {
365                                 title: 'Chrome 12',
366                                 platform: 'Linux i686',
367                                 profile: {
368                                         name: 'chrome',
369                                         layout: 'webkit',
370                                         layoutVersion: 534,
371                                         platform: 'linux',
372                                         version: '12.0.742.68',
373                                         versionBase: '12',
374                                         versionNumber: 12
375                                 },
376                                 wikiEditor: {
377                                         ltr: true,
378                                         rtl: true
379                                 }
380                         },
381                         // Android WebKit Browser 2.3
382                         'Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1': {
383                                 title: 'Android WebKit Browser 2.3',
384                                 platform: 'Linux armv7l',
385                                 profile: {
386                                         name: 'android',
387                                         layout: 'webkit',
388                                         layoutVersion: 533,
389                                         platform: 'linux',
390                                         version: '2.3.5',
391                                         versionBase: '2',
392                                         versionNumber: 2.3
393                                 },
394                                 wikiEditor: {
395                                         ltr: true,
396                                         rtl: true
397                                 }
398                         },
399                         // Bug #34924
400                         'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) rekonq Safari/534.34': {
401                                 title: 'Rekonq',
402                                 platform: 'Linux i686',
403                                 profile: {
404                                         name: 'rekonq',
405                                         layout: 'webkit',
406                                         layoutVersion: 534,
407                                         platform: 'linux',
408                                         version: '534.34',
409                                         versionBase: '534',
410                                         versionNumber: 534.34
411                                 },
412                                 wikiEditor: {
413                                         ltr: true,
414                                         rtl: true
415                                 }
416                         }
417                 },
418                 testMap = {
419                         // Example from WikiEditor
420                         // Make sure to use raw numbers, a string like "7.0" would fail on a
421                         // version 10 browser since in string comparaison "10" is before "7.0" :)
422                         'ltr': {
423                                 'msie': [['>=', 7.0]],
424                                 'firefox': [['>=', 2]],
425                                 'opera': [['>=', 9.6]],
426                                 'safari': [['>=', 3]],
427                                 'chrome': [['>=', 3]],
428                                 'netscape': [['>=', 9]],
429                                 'blackberry': false,
430                                 'ipod': false,
431                                 'iphone': false
432                         },
433                         'rtl': {
434                                 'msie': [['>=', 8]],
435                                 'firefox': [['>=', 2]],
436                                 'opera': [['>=', 9.6]],
437                                 'safari': [['>=', 3]],
438                                 'chrome': [['>=', 3]],
439                                 'netscape': [['>=', 9]],
440                                 'blackberry': false,
441                                 'ipod': false,
442                                 'iphone': false
443                         }
444                 }
445         ;
447         // Count test cases
448         $.each( uas, function () {
449                 uacount++;
450         } );
452         QUnit.test( 'profile( navObject )', 7, function ( assert ) {
453                 var p = $.client.profile();
455                 function unknownOrType( val, type, summary ) {
456                         assert.ok( typeof val === type || val === 'unknown', summary );
457                 }
459                 assert.equal( typeof p, 'object', 'profile returns an object' );
460                 unknownOrType( p.layout, 'string', 'p.layout is a string (or "unknown")' );
461                 unknownOrType( p.layoutVersion, 'number', 'p.layoutVersion is a number (or "unknown")' );
462                 unknownOrType( p.platform, 'string', 'p.platform is a string (or "unknown")' );
463                 unknownOrType( p.version, 'string', 'p.version is a string (or "unknown")' );
464                 unknownOrType( p.versionBase, 'string', 'p.versionBase is a string (or "unknown")' );
465                 assert.equal( typeof p.versionNumber, 'number', 'p.versionNumber is a number' );
466         } );
468         QUnit.test( 'profile( navObject ) - samples', uacount, function ( assert ) {
469                 // Loop through and run tests
470                 $.each( uas, function ( rawUserAgent, data ) {
471                         // Generate a client profile object and compare recursively
472                         var ret = $.client.profile( {
473                                 userAgent: rawUserAgent,
474                                 platform: data.platform
475                         } );
476                         assert.deepEqual( ret, data.profile, 'Client profile support check for ' + data.title + ' (' + data.platform + '): ' + rawUserAgent );
477                 } );
478         } );
480         QUnit.test( 'test( testMap )', 4, function ( assert ) {
481                 // .test() uses eval, make sure no exceptions are thrown
482                 // then do a basic return value type check
483                 var testMatch = $.client.test( testMap ),
484                         ie7Profile = $.client.profile( {
485                                 'userAgent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
486                                 'platform': ''
487                         } );
489                 assert.equal( typeof testMatch, 'boolean', 'map with ltr/rtl split returns a boolean value' );
491                 testMatch = $.client.test( testMap.ltr );
493                 assert.equal( typeof testMatch, 'boolean', 'simple map (without ltr/rtl split) returns a boolean value' );
495                 assert.equal( $.client.test( {
496                         'msie': null
497                 }, ie7Profile ), true, 'returns true if any version of a browser are allowed (null)' );
499                 assert.equal( $.client.test( {
500                         'msie': false
501                 }, ie7Profile ), false, 'returns false if all versions of a browser are not allowed (false)' );
502         } );
504         QUnit.test( 'test( testMap, exactMatchOnly )', 2, function ( assert ) {
505                 var ie7Profile = $.client.profile( {
506                         'userAgent': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)',
507                         'platform': ''
508                 } );
510                 assert.equal( $.client.test( {
511                         'firefox': [['>=', 2]]
512                 }, ie7Profile, false ), true, 'returns true if browser not found and exactMatchOnly not set' );
514                 assert.equal( $.client.test( {
515                         'firefox': [['>=', 2]]
516                 }, ie7Profile, true ), false, 'returns false if browser not found and exactMatchOnly is set' );
517         } );
519         QUnit.test( 'test( testMap) - WikiEditor sample', uacount * 2, function ( assert ) {
520                 var $body = $( 'body' ),
521                         bodyClasses = $body.attr( 'class' );
523                 // Loop through and run tests
524                 $.each( uas, function ( agent, data ) {
525                         $.each( ['ltr', 'rtl'], function ( i, dir ) {
526                                 var profile, testMatch;
527                                 $body.removeClass( 'ltr rtl' ).addClass( dir );
528                                 profile = $.client.profile( {
529                                         userAgent: agent,
530                                         platform: data.platform
531                                 } );
532                                 testMatch = $.client.test( testMap, profile );
533                                 $body.removeClass( dir );
535                                 assert.equal( testMatch, data.wikiEditor[dir], 'testing comparison based on ' + dir + ', ' + agent );
536                         } );
537                 } );
539                 // Restore body classes
540                 $body.attr( 'class', bodyClasses );
541         } );
542 }( jQuery ) );