Debian Codegraph @

closure-library/closure/goog/debug/errorhandler_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.ErrorHandlerTest');
16 goog.setTestOnly('goog.debug.ErrorHandlerTest');
17
18 goog.require('goog.debug.ErrorHandler');
19 goog.require('goog.testing.MockControl');
20 goog.require('goog.testing.jsunit');
21
22 var oldGetObjectByName;
23
24 // provide our own window that implements our instrumented and
25 // immediate-call versions of setTimeout and setInterval
26 var fakeWin = {};
27
28 var errorHandler;
29 var mockControl;
30
31 function badTimer() {
32 arguments.callee.called = true;
33 throw 'die die die';
34 }
35
36 function setUp() {
37 mockControl = new goog.testing.MockControl();
38 // On IE, globalEval happens async. So make it synchronous.
39 goog.globalEval = function(str) { eval(str); };
40
41 oldGetObjectByName = goog.getObjectByName;
42 goog.getObjectByName = function(name) {
43 if (name == 'window') {
44 return fakeWin;
45 } else {
46 return oldGetObjectByName(name);
47 }
48 };
49
50 fakeWin.setTimeout = function(fn, time) {
51 fakeWin.setTimeout.called = true;
52 fakeWin.setTimeout.that = this;
53 if (goog.isString(fn)) {
54 eval(fn);
55 } else {
56 fn.apply(this, Array.prototype.slice.call(arguments, 2));
57 }
58 };
59
60 fakeWin.setInterval = function(fn, time) {
61 fakeWin.setInterval.called = true;
62 fakeWin.setInterval.that = this;
63 if (goog.isString(fn)) {
64 eval(fn);
65 } else {
66 fn.apply(this, Array.prototype.slice.call(arguments, 2));
67 }
68 };
69
70 fakeWin.requestAnimationFrame = function(fn) {
71 fakeWin.requestAnimationFrame.called = true;
72 fakeWin.requestAnimationFrame.that = this;
73 fn();
74 };
75
76 // just record the exception in the error handler when it happens
77 errorHandler = new goog.debug.ErrorHandler(function(ex) { this.ex = ex; });
78 }
79
80 function tearDown() {
81 mockControl.$tearDown();
82 goog.dispose(errorHandler);
83 errorHandler = null;
84
85 goog.getObjectByName = oldGetObjectByName;
86
87 delete badTimer['__protected__'];
88 }
89
90 function testWrapSetTimeout() {
91 errorHandler.protectWindowSetTimeout();
92
93 var caught;
94
95 try {
96 fakeWin.setTimeout(badTimer, 3);
97 } catch (ex) {
98 caught = ex;
99 }
100 assertSetTimeoutError(caught);
101 }
102
103 function testWrapSetTimeoutWithoutException() {
104 errorHandler.protectWindowSetTimeout();
105
106 fakeWin.setTimeout(function(x, y) {
107 assertEquals('test', x);
108 assertEquals(7, y);
109 }, 3, 'test', 7);
110 }
111
112 function testWrapSetTimeoutWithString() {
113 errorHandler.protectWindowSetTimeout();
114
115 var caught;
116
117 try {
118 fakeWin.setTimeout('badTimer()', 3);
119 } catch (ex) {
120 caught = ex;
121 }
122 assertSetTimeoutError(caught);
123 }
124
125 function testWrapSetInterval() {
126 errorHandler.protectWindowSetInterval();
127
128 var caught;
129
130 try {
131 fakeWin.setInterval(badTimer, 3);
132 } catch (ex) {
133 caught = ex;
134 }
135 assertSetIntervalError(caught);
136 }
137
138 function testWrapSetIntervalWithoutException() {
139 errorHandler.protectWindowSetInterval();
140
141 fakeWin.setInterval(function(x, y) {
142 assertEquals('test', x);
143 assertEquals(7, y);
144 }, 3, 'test', 7);
145 }
146
147 function testWrapSetIntervalWithString() {
148 errorHandler.protectWindowSetInterval();
149
150 var caught;
151
152 try {
153 fakeWin.setInterval('badTimer()', 3);
154 } catch (ex) {
155 caught = ex;
156 }
157 assertSetIntervalError(caught);
158 }
159
160 function testWrapRequestAnimationFrame() {
161 errorHandler.protectWindowRequestAnimationFrame();
162
163 var caught;
164 try {
165 fakeWin.requestAnimationFrame(badTimer);
166 } catch (ex) {
167 caught = ex;
168 }
169 assertRequestAnimationFrameError(caught);
170 }
171
172 function testDisposal() {
173 fakeWin = goog.getObjectByName('window');
174 var originalSetTimeout = fakeWin.setTimeout;
175 var originalSetInterval = fakeWin.setInterval;
176
177 errorHandler.protectWindowSetTimeout();
178 errorHandler.protectWindowSetInterval();
179
180 assertNotEquals(originalSetTimeout, fakeWin.setTimeout);
181 assertNotEquals(originalSetInterval, fakeWin.setInterval);
182
183 errorHandler.dispose();
184
185 assertEquals(originalSetTimeout, fakeWin.setTimeout);
186 assertEquals(originalSetInterval, fakeWin.setInterval);
187 }
188
189 function testUnwrap() {
190 var fn = function() {};
191 var wrappedFn = errorHandler.wrap(fn);
192 assertNotEquals(wrappedFn, fn);
193
194 assertEquals(fn, errorHandler.unwrap(fn));
195 assertEquals(fn, errorHandler.unwrap(wrappedFn));
196 }
197
198 function testStackPreserved() {
199 var e;
200 var hasStacks;
201 function specialFunctionName() {
202 var e = Error();
203 hasStacks = !!e.stack;
204 throw e;
205 }
206 var wrappedFn = errorHandler.wrap(specialFunctionName);
207 try {
208 wrappedFn();
209 } catch (exception) {
210 e = exception;
211 }
212 assertTrue(!!e);
213 if (hasStacks) {
214 assertContains('specialFunctionName', e.stack);
215 }
216 }
217
218 function testGetProtectedFunction() {
219 var fn = function() { throw new Error('Foo'); };
220 var protectedFn = errorHandler.getProtectedFunction(fn);
221 var e = assertThrows(protectedFn);
222 assertTrue(e instanceof goog.debug.ErrorHandler.ProtectedFunctionError);
223 assertEquals('Foo', e.cause.message);
224 }
225
226 function testGetProtectedFunctionNullError() {
227 var fn = function() { throw null; };
228 var protectedFn = errorHandler.getProtectedFunction(fn);
229 var e = assertThrows(protectedFn);
230 assertTrue(e instanceof goog.debug.ErrorHandler.ProtectedFunctionError);
231 assertNull(e.cause);
232 }
233
234 function testGetProtectedFunction_withoutWrappedErrors() {
235 var shouldCallErrorLog = !!Error.captureStackTrace;
236 if (shouldCallErrorLog) {
237 mockControl.createMethodMock(goog.global.console, 'error');
238 }
239 errorHandler.setWrapErrors(false);
240 var fn = function() {
241 var e = new Error('Foo');
242 e.stack = 'STACK';
243 throw e;
244 };
245 var protectedFn = errorHandler.getProtectedFunction(fn);
246 if (shouldCallErrorLog) {
247 goog.global.console.error('Foo', 'STACK');
248 }
249 mockControl.$replayAll();
250 var e = assertThrows(protectedFn);
251 mockControl.$verifyAll();
252 assertTrue(e instanceof Error);
253 assertEquals('Foo', e.message);
254 assertEquals(e.stack, 'STACK');
255 }
256
257 function testGetProtectedFunction_withoutWrappedErrorsWithMessagePrefix() {
258 errorHandler.setWrapErrors(false);
259 errorHandler.setPrefixErrorMessages(true);
260 var fn = function() { throw new Error('Foo'); };
261 var protectedFn = errorHandler.getProtectedFunction(fn);
262 var e = assertThrows(protectedFn);
263 assertTrue(e instanceof Error);
264 assertEquals(
265 goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX + 'Foo',
266 e.message);
267
268 var stringError = function() { throw 'String'; };
269 protectedFn = errorHandler.getProtectedFunction(stringError);
270 e = assertThrows(protectedFn);
271 assertEquals('string', typeof e);
272 assertEquals(
273 goog.debug.ErrorHandler.ProtectedFunctionError.MESSAGE_PREFIX + 'String',
274 e);
275 }
276
277 function testProtectedFunction_infiniteLoop() {
278 var numErrors = 0;
279 var errorHandler = new goog.debug.ErrorHandler(function(ex) { numErrors++; });
280 errorHandler.protectWindowSetTimeout();
281
282 fakeWin.setTimeout(function() { fakeWin.setTimeout(badTimer, 3); }, 3);
283 assertEquals(
284 'Error handler should only have been executed once.', 1, numErrors);
285 }
286
287 function assertSetTimeoutError(caught) {
288 assertMethodCalledHelper('setTimeout', caught);
289 }
290
291 function assertSetIntervalError(caught) {
292 assertMethodCalledHelper('setInterval', caught);
293 }
294
295 function assertRequestAnimationFrameError(caught) {
296 assertMethodCalledHelper('requestAnimationFrame', caught);
297 }
298
299 function assertMethodCalledHelper(method, caught) {
300 assertTrue('exception not thrown', !!caught);
301 assertEquals(
302 'exception not caught by error handler', caught.cause, errorHandler.ex);
303 assertTrue('fake ' + method + ' not called', !!fakeWin[method].called);
304 assertTrue(
305 '"this" not passed to original ' + method,
306 fakeWin[method].that === fakeWin);
307 }