Debian Codegraph @

closure-library/closure/goog/debug/errorhandler_async_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.debug.ErrorHandlerAsyncTest');
16 goog.setTestOnly('goog.debug.ErrorHandlerAsyncTest');
17
18 goog.require('goog.Promise');
19 goog.require('goog.debug.ErrorHandler');
20 goog.require('goog.testing.TestCase');
21 goog.require('goog.testing.jsunit');
22 goog.require('goog.userAgent');
23
24
25 /** @type {!goog.promise.Resolver} */
26 var resolver;
27 var testCase = new goog.testing.TestCase(document.title);
28
29 testCase.setUpPage = function() {
30 resolver = goog.Promise.withResolver();
31
32 this.oldTimeout = window.setTimeout;
33 this.oldInterval = window.setInterval;
34 this.oldRequestAnimationFrame = window.requestAnimationFrame;
35
36 // Whether requestAnimationFrame is available for testing.
37 this.testingReqAnimFrame = !!window.requestAnimationFrame;
38
39 this.handler = new goog.debug.ErrorHandler(goog.bind(this.onException, this));
40 this.handler.protectWindowSetTimeout();
41 this.handler.protectWindowSetInterval();
42 this.handler.protectWindowRequestAnimationFrame();
43 this.exceptions = [];
44 this.errors = 0;
45
46 // Override the error event handler, since we are purposely throwing
47 // exceptions from global functions, and expect them
48 this.oldWindowOnError = window.onerror;
49 window.onerror = goog.bind(this.onError, this);
50
51 window.setTimeout(goog.bind(this.timeOut, this), 10);
52 this.intervalId = window.setInterval(goog.bind(this.interval, this), 20);
53
54 if (this.testingReqAnimFrame) {
55 window.requestAnimationFrame(goog.bind(this.animFrame, this));
56 }
57 this.promiseTimeout = 10000; // 10s.
58 };
59
60 testCase.tearDownPage = function() {
61 window.setTimeout = this.oldTimeout;
62 window.setInterval = this.oldInterval;
63 window.requestAnimationFrame = this.oldRequestAnimationFrame;
64 };
65
66 testCase.onException = function(e) {
67 this.exceptions.push(e);
68 if (this.timeoutHit && this.intervalHit &&
69 (!this.testingReqAnimFrame || this.animFrameHit)) {
70 resolver.resolve();
71 }
72 };
73
74 testCase.onError = function(msg, url, line) {
75 this.errors++;
76 return true;
77 };
78
79 testCase.timeOut = function() {
80 this.timeoutHit = true;
81 throw arguments.callee;
82 };
83
84 testCase.interval = function() {
85 this.intervalHit = true;
86 window.clearTimeout(this.intervalId);
87 throw arguments.callee;
88 };
89
90 testCase.animFrame = function() {
91 this.animFrameHit = true;
92 throw arguments.callee;
93 };
94
95 testCase.addNewTest('testResults', function() {
96 return resolver.promise.then(function() {
97 var timeoutHit, intervalHit, animFrameHit;
98
99 for (var i = 0; i < this.exceptions.length; ++i) {
100 switch (this.exceptions[i]) {
101 case this.timeOut:
102 timeoutHit = true;
103 break;
104 case this.interval:
105 intervalHit = true;
106 break;
107 case this.animFrame:
108 animFrameHit = true;
109 break;
110 }
111 }
112
113 assertTrue('timeout exception not received', timeoutHit);
114 assertTrue('timeout not called', this.timeoutHit);
115 assertTrue('interval exception not received', intervalHit);
116 assertTrue('interval not called', this.intervalHit);
117 if (this.testingReqAnimFrame) {
118 assertTrue('anim frame exception not received', animFrameHit);
119 assertTrue('animFrame not called', this.animFrameHit);
120 }
121
122 if (!goog.userAgent.WEBKIT) {
123 var expectedRethrownCount = this.testingReqAnimFrame ? 3 : 2;
124 assertEquals(
125 expectedRethrownCount + ' exceptions should have been rethrown',
126 expectedRethrownCount, this.errors);
127 }
128 }, null, this);
129 });
130
131 // Standalone Closure Test Runner.
132 G_testRunner.initialize(testCase);