OLD | NEW |
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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 // This shouldn't ever be called, so it's a bit pointless. | 253 // This shouldn't ever be called, so it's a bit pointless. |
254 chrome.test.fail(); | 254 chrome.test.fail(); |
255 }); | 255 }); |
256 // Didn't crash. | 256 // Didn't crash. |
257 chrome.test.succeed(); | 257 chrome.test.succeed(); |
258 } catch (e) { | 258 } catch (e) { |
259 chrome.test.fail(e.stack); | 259 chrome.test.fail(e.stack); |
260 } | 260 } |
261 }, | 261 }, |
262 | 262 |
| 263 // Tests that reloading a child frame disconnects the port if it was the |
| 264 // only recipient of the port (i.e. no onConnect in main frame). |
| 265 function connectChildFrameAndNavigate() { |
| 266 listenOnce(chrome.runtime.onMessage, function(msg) { |
| 267 chrome.test.assertEq('testConnectChildFrameAndNavigateSetupDone', msg); |
| 268 // Now we have set up a frame and ensured that there is no onConnect |
| 269 // handler in the main frame. Run the actual test: |
| 270 var port = chrome.tabs.connect(testTab.id); |
| 271 listenOnce(port.onDisconnect, function() {}); |
| 272 port.postMessage({testConnectChildFrameAndNavigate: true}); |
| 273 }); |
| 274 chrome.tabs.connect(testTab.id) |
| 275 .postMessage({testConnectChildFrameAndNavigateSetup: true}); |
| 276 }, |
| 277 |
263 // Tests that we get the disconnect event when the tab context closes. | 278 // Tests that we get the disconnect event when the tab context closes. |
264 function disconnectOnClose() { | 279 function disconnectOnClose() { |
265 var port = chrome.tabs.connect(testTab.id); | 280 var port = chrome.tabs.connect(testTab.id); |
266 port.postMessage({testDisconnectOnClose: true}); | 281 port.postMessage({testDisconnectOnClose: true}); |
267 listenOnce(port.onDisconnect, function() { | 282 listenOnce(port.onDisconnect, function() { |
268 testTab = null; // the tab is about:blank now. | 283 testTab = null; // the tab is about:blank now. |
269 }); | 284 }); |
270 }, | 285 }, |
271 | 286 |
272 // Tests that the sendRequest API is disabled. | 287 // Tests that the sendRequest API is disabled. |
(...skipping 20 matching lines...) Expand all Loading... |
293 // Tests that chrome.runtime.sendMessage is *not* delivered to the current | 308 // Tests that chrome.runtime.sendMessage is *not* delivered to the current |
294 // context, consistent behavior with chrome.runtime.connect() and web APIs | 309 // context, consistent behavior with chrome.runtime.connect() and web APIs |
295 // like localStorage changed listeners. | 310 // like localStorage changed listeners. |
296 // Regression test for http://crbug.com/479951. | 311 // Regression test for http://crbug.com/479951. |
297 function sendMessageToCurrentContextFails() { | 312 function sendMessageToCurrentContextFails() { |
298 var stopFailing = failWhileListening(chrome.runtime.onMessage); | 313 var stopFailing = failWhileListening(chrome.runtime.onMessage); |
299 chrome.runtime.sendMessage('ping', chrome.test.callbackFail( | 314 chrome.runtime.sendMessage('ping', chrome.test.callbackFail( |
300 'Could not establish connection. Receiving end does not exist.', | 315 'Could not establish connection. Receiving end does not exist.', |
301 function() { | 316 function() { |
302 stopFailing(); | 317 stopFailing(); |
303 chrome.test.succeed(); | |
304 } | 318 } |
305 )); | 319 )); |
306 }, | 320 }, |
307 | 321 |
308 // Like sendMessageToCurrentContextFails, but with the sendMessage call not | 322 // Like sendMessageToCurrentContextFails, but with the sendMessage call not |
309 // given a callback. This requires a more creative test setup because there | 323 // 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. | 324 // is no callback to signal when it's supposed to have been done. |
311 // Regression test for http://crbug.com/479951. | 325 // Regression test for http://crbug.com/479951. |
312 // | 326 // |
313 // NOTE(kalman): This test is correct. However, the patch which fixes it | 327 // NOTE(kalman): This test is correct. However, the patch which fixes it |
314 // (see bug) was reverted, and I don't plan on resubmitting, so instead | 328 // (see bug) was reverted, and I don't plan on resubmitting, so instead |
315 // I'll comment out this test, and leave it here for the record. | 329 // I'll comment out this test, and leave it here for the record. |
316 // | 330 // |
317 // function sendMessageToCurrentTextWithoutCallbackFails() { | 331 // function sendMessageToCurrentTextWithoutCallbackFails() { |
318 // // Make the iframe - in a different context - watch for the message | 332 // // Make the iframe - in a different context - watch for the message |
319 // // event. It *should* get it, while the current context's one doesn't. | 333 // // event. It *should* get it, while the current context's one doesn't. |
320 // var iframe = document.createElement('iframe'); | 334 // var iframe = document.createElement('iframe'); |
321 // iframe.src = chrome.runtime.getURL('blank_iframe.html'); | 335 // iframe.src = chrome.runtime.getURL('blank_iframe.html'); |
322 // document.body.appendChild(iframe); | 336 // document.body.appendChild(iframe); |
323 | 337 |
324 // var stopFailing = failWhileListening(chrome.runtime.onMessage); | 338 // var stopFailing = failWhileListening(chrome.runtime.onMessage); |
325 // chrome.test.listenOnce( | 339 // chrome.test.listenOnce( |
326 // iframe.contentWindow.chrome.runtime.onMessage, | 340 // iframe.contentWindow.chrome.runtime.onMessage, |
327 // function(msg, sender) { | 341 // function(msg, sender) { |
328 // chrome.test.assertEq('ping', msg); | 342 // chrome.test.assertEq('ping', msg); |
329 // chrome.test.assertEq(chrome.runtime.id, sender.id); | 343 // chrome.test.assertEq(chrome.runtime.id, sender.id); |
330 // chrome.test.assertEq(location.href, sender.url); | 344 // chrome.test.assertEq(location.href, sender.url); |
331 // setTimeout(function() { | 345 // setTimeout(function() { |
332 // stopFailing(); | 346 // stopFailing(); |
333 // chrome.test.succeed(); | |
334 // }, 0); | 347 // }, 0); |
335 // } | 348 // } |
336 // ); | 349 // ); |
337 // | 350 // |
338 // chrome.runtime.sendMessage('ping'); | 351 // chrome.runtime.sendMessage('ping'); |
339 // }, | 352 // }, |
340 | |
341 ]); | 353 ]); |
342 }); | 354 }); |
343 | 355 |
344 function connectToTabWithFrameId(frameId, expectedMessages) { | 356 function connectToTabWithFrameId(frameId, expectedMessages) { |
345 var port = chrome.tabs.connect(testTab.id, { | 357 var port = chrome.tabs.connect(testTab.id, { |
346 frameId: frameId | 358 frameId: frameId |
347 }); | 359 }); |
348 var messages = []; | 360 var messages = []; |
349 var isDone = false; | 361 var isDone = false; |
350 listenForever(port.onMessage, function(message) { | 362 port.onMessage.addListener(function(message) { |
351 if (isDone) // Should not get any messages after completing the test. | 363 if (isDone) { // Should not get any messages after completing the test. |
352 chrome.test.fail( | 364 chrome.test.fail( |
353 'Unexpected message from port to frame ' + frameId + ': ' + message); | 365 'Unexpected message from port to frame ' + frameId + ': ' + message); |
| 366 return; |
| 367 } |
354 | 368 |
355 messages.push(message); | 369 messages.push(message); |
356 isDone = messages.length == expectedMessages.length; | 370 isDone = messages.length == expectedMessages.length; |
357 if (isDone) { | 371 if (isDone) { |
358 chrome.test.assertEq(expectedMessages.sort(), messages.sort()); | 372 chrome.test.assertEq(expectedMessages.sort(), messages.sort()); |
359 chrome.test.succeed(); | 373 chrome.test.succeed(); |
360 } | 374 } |
361 }); | 375 }); |
362 listenOnce(port.onDisconnect, function() { | 376 port.onDisconnect.addListener(function() { |
363 if (!isDone) // The event should never be triggered when we expect messages. | 377 if (!isDone) // The event should never be triggered when we expect messages. |
364 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId); | 378 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId); |
365 }); | 379 }); |
366 port.postMessage({testSendMessageToFrame: true}); | 380 port.postMessage({testSendMessageToFrame: true}); |
367 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId); | 381 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId); |
368 } | 382 } |
369 | 383 |
370 // Listens to |event| and returns a callback to run to stop listening. While | 384 // Listens to |event| and returns a callback to run to stop listening. While |
371 // listening, if |event| is fired, calls chrome.test.fail(). | 385 // listening, if |event| is fired, calls chrome.test.fail(). |
372 function failWhileListening(event, doneListening) { | 386 function failWhileListening(event, doneListening) { |
373 var failListener = function() { | 387 var failListener = function() { |
374 chrome.test.fail('Event listener ran, but it shouldn\'t have. ' + | 388 chrome.test.fail('Event listener ran, but it shouldn\'t have. ' + |
375 'It\'s possible that may be triggered flakily, but this ' + | 389 'It\'s possible that may be triggered flakily, but this ' + |
376 'really is a real failure, not flaky sadness. Promise!'); | 390 'really is a real failure, not flaky sadness. Promise!'); |
377 }; | 391 }; |
378 var release = chrome.test.callbackAdded(); | 392 var release = chrome.test.callbackAdded(); |
379 event.addListener(failListener); | 393 event.addListener(failListener); |
380 return function() { | 394 return function() { |
381 event.removeListener(failListener); | 395 event.removeListener(failListener); |
382 release(); | 396 release(); |
383 }; | 397 }; |
384 } | 398 } |
OLD | NEW |