| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Common test utilities. | 5 // Common test utilities. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Allows console.log output. | 8 * Allows console.log output. |
| 9 */ | 9 */ |
| 10 var showConsoleLogOutput = false; | 10 var showConsoleLogOutput = false; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 /** | 75 /** |
| 76 * MockPromise | 76 * MockPromise |
| 77 * The JS test harness expects all calls to complete synchronously. | 77 * The JS test harness expects all calls to complete synchronously. |
| 78 * As a result, we can't use built-in JS promises since they run asynchronously. | 78 * As a result, we can't use built-in JS promises since they run asynchronously. |
| 79 * Instead of mocking all possible calls to promises, a skeleton | 79 * Instead of mocking all possible calls to promises, a skeleton |
| 80 * implementation is provided to get the tests to pass. | 80 * implementation is provided to get the tests to pass. |
| 81 */ | 81 */ |
| 82 var Promise = function() { | 82 var Promise = function() { |
| 83 function PromisePrototypeObject(asyncTask) { | 83 function PromisePrototypeObject(asyncTask) { |
| 84 var result; | 84 var result; |
| 85 var resolved = false; | |
| 86 asyncTask( | 85 asyncTask( |
| 87 function(asyncResult) { | 86 function(asyncResult) { |
| 88 result = asyncResult; | 87 result = asyncResult; |
| 89 resolved = true; | |
| 90 }, | 88 }, |
| 91 function(asyncFailureResult) { | 89 function() {}); // Errors are unsupported. |
| 92 result = asyncFailureResult; | |
| 93 resolved = false; | |
| 94 }); | |
| 95 | 90 |
| 96 function then(callback) { | 91 function then(callback) { |
| 97 if (resolved) { | 92 callback.call(null, result); |
| 98 callback.call(null, result); | |
| 99 } | |
| 100 return this; | |
| 101 } | 93 } |
| 102 | 94 return {then: then, isPromise: true}; |
| 103 // Promises use the function name "catch" to call back error handlers. | |
| 104 // We can't use "catch" since function or variable names cannot use the word | |
| 105 // "catch". | |
| 106 function catchFunc(callback) { | |
| 107 if (!resolved) { | |
| 108 callback.call(null, result); | |
| 109 } | |
| 110 return this; | |
| 111 } | |
| 112 | |
| 113 return {then: then, catch: catchFunc, isPromise: true}; | |
| 114 } | 95 } |
| 115 | 96 |
| 116 function all(arrayOfPromises) { | 97 function all(arrayOfPromises) { |
| 117 var results = []; | 98 var results = []; |
| 118 for (i = 0; i < arrayOfPromises.length; i++) { | 99 for (i = 0; i < arrayOfPromises.length; i++) { |
| 119 if (arrayOfPromises[i].isPromise) { | 100 if (arrayOfPromises[i].isPromise) { |
| 120 arrayOfPromises[i].then(function(result) { | 101 arrayOfPromises[i].then(function(result) { |
| 121 results[i] = result; | 102 results[i] = result; |
| 122 }); | 103 }); |
| 123 } else { | 104 } else { |
| 124 results[i] = arrayOfPromises[i]; | 105 results[i] = arrayOfPromises[i]; |
| 125 } | 106 } |
| 126 } | 107 } |
| 127 var promise = new PromisePrototypeObject(function(resolve) { | 108 var promise = new PromisePrototypeObject(function(resolve) { |
| 128 resolve(results); | 109 resolve(results); |
| 129 }); | 110 }); |
| 130 return promise; | 111 return promise; |
| 131 } | 112 } |
| 132 PromisePrototypeObject.all = all; | 113 PromisePrototypeObject.all = all; |
| 133 return PromisePrototypeObject; | 114 return PromisePrototypeObject; |
| 134 }(); | 115 }(); |
| OLD | NEW |