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; |
85 asyncTask( | 86 asyncTask( |
86 function(asyncResult) { | 87 function(asyncResult) { |
87 result = asyncResult; | 88 result = asyncResult; |
| 89 resolved = true; |
88 }, | 90 }, |
89 function() {}); // Errors are unsupported. | 91 function(asyncFailureResult) { |
| 92 result = asyncFailureResult; |
| 93 resolved = false; |
| 94 }); |
90 | 95 |
91 function then(callback) { | 96 function then(callback) { |
92 callback.call(null, result); | 97 if (resolved) { |
| 98 callback.call(null, result); |
| 99 } |
| 100 return this; |
93 } | 101 } |
94 return {then: then, isPromise: true}; | 102 |
| 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}; |
95 } | 114 } |
96 | 115 |
97 function all(arrayOfPromises) { | 116 function all(arrayOfPromises) { |
98 var results = []; | 117 var results = []; |
99 for (i = 0; i < arrayOfPromises.length; i++) { | 118 for (i = 0; i < arrayOfPromises.length; i++) { |
100 if (arrayOfPromises[i].isPromise) { | 119 if (arrayOfPromises[i].isPromise) { |
101 arrayOfPromises[i].then(function(result) { | 120 arrayOfPromises[i].then(function(result) { |
102 results[i] = result; | 121 results[i] = result; |
103 }); | 122 }); |
104 } else { | 123 } else { |
105 results[i] = arrayOfPromises[i]; | 124 results[i] = arrayOfPromises[i]; |
106 } | 125 } |
107 } | 126 } |
108 var promise = new PromisePrototypeObject(function(resolve) { | 127 var promise = new PromisePrototypeObject(function(resolve) { |
109 resolve(results); | 128 resolve(results); |
110 }); | 129 }); |
111 return promise; | 130 return promise; |
112 } | 131 } |
113 PromisePrototypeObject.all = all; | 132 PromisePrototypeObject.all = all; |
114 return PromisePrototypeObject; | 133 return PromisePrototypeObject; |
115 }(); | 134 }(); |
OLD | NEW |