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