OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
191 * @see registerMockGlobals | 191 * @see registerMockGlobals |
192 */ | 192 */ |
193 makeAndRegisterMockGlobals: function(functionNames) { | 193 makeAndRegisterMockGlobals: function(functionNames) { |
194 var MockClass = makeMockClass(functionNames); | 194 var MockClass = makeMockClass(functionNames); |
195 this.mockGlobals = mock(MockClass); | 195 this.mockGlobals = mock(MockClass); |
196 registerMockGlobals(this.mockGlobals, MockClass); | 196 registerMockGlobals(this.mockGlobals, MockClass); |
197 return this.mockGlobals; | 197 return this.mockGlobals; |
198 }, | 198 }, |
199 | 199 |
200 /** | 200 /** |
201 * Create a container of mocked standalone functions to handle | |
202 * '.'-separated |apiNames|, assign it to |this.mockApis|, register its API | |
203 * overrides and return it. | |
204 * @return {Mock} Mock handler class. | |
205 * @see makeMockFunctions | |
206 * @see registerMockApis | |
207 */ | |
208 makeAndRegisterMockApis: function (apiNames) { | |
209 var apiMockNames = []; | |
arv (Not doing code reviews)
2013/07/12 14:18:34
Maybe:
var apiMockNames = apiNames.map(function(n
vadimt
2013/07/12 18:22:35
Cool
| |
210 for(var i = 0; i < apiNames.length; i++) { | |
211 apiMockNames.push(apiNames [i].replace(/\./g, '_')); | |
arv (Not doing code reviews)
2013/07/12 14:18:34
Another option is to keep the '.' in the name. The
vadimt
2013/07/12 18:22:35
I considered this. I feel that the using code woul
| |
212 } | |
213 | |
214 this.mockApis = makeMockFunctions(apiMockNames); | |
215 registerMockApis(this.mockApis); | |
216 return this.mockApis; | |
217 }, | |
218 | |
219 /** | |
220 * Create a container of mocked standalone functions to handle | |
221 * |functionNames|, assign it to |this.mockLocalFunctions| and return it. | |
222 * @return {Mock} Mock handler class. | |
arv (Not doing code reviews)
2013/07/12 14:18:34
@param {!Array.<string>} functionNames
vadimt
2013/07/12 18:22:35
Done.
| |
223 * @see makeMockFunctions | |
224 */ | |
225 makeMockLocalFunctions: function(functionNames) { | |
226 this.mockLocalFunctions = makeMockFunctions(functionNames); | |
227 return this.mockLocalFunctions; | |
228 }, | |
229 | |
230 /** | |
201 * Override this method to perform initialization during preload (such as | 231 * Override this method to perform initialization during preload (such as |
202 * creating mocks and registering handlers). | 232 * creating mocks and registering handlers). |
203 * @type {Function} | 233 * @type {Function} |
204 */ | 234 */ |
205 preLoad: function() {}, | 235 preLoad: function() {}, |
206 | 236 |
207 /** | 237 /** |
208 * Override this method to perform tasks before running your test. | 238 * Override this method to perform tasks before running your test. |
209 * @type {Function} | 239 * @type {Function} |
210 */ | 240 */ |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 globalOverrides[name] = { | 529 globalOverrides[name] = { |
500 object: object, | 530 object: object, |
501 callback: callback, | 531 callback: callback, |
502 }; | 532 }; |
503 | 533 |
504 if (!deferGlobalOverrides) | 534 if (!deferGlobalOverrides) |
505 overrideGlobal(name); | 535 overrideGlobal(name); |
506 } | 536 } |
507 | 537 |
508 /** | 538 /** |
539 * Registers the mock API call and its function. | |
540 * @param {string} name The '_'-separated name of the API call. | |
541 * @param {function(...)} theFunction Mock function for this API call. | |
542 */ | |
543 function registerMockApi(name, theFunction) { | |
544 var path = name.split('_'); | |
545 | |
546 var namespace = this; | |
547 for(var i = 0; i < path.length-1; ++i) { | |
arv (Not doing code reviews)
2013/07/12 14:18:34
missing whitespaces around bin op
arv (Not doing code reviews)
2013/07/12 14:18:34
i++
vadimt
2013/07/12 18:22:35
Done.
vadimt
2013/07/12 18:22:35
Done.
| |
548 var fieldName = path[i]; | |
549 if(!namespace[fieldName]) | |
550 namespace[fieldName] = {}; | |
551 | |
552 namespace = namespace[fieldName]; | |
arv (Not doing code reviews)
2013/07/12 14:18:34
Once again I'm confused. It seems like you are bui
vadimt
2013/07/12 18:22:35
See above about external mock4js that generates th
| |
553 } | |
554 | |
555 var fieldName = path[path.length-1]; | |
556 assertEquals(undefined, namespace[fieldName]); | |
557 namespace[fieldName] = theFunction; | |
558 } | |
559 | |
560 /** | |
509 * Empty function for use in making mocks. | 561 * Empty function for use in making mocks. |
510 * @const | 562 * @const |
511 */ | 563 */ |
512 function emptyFunction() {} | 564 function emptyFunction() {} |
513 | 565 |
514 /** | 566 /** |
515 * Make a mock from the supplied |methodNames| array. | 567 * Make a mock from the supplied |methodNames| array. |
516 * @param {Array.<string>} methodNames Array of names of methods to mock. | 568 * @param {Array.<string>} methodNames Array of names of methods to mock. |
517 * @return {Function} Constructor with prototype filled in with methods | 569 * @return {Function} Constructor with prototype filled in with methods |
518 * matching |methodNames|. | 570 * matching |methodNames|. |
519 */ | 571 */ |
520 function makeMockClass(methodNames) { | 572 function makeMockClass(methodNames) { |
521 function MockConstructor() {} | 573 function MockConstructor() {} |
522 for(var i = 0; i < methodNames.length; i++) | 574 for(var i = 0; i < methodNames.length; i++) |
523 MockConstructor.prototype[methodNames[i]] = emptyFunction; | 575 MockConstructor.prototype[methodNames[i]] = emptyFunction; |
524 return MockConstructor; | 576 return MockConstructor; |
525 } | 577 } |
526 | 578 |
527 /** | 579 /** |
580 * Create a new class to handle |functionNames|, add method 'functions()' | |
581 * that returns a container of standalone functions based on the mock class | |
582 * members, and return it. | |
583 * @return {Mock} Mock handler class. | |
584 */ | |
585 function makeMockFunctions(functionNames) { | |
586 var MockClass = makeMockClass(functionNames); | |
587 var mockFunctions = mock(MockClass); | |
588 var mockProxy = mockFunctions.proxy(); | |
589 | |
590 mockFunctions.functions_ = {}; | |
591 | |
592 for (var func in MockClass.prototype) { | |
593 if (typeof MockClass.prototype[func] === 'function') | |
arv (Not doing code reviews)
2013/07/12 14:18:34
What about accessors? Maybe add a todo?
vadimt
2013/07/12 18:22:35
This method is used to generate:
1. Local function
| |
594 mockFunctions.functions_[func] = mockProxy[func].bind(mockProxy); | |
595 } | |
596 | |
597 mockFunctions.functions = function () { | |
598 return this.functions_; | |
arv (Not doing code reviews)
2013/07/12 14:18:34
Is this the correct this? If so, maybe just rename
vadimt
2013/07/12 18:22:35
Yes, this is a correct 'this': the one of mockFunc
| |
599 }; | |
600 | |
601 return mockFunctions; | |
602 } | |
603 | |
604 /** | |
528 * Register all methods of {@code mockClass.prototype} as overrides to global | 605 * Register all methods of {@code mockClass.prototype} as overrides to global |
529 * functions of the same name as the method, using the proxy of the | 606 * functions of the same name as the method, using the proxy of the |
530 * |mockObject| to handle the functions. | 607 * |mockObject| to handle the functions. |
531 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. | 608 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. |
532 * @param {function(new:Object)} mockClass Constructor for the mocked class. | 609 * @param {function(new:Object)} mockClass Constructor for the mocked class. |
533 * @see registerMockGlobal | 610 * @see registerMockGlobal |
534 */ | 611 */ |
535 function registerMockGlobals(mockObject, mockClass) { | 612 function registerMockGlobals(mockObject, mockClass) { |
536 var mockProxy = mockObject.proxy(); | 613 var mockProxy = mockObject.proxy(); |
537 for (var func in mockClass.prototype) { | 614 for (var func in mockClass.prototype) { |
538 if (typeof mockClass.prototype[func] === 'function') | 615 if (typeof mockClass.prototype[func] === 'function') |
539 registerMockGlobal(func, mockProxy, mockProxy[func]); | 616 registerMockGlobal(func, mockProxy, mockProxy[func]); |
540 } | 617 } |
541 } | 618 } |
542 | 619 |
543 /** | 620 /** |
621 * Register all functions in |mockObject.functions()| as global API calls. | |
622 * @param {Mock4JS.Mock} mockObject The mock to register callbacks against. | |
623 * @see registerMockApi | |
624 */ | |
625 function registerMockApis(mockObject) { | |
626 var functions = mockObject.functions(); | |
627 for (var func in functions) { | |
628 if (typeof functions[func] === 'function') | |
629 registerMockApi(func, functions[func]); | |
630 } | |
631 } | |
632 | |
633 /** | |
544 * Overrides {@code chrome.send} for routing messages to javascript | 634 * Overrides {@code chrome.send} for routing messages to javascript |
545 * functions. Also falls back to sending with the original chrome object. | 635 * functions. Also falls back to sending with the original chrome object. |
546 * @param {string} messageName The message to route. | 636 * @param {string} messageName The message to route. |
547 */ | 637 */ |
548 function send(messageName) { | 638 function send(messageName) { |
549 var callback = sendCallbacks[messageName]; | 639 var callback = sendCallbacks[messageName]; |
550 if (callback != undefined) | 640 if (callback != undefined) |
551 callback[1].apply(callback[0], Array.prototype.slice.call(arguments, 1)); | 641 callback[1].apply(callback[0], Array.prototype.slice.call(arguments, 1)); |
552 else | 642 else |
553 this.__proto__.send.apply(this.__proto__, arguments); | 643 this.__proto__.send.apply(this.__proto__, arguments); |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1536 exports.TEST = TEST; | 1626 exports.TEST = TEST; |
1537 exports.TEST_F = TEST_F; | 1627 exports.TEST_F = TEST_F; |
1538 exports.RUNTIME_TEST_F = TEST_F; | 1628 exports.RUNTIME_TEST_F = TEST_F; |
1539 exports.GEN = GEN; | 1629 exports.GEN = GEN; |
1540 exports.GEN_INCLUDE = GEN_INCLUDE; | 1630 exports.GEN_INCLUDE = GEN_INCLUDE; |
1541 exports.WhenTestDone = WhenTestDone; | 1631 exports.WhenTestDone = WhenTestDone; |
1542 | 1632 |
1543 // Import the Mock4JS helpers. | 1633 // Import the Mock4JS helpers. |
1544 Mock4JS.addMockSupport(exports); | 1634 Mock4JS.addMockSupport(exports); |
1545 })(this); | 1635 })(this); |
OLD | NEW |