OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview Library providing basic test framework functionality. | 6 * @fileoverview Library providing basic test framework functionality. |
7 */ | 7 */ |
8 | 8 |
9 /** | 9 /** |
10 * Namespace for |Test|. | 10 * Namespace for |Test|. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 */ | 114 */ |
115 testShouldFail: false, | 115 testShouldFail: false, |
116 | 116 |
117 /** | 117 /** |
118 * Extra libraries to add before loading this test file. | 118 * Extra libraries to add before loading this test file. |
119 * @type {Array.<string>} | 119 * @type {Array.<string>} |
120 */ | 120 */ |
121 extraLibraries: [], | 121 extraLibraries: [], |
122 | 122 |
123 /** | 123 /** |
| 124 * Create a new class to handle |messageNames|, assign it to |
| 125 * |this.mockHandler|, register its messages and return it. |
| 126 * @return {Mock} Mock handler class assigned to |this.mockHandler|. |
| 127 */ |
| 128 makeAndRegisterMockHandler: function(messageNames) { |
| 129 var MockClass = makeMockClass(messageNames); |
| 130 this.mockHandler = mock(MockClass); |
| 131 registerMockMessageCallbacks(this.mockHandler, MockClass); |
| 132 return this.mockHandler; |
| 133 }, |
| 134 |
| 135 /** |
| 136 * Create a new class to handle |functionNames|, assign it to |
| 137 * |this.mockGlobals|, register its global overrides, and return it. |
| 138 * @return {Mock} Mock handler class assigned to |this.mockGlobals|. |
| 139 * @see registerMockGlobals |
| 140 */ |
| 141 makeAndRegisterMockGlobals: function(functionNames) { |
| 142 var MockClass = makeMockClass(functionNames); |
| 143 this.mockGlobals = mock(MockClass); |
| 144 registerMockGlobals(this.mockGlobals, MockClass); |
| 145 return this.mockGlobals; |
| 146 }, |
| 147 |
| 148 /** |
124 * Override this method to perform initialization during preload (such as | 149 * Override this method to perform initialization during preload (such as |
125 * creating mocks and registering handlers). | 150 * creating mocks and registering handlers). |
126 * @type {Function} | 151 * @type {Function} |
127 */ | 152 */ |
128 preLoad: function() {}, | 153 preLoad: function() {}, |
129 | 154 |
130 /** | 155 /** |
131 * Override this method to perform tasks before running your test. | 156 * Override this method to perform tasks before running your test. |
132 * @type {Function} | 157 * @type {Function} |
133 */ | 158 */ |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 assertEquals(undefined, globalOverride.original); | 407 assertEquals(undefined, globalOverride.original); |
383 globalOverride.original = this[funcName]; | 408 globalOverride.original = this[funcName]; |
384 this[funcName] = globalOverride.callback.bind(globalOverride.object); | 409 this[funcName] = globalOverride.callback.bind(globalOverride.object); |
385 } | 410 } |
386 | 411 |
387 /** | 412 /** |
388 * Registers the global function name, object and callback. | 413 * Registers the global function name, object and callback. |
389 * @param {string} name The name of the message to route to this |callback|. | 414 * @param {string} name The name of the message to route to this |callback|. |
390 * @param {Object} object Pass as |this| when calling the |callback|. | 415 * @param {Object} object Pass as |this| when calling the |callback|. |
391 * @param {function(...)} callback Called by {@code chrome.send}. | 416 * @param {function(...)} callback Called by {@code chrome.send}. |
| 417 * @see overrideGlobal |
392 */ | 418 */ |
393 function registerMockGlobal(name, object, callback) { | 419 function registerMockGlobal(name, object, callback) { |
394 assertEquals(undefined, globalOverrides[name]); | 420 assertEquals(undefined, globalOverrides[name]); |
395 globalOverrides[name] = { | 421 globalOverrides[name] = { |
396 object: object, | 422 object: object, |
397 callback: callback, | 423 callback: callback, |
398 }; | 424 }; |
399 | 425 |
400 if (!deferGlobalOverrides) | 426 if (!deferGlobalOverrides) |
401 overrideGlobal(name); | 427 overrideGlobal(name); |
402 } | 428 } |
403 | 429 |
404 /** | 430 /** |
| 431 * Empty function for use in making mocks. |
| 432 * @const |
| 433 */ |
| 434 function emptyFunction() {} |
| 435 |
| 436 /** |
| 437 * Make a mock from the supplied |methodNames| array. |
| 438 * @param {Array.<string>} methodNames Array of names of methods to mock. |
| 439 * @return {Function} Constructor with prototype filled in with methods |
| 440 * matching |methodNames|. |
| 441 */ |
| 442 function makeMockClass(methodNames) { |
| 443 function MockConstructor() {} |
| 444 for(var i = 0; i < methodNames.length; i++) |
| 445 MockConstructor.prototype[methodNames[i]] = emptyFunction; |
| 446 return MockConstructor; |
| 447 } |
| 448 |
| 449 /** |
405 * Register all methods of {@code mockClass.prototype} as overrides to global | 450 * Register all methods of {@code mockClass.prototype} as overrides to global |
406 * functions of the same name as the method, using the proxy of the | 451 * functions of the same name as the method, using the proxy of the |
407 * |mockObject| to handle the functions. | 452 * |mockObject| to handle the functions. |
408 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. | 453 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. |
409 * @param {function(new:Object)} mockClass Constructor for the mocked class. | 454 * @param {function(new:Object)} mockClass Constructor for the mocked class. |
410 * @see registerMessageCallback | 455 * @see registerMockGlobal |
411 */ | 456 */ |
412 function registerMockGlobals(mockObject, mockClass) { | 457 function registerMockGlobals(mockObject, mockClass) { |
413 var mockProxy = mockObject.proxy(); | 458 var mockProxy = mockObject.proxy(); |
414 for (var func in mockClass.prototype) { | 459 for (var func in mockClass.prototype) { |
415 if (typeof mockClass.prototype[func] === 'function') | 460 if (typeof mockClass.prototype[func] === 'function') |
416 registerMockGlobal(func, mockProxy, mockProxy[func]); | 461 registerMockGlobal(func, mockProxy, mockProxy[func]); |
417 } | 462 } |
418 } | 463 } |
419 | 464 |
420 /** | 465 /** |
(...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1353 exports.DUMMY_URL = DUMMY_URL; | 1398 exports.DUMMY_URL = DUMMY_URL; |
1354 exports.TEST = TEST; | 1399 exports.TEST = TEST; |
1355 exports.TEST_F = TEST_F; | 1400 exports.TEST_F = TEST_F; |
1356 exports.GEN = GEN; | 1401 exports.GEN = GEN; |
1357 exports.GEN_INCLUDE = GEN_INCLUDE; | 1402 exports.GEN_INCLUDE = GEN_INCLUDE; |
1358 exports.WhenTestDone = WhenTestDone; | 1403 exports.WhenTestDone = WhenTestDone; |
1359 | 1404 |
1360 // Import the Mock4JS helpers. | 1405 // Import the Mock4JS helpers. |
1361 Mock4JS.addMockSupport(exports); | 1406 Mock4JS.addMockSupport(exports); |
1362 })(this); | 1407 })(this); |
OLD | NEW |