OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // This port is opened by a content script in an interstitial. This is used by |
| 6 // the tests to drive the tests. |
| 7 var testPort; |
| 8 var testPortPromise = new Promise(function(resolve) { |
| 9 chrome.test.listenOnce(chrome.runtime.onConnect, function(port) { |
| 10 testPort = port; |
| 11 resolve(); |
| 12 }); |
| 13 }); |
| 14 |
| 15 // Start a test and wait until the set-up (not necessarily the test!) is done. |
| 16 function sendToInterstitialAndWait(testName) { |
| 17 testPort.postMessage(testName); |
| 18 chrome.test.listenOnce(testPort.onMessage, function(msg) { |
| 19 chrome.test.assertEq(testName, msg); |
| 20 }); |
| 21 } |
| 22 |
| 23 function assertIsPortFromInterstitial(port, expectedName) { |
| 24 chrome.test.assertEq(expectedName, port.name); |
| 25 chrome.test.assertEq(undefined, port.sender.tab); |
| 26 chrome.test.assertEq(undefined, port.sender.frameId); |
| 27 chrome.test.assertTrue(port.sender.url.startsWith('data:')); |
| 28 } |
| 29 |
| 30 var httpsTabId; |
| 31 var httpsTabIdPromise = new Promise(function(resolve) { |
| 32 // The test runner will open a https page after loading the extension. |
| 33 chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) { |
| 34 if (tab.url.startsWith('https://')) { |
| 35 chrome.tabs.onUpdated.removeListener(listener); |
| 36 httpsTabId = tabId; |
| 37 resolve(); |
| 38 } |
| 39 }); |
| 40 }); |
| 41 |
| 42 chrome.test.runTests([ |
| 43 function waitForHttpsPage() { |
| 44 httpsTabIdPromise.then(chrome.test.callbackPass(function() { |
| 45 chrome.tabs.query({ |
| 46 url: 'https://*/*' |
| 47 }, chrome.test.callbackPass(function(tabs) { |
| 48 // Sanity check. There should only be one https tab. |
| 49 chrome.test.assertEq(1, tabs.length); |
| 50 })); |
| 51 })); |
| 52 }, |
| 53 |
| 54 // All following tests rely on testPort, so ensure that it exists! |
| 55 function waitForPortFromInterstitial() { |
| 56 testPortPromise.then(chrome.test.callbackPass(function() { |
| 57 assertIsPortFromInterstitial(testPort, 'port from interstitial'); |
| 58 })); |
| 59 }, |
| 60 |
| 61 // Tests whether ping-ponging with sendMessage works. |
| 62 function testSendMessage() { |
| 63 chrome.test.listenOnce(chrome.runtime.onMessage, |
| 64 function(msg, sender, sendResponse) { |
| 65 chrome.test.assertEq('First from interstitial', msg); |
| 66 |
| 67 var kResponse = 'hello me!'; |
| 68 chrome.test.listenOnce(chrome.runtime.onMessage, function(msg) { |
| 69 chrome.test.assertEq('interstitial received: ' + kResponse, msg); |
| 70 }); |
| 71 sendResponse(kResponse); |
| 72 }); |
| 73 sendToInterstitialAndWait('testSendMessage'); |
| 74 }, |
| 75 |
| 76 // Tests whether the onDisconnect event is fired in the interstitial page. |
| 77 function testDisconnectByBackground() { |
| 78 chrome.test.listenOnce(chrome.runtime.onConnect, |
| 79 function(port) { |
| 80 assertIsPortFromInterstitial(port, 'disconnect by background'); |
| 81 port.disconnect(); |
| 82 }); |
| 83 sendToInterstitialAndWait('testDisconnectByBackground'); |
| 84 }, |
| 85 |
| 86 // Tests whether the onDisconnect event is fired when the port is closed from |
| 87 // the content script in the interstitial page. |
| 88 function testDisconnectByInterstitial() { |
| 89 chrome.test.listenOnce(chrome.runtime.onConnect, |
| 90 function(port) { |
| 91 assertIsPortFromInterstitial(port, 'disconnect by interstitial'); |
| 92 chrome.test.listenOnce(port.onDisconnect, function() { |
| 93 chrome.test.assertNoLastError(); |
| 94 }); |
| 95 }); |
| 96 sendToInterstitialAndWait('testDisconnectByInterstitial'); |
| 97 }, |
| 98 |
| 99 // Closing the interstitial should cause the ports to disconnect. |
| 100 function testDisconnectByClosingInterstitial() { |
| 101 chrome.test.listenOnce(testPort.onDisconnect, function() { |
| 102 chrome.test.assertNoLastError(); |
| 103 testPort = null; |
| 104 }); |
| 105 // Close the interstitial. Should trigger onDisconnect. |
| 106 chrome.tabs.update(httpsTabId, { |
| 107 url: 'about:blank' |
| 108 }); |
| 109 }, |
| 110 ]); |
OLD | NEW |