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

Side by Side Diff: chrome/test/data/webui/cr_test.html

Issue 1622663002: WebUI: Replace cr.sendWithCallback with cr.sendWithPromise. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits. Created 4 years, 11 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
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <body> 3 <body>
4 4
5 <script> 5 <script>
6 6
7 var EventTarget; 7 var EventTarget;
8 8
9 function setUp() { 9 function setUp() {
10 EventTarget = cr.EventTarget; 10 EventTarget = cr.EventTarget;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 236
237 var y = Foo.getInstance(); 237 var y = Foo.getInstance();
238 assertEquals(x, y, 'Should return the same object'); 238 assertEquals(x, y, 'Should return the same object');
239 239
240 delete Foo.instance_; 240 delete Foo.instance_;
241 241
242 var z = Foo.getInstance(); 242 var z = Foo.getInstance();
243 assertEquals('object', typeof z, 'Should work after clearing for testing'); 243 assertEquals('object', typeof z, 'Should work after clearing for testing');
244 assertNotEqual(null, z, 'Created object should not be null'); 244 assertNotEqual(null, z, 'Created object should not be null');
245 245
246 assertNotEqual(x, z, 246 assertNotEqual(x, z,
247 'Should return a different object after clearing for testing'); 247 'Should return a different object after clearing for testing');
248 } 248 }
249 249
250 function testDefineWithGetter() { 250 function testDefineWithGetter() {
251 var v = 0; 251 var v = 0;
252 cr.define('foo', function() { 252 cr.define('foo', function() {
253 return { 253 return {
254 get v() { 254 get v() {
255 return v; 255 return v;
256 } 256 }
257 } 257 }
258 }); 258 });
259 259
260 assertEquals(0, foo.v); 260 assertEquals(0, foo.v);
261 261
262 v = 1; 262 v = 1;
263 assertEquals(1, foo.v); 263 assertEquals(1, foo.v);
264 } 264 }
265 265
266 /** 266 /**
267 * Executes a function given a potentially namespaced function name, e.g., 267 * Tests that an event fired by a WebUI handler is sent to all listeners.
268 * cr.webUIListenerCallback.
269 * @param {string} functionName The name of the function, including any
270 * namespaces, to execute.
271 */
272 function executeFunctionByName(functionName) {
273 var args = Array.prototype.slice.call(arguments, 1);
274 var func = (0, eval)(functionName);
275 func.apply(undefined, args);
276 }
277
278 /**
279 * Tests that cr.sendWithCallback correctly handles the case where the JS sends
280 * no arguments to the WebUI handler.
281 */
282 function testSendWithCallback_PassesJSArgs() {
283 var testMethodName = 'getFullscreenState';
284
285 // Mock out chrome.send to emulate a WebUI handler calling back with the
286 // result of a getFullscreenState call.
287 window.chrome = {};
288 window.chrome.send = function(method, args) {
289 assertEquals(testMethodName, method);
290 var callbackName = args[0];
291 var id = args[1];
292 executeFunctionByName(callbackName, id, /* fullscreen */ true);
293 };
294
295 var callbackResponse;
296 cr.sendWithCallback(testMethodName, undefined, function(fullscreen) {
297 callbackResponse = fullscreen;
298 });
299
300 assertTrue(callbackResponse);
301 }
302
303 /**
304 * Tests that cr.sendWithCallback passes arguments from JS to the WebUI
305 * handler.
306 */
307 function testSendWithCallback_PassesJSArgs() {
308 var testMethodName = 'getSquareOfX';
309
310 // Mock out chrome.send to emulate a WebUI handler calling back with the
311 // result of a getSquareOfX call.
312 window.chrome = {};
313 window.chrome.send = function(method, args) {
314 assertEquals(testMethodName, method);
315 var callbackName = args[0];
316 var id = args[1];
317 var x = args[2];
318 executeFunctionByName(callbackName, id, x * x);
319 };
320
321 var callbackResponse;
322 cr.sendWithCallback(testMethodName, [5], function(square) {
323 callbackResponse = square;
324 });
325
326 assertEquals(25, callbackResponse);
327 }
328
329 /**
330 * Tests that an event fired by a WebUI handler is sent to all listeners.
331 */ 268 */
332 function testAddWebUIListener() { 269 function testAddWebUIListener() {
333 var responses = new Map(); 270 var responses = new Map();
334 cr.addWebUIListener('fullscreen-enabled', function(enabled) { 271 cr.addWebUIListener('fullscreen-enabled', function(enabled) {
335 responses.set('first', enabled); 272 responses.set('first', enabled);
336 }); 273 });
337 cr.addWebUIListener('fullscreen-enabled', function(enabled) { 274 cr.addWebUIListener('fullscreen-enabled', function(enabled) {
338 responses.set('second', enabled); 275 responses.set('second', enabled);
339 }); 276 });
340 277
341 executeFunctionByName( 278 cr.webUIListenerCallback('fullscreen-enabled', true);
342 'cr.webUIListenerCallback', 'fullscreen-enabled', true);
343
344 assertTrue(responses.get('first')); 279 assertTrue(responses.get('first'));
345 assertTrue(responses.get('second')); 280 assertTrue(responses.get('second'));
346 } 281 }
347 282
348 </script> 283 </script>
349 284
350 </body> 285 </body>
351 </html> 286 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698