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

Side by Side Diff: chrome/test/data/extensions/api_test/messaging/connect/test.js

Issue 1108203004: Test that chrome.runtime.onMessage isn't fired for chrome.runtime.sendMessage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 var listenOnce = chrome.test.listenOnce; 5 var listenOnce = chrome.test.listenOnce;
6 var listenForever = chrome.test.listenForever; 6 var listenForever = chrome.test.listenForever;
7 7
8 JSON.parse = function() { 8 JSON.parse = function() {
9 return "JSON.parse clobbered by extension."; 9 return "JSON.parse clobbered by extension.";
10 }; 10 };
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 try { 283 try {
284 chrome.extension.onRequest.addListener(function() {}); 284 chrome.extension.onRequest.addListener(function() {});
285 } catch(e) { 285 } catch(e) {
286 error = e; 286 error = e;
287 } 287 }
288 chrome.test.assertTrue(error != undefined); 288 chrome.test.assertTrue(error != undefined);
289 289
290 chrome.test.succeed(); 290 chrome.test.succeed();
291 }, 291 },
292 292
293 // Tests that chrome.runtime.sendMessage is *not* delivered to the current
294 // context, consistent behavior with chrome.runtime.connect() and web APIs
295 // like localStorage changed listeners.
296 // Regression test for http://crbug.com/479951.
297 function sendMessageToCurrentContextFails() {
298 var stopFailing = failWhileListening(chrome.runtime.onMessage);
Devlin 2015/04/28 22:24:40 nit: stopFailing is kinda a funny name...but I got
not at google - send to devlin 2015/04/28 22:46:31 I think of it like a function. function stopFaili
299 chrome.runtime.sendMessage('ping', chrome.test.callbackFail(
300 'Could not establish connection. Receiving end does not exist.',
301 function() {
302 stopFailing();
303 chrome.test.succeed();
304 }
305 ));
306 },
307
308 // Like sendMessageToCurrentContextFails, but with the sendMessage call not
309 // given a callback. This requires a more creative test setup because there
310 // is no callback to signal when it's supposed to have been done.
311 // Regression test for http://crbug.com/479951.
312 function sendMessageToCurrentTextWithoutCallbackFails() {
313 // Make the iframe - in a different context - watch for the message
314 // event. It *should* get it, while the current context's one doesn't.
315 var iframe = document.createElement('iframe');
316 iframe.src = chrome.runtime.getURL('blank_iframe.html');
317 document.body.appendChild(iframe);
318
319 var stopFailing = failWhileListening(chrome.runtime.onMessage);
320 chrome.test.listenOnce(
321 iframe.contentWindow.chrome.runtime.onMessage,
322 function(msg, sender) {
323 chrome.test.assertEq('ping', msg);
324 chrome.test.assertEq(chrome.runtime.id, sender.id);
325 chrome.test.assertEq(location.href, sender.url);
326 setTimeout(function() {
327 stopFailing();
328 chrome.test.succeed();
329 }, 0);
330 }
331 );
332
333 chrome.runtime.sendMessage('ping');
334 },
293 ]); 335 ]);
294 }); 336 });
295 337
296 function connectToTabWithFrameId(frameId, expectedMessages) { 338 function connectToTabWithFrameId(frameId, expectedMessages) {
297 var port = chrome.tabs.connect(testTab.id, { 339 var port = chrome.tabs.connect(testTab.id, {
298 frameId: frameId 340 frameId: frameId
299 }); 341 });
300 var messages = []; 342 var messages = [];
301 var isDone = false; 343 var isDone = false;
302 listenForever(port.onMessage, function(message) { 344 listenForever(port.onMessage, function(message) {
303 if (isDone) // Should not get any messages after completing the test. 345 if (isDone) // Should not get any messages after completing the test.
304 chrome.test.fail( 346 chrome.test.fail(
305 'Unexpected message from port to frame ' + frameId + ': ' + message); 347 'Unexpected message from port to frame ' + frameId + ': ' + message);
306 348
307 messages.push(message); 349 messages.push(message);
308 isDone = messages.length == expectedMessages.length; 350 isDone = messages.length == expectedMessages.length;
309 if (isDone) { 351 if (isDone) {
310 chrome.test.assertEq(expectedMessages.sort(), messages.sort()); 352 chrome.test.assertEq(expectedMessages.sort(), messages.sort());
311 chrome.test.succeed(); 353 chrome.test.succeed();
312 } 354 }
313 }); 355 });
314 listenOnce(port.onDisconnect, function() { 356 listenOnce(port.onDisconnect, function() {
315 if (!isDone) // The event should never be triggered when we expect messages. 357 if (!isDone) // The event should never be triggered when we expect messages.
316 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId); 358 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId);
317 }); 359 });
318 port.postMessage({testSendMessageToFrame: true}); 360 port.postMessage({testSendMessageToFrame: true});
319 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId); 361 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId);
320 } 362 }
363
364 // Listens to |event| and returns a callback to run to stop listening. While
365 // listening, if |event| is fired, calls chrome.test.fail().
366 function failWhileListening(event, doneListening) {
367 var failListener = function() {
368 chrome.test.fail('Event listener ran, but it shouldn\'t have. ' +
369 'It\'s possible that may be triggered flakily, but this ' +
370 'really is a real failure, not flaky sadness. Promise!');
371 };
372 var release = chrome.test.callbackAdded();
373 event.addListener(failListener);
374 return function() {
375 event.removeListener(failListener);
376 release();
377 };
378 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698