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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/test/data/webui/test_browser_proxy.js
diff --git a/chrome/test/data/webui/test_browser_proxy.js b/chrome/test/data/webui/test_browser_proxy.js
new file mode 100644
index 0000000000000000000000000000000000000000..9d124eaed442f1af3a9b53438ef889d261f02f04
--- /dev/null
+++ b/chrome/test/data/webui/test_browser_proxy.js
@@ -0,0 +1,83 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * A base class for all test browser proxies to inherit from. Provides helper
+ * methods for allowing tests to track when a method was called.
+ *
+ * Subclasses are responsible for calling |methodCalled|, when a method is
+ * called, which will trigger callers of |whenCalled| to get notified.
+ * For example:
+ * --------------------------------------------------------------------------
+ * var MyTestBrowserProxy = function() {
+ * TestBrowserProxy.call(this, ['myMethod']);
+ * };
+ * MyTestBrowserProxy.prototype = function() {
+ * __proto__: TestBrowserProxy.prototype,
+ *
+ * myMethod: function(someId) {
+ * this.methodCalled('myMethod', someId);
+ * },
+ * };
+ *
+ * // Test code sample
+ *
+ * var testBrowserProxy = new MyTestBrowserProxy();
+ * // ...Replacing real proxy with test proxy....
+ * simulateClickFooButton();
+ * testBrowserProxy.whenCalled('fooMethod').then(function(id) {
+ * assertEquals(EXPECTED_ID, id);
+ * });
+ * --------------------------------------------------------------------------
+ *
+ * @constructor
+ * @param {!Array<string>} methodNames Names of all methods whose calls
+ * need to be tracked.
+ */
+var TestBrowserProxy = function(methodNames) {
+ /** @private {!Map<string, !PromiseResolver>} */
+ this.resolverMap_ = new Map();
+ methodNames.forEach(this.resetResolver, this);
+};
+
+TestBrowserProxy.prototype = {
+ /**
+ * Called by subclasses when a tracked method is called from the code that
+ * is being tested.
+ * @param {string} methodName
+ * @param {*=} opt_arg Optional argument to be forwarded to the testing
+ * code, useful for checking whether the proxy method was called with
+ * the expected arguments.
+ * @protected
+ */
+ methodCalled: function(methodName, opt_arg) {
+ this.resolverMap_.get(methodName).resolve(opt_arg);
+ },
+
+ /**
+ * @param {string} methodName
+ * @return {!Promise} A promise that is resolved when the given method
+ * is called.
+ */
+ whenCalled: function(methodName) {
+ return this.resolverMap_.get(methodName).promise;
+ },
+
+ /**
+ * Resets the PromiseResolver associated with the given method.
+ * @param {string} methodName
+ */
+ resetResolver: function(methodName) {
+ this.resolverMap_.set(methodName, new PromiseResolver());
+ },
+
+ /**
+ * Resets all PromiseResolvers.
+ */
+ reset: function() {
+ this.resolverMap_.forEach(function(value, methodName) {
+ this.resolverMap_.set(methodName, new PromiseResolver());
+ }.bind(this));
+ },
+};
« 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