Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: chrome/browser/resources/google_now/common_test_util.js

Issue 202293003: Server Request Tests and Mock Promise Enhancements (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CR Feedback Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/google_now/background_unittest.gtestjs ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 71
72 return mockEventContainer; 72 return mockEventContainer;
73 } 73 }
74 74
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 *
82 * This functionality and logic originates from ECMAScript 6's spec of promises.
81 */ 83 */
82 var Promise = function() { 84 var Promise = function() {
83 function PromisePrototypeObject(asyncTask) { 85 function PromisePrototypeObject(asyncTask) {
84 var result; 86 function isThenable(value) {
85 var resolved = false; 87 return (typeof value === 'object') && isCallable(value.then);
86 asyncTask( 88 }
87 function(asyncResult) {
88 result = asyncResult;
89 resolved = true;
90 },
91 function(asyncFailureResult) {
92 result = asyncFailureResult;
93 resolved = false;
94 });
95 89
96 function then(callback) { 90 function isCallable(value) {
97 if (resolved) { 91 return typeof value === 'function';
98 callback.call(null, result); 92 }
93
94 function callResolveRejectFunc(func) {
95 var funcResult;
96 var funcResolved = false;
97 func(
98 function(resolveResult) {
99 funcResult = resolveResult;
100 funcResolved = true;
101 },
102 function(rejectResult) {
103 funcResult = rejectResult;
104 funcResolved = false;
105 });
106 return { result: funcResult, resolved: funcResolved };
107 }
108
109 function then(onResolve, onReject) {
110 var resolutionHandler =
111 isCallable(onResolve) ? onResolve : function() { return result; };
112 var rejectionHandler =
113 isCallable(onReject) ? onReject : function() { return result; };
114 var handlerResult =
115 resolved ? resolutionHandler(result) : rejectionHandler(result);
116 var promiseResolved = resolved;
117 if (isThenable(handlerResult)) {
118 var resolveReject = callResolveRejectFunc(handlerResult.then);
119 handlerResult = resolveReject.result;
120 promiseResolved = resolveReject.resolved;
99 } 121 }
100 return this; 122
123 if (promiseResolved) {
124 return Promise.resolve(handlerResult);
125 } else {
126 return Promise.reject(handlerResult);
127 }
101 } 128 }
102 129
103 // Promises use the function name "catch" to call back error handlers. 130 // 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 131 // We can't use "catch" since function or variable names cannot use the word
105 // "catch". 132 // "catch".
106 function catchFunc(callback) { 133 function catchFunc(onRejected) {
107 if (!resolved) { 134 return this.then(undefined, onRejected);
108 callback.call(null, result); 135 }
109 } 136
110 return this; 137 var resolveReject = callResolveRejectFunc(asyncTask);
138 var result = resolveReject.result;
139 var resolved = resolveReject.resolved;
140
141 if (isThenable(result)) {
142 var thenResolveReject = callResolveRejectFunc(result.then);
143 result = thenResolveReject.result;
144 resolved = thenResolveReject.resolved;
111 } 145 }
112 146
113 return {then: then, catch: catchFunc, isPromise: true}; 147 return {then: then, catch: catchFunc, isPromise: true};
114 } 148 }
115 149
116 function all(arrayOfPromises) { 150 function all(arrayOfPromises) {
117 var results = []; 151 var results = [];
118 for (i = 0; i < arrayOfPromises.length; i++) { 152 for (i = 0; i < arrayOfPromises.length; i++) {
119 if (arrayOfPromises[i].isPromise) { 153 if (arrayOfPromises[i].isPromise) {
120 arrayOfPromises[i].then(function(result) { 154 arrayOfPromises[i].then(function(result) {
121 results[i] = result; 155 results[i] = result;
122 }); 156 });
123 } else { 157 } else {
124 results[i] = arrayOfPromises[i]; 158 results[i] = arrayOfPromises[i];
125 } 159 }
126 } 160 }
127 var promise = new PromisePrototypeObject(function(resolve) { 161 var promise = new PromisePrototypeObject(function(resolve) {
128 resolve(results); 162 resolve(results);
129 }); 163 });
130 return promise; 164 return promise;
131 } 165 }
132 166
133 function resolve(value) { 167 function resolve(value) {
134 var promise = new PromisePrototypeObject(function(resolve) { 168 var promise = new PromisePrototypeObject(function(resolve) {
135 resolve(value); 169 resolve(value);
136 }); 170 });
137 return promise; 171 return promise;
138 } 172 }
139 173
174 function reject(value) {
175 var promise = new PromisePrototypeObject(function(resolve, reject) {
176 reject(value);
177 });
178 return promise;
179 }
180
140 PromisePrototypeObject.all = all; 181 PromisePrototypeObject.all = all;
141 PromisePrototypeObject.resolve = resolve; 182 PromisePrototypeObject.resolve = resolve;
183 PromisePrototypeObject.reject = reject;
142 return PromisePrototypeObject; 184 return PromisePrototypeObject;
143 }(); 185 }();
144 186
145
146 /** 187 /**
147 * Sets up the test to expect a Chrome Local Storage call. 188 * Sets up the test to expect a Chrome Local Storage call.
148 * @param {Object} fixture Mock JS Test Object. 189 * @param {Object} fixture Mock JS Test Object.
149 * @param {Object} defaultObject Storage request default object. 190 * @param {Object} defaultObject Storage request default object.
150 * @param {Object} result Storage result. 191 * @param {Object} result Storage result.
151 * @param {boolean=} opt_AllowRejection Allow Promise Rejection 192 * @param {boolean=} opt_AllowRejection Allow Promise Rejection
152 */ 193 */
153 function expectChromeLocalStorageGet( 194 function expectChromeLocalStorageGet(
154 fixture, defaultObject, result, opt_AllowRejection) { 195 fixture, defaultObject, result, opt_AllowRejection) {
155 if (opt_AllowRejection === undefined) { 196 if (opt_AllowRejection === undefined) {
156 fixture.mockApis.expects(once()). 197 fixture.mockApis.expects(once()).
157 fillFromChromeLocalStorage(eqJSON(defaultObject)). 198 fillFromChromeLocalStorage(eqJSON(defaultObject)).
158 will(returnValue(Promise.resolve(result))); 199 will(returnValue(Promise.resolve(result)));
159 } else { 200 } else {
160 fixture.mockApis.expects(once()). 201 fixture.mockApis.expects(once()).
161 fillFromChromeLocalStorage(eqJSON(defaultObject), opt_AllowRejection). 202 fillFromChromeLocalStorage(eqJSON(defaultObject), opt_AllowRejection).
162 will(returnValue(Promise.resolve(result))); 203 will(returnValue(Promise.resolve(result)));
163 } 204 }
164 } 205 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/google_now/background_unittest.gtestjs ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698