| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('settings', function() { | |
| 6 /** | |
| 7 * A base class for all test browser proxies to inherit from. Provides helper | |
| 8 * methods for allowing tests to track when a method was called. | |
| 9 * | |
| 10 * Subclasses are responsible for calling |methodCalled|, when a method is | |
| 11 * called, which will trigger callers of |whenCalled| to get notified. | |
| 12 * For example: | |
| 13 * -------------------------------------------------------------------------- | |
| 14 * var MyTestBrowserProxy = function() { | |
| 15 * TestBrowserProxy.call(this, ['myMethod']); | |
| 16 * }; | |
| 17 * MyTestBrowserProxy.prototype = function() { | |
| 18 * __proto__: TestBrowserProxy.prototype, | |
| 19 * | |
| 20 * myMethod: function(someId) { | |
| 21 * this.methodCalled('myMethod', someId); | |
| 22 * }, | |
| 23 * }; | |
| 24 * | |
| 25 * // Test code sample | |
| 26 * | |
| 27 * var testBrowserProxy = new MyTestBrowserProxy(); | |
| 28 * // ...Replacing real proxy with test proxy.... | |
| 29 * simulateClickFooButton(); | |
| 30 * testBrowserProxy.whenCalled('fooMethod').then(function(id) { | |
| 31 * assertEquals(EXPECTED_ID, id); | |
| 32 * }); | |
| 33 * -------------------------------------------------------------------------- | |
| 34 * | |
| 35 * @constructor | |
| 36 * @param {!Array<string>} methodNames Names of all methods whose calls | |
| 37 * need to be tracked. | |
| 38 */ | |
| 39 var TestBrowserProxy = function(methodNames) { | |
| 40 /** @private {!Map<string, !PromiseResolver>} */ | |
| 41 this.resolverMap_ = new Map(); | |
| 42 methodNames.forEach(this.resetResolver, this); | |
| 43 }; | |
| 44 | |
| 45 TestBrowserProxy.prototype = { | |
| 46 /** | |
| 47 * Called by subclasses when a tracked method is called from the code that | |
| 48 * is being tested. | |
| 49 * @param {string} methodName | |
| 50 * @param {*=} opt_arg Optional argument to be forwarded to the testing | |
| 51 * code, useful for checking whether the proxy method was called with | |
| 52 * the expected arguments. | |
| 53 * @protected | |
| 54 */ | |
| 55 methodCalled: function(methodName, opt_arg) { | |
| 56 this.resolverMap_.get(methodName).resolve(opt_arg); | |
| 57 }, | |
| 58 | |
| 59 /** | |
| 60 * @param {string} methodName | |
| 61 * @return {!Promise} A promise that is resolved when the given method | |
| 62 * is called. | |
| 63 */ | |
| 64 whenCalled: function(methodName) { | |
| 65 return this.resolverMap_.get(methodName).promise; | |
| 66 }, | |
| 67 | |
| 68 /** | |
| 69 * Resets the PromiseResolver associated with the given method. | |
| 70 * @param {string} methodName | |
| 71 */ | |
| 72 resetResolver: function(methodName) { | |
| 73 this.resolverMap_.set(methodName, new PromiseResolver()); | |
| 74 }, | |
| 75 | |
| 76 /** | |
| 77 * Resets all PromiseResolvers. | |
| 78 */ | |
| 79 reset: function() { | |
| 80 this.resolverMap_.forEach(function(value, methodName) { | |
| 81 this.resolverMap_.set(methodName, new PromiseResolver()); | |
| 82 }.bind(this)); | |
| 83 }, | |
| 84 }; | |
| 85 | |
| 86 return { | |
| 87 TestBrowserProxy: TestBrowserProxy, | |
| 88 }; | |
| 89 }); | |
| OLD | NEW |