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
|
<!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.
-->
<!--
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.debug.ErrorHandler async tests</title>
<!-- These paths should be relative to the test -->
<script src="../base.js"></script>
<script>
goog.require('goog.testing.jsunit');
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.debug.ErrorHandler');
goog.require('goog.userAgent');
</script>
</head>
<body>
<script>
var testCase = new goog.testing.AsyncTestCase(document.title);
testCase.setUpPage = function() {
this.waitForAsync('setUpPage');
this.stepTimeout = 5 * 1000;
this.oldTimeout = window.setTimeout;
this.oldInterval = window.setInterval;
this.handler = new goog.debug.ErrorHandler(
goog.bind(this.onException, this));
this.handler.protectWindowSetTimeout();
this.handler.protectWindowSetInterval();
this.exceptions = [];
this.errors = 0;
// Override the error event handler, since we are purposely throwing
// exceptions from global functions, and expect them
this.oldWindowOnError = window.onerror;
window.onerror = goog.bind(this.onError, this);
this.add(new goog.testing.TestCase.Test(
'test results', this.testResults, this));
this.timeoutId = window.setTimeout(goog.bind(this.timeOut, this), 10);
this.intervalId = window.setInterval(goog.bind(this.interval, this), 20);
};
testCase.tearDownPage = function() {
window.setTimeout = this.oldTimeout;
window.setInterval = this.oldInterval;
};
testCase.onException = function(e) {
this.exceptions.push(e);
if (this.timeoutHit && this.intervalHit) {
this.continueTesting();
}
};
testCase.onError = function(msg, url, line) {
this.errors++;
return true;
};
testCase.timeOut = function() {
this.timeoutHit = true;
throw arguments.callee;
};
testCase.interval = function() {
this.intervalHit = true;
window.clearTimeout(this.intervalId);
throw arguments.callee;
};
testCase.testResults = function() {
var timeoutHit, intervalHit;
for (var i = 0; i < this.exceptions.length; ++i) {
switch (this.exceptions[i]) {
case this.timeOut: timeoutHit = true; break;
case this.interval: intervalHit = true; break;
}
}
assertTrue('timeout exception not received', timeoutHit);
assertTrue('interval exception not received', intervalHit);
assertTrue('timeout not called', this.timeoutHit);
assertTrue('interval not called', this.intervalHit);
if (!goog.userAgent.WEBKIT) {
assertEquals('2 exceptions should have been caught and rethrown',
2, this.errors)
}
};
function setUpPage() {
testCase.runTests();
}
// Standalone Closure Test Runner.
if (typeof G_testRunner != 'undefined') {
G_testRunner.initialize(testCase);
}
</script>
</body>
</html>
|