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 // Tests for browser_tests --gtest_filter=ExtensionApiTest.Messaging |
| 6 |
| 7 var assertEq = chrome.test.assertEq; |
| 8 var assertTrue = chrome.test.assertTrue; |
5 var listenOnce = chrome.test.listenOnce; | 9 var listenOnce = chrome.test.listenOnce; |
6 var listenForever = chrome.test.listenForever; | 10 var listenForever = chrome.test.listenForever; |
7 | 11 |
8 JSON.parse = function() { | 12 JSON.parse = function() { |
9 return "JSON.parse clobbered by extension."; | 13 return "JSON.parse clobbered by extension."; |
10 }; | 14 }; |
11 | 15 |
12 JSON.stringify = function() { | 16 JSON.stringify = function() { |
13 return "JSON.stringify clobbered by extension."; | 17 return "JSON.stringify clobbered by extension."; |
14 }; | 18 }; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 port.disconnect(); | 57 port.disconnect(); |
54 }); | 58 }); |
55 }, | 59 }, |
56 | 60 |
57 // Tests that port name is sent & received correctly. | 61 // Tests that port name is sent & received correctly. |
58 function portName() { | 62 function portName() { |
59 var portName = "lemonjello"; | 63 var portName = "lemonjello"; |
60 var port = chrome.tabs.connect(testTab.id, {name: portName}); | 64 var port = chrome.tabs.connect(testTab.id, {name: portName}); |
61 port.postMessage({testPortName: true}); | 65 port.postMessage({testPortName: true}); |
62 listenOnce(port.onMessage, function(msg) { | 66 listenOnce(port.onMessage, function(msg) { |
63 chrome.test.assertEq(msg.portName, portName); | 67 assertEq(msg.portName, portName); |
64 port.disconnect(); | 68 port.disconnect(); |
65 }); | 69 }); |
66 }, | 70 }, |
67 | 71 |
68 // Tests that postMessage from the tab and its response works. | 72 // Tests that postMessage from the tab and its response works. |
69 function postMessageFromTab() { | 73 function postMessageFromTab() { |
70 listenOnce(chrome.runtime.onConnect, function(port) { | 74 listenOnce(chrome.runtime.onConnect, function(port) { |
71 chrome.test.assertEq({ | 75 assertEq({ |
72 tab: testTab, | 76 tab: testTab, |
73 frameId: 0, // Main frame | 77 frameId: 0, // Main frame |
74 url: testTab.url, | 78 url: testTab.url, |
75 id: chrome.runtime.id | 79 id: chrome.runtime.id |
76 }, port.sender); | 80 }, port.sender); |
77 listenOnce(port.onMessage, function(msg) { | 81 listenOnce(port.onMessage, function(msg) { |
78 chrome.test.assertTrue(msg.testPostMessageFromTab); | 82 assertTrue(msg.testPostMessageFromTab); |
79 port.postMessage({success: true, portName: port.name}); | 83 port.postMessage({success: true, portName: port.name}); |
80 chrome.test.log("postMessageFromTab: got message from tab"); | 84 chrome.test.log("postMessageFromTab: got message from tab"); |
81 }); | 85 }); |
82 }); | 86 }); |
83 | 87 |
84 var port = chrome.tabs.connect(testTab.id); | 88 var port = chrome.tabs.connect(testTab.id); |
85 port.postMessage({testPostMessageFromTab: true}); | 89 port.postMessage({testPostMessageFromTab: true}); |
86 chrome.test.log("postMessageFromTab: sent first message to tab"); | 90 chrome.test.log("postMessageFromTab: sent first message to tab"); |
87 listenOnce(port.onMessage, function(msg) { | 91 listenOnce(port.onMessage, function(msg) { |
88 port.disconnect(); | 92 port.disconnect(); |
89 }); | 93 }); |
90 }, | 94 }, |
91 | 95 |
92 // Tests receiving a request from a content script and responding. | 96 // Tests receiving a request from a content script and responding. |
93 function sendMessageFromTab() { | 97 function sendMessageFromTab() { |
94 var doneListening = listenForever( | 98 var doneListening = listenForever( |
95 chrome.runtime.onMessage, | 99 chrome.runtime.onMessage, |
96 function(request, sender, sendResponse) { | 100 function(request, sender, sendResponse) { |
97 chrome.test.assertEq({ | 101 assertEq({ |
98 tab: testTab, | 102 tab: testTab, |
99 frameId: 0, // Main frame | 103 frameId: 0, // Main frame |
100 url: testTab.url, | 104 url: testTab.url, |
101 id: chrome.runtime.id | 105 id: chrome.runtime.id |
102 }, sender); | 106 }, sender); |
103 if (request.step == 1) { | 107 if (request.step == 1) { |
104 // Step 1: Page should send another request for step 2. | 108 // Step 1: Page should send another request for step 2. |
105 chrome.test.log("sendMessageFromTab: got step 1"); | 109 chrome.test.log("sendMessageFromTab: got step 1"); |
106 sendResponse({nextStep: true}); | 110 sendResponse({nextStep: true}); |
107 } else { | 111 } else { |
108 // Step 2. | 112 // Step 2. |
109 chrome.test.assertEq(request.step, 2); | 113 assertEq(2, request.step); |
110 sendResponse(); | 114 sendResponse(); |
111 doneListening(); | 115 doneListening(); |
112 } | 116 } |
113 }); | 117 }); |
114 | 118 |
115 var port = chrome.tabs.connect(testTab.id); | 119 var port = chrome.tabs.connect(testTab.id); |
116 port.postMessage({testSendMessageFromTab: true}); | 120 port.postMessage({testSendMessageFromTab: true}); |
117 port.disconnect(); | 121 port.disconnect(); |
118 chrome.test.log("sendMessageFromTab: sent first message to tab"); | 122 chrome.test.log("sendMessageFromTab: sent first message to tab"); |
119 }, | 123 }, |
(...skipping 24 matching lines...) Expand all Loading... |
144 return frame.frameId > 0; // Exclude main frame. | 148 return frame.frameId > 0; // Exclude main frame. |
145 }).map(function(frame) { | 149 }).map(function(frame) { |
146 return { | 150 return { |
147 tab: testTab, | 151 tab: testTab, |
148 frameId: frame.frameId, | 152 frameId: frame.frameId, |
149 url: frame.url, | 153 url: frame.url, |
150 id: chrome.runtime.id | 154 id: chrome.runtime.id |
151 }; | 155 }; |
152 }).sort(sortByFrameId); | 156 }).sort(sortByFrameId); |
153 senders.sort(sortByFrameId); | 157 senders.sort(sortByFrameId); |
154 chrome.test.assertEq(expectedSenders, senders); | 158 assertEq(expectedSenders, senders); |
155 doneListening(); | 159 doneListening(); |
156 }); | 160 }); |
157 } | 161 } |
158 } | 162 } |
159 ); | 163 ); |
160 | 164 |
161 var port = chrome.tabs.connect(testTab.id); | 165 var port = chrome.tabs.connect(testTab.id); |
162 port.postMessage({testSendMessageFromFrame: true}); | 166 port.postMessage({testSendMessageFromFrame: true}); |
163 port.disconnect(); | 167 port.disconnect(); |
164 chrome.test.log("sendMessageFromFrameInTab: send 1st message to tab"); | 168 chrome.test.log("sendMessageFromFrameInTab: send 1st message to tab"); |
(...skipping 11 matching lines...) Expand all Loading... |
176 | 180 |
177 // connect with a positive frameId should trigger onConnect in that specific | 181 // connect with a positive frameId should trigger onConnect in that specific |
178 // frame only. | 182 // frame only. |
179 function sendMessageToFrameInTab() { | 183 function sendMessageToFrameInTab() { |
180 chrome.webNavigation.getAllFrames({ | 184 chrome.webNavigation.getAllFrames({ |
181 tabId: testTab.id | 185 tabId: testTab.id |
182 }, function(details) { | 186 }, function(details) { |
183 var frames = details.filter(function(frame) { | 187 var frames = details.filter(function(frame) { |
184 return /\?testSendMessageFromFrame1$/.test(frame.url); | 188 return /\?testSendMessageFromFrame1$/.test(frame.url); |
185 }); | 189 }); |
186 chrome.test.assertEq(1, frames.length); | 190 assertEq(1, frames.length); |
187 connectToTabWithFrameId(frames[0].frameId, ['from_1']); | 191 connectToTabWithFrameId(frames[0].frameId, ['from_1']); |
188 }); | 192 }); |
189 }, | 193 }, |
190 | 194 |
191 // sendMessage with an invalid frameId should fail. | 195 // sendMessage with an invalid frameId should fail. |
192 function sendMessageToInvalidFrameInTab() { | 196 function sendMessageToInvalidFrameInTab() { |
193 chrome.tabs.sendMessage(testTab.id, {}, { | 197 chrome.tabs.sendMessage(testTab.id, {}, { |
194 frameId: 999999999 // Some (hopefully) invalid frameId. | 198 frameId: 999999999 // Some (hopefully) invalid frameId. |
195 }, chrome.test.callbackFail( | 199 }, chrome.test.callbackFail( |
196 'Could not establish connection. Receiving end does not exist.')); | 200 'Could not establish connection. Receiving end does not exist.')); |
(...skipping 29 matching lines...) Expand all Loading... |
226 | 230 |
227 var port = chrome.tabs.connect(testTab.id); | 231 var port = chrome.tabs.connect(testTab.id); |
228 port.postMessage({testConnectFromTabError: true}); | 232 port.postMessage({testConnectFromTabError: true}); |
229 port.disconnect(); | 233 port.disconnect(); |
230 chrome.test.log("testConnectFromTabError: sent 1st message to tab"); | 234 chrome.test.log("testConnectFromTabError: sent 1st message to tab"); |
231 }, | 235 }, |
232 | 236 |
233 // Tests sending a request to a tab and receiving a response. | 237 // Tests sending a request to a tab and receiving a response. |
234 function sendMessage() { | 238 function sendMessage() { |
235 chrome.tabs.sendMessage(testTab.id, {step2: 1}, function(response) { | 239 chrome.tabs.sendMessage(testTab.id, {step2: 1}, function(response) { |
236 chrome.test.assertTrue(response.success); | 240 assertTrue(response.success); |
237 chrome.test.succeed(); | 241 chrome.test.succeed(); |
238 }); | 242 }); |
239 }, | 243 }, |
240 | 244 |
241 // Tests that we get the disconnect event when the tab disconnect. | 245 // Tests that we get the disconnect event when the tab disconnect. |
242 function disconnect() { | 246 function disconnect() { |
243 var port = chrome.tabs.connect(testTab.id); | 247 var port = chrome.tabs.connect(testTab.id); |
244 port.postMessage({testDisconnect: true}); | 248 port.postMessage({testDisconnect: true}); |
245 listenOnce(port.onDisconnect, function() {}); | 249 listenOnce(port.onDisconnect, function() {}); |
246 }, | 250 }, |
(...skipping 23 matching lines...) Expand all Loading... |
270 }, | 274 }, |
271 | 275 |
272 // Tests that the sendRequest API is disabled. | 276 // Tests that the sendRequest API is disabled. |
273 function sendRequest() { | 277 function sendRequest() { |
274 var error; | 278 var error; |
275 try { | 279 try { |
276 chrome.extension.sendRequest("hi"); | 280 chrome.extension.sendRequest("hi"); |
277 } catch(e) { | 281 } catch(e) { |
278 error = e; | 282 error = e; |
279 } | 283 } |
280 chrome.test.assertTrue(error != undefined); | 284 assertTrue(error != undefined); |
281 | 285 |
282 error = undefined; | 286 error = undefined; |
283 try { | 287 try { |
284 chrome.extension.onRequest.addListener(function() {}); | 288 chrome.extension.onRequest.addListener(function() {}); |
285 } catch(e) { | 289 } catch(e) { |
286 error = e; | 290 error = e; |
287 } | 291 } |
288 chrome.test.assertTrue(error != undefined); | 292 assertTrue(error != undefined); |
289 | 293 |
290 chrome.test.succeed(); | 294 chrome.test.succeed(); |
291 }, | 295 }, |
292 | 296 |
293 // Tests that chrome.runtime.sendMessage is *not* delivered to the current | 297 // Tests that chrome.runtime.sendMessage is *not* delivered to the current |
294 // context, consistent behavior with chrome.runtime.connect() and web APIs | 298 // context, consistent behavior with chrome.runtime.connect() and web APIs |
295 // like localStorage changed listeners. | 299 // like localStorage changed listeners. |
296 // Regression test for http://crbug.com/479951. | 300 // Regression test for http://crbug.com/479951. |
297 function sendMessageToCurrentContextFails() { | 301 function sendMessageToCurrentContextFails() { |
298 var stopFailing = failWhileListening(chrome.runtime.onMessage); | 302 var stopFailing = failWhileListening(chrome.runtime.onMessage); |
(...skipping 11 matching lines...) Expand all Loading... |
310 // is no callback to signal when it's supposed to have been done. | 314 // is no callback to signal when it's supposed to have been done. |
311 // Regression test for http://crbug.com/479951. | 315 // Regression test for http://crbug.com/479951. |
312 function sendMessageToCurrentTextWithoutCallbackFails() { | 316 function sendMessageToCurrentTextWithoutCallbackFails() { |
313 // Make the iframe - in a different context - watch for the message | 317 // 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. | 318 // event. It *should* get it, while the current context's one doesn't. |
315 var iframe = document.createElement('iframe'); | 319 var iframe = document.createElement('iframe'); |
316 iframe.src = chrome.runtime.getURL('blank_iframe.html'); | 320 iframe.src = chrome.runtime.getURL('blank_iframe.html'); |
317 document.body.appendChild(iframe); | 321 document.body.appendChild(iframe); |
318 | 322 |
319 var stopFailing = failWhileListening(chrome.runtime.onMessage); | 323 var stopFailing = failWhileListening(chrome.runtime.onMessage); |
320 chrome.test.listenOnce( | 324 listenOnce( |
321 iframe.contentWindow.chrome.runtime.onMessage, | 325 iframe.contentWindow.chrome.runtime.onMessage, |
322 function(msg, sender) { | 326 function(msg, sender) { |
323 chrome.test.assertEq('ping', msg); | 327 assertEq('ping', msg); |
324 chrome.test.assertEq(chrome.runtime.id, sender.id); | 328 assertEq(chrome.runtime.id, sender.id); |
325 chrome.test.assertEq(location.href, sender.url); | 329 assertEq(location.href, sender.url); |
326 setTimeout(function() { | 330 setTimeout(function() { |
327 stopFailing(); | 331 stopFailing(); |
328 chrome.test.succeed(); | 332 chrome.test.succeed(); |
329 }, 0); | 333 }, 0); |
330 } | 334 } |
331 ); | 335 ); |
332 | 336 |
333 chrome.runtime.sendMessage('ping'); | 337 chrome.runtime.sendMessage('ping'); |
334 }, | 338 }, |
| 339 |
| 340 // Tests that destroying a port clears its onMessage and onDisconnect |
| 341 // listeners. |
| 342 function disconnectingPortRemovesEventListeners() { |
| 343 var port = chrome.runtime.connect(chrome.runtime.id); |
| 344 assertTrue(!port.onMessage.hasListeners() && |
| 345 !port.onDisconnect.hasListeners()); |
| 346 port.onMessage.addListener(function() {}); |
| 347 port.onDisconnect.addListener(function() {}); |
| 348 assertTrue(port.onMessage.hasListeners() && |
| 349 port.onDisconnect.hasListeners()); |
| 350 port.disconnect(); |
| 351 assertTrue(!port.onMessage.hasListeners() && |
| 352 !port.onDisconnect.hasListeners()); |
| 353 chrome.test.succeed(); |
| 354 } |
335 ]); | 355 ]); |
336 }); | 356 }); |
337 | 357 |
338 function connectToTabWithFrameId(frameId, expectedMessages) { | 358 function connectToTabWithFrameId(frameId, expectedMessages) { |
339 var port = chrome.tabs.connect(testTab.id, { | 359 var port = chrome.tabs.connect(testTab.id, { |
340 frameId: frameId | 360 frameId: frameId |
341 }); | 361 }); |
342 var messages = []; | 362 var messages = []; |
343 var isDone = false; | 363 var isDone = false; |
344 listenForever(port.onMessage, function(message) { | 364 listenForever(port.onMessage, function(message) { |
345 if (isDone) // Should not get any messages after completing the test. | 365 if (isDone) // Should not get any messages after completing the test. |
346 chrome.test.fail( | 366 chrome.test.fail( |
347 'Unexpected message from port to frame ' + frameId + ': ' + message); | 367 'Unexpected message from port to frame ' + frameId + ': ' + message); |
348 | 368 |
349 messages.push(message); | 369 messages.push(message); |
350 isDone = messages.length == expectedMessages.length; | 370 isDone = messages.length == expectedMessages.length; |
351 if (isDone) { | 371 if (isDone) { |
352 chrome.test.assertEq(expectedMessages.sort(), messages.sort()); | 372 assertEq(expectedMessages.sort(), messages.sort()); |
353 chrome.test.succeed(); | 373 chrome.test.succeed(); |
354 } | 374 } |
355 }); | 375 }); |
356 listenOnce(port.onDisconnect, function() { | 376 listenOnce(port.onDisconnect, function() { |
357 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. |
358 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId); | 378 chrome.test.fail('Unexpected disconnect from port to frame ' + frameId); |
359 }); | 379 }); |
360 port.postMessage({testSendMessageToFrame: true}); | 380 port.postMessage({testSendMessageToFrame: true}); |
361 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId); | 381 chrome.test.log('connectToTabWithFrameId: port to frame ' + frameId); |
362 } | 382 } |
363 | 383 |
364 // 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 |
365 // listening, if |event| is fired, calls chrome.test.fail(). | 385 // listening, if |event| is fired, calls chrome.test.fail(). |
366 function failWhileListening(event, doneListening) { | 386 function failWhileListening(event, doneListening) { |
367 var failListener = function() { | 387 var failListener = function() { |
368 chrome.test.fail('Event listener ran, but it shouldn\'t have. ' + | 388 chrome.test.fail('Event listener ran, but it shouldn\'t have. ' + |
369 'It\'s possible that may be triggered flakily, but this ' + | 389 'It\'s possible that may be triggered flakily, but this ' + |
370 'really is a real failure, not flaky sadness. Promise!'); | 390 'really is a real failure, not flaky sadness. Promise!'); |
371 }; | 391 }; |
372 var release = chrome.test.callbackAdded(); | 392 var release = chrome.test.callbackAdded(); |
373 event.addListener(failListener); | 393 event.addListener(failListener); |
374 return function() { | 394 return function() { |
375 event.removeListener(failListener); | 395 event.removeListener(failListener); |
376 release(); | 396 release(); |
377 }; | 397 }; |
378 } | 398 } |
OLD | NEW |