Chromium Code Reviews| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 chrome.test.assertEq(msg.portName, portName); | 63 chrome.test.assertEq(msg.portName, portName); |
| 64 port.disconnect(); | 64 port.disconnect(); |
| 65 }); | 65 }); |
| 66 }, | 66 }, |
| 67 | 67 |
| 68 // Tests that postMessage from the tab and its response works. | 68 // Tests that postMessage from the tab and its response works. |
| 69 function postMessageFromTab() { | 69 function postMessageFromTab() { |
| 70 listenOnce(chrome.runtime.onConnect, function(port) { | 70 listenOnce(chrome.runtime.onConnect, function(port) { |
| 71 chrome.test.assertEq({ | 71 chrome.test.assertEq({ |
| 72 tab: testTab, | 72 tab: testTab, |
| 73 frameId: 0, // Main frame | |
| 73 url: testTab.url, | 74 url: testTab.url, |
| 74 id: chrome.runtime.id | 75 id: chrome.runtime.id |
| 75 }, port.sender); | 76 }, port.sender); |
| 76 listenOnce(port.onMessage, function(msg) { | 77 listenOnce(port.onMessage, function(msg) { |
| 77 chrome.test.assertTrue(msg.testPostMessageFromTab); | 78 chrome.test.assertTrue(msg.testPostMessageFromTab); |
| 78 port.postMessage({success: true, portName: port.name}); | 79 port.postMessage({success: true, portName: port.name}); |
| 79 chrome.test.log("postMessageFromTab: got message from tab"); | 80 chrome.test.log("postMessageFromTab: got message from tab"); |
| 80 }); | 81 }); |
| 81 }); | 82 }); |
| 82 | 83 |
| 83 var port = chrome.tabs.connect(testTab.id); | 84 var port = chrome.tabs.connect(testTab.id); |
| 84 port.postMessage({testPostMessageFromTab: true}); | 85 port.postMessage({testPostMessageFromTab: true}); |
| 85 chrome.test.log("postMessageFromTab: sent first message to tab"); | 86 chrome.test.log("postMessageFromTab: sent first message to tab"); |
| 86 listenOnce(port.onMessage, function(msg) { | 87 listenOnce(port.onMessage, function(msg) { |
| 87 port.disconnect(); | 88 port.disconnect(); |
| 88 }); | 89 }); |
| 89 }, | 90 }, |
| 90 | 91 |
| 91 // Tests receiving a request from a content script and responding. | 92 // Tests receiving a request from a content script and responding. |
| 92 function sendMessageFromTab() { | 93 function sendMessageFromTab() { |
| 93 var doneListening = listenForever( | 94 var doneListening = listenForever( |
| 94 chrome.runtime.onMessage, | 95 chrome.runtime.onMessage, |
| 95 function(request, sender, sendResponse) { | 96 function(request, sender, sendResponse) { |
| 96 chrome.test.assertEq({ | 97 chrome.test.assertEq({ |
| 97 tab: testTab, | 98 tab: testTab, |
| 99 frameId: 0, // Main frame | |
| 98 url: testTab.url, | 100 url: testTab.url, |
| 99 id: chrome.runtime.id | 101 id: chrome.runtime.id |
| 100 }, sender); | 102 }, sender); |
| 101 if (request.step == 1) { | 103 if (request.step == 1) { |
| 102 // Step 1: Page should send another request for step 2. | 104 // Step 1: Page should send another request for step 2. |
| 103 chrome.test.log("sendMessageFromTab: got step 1"); | 105 chrome.test.log("sendMessageFromTab: got step 1"); |
| 104 sendResponse({nextStep: true}); | 106 sendResponse({nextStep: true}); |
| 105 } else { | 107 } else { |
| 106 // Step 2. | 108 // Step 2. |
| 107 chrome.test.assertEq(request.step, 2); | 109 chrome.test.assertEq(request.step, 2); |
| 108 sendResponse(); | 110 sendResponse(); |
| 109 doneListening(); | 111 doneListening(); |
| 110 } | 112 } |
| 111 }); | 113 }); |
| 112 | 114 |
| 113 var port = chrome.tabs.connect(testTab.id); | 115 var port = chrome.tabs.connect(testTab.id); |
| 114 port.postMessage({testSendMessageFromTab: true}); | 116 port.postMessage({testSendMessageFromTab: true}); |
| 115 port.disconnect(); | 117 port.disconnect(); |
| 116 chrome.test.log("sendMessageFromTab: sent first message to tab"); | 118 chrome.test.log("sendMessageFromTab: sent first message to tab"); |
| 117 }, | 119 }, |
| 118 | 120 |
| 121 // Tests that a message from a child frame has a correct frameId. | |
| 122 function sendMessageFromFrameInTab() { | |
| 123 var senderFrameUrl = {}; | |
| 124 listenForever( | |
| 125 chrome.runtime.onMessage, | |
| 126 function(request, sender, sendResponse) { | |
| 127 // Child frames have a positive frameId. | |
| 128 chrome.test.assertTrue(sender.frameId >= 1, | |
| 129 'frameId must be positive, but it is ' + sender.frameId); | |
| 130 chrome.test.assertFalse(sender.frameId in messageSenders, | |
| 131 'frameId ' + sender.frameId + ' was already seen before!'); | |
| 132 senderFrameUrl[sender.frameId] = request.frameUrl; | |
| 133 delete sender.frameId; | |
| 134 chrome.test.assertEq({ | |
|
not at google - send to devlin
2014/11/11 22:23:00
Test is better now, but I think you can make it ev
robwu
2014/11/11 22:44:38
The list of frames *could* potentially contain mor
| |
| 135 tab: testTab, | |
| 136 url: request.frameUrl, | |
| 137 id: chrome.runtime.id | |
| 138 }, sender); | |
| 139 | |
| 140 if (Object.keys(senderFrameUrl).length == 2) { | |
| 141 // Now test whether the frameId that we got back makes sense... | |
| 142 chrome.webNavigation.getAllFrames({ | |
| 143 tabId: testTab.id | |
| 144 }, function(details) { | |
| 145 var framesFound = 0; | |
| 146 details.forEach(function(frame) { | |
| 147 var url = senderFrameUrl[frame.frameId]; | |
| 148 if (url) { | |
| 149 chrome.test.assertEq(frame.url, url); | |
| 150 ++framesFound; | |
| 151 } | |
| 152 }); | |
| 153 chrome.test.assertEq(framesFound, 2); | |
| 154 doneListening(); | |
| 155 }); | |
| 156 } | |
| 157 } | |
| 158 ); | |
| 159 | |
| 160 var port = chrome.tabs.connect(testTab.id); | |
| 161 port.postMessage({testSendMessageFromFrame: true}); | |
| 162 port.disconnect(); | |
| 163 chrome.test.log("sendMessageFromFrameInTab: send 1st message to tab"); | |
| 164 }, | |
| 165 | |
| 119 // Tests error handling when sending a request from a content script to an | 166 // Tests error handling when sending a request from a content script to an |
| 120 // invalid extension. | 167 // invalid extension. |
| 121 function sendMessageFromTabError() { | 168 function sendMessageFromTabError() { |
| 122 listenOnce( | 169 listenOnce( |
| 123 chrome.runtime.onMessage, | 170 chrome.runtime.onMessage, |
| 124 function(request, sender, sendResponse) { | 171 function(request, sender, sendResponse) { |
| 125 if (!request.success) | 172 if (!request.success) |
| 126 chrome.test.fail(); | 173 chrome.test.fail(); |
| 127 } | 174 } |
| 128 ); | 175 ); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 } catch(e) { | 252 } catch(e) { |
| 206 error = e; | 253 error = e; |
| 207 } | 254 } |
| 208 chrome.test.assertTrue(error != undefined); | 255 chrome.test.assertTrue(error != undefined); |
| 209 | 256 |
| 210 chrome.test.succeed(); | 257 chrome.test.succeed(); |
| 211 }, | 258 }, |
| 212 | 259 |
| 213 ]); | 260 ]); |
| 214 }); | 261 }); |
| OLD | NEW |