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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
QUnit.begin = function() {
console.log("Starting test suite");
console.log("================================================\n");
};
QUnit.moduleDone = function(opts) {
if(opts.failed === 0) {
console.log("\u2714 All tests passed in '"+opts.name+"' module");
} else {
console.log("\u2716 "+ opts.failed +" tests failed in '"+opts.name+"' module");
}
};
QUnit.done = function(opts) {
console.log("\n================================================");
console.log("Tests completed in "+opts.runtime+" milliseconds");
console.log(opts.passed + " tests of "+opts.total+" passed, "+opts.failed+" failed.");
};
module('Basics', {
setup:function() {
},
teardown:function() {
}
});
test("globals set up", function() {
ok(window.Modernizr, 'global modernizr object created');
});
test("bind is implemented", function() {
ok(Function.prototype.bind, 'bind is a member of Function.prototype');
var a = function(){
return this.modernizr;
};
a = a.bind({modernizr: 'just awsome'});
equal("just awsome", a(), 'bind works as expected');
// thank you webkit layoutTests
var result;
function F(x, y)
{
result = this + " -> x:" + x + ", y:" + y;
}
G = F.bind("'a'", "'b'");
H = G.bind("'Cannot rebind this!'", "'c'");
G(1,2);
equal(result, "\'a\' -> x:\'b\', y:1");
H(1,2);
equal(result, "\'a\' -> x:\'b\', y:\'c\'");
var f = new F(1,2);
equal(result, "[object Object] -> x:1, y:2");
var g = new G(1,2);
equal(result, "[object Object] -> x:\'b\', y:1");
var h = new H(1,2);
equal(result, "[object Object] -> x:\'b\', y:\'c\'");
ok(f instanceof F, "f instanceof F");
ok(g instanceof F, "g instanceof F");
ok(h instanceof F, "h instanceof F");
// Bound functions don't have a 'prototype' property.
ok("prototype" in F, '"prototype" in F');
// The object passed to bind as 'this' must be callable.
raises(function(){
Function.bind.call(undefined);
});
// Objects that allow call but not construct can be bound, but should throw if used with new.
var abcAt = String.prototype.charAt.bind("abc");
equal(abcAt(1), "b", 'Objects that allow call but not construct can be bound...');
equal(1, Function.bind.length, 'it exists');
});
test("document.documentElement is valid and correct",1, function() {
equal(document.documentElement,document.getElementsByTagName('html')[0]);
});
test("no-js class is gone.", function() {
ok(!/(?:^|\s)no-js(?:^|\s)/.test(document.documentElement.className),
'no-js class is gone');
ok(/(?:^|\s)js(?:^|\s)/.test(document.documentElement.className),
'html.js class is present');
ok(/(?:^|\s)\+no-js(?:\s|$)/.test(document.documentElement.className),
'html.+no-js class is still present');
ok(/(?:^|\s)no-js-(?:\s|$)/.test(document.documentElement.className),
'html.no-js- class is still present');
ok(/(?:^|\s)i-has-no-js(?:\s|$)/.test(document.documentElement.className),
'html.i-has-no-js class is still present');
if (document.querySelector){
ok(document.querySelector('html.js') == document.documentElement,
"document.querySelector('html.js') matches.");
}
});
test('html shim worked', function(){
expect(2);
// the exact test we use in the script
var elem = document.getElementsByTagName("section")[0];
elem.id = "html5section";
ok( elem.childNodes.length === 1 , 'unknown elements dont collapse');
elem.style.color = 'red';
ok( /red|#ff0000/i.test(elem.style.color), 'unknown elements are styleable')
});
module('Modernizr classes and bools', {
setup:function() {
},
teardown:function() {
}
});
test('html classes are looking good',function(){
var classes = TEST.trim(document.documentElement.className).split(/\s+/);
var modprops = Object.keys(Modernizr),
newprops = modprops;
// decrement for the properties that are private
for (var i = -1, len = TEST.privates.length; ++i < len; ){
var item = TEST.privates[i];
equal(-1, TEST.inArray(item, classes), 'private Modernizr object '+ item +'should not have matching classes');
equal(-1, TEST.inArray('no-' + item, classes), 'private Modernizr object no-'+item+' should not have matching classes');
}
// decrement for the non-boolean objects
// for (var i = -1, len = TEST.inputs.length; ++i < len; ){
// if (Modernizr[TEST.inputs[i]] != undefined) newprops--;
// }
// TODO decrement for the extraclasses
// decrement for deprecated ones.
$.each( TEST.deprecated, function(key, val){
newprops.splice( TEST.inArray(item, newprops), 1);
});
//equal(classes,newprops,'equal number of classes and global object props');
if (classes.length !== newprops){
//window.console && console.log(classes, newprops);
}
for (var i = 0, len = classes.length, aclass; i <len; i++){
aclass = classes[i];
// Skip js related classes.
if (/^(?:js|\+no-js|no-js-|i-has-no-js)$/.test(aclass)) continue;
if (aclass.indexOf('no-') === 0){
aclass = aclass.replace('no-','');
equal(Modernizr[aclass], false,
aclass + ' is correctly false in the classes and object')
} else {
equal(Modernizr[aclass], true,
aclass + ' is correctly true in the classes and object')
}
}
for (var i = 0, len = classes.length, aclass; i <len; i++){
equal(classes[i],classes[i].toLowerCase(),'all classes are lowerCase.');
}
// Remove fake no-js classes before test.
var docElClass = document.documentElement.className;
$.each(['\\+no-js', 'no-js-', 'i-has-no-js'], function(i, fakeClass) {
docElClass = docElClass.replace(new RegExp('(^|\\s)' + fakeClass + '(\\s|$)', 'g'), '$1$2');
});
equal(/[^\s]no-/.test(docElClass), false, 'whitespace between all classes.');
})
test('Modernizr properties are looking good',function(){
var count = 0,
nobool = TEST.API.concat(TEST.inputs)
.concat(TEST.audvid)
.concat(TEST.privates)
.concat(['textarea']); // due to forms-placeholder.js test
for (var prop in window.Modernizr){
if (Modernizr.hasOwnProperty(prop)){
if (TEST.inArray(prop,nobool) >= 0) continue;
ok(Modernizr[prop] === true || Modernizr[prop] === false,
'Modernizr.'+prop+' is a straight up boolean');
equal(prop,prop.toLowerCase(),'all properties are lowerCase.')
}
}
})
test('Modernizr.audio and Modernizr.video',function(){
for (var i = -1, len = TEST.audvid.length; ++i < len;){
var prop = TEST.audvid[i];
if (Modernizr[prop].toString() == 'true'){
ok(Modernizr[prop], 'Modernizr.'+prop+' is truthy.');
equal(Modernizr[prop] == true,true, 'Modernizr.'+prop+' is == true')
equal(typeof Modernizr[prop] === 'object',true,'Moderizr.'+prop+' is truly an object');
equal(Modernizr[prop] !== true,true, 'Modernizr.'+prop+' is !== true')
} else {
equal(Modernizr[prop] != true,true, 'Modernizr.'+prop+' is != true')
}
}
});
test('Modernizr results match expected values',function(){
// i'm bringing over a few tests from inside Modernizr.js
equal(!!document.createElement('canvas').getContext,Modernizr.canvas,'canvas test consistent');
equal(!!window.Worker,Modernizr.webworkers,'web workers test consistent')
});
module('Modernizr\'s API methods', {
setup:function() {
},
teardown:function() {
}
});
test('Modernizr.addTest()',22,function(){
var docEl = document.documentElement;
Modernizr.addTest('testtrue',function(){
return true;
});
Modernizr.addTest('testtruthy',function(){
return 100;
});
Modernizr.addTest('testfalse',function(){
return false;
});
Modernizr.addTest('testfalsy',function(){
return undefined;
});
ok(docEl.className.indexOf(' testtrue') >= 0,'positive class added');
equal(Modernizr.testtrue,true,'positive prop added');
ok(docEl.className.indexOf(' testtruthy') >= 0,'positive class added');
equal(Modernizr.testtruthy,100,'truthy value is not casted to straight boolean');
ok(docEl.className.indexOf(' no-testfalse') >= 0,'negative class added');
equal(Modernizr.testfalse,false,'negative prop added');
ok(docEl.className.indexOf(' no-testfalsy') >= 0,'negative class added');
equal(Modernizr.testfalsy,undefined,'falsy value is not casted to straight boolean');
Modernizr.addTest('testcamelCase',function(){
return true;
});
ok(docEl.className.indexOf(' testcamelCase') === -1,
'camelCase test name toLowerCase()\'d');
// okay new signature for this API! woo
Modernizr.addTest('testboolfalse', false);
ok(~docEl.className.indexOf(' no-testboolfalse'), 'Modernizr.addTest(feature, bool): negative class added');
equal(Modernizr.testboolfalse, false, 'Modernizr.addTest(feature, bool): negative prop added');
Modernizr.addTest('testbooltrue', true);
ok(~docEl.className.indexOf(' testbooltrue'), 'Modernizr.addTest(feature, bool): positive class added');
equal(Modernizr.testbooltrue, true, 'Modernizr.addTest(feature, bool): positive prop added');
Modernizr.addTest({'testobjboolfalse': false,
'testobjbooltrue' : true });
ok(~docEl.className.indexOf(' no-testobjboolfalse'), 'Modernizr.addTest({feature: bool}): negative class added');
equal(Modernizr.testobjboolfalse, false, 'Modernizr.addTest({feature: bool}): negative prop added');
ok(~docEl.className.indexOf(' testobjbooltrue'), 'Modernizr.addTest({feature: bool}): positive class added');
equal(Modernizr.testobjbooltrue, true, 'Modernizr.addTest({feature: bool}): positive prop added');
Modernizr.addTest({'testobjfnfalse': function(){ return false },
'testobjfntrue' : function(){ return true } });
ok(~docEl.className.indexOf(' no-testobjfnfalse'), 'Modernizr.addTest({feature: bool}): negative class added');
equal(Modernizr.testobjfnfalse, false, 'Modernizr.addTest({feature: bool}): negative prop added');
ok(~docEl.className.indexOf(' testobjfntrue'), 'Modernizr.addTest({feature: bool}): positive class added');
equal(Modernizr.testobjfntrue, true, 'Modernizr.addTest({feature: bool}): positive prop added');
Modernizr
.addTest('testchainone', true)
.addTest({ testchaintwo: true })
.addTest('testchainthree', function(){ return true; });
ok( Modernizr.testchainone == Modernizr.testchaintwo == Modernizr.testchainthree, 'addTest is chainable');
}); // eo addTest
test('Modernizr.mq: media query testing',function(){
var $html = $('html');
$.mobile = {};
// from jquery mobile
$.mobile.media = (function() {
// TODO: use window.matchMedia once at least one UA implements it
var cache = {},
testDiv = $( "<div id='jquery-mediatest'>" ),
fakeBody = $( "<body>" ).append( testDiv );
return function( query ) {
if ( !( query in cache ) ) {
var styleBlock = document.createElement('style'),
cssrule = "@media " + query + " { #jquery-mediatest { position:absolute; } }";
//must set type for IE!
styleBlock.type = "text/css";
if (styleBlock.styleSheet){
styleBlock.styleSheet.cssText = cssrule;
}
else {
styleBlock.appendChild(document.createTextNode(cssrule));
}
$html.prepend( fakeBody ).prepend( styleBlock );
cache[ query ] = testDiv.css( "position" ) === "absolute";
fakeBody.add( styleBlock ).remove();
}
return cache[ query ];
};
})();
ok(Modernizr.mq,'Modernizr.mq() doesn\' freak out.');
equal($.mobile.media('only screen'), Modernizr.mq('only screen'),'screen media query matches jQuery mobile\'s result');
equal(Modernizr.mq('only all'), Modernizr.mq('only all'), 'Cache hit matches');
});
test('Modernizr.hasEvent()',function(){
ok(typeof Modernizr.hasEvent == 'function','Modernizr.hasEvent() is a function');
equal(Modernizr.hasEvent('click'), true,'click event is supported');
equal(Modernizr.hasEvent('modernizrcustomevent'), false,'random event is definitely not supported');
/* works fine in webkit but not gecko
equal( Modernizr.hasEvent('resize', window),
!Modernizr.hasEvent('resize', document.createElement('div')),
'Resize is supported in window but not a div, typically...');
*/
});
test('Modernizr.testStyles()',function(){
equal(typeof Modernizr.testStyles, 'function','Modernizr.testStyles() is a function');
var style = '#modernizr{ width: 9px; height: 4px; font-size: 0; color: papayawhip; }';
Modernizr.testStyles(style, function(elem, rule){
equal(style, rule, 'rule passsed back matches what i gave it.')
equal(elem.offsetWidth, 9, 'width was set through the style');
equal(elem.offsetHeight, 4, 'height was set through the style');
equal(elem.id, 'modernizr', 'element is indeed the modernizr element');
});
});
test('Modernizr._[properties]',function(){
equal(6, Modernizr._prefixes.length, 'Modernizr._prefixes has 6 items');
equal(4, Modernizr._domPrefixes.length, 'Modernizr.domPrefixes has 4 items');
});
test('Modernizr.testProp()',function(){
equal(true, Modernizr.testProp('margin'), 'Everyone supports margin');
equal(false, Modernizr.testProp('happiness'), 'Nobody supports the happiness style. :(');
equal(true, Modernizr.testProp('fontSize'), 'Everyone supports fontSize');
equal(false, Modernizr.testProp('font-size'), 'Nobody supports font-size');
equal('pointerEvents' in document.createElement('div').style,
Modernizr.testProp('pointerEvents'),
'results for `pointer-events` are consistent with a homegrown feature test');
});
test('Modernizr.testAllProps()',function(){
equal(true, Modernizr.testAllProps('margin'), 'Everyone supports margin');
equal(false, Modernizr.testAllProps('happiness'), 'Nobody supports the happiness style. :(');
equal(true, Modernizr.testAllProps('fontSize'), 'Everyone supports fontSize');
equal(false, Modernizr.testAllProps('font-size'), 'Nobody supports font-size');
equal(Modernizr.csstransitions, Modernizr.testAllProps('transition'), 'Modernizr result matches API result: csstransitions');
equal(Modernizr.csscolumns, Modernizr.testAllProps('columnCount'), 'Modernizr result matches API result: csscolumns')
});
test('Modernizr.prefixed() - css and DOM resolving', function(){
// https://gist.github.com/523692
function gimmePrefix(prop, obj){
var prefixes = ['Moz','Khtml','Webkit','O','ms'],
domPrefixes = ['moz','khtml','webkit','o','ms'],
elem = document.createElement('div'),
upper = prop.charAt(0).toUpperCase() + prop.slice(1);
if(!obj) {
if (prop in elem.style)
return prop;
for (var len = prefixes.length; len--; ){
if ((prefixes[len] + upper) in elem.style)
return (prefixes[len] + upper);
}
} else {
if (prop in obj)
return prop;
for (var len = domPrefixes.length; len--; ){
if ((domPrefixes[len] + upper) in obj)
return (domPrefixes[len] + upper);
}
}
return false;
}
var propArr = ['transition', 'backgroundSize', 'boxSizing', 'borderImage',
'borderRadius', 'boxShadow', 'columnCount'];
var domPropArr = [{ 'prop': 'requestAnimationFrame', 'obj': window },
{ 'prop': 'querySelectorAll', 'obj': document },
{ 'prop': 'matchesSelector', 'obj': document.createElement('div') }];
for (var i = -1, len = propArr.length; ++i < len; ){
var prop = propArr[i];
equal(Modernizr.prefixed(prop), gimmePrefix(prop), 'results for ' + prop + ' match the homebaked prefix finder');
}
for (var i = -1, len = domPropArr.length; ++i < len; ){
var prop = domPropArr[i];
ok(!!~Modernizr.prefixed(prop.prop, prop.obj, false).toString().indexOf(gimmePrefix(prop.prop, prop.obj)), 'results for ' + prop.prop + ' match the homebaked prefix finder');
}
});
// FIXME: so a few of these are whitelisting for webkit. i'd like to improve that.
test('Modernizr.prefixed autobind', function(){
var rAFName;
// quick sniff to find the local rAF prefixed name.
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !rAFName; ++x) {
rAFName = window[vendors[x]+'RequestAnimationFrame'] && vendors[x]+'RequestAnimationFrame';
}
if (rAFName){
// rAF returns a function
equal(
'function',
typeof Modernizr.prefixed('requestAnimationFrame', window),
"Modernizr.prefixed('requestAnimationFrame', window) returns a function")
// unless we false it to a string
equal(
rAFName,
Modernizr.prefixed('requestAnimationFrame', window, false),
"Modernizr.prefixed('requestAnimationFrame', window, false) returns a string (the prop name)")
}
if (document.body.webkitMatchesSelector || document.body.mozMatchesSelector){
var fn = Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body);
//returns function
equal(
'function',
typeof fn,
"Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body) returns a function");
// fn scoping
equal(
true,
fn('body'),
"Modernizr.prefixed('matchesSelector', HTMLElement.prototype, document.body) is scoped to the body")
}
// Webkit only: are there other objects that are prefixed?
if (window.webkitNotifications){
// should be an object.
equal(
'object',
typeof Modernizr.prefixed('Notifications', window),
"Modernizr.prefixed('Notifications') returns an object");
}
// Webkit only:
if (typeof document.webkitIsFullScreen !== 'undefined'){
// boolean
equal(
'boolean',
typeof Modernizr.prefixed('isFullScreen', document),
"Modernizr.prefixed('isFullScreen') returns a boolean");
}
// Moz only:
if (typeof document.mozFullScreen !== 'undefined'){
// boolean
equal(
'boolean',
typeof Modernizr.prefixed('fullScreen', document),
"Modernizr.prefixed('fullScreen') returns a boolean");
}
// Webkit-only.. takes advantage of Webkit's mixed case of prefixes
if (document.body.style.WebkitAnimation){
// string
equal(
'string',
typeof Modernizr.prefixed('animation', document.body.style),
"Modernizr.prefixed('animation', document.body.style) returns value of that, as a string");
equal(
animationStyle.toLowerCase(),
Modernizr.prefixed('animation', document.body.style, false).toLowerCase(),
"Modernizr.prefixed('animation', document.body.style, false) returns the (case-normalized) name of the property: webkitanimation");
}
equal(
false,
Modernizr.prefixed('doSomethingAmazing$#$', window),
"Modernizr.prefixed('doSomethingAmazing$#$', window) : Gobbledygook with prefixed(str,obj) returns false");
equal(
false,
Modernizr.prefixed('doSomethingAmazing$#$', window, document.body),
"Modernizr.prefixed('doSomethingAmazing$#$', window) : Gobbledygook with prefixed(str,obj, scope) returns false");
equal(
false,
Modernizr.prefixed('doSomethingAmazing$#$', window, false),
"Modernizr.prefixed('doSomethingAmazing$#$', window) : Gobbledygook with prefixed(str,obj, false) returns false");
});
|