Chromium Code Reviews| 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; |
| +}(); |