Skip to content

Commit 62a393a

Browse files
Rewrite the wait function so that it gives useful failure messages in IE 6-7.
1 parent 560f349 commit 62a393a

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

test.new/static/js/test_helpers.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
(function () {
33

4+
var IS_IE6 = (navigator.userAgent.indexOf('MSIE 6.0') > -1);
5+
var IS_IE7 = (navigator.userAgent.indexOf('MSIE 7.0') > -1);
46
var CONSOLE_LOG_SUPPORTED = ('console' in window) && console.log;
57
var CONSOLE_GROUP_SUPPORTED = ('console' in window) && console.group &&
68
console.groupEnd;
@@ -26,10 +28,33 @@
2628

2729
window.info = info;
2830

29-
// A function that acts like setTimeout, except with arguments reversed. This
31+
// A function that acts like setTimeout, except with arguments reversed. This
3032
// is far more readable within tests.
31-
function wait(duration, fn) {
32-
return setTimeout(fn, duration);
33+
function wait(duration, done, fn) {
34+
var handler = function () {
35+
try {
36+
fn();
37+
} catch (e) {
38+
// In IE6, manually throwing errors doesn't trigger window.onerror.
39+
// Instead, we'll pass an actual error to the `done` callback.
40+
if (IS_IE6) {
41+
if (Object.isFunction(done)) {
42+
return done(new Error(e.message));
43+
}
44+
}
45+
46+
// In IE7, window.onerror will get triggered, but with a generic
47+
// error message. Instead we need to throw an actual Error object
48+
// because it does not grok this whole custom error thing.
49+
if (IS_IE7) {
50+
throw new Error(e.message);
51+
}
52+
53+
// In other browsers, we can just re-throw it and be fine.
54+
throw e;
55+
}
56+
};
57+
return setTimeout(handler, duration);
3358
}
3459
window.wait = wait;
3560

test.new/tests/dom.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ suite('DOM', function () {
429429
$('testdiv').update('hello from div!<script>\ntestVar="hello!";\n</'+'script>');
430430
assert.equal('hello from div!', $('testdiv').innerHTML);
431431

432-
wait(100, function () {
432+
wait(100, done, function () {
433433
assert.equal('hello!', testVar);
434434

435435
Element.update('testdiv','another hello from div!\n<script>testVar="another hello!"</'+'script>\nhere it goes');
@@ -441,19 +441,19 @@ suite('DOM', function () {
441441
/^another hello from div!\s+here it goes$/
442442
);
443443

444-
wait(100, function() {
444+
wait(100, done, function() {
445445
assert.equal('another hello!', testVar);
446446

447447
Element.update('testdiv',
448448
'a\n<script>testVar="a"\ntestVar="b"</'+'script>');
449449

450-
wait(100, function () {
450+
wait(100, done, function () {
451451
assert.equal('b', testVar);
452452

453453
Element.update('testdiv',
454454
'x<script>testVar2="a"</'+'script>\nblah\n'+
455455
'x<script>testVar2="b"</'+'script>');
456-
wait(100, function () {
456+
wait(100, done, function () {
457457
assert.equal('b', testVar2);
458458
done();
459459
});
@@ -560,7 +560,7 @@ suite('DOM', function () {
560560
test('#replace (with script)', function (done) {
561561
$('testdiv-replace-4').replace('hello from div!<script>testVarReplace="hello!"</'+'script>');
562562
assert.equal('hello from div!', $('testdiv-replace-container-4').innerHTML);
563-
wait(100, function(){
563+
wait(100, done, function(){
564564
assert.equal('hello!', testVarReplace);
565565

566566
$('testdiv-replace-5').replace('another hello from div!\n<script>testVarReplace="another hello!"</'+'script>\nhere it goes');
@@ -570,7 +570,7 @@ suite('DOM', function () {
570570
$('testdiv-replace-container-5').innerHTML,
571571
/^another hello from div!\s+here it goes$/
572572
);
573-
wait(100, function(){
573+
wait(100, done, function(){
574574
assert.equal('another hello!', testVarReplace);
575575
done();
576576
});

test.new/tests/form.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ suite('Form', function () {
111111
assert.equal(0, timedCounter);
112112

113113
// Test it doesn't change on first check.
114-
wait(550, function () {
114+
wait(550, done, function () {
115115
assert.equal(0, timedCounter, 'callbacks should be 0');
116116

117117
// Change it, ensure it hasn't changed immediately.
118118
$('input_enabled').value = 'yowza!';
119119
assert.equal(0, timedCounter, 'callbacks should be 0 still');
120120

121-
wait(550, function () {
121+
wait(550, done, function () {
122122
assert.equal(1, timedCounter, 'callbacks should be 1');
123123

124-
wait(550, function () {
124+
wait(550, done, function () {
125125
assert.equal(1, timedCounter, 'callbacks should be 1 still');
126126
observer.stop();
127127
done();

test.new/tests/function.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ suite('Function', function () {
118118
delayedFunction.delay(0.8);
119119
delayedFunctionWithArgs.delay(0.8, 'hello', 'world');
120120
assert.isUndefined(window.delayed);
121-
wait(1000, function() {
121+
wait(1000, done, function() {
122122
assert(window.delayed);
123123
assert.equal('hello world', window.delayedWithArgs);
124124
done();
@@ -152,13 +152,13 @@ suite('Function', function () {
152152
var deferredFunction = function() { window.deferred = true; };
153153
deferredFunction.defer();
154154
assert.isUndefined(window.deferred);
155-
wait(50, function() {
155+
wait(50, done, function() {
156156
assert(window.deferred);
157157

158158
window.deferredValue = 0;
159159
var deferredFunction2 = function(arg) { window.deferredValue = arg; };
160160
deferredFunction2.defer('test');
161-
wait(50, function() {
161+
wait(50, done, function() {
162162
assert.equal('test', window.deferredValue);
163163

164164
window.deferBoundProperly = false;
@@ -170,7 +170,7 @@ suite('Function', function () {
170170

171171
func.bind(obj).defer();
172172

173-
wait(50, function() {
173+
wait(50, done, function() {
174174
assert(window.deferBoundProperly,
175175
"deferred bound functions should preserve original scope");
176176

@@ -179,7 +179,7 @@ suite('Function', function () {
179179

180180
str.evalScripts.bind(str).defer();
181181

182-
wait(50, function() {
182+
wait(50, done, function() {
183183
assert(window.deferBoundProperlyOnString);
184184
done();
185185
});

test.new/tests/layout.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -388,34 +388,34 @@ suite("Layout",function(){
388388
suite('document.viewport', function () {
389389

390390
test('#getDimensions', function (done) {
391-
this.timeout(5000);
391+
this.timeout(60000);
392392
var original = document.viewport.getDimensions();
393393

394394
try {
395395
window.resizeTo(800, 600);
396396
} catch (e) {
397397
info("Can't resize.");
398-
return;
399-
}
400398

401-
wait(1000, function() {
399+
return done();
400+
}
401+
wait(1000, done, function() {
402402
var before = document.viewport.getDimensions();
403403

404404
var delta = { width: 800 - before.width, height: 600 - before.height };
405405

406406
window.resizeBy(50, 50);
407-
wait(1000, function() {
407+
wait(1000, done, function() {
408408
var after = document.viewport.getDimensions();
409409

410410
// Assume that JavaScript window resizing is disabled if before width
411411
// and after width are the same.
412412
if (before.width === after.width) {
413413
RESIZE_DISABLED = true;
414414
info("SKIPPING REMAINING TESTS (JavaScript window resizing disabled)");
415-
done();
416-
return;
415+
return done();
417416
}
418417

418+
419419
assert.equal(
420420
before.width + 50, after.width,
421421
"NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THIS TEST TO PASS"
@@ -425,7 +425,7 @@ suite("Layout",function(){
425425
"NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THIS TEST TO PASS"
426426
);
427427

428-
wait(1000, function() {
428+
wait(1000, done, function() {
429429
// Restore original dimensions.
430430
window.resizeTo(
431431
original.width + delta.width,
@@ -464,20 +464,21 @@ suite("Layout",function(){
464464

465465
if (RESIZE_DISABLED) {
466466
info("SKIPPING REMAINING TESTS (JavaScript window resizing disabled)");
467+
done();
467468
return;
468469
}
469470

470471
window.resizeTo(200, 650);
471472

472-
wait(1000, function() {
473+
wait(1000, done, function() {
473474
var before = document.viewport.getDimensions();
474475
var delta = { width: 200 - before.width, height: 650 - before.height };
475476

476477
window.scrollTo(25, 35);
477478
assert.equal(25, document.viewport.getScrollOffsets().left,
478479
"NOTE: YOU MUST ALLOW JAVASCRIPT TO RESIZE YOUR WINDOW FOR THESE TESTS TO PASS");
479480

480-
wait(1000, function() {
481+
wait(1000, done, function() {
481482
// Restore original dimensions.
482483
window.resizeTo(
483484
original.width + delta.width,

test.new/tests/periodical_executer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ suite('PeriodicalExecuter', function () {
1111
// peEventFired will stop the PeriodicalExecuter after 3 callbacks
1212
new PeriodicalExecuter(peEventFired, 0.05);
1313

14-
wait(600, function() {
14+
wait(600, done, function() {
1515
assert.equal(3, peEventCount);
1616
done();
1717
});

0 commit comments

Comments
 (0)