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

Unified Diff: chrome/browser/resources/google_now/common_test_util.js

Issue 158003003: Convert Google Now's State Change Gathering Mechanism to use Promises (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Typo Created 6 years, 10 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
Index: chrome/browser/resources/google_now/common_test_util.js
diff --git a/chrome/browser/resources/google_now/common_test_util.js b/chrome/browser/resources/google_now/common_test_util.js
index 28c4b930a67f8a69480d53e58a520342d65cd8a2..98a2141106f0d07a1a0a6c4f4e645d94b89508f1 100644
--- a/chrome/browser/resources/google_now/common_test_util.js
+++ b/chrome/browser/resources/google_now/common_test_util.js
@@ -71,3 +71,45 @@ function getMockHandlerContainer(eventIdentifier) {
return mockEventContainer;
}
+
+/**
+ * MockPromise
+ * The JS test harness expects all calls to complete synchronously.
+ * As a result, we can't use built-in JS promises since they run asynchronously.
+ * Instead of mocking all possible calls to promises, a skeleton
+ * implementation is provided to get the tests to pass.
+ */
+var Promise = function() {
+ function PromisePrototypeObject(asyncTask) {
+ var result;
+ asyncTask(
+ function(asyncResult) {
+ result = [asyncResult];
+ },
+ function() {}); // Errors are unsupported.
rgustafson 2014/02/11 03:18:59 Two spaces between ; and comment.
+
+ function then(callback) {
+ callback.apply(null, result);
+ }
+ return {then: then, isPromise: true};
+ }
+
+ function all(arrayOfPromises) {
+ var results = [];
+ for (i = 0; i < arrayOfPromises.length; i++) {
+ if (arrayOfPromises[i].isPromise) {
+ arrayOfPromises[i].then(function(result) {
+ results[i] = result;
+ });
+ } else {
+ results[i] = arrayOfPromises[i];
+ }
+ }
+ var promise = new PromisePrototypeObject(function(onSuccess) {
+ onSuccess(results);
+ });
+ return promise;
+ }
+ PromisePrototypeObject.all = all;
+ return PromisePrototypeObject;
+}();

Powered by Google App Engine
This is Rietveld 408576698