| OLD | NEW |
| 1 <script> | 1 <!-- |
| 2 JSON.parse = function() { | 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this |
| 3 return "JSON.parse clobbered by extension."; | 3 * source code is governed by a BSD-style license that can be found in the |
| 4 }; | 4 * LICENSE file. |
| 5 | 5 --> |
| 6 JSON.stringify = function() { | 6 <script src="test.js"></script> |
| 7 return "JSON.stringify clobbered by extension."; | |
| 8 }; | |
| 9 | |
| 10 Array.prototype.toJSON = function() { | |
| 11 return "Array.prototype.toJSON clobbered by extension."; | |
| 12 }; | |
| 13 | |
| 14 Object.prototype.toJSON = function() { | |
| 15 return "Object.prototype.toJSON clobbered by extension."; | |
| 16 }; | |
| 17 | |
| 18 // Keep track of the tab that we're running tests in, for simplicity. | |
| 19 var testTab = null; | |
| 20 | |
| 21 chrome.test.getConfig(function(config) { | |
| 22 chrome.test.runTests([ | |
| 23 function setupTestTab() { | |
| 24 chrome.test.log("Creating tab..."); | |
| 25 chrome.tabs.create({ | |
| 26 url: "http://localhost:PORT/files/extensions/test_file.html" | |
| 27 .replace(/PORT/, config.testServer.port) | |
| 28 }, function(tab) { | |
| 29 chrome.tabs.onUpdated.addListener(function listener(tabid, info) { | |
| 30 if (tab.id == tabid && info.status == 'complete') { | |
| 31 chrome.test.log("Created tab: " + tab.url); | |
| 32 chrome.tabs.onUpdated.removeListener(listener); | |
| 33 testTab = tab; | |
| 34 chrome.test.succeed(); | |
| 35 } | |
| 36 }); | |
| 37 }); | |
| 38 }, | |
| 39 | |
| 40 // Tests that postMessage to the tab and its response works. | |
| 41 function postMessage() { | |
| 42 var port = chrome.tabs.connect(testTab.id); | |
| 43 port.postMessage({testPostMessage: true}); | |
| 44 port.onMessage.addListener(function(msg) { | |
| 45 port.disconnect(); | |
| 46 chrome.test.succeed(); | |
| 47 }); | |
| 48 }, | |
| 49 | |
| 50 // Tests that port name is sent & received correctly. | |
| 51 function portName() { | |
| 52 var portName = "lemonjello"; | |
| 53 var port = chrome.tabs.connect(testTab.id, {name: portName}); | |
| 54 port.postMessage({testPortName: true}); | |
| 55 port.onMessage.addListener(function(msg) { | |
| 56 chrome.test.assertEq(msg.portName, portName); | |
| 57 port.disconnect(); | |
| 58 chrome.test.succeed(); | |
| 59 }); | |
| 60 }, | |
| 61 | |
| 62 // Tests that postMessage from the tab and its response works. | |
| 63 function postMessageFromTab() { | |
| 64 chrome.extension.onConnect.addListener(function(port) { | |
| 65 port.onMessage.addListener(function(msg) { | |
| 66 chrome.test.assertTrue(msg.testPostMessageFromTab); | |
| 67 port.postMessage({success: true, portName: port.name}); | |
| 68 chrome.test.log("postMessageFromTab: got message from tab"); | |
| 69 }); | |
| 70 }); | |
| 71 | |
| 72 var port = chrome.tabs.connect(testTab.id); | |
| 73 port.postMessage({testPostMessageFromTab: true}); | |
| 74 chrome.test.log("postMessageFromTab: sent first message to tab"); | |
| 75 port.onMessage.addListener(function(msg) { | |
| 76 port.disconnect(); | |
| 77 chrome.test.succeed(); | |
| 78 }); | |
| 79 }, | |
| 80 | |
| 81 // Tests receiving a request from a content script and responding. | |
| 82 function sendRequestFromTab() { | |
| 83 var doneListening = chrome.test.listenForever( | |
| 84 chrome.extension.onRequest, | |
| 85 function(request, sender, sendResponse) { | |
| 86 chrome.test.assertTrue("url" in sender.tab, "no tab available."); | |
| 87 chrome.test.assertEq(sender.id, location.host); | |
| 88 if (request.step == 1) { | |
| 89 // Step 1: Page should send another request for step 2. | |
| 90 chrome.test.log("sendRequestFromTab: got step 1"); | |
| 91 sendResponse({nextStep: true}); | |
| 92 } else { | |
| 93 // Step 2. | |
| 94 chrome.test.assertEq(request.step, 2); | |
| 95 sendResponse(); | |
| 96 doneListening(); | |
| 97 } | |
| 98 }); | |
| 99 | |
| 100 var port = chrome.tabs.connect(testTab.id); | |
| 101 port.postMessage({testSendRequestFromTab: true}); | |
| 102 port.disconnect(); | |
| 103 chrome.test.log("sendRequestFromTab: sent first message to tab"); | |
| 104 }, | |
| 105 | |
| 106 // Tests error handling when sending a request from a content script to an | |
| 107 // invalid extension. | |
| 108 function sendRequestFromTabError() { | |
| 109 chrome.test.listenOnce( | |
| 110 chrome.extension.onRequest, | |
| 111 function(request, sender, sendResponse) { | |
| 112 if (!request.success) | |
| 113 chrome.test.fail(); | |
| 114 } | |
| 115 ); | |
| 116 | |
| 117 var port = chrome.tabs.connect(testTab.id); | |
| 118 port.postMessage({testSendRequestFromTabError: true}); | |
| 119 port.disconnect(); | |
| 120 chrome.test.log("testSendRequestFromTabError: send 1st message to tab"); | |
| 121 }, | |
| 122 | |
| 123 // Tests error handling when connecting to an invalid extension from a | |
| 124 // content script. | |
| 125 function connectFromTabError() { | |
| 126 chrome.test.listenOnce( | |
| 127 chrome.extension.onRequest, | |
| 128 function(request, sender, sendResponse) { | |
| 129 if (!request.success) | |
| 130 chrome.test.fail(); | |
| 131 } | |
| 132 ); | |
| 133 | |
| 134 var port = chrome.tabs.connect(testTab.id); | |
| 135 port.postMessage({testConnectFromTabError: true}); | |
| 136 port.disconnect(); | |
| 137 chrome.test.log("testConnectFromTabError: sent 1st message to tab"); | |
| 138 }, | |
| 139 | |
| 140 // Tests sending a request to a tab and receiving a response. | |
| 141 function sendRequest() { | |
| 142 chrome.tabs.sendRequest(testTab.id, {step2: 1}, function(response) { | |
| 143 chrome.test.assertTrue(response.success); | |
| 144 chrome.test.succeed(); | |
| 145 }); | |
| 146 }, | |
| 147 | |
| 148 // Tests that we get the disconnect event when the tab disconnect. | |
| 149 function disconnect() { | |
| 150 var port = chrome.tabs.connect(testTab.id); | |
| 151 port.postMessage({testDisconnect: true}); | |
| 152 port.onDisconnect.addListener(function() { | |
| 153 chrome.test.succeed(); | |
| 154 }); | |
| 155 }, | |
| 156 | |
| 157 // Tests that we get the disconnect event when the tab context closes. | |
| 158 function disconnectOnClose() { | |
| 159 var port = chrome.tabs.connect(testTab.id); | |
| 160 port.postMessage({testDisconnectOnClose: true}); | |
| 161 port.onDisconnect.addListener(function() { | |
| 162 chrome.test.succeed(); | |
| 163 testTab = null; // the tab is about:blank now. | |
| 164 }); | |
| 165 }, | |
| 166 ]); | |
| 167 }); | |
| 168 | |
| 169 </script> | |
| OLD | NEW |