Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / tests / qunit / suites / resources / jquery / jquery.client.test.js
blob88bbf5c475f9c4b91a3cc8359151326b3b8e158a
1 ( function ( $ ) {
2         var uacount, uas, testMap;
4         QUnit.module( 'jquery.client', QUnit.newMwEnvironment() );
6         /** Number of user-agent defined */
7         uacount = 0;
9         uas = ( function () {
11                 // Object keyed by userAgent. Value is an array (human-readable name, client-profile object, navigator.platform value)
12                 // Info based on results from http://toolserver.org/~krinkle/testswarm/job/174/
13                 var uas = {
14                         // Internet Explorer 6
15                         // Internet Explorer 7
16                         'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)': {
17                                 title: 'Internet Explorer 7',
18                                 platform: 'Win32',
19                                 profile: {
20                                         name: 'msie',
21                                         layout: 'trident',
22                                         layoutVersion: 'unknown',
23                                         platform: 'win',
24                                         version: '7.0',
25                                         versionBase: '7',
26                                         versionNumber: 7
27                                 },
28                                 wikiEditor: {
29                                         ltr: true,
30                                         rtl: false
31                                 }
32                         },
33                         // Internet Explorer 8
34                         // Internet Explorer 9
35                         // Internet Explorer 10
36                         'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)': {
37                                 title: 'Internet Explorer 10',
38                                 platform: 'Win32',
39                                 profile: {
40                                         name: 'msie',
41                                         layout: 'trident',
42                                         layoutVersion: 'unknown', // should be able to report 6?
43                                         platform: 'win',
44                                         version: '10.0',
45                                         versionBase: '10',
46                                         versionNumber: 10
47                                 },
48                                 wikiEditor: {
49                                         ltr: true,
50                                         rtl: true
51                                 }
52                         },
53                         // Firefox 2
54                         // Firefox 3.5
55                         'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.19) Gecko/20110420 Firefox/3.5.19': {
56                                 title: 'Firefox 3.5',
57                                 platform: 'MacIntel',
58                                 profile: {
59                                         name: 'firefox',
60                                         layout: 'gecko',
61                                         layoutVersion: 20110420,
62                                         platform: 'mac',
63                                         version: '3.5.19',
64                                         versionBase: '3',
65                                         versionNumber: 3.5
66                                 },
67                                 wikiEditor: {
68                                         ltr: true,
69                                         rtl: true
70                                 }
71                         },
72                         // Firefox 3.6
73                         'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110422 Ubuntu/10.10 (maverick) Firefox/3.6.17': {
74                                 title: 'Firefox 3.6',
75                                 platform: 'Linux i686',
76                                 profile: {
77                                         name: 'firefox',
78                                         layout: 'gecko',
79                                         layoutVersion: 20110422,
80                                         platform: 'linux',
81                                         version: '3.6.17',
82                                         versionBase: '3',
83                                         versionNumber: 3.6
84                                 },
85                                 wikiEditor: {
86                                         ltr: true,
87                                         rtl: true
88                                 }
89                         },
90                         // Firefox 4
91                         'Mozilla/5.0 (Windows NT 6.0; rv:2.0.1) Gecko/20100101 Firefox/4.0.1': {
92                                 title: 'Firefox 4',
93                                 platform: 'Win32',
94                                 profile: {
95                                         name: 'firefox',
96                                         layout: 'gecko',
97                                         layoutVersion: 20100101,
98                                         platform: 'win',
99                                         version: '4.0.1',
100                                         versionBase: '4',
101                                         versionNumber: 4
102                                 },
103                                 wikiEditor: {
104                                         ltr: true,
105                                         rtl: true
106                                 }
107                         },
108                         // Firefox 10 nightly build
109                         'Mozilla/5.0 (X11; Linux x86_64; rv:10.0a1) Gecko/20111103 Firefox/10.0a1': {
110                                 title: 'Firefox 10 nightly',
111                                 platform: 'Linux',
112                                 profile: {
113                                         name: 'firefox',
114                                         layout: 'gecko',
115                                         layoutVersion: 20111103,
116                                         platform: 'linux',
117                                         version: '10.0a1',
118                                         versionBase: '10',
119                                         versionNumber: 10
120                                 },
121                                 wikiEditor: {
122                                         ltr: true,
123                                         rtl: true
124                                 }
125                         },
126                         // Iceweasel 10.0.6
127                         'Mozilla/5.0 (X11; Linux i686; rv:10.0.6) Gecko/20100101 Iceweasel/10.0.6': {
128                                 title: 'Iceweasel 10.0.6',
129                                 platform: 'Linux',
130                                 profile: {
131                                         name: 'iceweasel',
132                                         layout: 'gecko',
133                                         layoutVersion: 20100101,
134                                         platform: 'linux',
135                                         version: '10.0.6',
136                                         versionBase: '10',
137                                         versionNumber: 10
138                                 },
139                                 wikiEditor: {
140                                         ltr: true,
141                                         rtl: true
142                                 }
143                         },
144                         // Firefox 5
145                         // Safari 3
146                         // Safari 4
147                         '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': {
148                                 title: 'Safari 4',
149                                 platform: 'MacIntel',
150                                 profile: {
151                                         name: 'safari',
152                                         layout: 'webkit',
153                                         layoutVersion: 531,
154                                         platform: 'mac',
155                                         version: '4.0.5',
156                                         versionBase: '4',
157                                         versionNumber: 4
158                                 },
159                                 wikiEditor: {
160                                         ltr: true,
161                                         rtl: true
162                                 }
163                         },
164                         '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': {
165                                 title: 'Safari 4',
166                                 platform: 'Win32',
167                                 profile: {
168                                         name: 'safari',
169                                         layout: 'webkit',
170                                         layoutVersion: 533,
171                                         platform: 'win',
172                                         version: '4.0.5',
173                                         versionBase: '4',
174                                         versionNumber: 4
175                                 },
176                                 wikiEditor: {
177                                         ltr: true,
178                                         rtl: true
179                                 }
180                         },
181                         // Safari 5
182                         // Opera 10+
183                         'Opera/9.80 (Windows NT 5.1)': {
184                                 title: 'Opera 10+ (exact version unspecified)',
185                                 platform: 'Win32',
186                                 profile: {
187                                         name: 'opera',
188                                         layout: 'presto',
189                                         layoutVersion: 'unknown',
190                                         platform: 'win',
191                                         version: '10',
192                                         versionBase: '10',
193                                         versionNumber: 10
194                                 },
195                                 wikiEditor: {
196                                         ltr: true,
197                                         rtl: true
198                                 }
199                         },
200                         // Opera 12
201                         'Opera/9.80 (Windows NT 5.1) Presto/2.12.388 Version/12.11': {
202                                 title: 'Opera 12',
203                                 platform: 'Win32',
204                                 profile: {
205                                         name: 'opera',
206                                         layout: 'presto',
207                                         layoutVersion: 'unknown',
208                                         platform: 'win',
209                                         version: '12.11',
210                                         versionBase: '12',
211                                         versionNumber: 12.11
212                                 },
213                                 wikiEditor: {
214                                         ltr: true,
215                                         rtl: true
216                                 }
217                         },
218                         // Chrome 5
219                         // Chrome 6
220                         // Chrome 7
221                         // Chrome 8
222                         // Chrome 9
223                         // Chrome 10
224                         // Chrome 11
225                         // Chrome 12
226                         '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': {
227                                 title: 'Chrome 12',
228                                 platform: 'MacIntel',
229                                 profile: {
230                                         name: 'chrome',
231                                         layout: 'webkit',
232                                         layoutVersion: 534,
233                                         platform: 'mac',
234                                         version: '12.0.742.112',
235                                         versionBase: '12',
236                                         versionNumber: 12
237                                 },
238                                 wikiEditor: {
239                                         ltr: true,
240                                         rtl: true
241                                 }
242                         },
243                         'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.68 Safari/534.30': {
244                                 title: 'Chrome 12',
245                                 platform: 'Linux i686',
246                                 profile: {
247                                         name: 'chrome',
248                                         layout: 'webkit',
249                                         layoutVersion: 534,
250                                         platform: 'linux',
251                                         version: '12.0.742.68',
252                                         versionBase: '12',
253                                         versionNumber: 12
254                                 },
255                                 wikiEditor: {
256                                         ltr: true,
257                                         rtl: true
258                                 }
259                         },
260                         // Bug #34924
261                         'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) rekonq Safari/534.34': {
262                                 title: 'Rekonq',
263                                 platform: 'Linux i686',
264                                 profile: {
265                                         name: 'rekonq',
266                                         layout: 'webkit',
267                                         layoutVersion: 534,
268                                         platform: 'linux',
269                                         version: '534.34',
270                                         versionBase: '534',
271                                         versionNumber: 534.34
272                                 },
273                                 wikiEditor: {
274                                         ltr: true,
275                                         rtl: true
276                                 }
277                         }
278                 };
279                 $.each( uas, function () {
280                         uacount++;
281                 } );
282                 return uas;
283         }() );
285         QUnit.test( 'profile userAgent support', uacount, function ( assert ) {
286                 // Generate a client profile object and compare recursively
287                 var uaTest = function ( rawUserAgent, data ) {
288                         var ret = $.client.profile( {
289                                 userAgent: rawUserAgent,
290                                 platform: data.platform
291                         } );
292                         assert.deepEqual( ret, data.profile, 'Client profile support check for ' + data.title + ' (' + data.platform + '): ' + rawUserAgent );
293                 };
295                 // Loop through and run tests
296                 $.each( uas, uaTest );
297         } );
299         QUnit.test( 'profile return validation for current user agent', 7, function ( assert ) {
300                 var p = $.client.profile();
302                 function unknownOrType( val, type, summary ) {
303                         assert.ok( typeof val === type || val === 'unknown', summary );
304                 }
306                 assert.equal( typeof p, 'object', 'profile returns an object' );
307                 unknownOrType( p.layout, 'string', 'p.layout is a string (or "unknown")' );
308                 unknownOrType( p.layoutVersion, 'number', 'p.layoutVersion is a number (or "unknown")' );
309                 unknownOrType( p.platform, 'string', 'p.platform is a string (or "unknown")' );
310                 unknownOrType( p.version, 'string', 'p.version is a string (or "unknown")' );
311                 unknownOrType( p.versionBase, 'string', 'p.versionBase is a string (or "unknown")' );
312                 assert.equal( typeof p.versionNumber, 'number', 'p.versionNumber is a number' );
313         } );
315         // Example from WikiEditor
316         // Make sure to use raw numbers, a string like "7.0" would fail on a
317         // version 10 browser since in string comparaison "10" is before "7.0" :)
318         testMap = {
319                 'ltr': {
320                         'msie': [['>=', 7.0]],
321                         'firefox': [['>=', 2]],
322                         'opera': [['>=', 9.6]],
323                         'safari': [['>=', 3]],
324                         'chrome': [['>=', 3]],
325                         'netscape': [['>=', 9]],
326                         'blackberry': false,
327                         'ipod': false,
328                         'iphone': false
329                 },
330                 'rtl': {
331                         'msie': [['>=', 8]],
332                         'firefox': [['>=', 2]],
333                         'opera': [['>=', 9.6]],
334                         'safari': [['>=', 3]],
335                         'chrome': [['>=', 3]],
336                         'netscape': [['>=', 9]],
337                         'blackberry': false,
338                         'ipod': false,
339                         'iphone': false
340                 }
341         };
343         QUnit.test( 'test', 1, function ( assert ) {
344                 // .test() uses eval, make sure no exceptions are thrown
345                 // then do a basic return value type check
346                 var testMatch = $.client.test( testMap );
348                 assert.equal( typeof testMatch, 'boolean', 'test returns a boolean value' );
350         } );
352         QUnit.test( 'User-agent matches against WikiEditor\'s compatibility map', uacount * 2, function ( assert ) {
353                 var $body = $( 'body' ),
354                         bodyClasses = $body.attr( 'class' );
356                 // Loop through and run tests
357                 $.each( uas, function ( agent, data ) {
358                         $.each( ['ltr', 'rtl'], function ( i, dir ) {
359                                 var profile, testMatch;
360                                 $body.removeClass( 'ltr rtl' ).addClass( dir );
361                                 profile = $.client.profile( {
362                                         userAgent: agent,
363                                         platform: data.platform
364                                 } );
365                                 testMatch = $.client.test( testMap, profile );
366                                 $body.removeClass( dir );
368                                 assert.equal( testMatch, data.wikiEditor[dir], 'testing comparison based on ' + dir + ', ' + agent );
369                         } );
370                 } );
372                 // Restore body classes
373                 $body.attr( 'class', bodyClasses );
374         } );
375 }( jQuery ) );