| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 */ | 103 */ |
| 104 isAsync: false, | 104 isAsync: false, |
| 105 | 105 |
| 106 /** | 106 /** |
| 107 * True when the test is expected to fail for testing the test framework. | 107 * True when the test is expected to fail for testing the test framework. |
| 108 * @type {boolean} | 108 * @type {boolean} |
| 109 */ | 109 */ |
| 110 testShouldFail: false, | 110 testShouldFail: false, |
| 111 | 111 |
| 112 /** | 112 /** |
| 113 * Create a new class to handle |messageNames|, assign it to |
| 114 * |this.mockHandler|, register its messages and return it. |
| 115 * @return {Mock} Mock handler class assigned to |this.mockHandler|. |
| 116 */ |
| 117 makeAndRegisterMockHandler: function(messageNames) { |
| 118 var MockClass = makeMockClass(messageNames); |
| 119 this.mockHandler = mock(MockClass); |
| 120 registerMockMessageCallbacks(this.mockHandler, MockClass); |
| 121 return this.mockHandler; |
| 122 }, |
| 123 |
| 124 /** |
| 125 * Create a new class to handle |functionNames|, assign it to |
| 126 * |this.mockGlobals|, register its global overrides, and return it. |
| 127 * @return {Mock} Mock handler class assigned to |this.mockGlobals|. |
| 128 */ |
| 129 makeAndRegisterMockGlobals: function(functionNames) { |
| 130 var MockClass = makeMockClass(functionNames); |
| 131 this.mockGlobals = mock(MockClass); |
| 132 registerMockGlobals(this.mockGlobals, MockClass); |
| 133 return this.mockGlobals; |
| 134 }, |
| 135 |
| 136 /** |
| 113 * Override this method to perform initialization during preload (such as | 137 * Override this method to perform initialization during preload (such as |
| 114 * creating mocks and registering handlers). | 138 * creating mocks and registering handlers). |
| 115 * @type {Function} | 139 * @type {Function} |
| 116 */ | 140 */ |
| 117 preLoad: function() {}, | 141 preLoad: function() {}, |
| 118 | 142 |
| 119 /** | 143 /** |
| 120 * Override this method to perform tasks before running your test. | 144 * Override this method to perform tasks before running your test. |
| 121 * @type {Function} | 145 * @type {Function} |
| 122 */ | 146 */ |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 globalOverrides[name] = { | 405 globalOverrides[name] = { |
| 382 object: object, | 406 object: object, |
| 383 callback: callback, | 407 callback: callback, |
| 384 }; | 408 }; |
| 385 | 409 |
| 386 if (!deferGlobalOverrides) | 410 if (!deferGlobalOverrides) |
| 387 overrideGlobal(name); | 411 overrideGlobal(name); |
| 388 } | 412 } |
| 389 | 413 |
| 390 /** | 414 /** |
| 415 * Empty function for use in making mocks. |
| 416 * @const |
| 417 */ |
| 418 function emptyFunction() {} |
| 419 |
| 420 /** |
| 421 * Make a mock from the supplied |methodNames| array. |
| 422 * @param {Array.<string>} methodNames Array of names of methods to mock. |
| 423 * @return {Function} Constructor with prototype filled in with methods |
| 424 * matching |methodNames|. |
| 425 */ |
| 426 function makeMockClass(methodNames) { |
| 427 function MockConstructor() {} |
| 428 for(var i = 0; i < methodNames.length; i++) |
| 429 MockConstructor.prototype[methodNames[i]] = emptyFunction; |
| 430 return MockConstructor; |
| 431 } |
| 432 |
| 433 /** |
| 391 * Register all methods of {@code mockClass.prototype} as overrides to global | 434 * Register all methods of {@code mockClass.prototype} as overrides to global |
| 392 * functions of the same name as the method, using the proxy of the | 435 * functions of the same name as the method, using the proxy of the |
| 393 * |mockObject| to handle the functions. | 436 * |mockObject| to handle the functions. |
| 394 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. | 437 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. |
| 395 * @param {function(new:Object)} mockClass Constructor for the mocked class. | 438 * @param {function(new:Object)} mockClass Constructor for the mocked class. |
| 396 * @see registerMessageCallback | 439 * @see registerMessageCallback |
| 397 */ | 440 */ |
| 398 function registerMockGlobals(mockObject, mockClass) { | 441 function registerMockGlobals(mockObject, mockClass) { |
| 399 var mockProxy = mockObject.proxy(); | 442 var mockProxy = mockObject.proxy(); |
| 400 for (var func in mockClass.prototype) { | 443 for (var func in mockClass.prototype) { |
| (...skipping 901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1302 window.SaveMockArguments = SaveMockArguments; | 1345 window.SaveMockArguments = SaveMockArguments; |
| 1303 window.DUMMY_URL = DUMMY_URL; | 1346 window.DUMMY_URL = DUMMY_URL; |
| 1304 window.TEST = TEST; | 1347 window.TEST = TEST; |
| 1305 window.TEST_F = TEST_F; | 1348 window.TEST_F = TEST_F; |
| 1306 window.GEN = GEN; | 1349 window.GEN = GEN; |
| 1307 window.WhenTestDone = WhenTestDone; | 1350 window.WhenTestDone = WhenTestDone; |
| 1308 | 1351 |
| 1309 // Import the Mock4JS helpers. | 1352 // Import the Mock4JS helpers. |
| 1310 Mock4JS.addMockSupport(window); | 1353 Mock4JS.addMockSupport(window); |
| 1311 })(('window' in this) ? window : this); | 1354 })(('window' in this) ? window : this); |
| OLD | NEW |