| 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 22 matching lines...) Expand all Loading... |
| 33 * function. Can be either 'chrome' or 'instrumented'. | 33 * function. Can be either 'chrome' or 'instrumented'. |
| 34 * @param {string} eventIdentifier Event identifier, such as | 34 * @param {string} eventIdentifier Event identifier, such as |
| 35 * 'runtime.onSuspend'. | 35 * 'runtime.onSuspend'. |
| 36 */ | 36 */ |
| 37 function mockChromeEvent(topLevelContainer, eventIdentifier) { | 37 function mockChromeEvent(topLevelContainer, eventIdentifier) { |
| 38 var eventIdentifierParts = eventIdentifier.split('.'); | 38 var eventIdentifierParts = eventIdentifier.split('.'); |
| 39 var eventName = eventIdentifierParts.pop(); | 39 var eventName = eventIdentifierParts.pop(); |
| 40 var originalMethodContainer = topLevelContainer; | 40 var originalMethodContainer = topLevelContainer; |
| 41 var mockEventContainer = mockEventHandlers; | 41 var mockEventContainer = mockEventHandlers; |
| 42 eventIdentifierParts.forEach(function(fragment) { | 42 eventIdentifierParts.forEach(function(fragment) { |
| 43 originalMethodContainer = | 43 originalMethodContainer = originalMethodContainer[fragment] = |
| 44 originalMethodContainer[fragment] = | |
| 45 originalMethodContainer[fragment] || {}; | 44 originalMethodContainer[fragment] || {}; |
| 46 mockEventContainer = | 45 mockEventContainer = mockEventContainer[fragment] = |
| 47 mockEventContainer[fragment] = | |
| 48 mockEventContainer[fragment] || {}; | 46 mockEventContainer[fragment] || {}; |
| 49 }); | 47 }); |
| 50 | 48 |
| 51 mockEventContainer[eventName] = []; | 49 mockEventContainer[eventName] = []; |
| 52 originalMethodContainer[eventName] = { | 50 originalMethodContainer[eventName] = { |
| 53 addListener: function(callback) { | 51 addListener: function(callback) { |
| 54 mockEventContainer[eventName].push(callback); | 52 mockEventContainer[eventName].push(callback); |
| 55 } | 53 } |
| 56 }; | 54 }; |
| 57 } | 55 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 var funcResolved = false; | 94 var funcResolved = false; |
| 97 func( | 95 func( |
| 98 function(resolveResult) { | 96 function(resolveResult) { |
| 99 funcResult = resolveResult; | 97 funcResult = resolveResult; |
| 100 funcResolved = true; | 98 funcResolved = true; |
| 101 }, | 99 }, |
| 102 function(rejectResult) { | 100 function(rejectResult) { |
| 103 funcResult = rejectResult; | 101 funcResult = rejectResult; |
| 104 funcResolved = false; | 102 funcResolved = false; |
| 105 }); | 103 }); |
| 106 return { result: funcResult, resolved: funcResolved }; | 104 return {result: funcResult, resolved: funcResolved}; |
| 107 } | 105 } |
| 108 | 106 |
| 109 function then(onResolve, onReject) { | 107 function then(onResolve, onReject) { |
| 110 var resolutionHandler = | 108 var resolutionHandler = isCallable(onResolve) ? onResolve : function() { |
| 111 isCallable(onResolve) ? onResolve : function() { return result; }; | 109 return result; |
| 112 var rejectionHandler = | 110 }; |
| 113 isCallable(onReject) ? onReject : function() { return result; }; | 111 var rejectionHandler = isCallable(onReject) ? onReject : function() { |
| 112 return result; |
| 113 }; |
| 114 var handlerResult = | 114 var handlerResult = |
| 115 resolved ? resolutionHandler(result) : rejectionHandler(result); | 115 resolved ? resolutionHandler(result) : rejectionHandler(result); |
| 116 var promiseResolved = resolved; | 116 var promiseResolved = resolved; |
| 117 if (isThenable(handlerResult)) { | 117 if (isThenable(handlerResult)) { |
| 118 var resolveReject = callResolveRejectFunc(handlerResult.then); | 118 var resolveReject = callResolveRejectFunc(handlerResult.then); |
| 119 handlerResult = resolveReject.result; | 119 handlerResult = resolveReject.result; |
| 120 promiseResolved = resolveReject.resolved; | 120 promiseResolved = resolveReject.resolved; |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (promiseResolved) { | 123 if (promiseResolved) { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 /** | 187 /** |
| 188 * Sets up the test to expect a Chrome Local Storage call. | 188 * Sets up the test to expect a Chrome Local Storage call. |
| 189 * @param {Object} fixture Mock JS Test Object. | 189 * @param {Object} fixture Mock JS Test Object. |
| 190 * @param {Object} defaultObject Storage request default object. | 190 * @param {Object} defaultObject Storage request default object. |
| 191 * @param {Object} result Storage result. | 191 * @param {Object} result Storage result. |
| 192 * @param {boolean=} opt_AllowRejection Allow Promise Rejection | 192 * @param {boolean=} opt_AllowRejection Allow Promise Rejection |
| 193 */ | 193 */ |
| 194 function expectChromeLocalStorageGet( | 194 function expectChromeLocalStorageGet( |
| 195 fixture, defaultObject, result, opt_AllowRejection) { | 195 fixture, defaultObject, result, opt_AllowRejection) { |
| 196 if (opt_AllowRejection === undefined) { | 196 if (opt_AllowRejection === undefined) { |
| 197 fixture.mockApis.expects(once()). | 197 fixture.mockApis.expects(once()) |
| 198 fillFromChromeLocalStorage(eqJSON(defaultObject)). | 198 .fillFromChromeLocalStorage(eqJSON(defaultObject)) |
| 199 will(returnValue(Promise.resolve(result))); | 199 .will(returnValue(Promise.resolve(result))); |
| 200 } else { | 200 } else { |
| 201 fixture.mockApis.expects(once()). | 201 fixture.mockApis.expects(once()) |
| 202 fillFromChromeLocalStorage(eqJSON(defaultObject), opt_AllowRejection). | 202 .fillFromChromeLocalStorage(eqJSON(defaultObject), opt_AllowRejection) |
| 203 will(returnValue(Promise.resolve(result))); | 203 .will(returnValue(Promise.resolve(result))); |
| 204 } | 204 } |
| 205 } | 205 } |
| OLD | NEW |