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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 */ | 64 */ |
65 function getMockHandlerContainer(eventIdentifier) { | 65 function getMockHandlerContainer(eventIdentifier) { |
66 var eventIdentifierParts = eventIdentifier.split('.'); | 66 var eventIdentifierParts = eventIdentifier.split('.'); |
67 var mockEventContainer = mockEventHandlers; | 67 var mockEventContainer = mockEventHandlers; |
68 eventIdentifierParts.forEach(function(fragment) { | 68 eventIdentifierParts.forEach(function(fragment) { |
69 mockEventContainer = mockEventContainer[fragment]; | 69 mockEventContainer = mockEventContainer[fragment]; |
70 }); | 70 }); |
71 | 71 |
72 return mockEventContainer; | 72 return mockEventContainer; |
73 } | 73 } |
| 74 |
| 75 /** |
| 76 * MockPromise |
| 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. |
| 79 * Instead of mocking all possible calls to promises, a skeleton |
| 80 * implementation is provided to get the tests to pass. |
| 81 */ |
| 82 var Promise = function() { |
| 83 function PromisePrototypeObject(asyncTask) { |
| 84 var result; |
| 85 asyncTask( |
| 86 function(asyncResult) { |
| 87 result = asyncResult; |
| 88 }, |
| 89 function() {}); // Errors are unsupported. |
| 90 |
| 91 function then(callback) { |
| 92 callback.call(null, result); |
| 93 } |
| 94 return {then: then, isPromise: true}; |
| 95 } |
| 96 |
| 97 function all(arrayOfPromises) { |
| 98 var results = []; |
| 99 for (i = 0; i < arrayOfPromises.length; i++) { |
| 100 if (arrayOfPromises[i].isPromise) { |
| 101 arrayOfPromises[i].then(function(result) { |
| 102 results[i] = result; |
| 103 }); |
| 104 } else { |
| 105 results[i] = arrayOfPromises[i]; |
| 106 } |
| 107 } |
| 108 var promise = new PromisePrototypeObject(function(resolve) { |
| 109 resolve(results); |
| 110 }); |
| 111 return promise; |
| 112 } |
| 113 PromisePrototypeObject.all = all; |
| 114 return PromisePrototypeObject; |
| 115 }(); |
OLD | NEW |