Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: chrome/test/data/webui/test_browser_proxy.js

Issue 2927653002: WebUI: Move test_browser_proxy.js one level up. (Closed)
Patch Set: Remove namespace Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/test/data/webui/settings/test_site_settings_prefs_browser_proxy.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 };
OLDNEW
« no previous file with comments | « chrome/test/data/webui/settings/test_site_settings_prefs_browser_proxy.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698