OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 JSON.parse = function() { | 5 JSON.parse = function() { |
6 return "JSON.parse clobbered by content script."; | 6 return "JSON.parse clobbered by content script."; |
7 }; | 7 }; |
8 | 8 |
9 JSON.stringify = function() { | 9 JSON.stringify = function() { |
10 return "JSON.stringify clobbered by content script."; | 10 return "JSON.stringify clobbered by content script."; |
(...skipping 11 matching lines...) Expand all Loading... |
22 chrome.runtime.onConnect.addListener(function(port) { | 22 chrome.runtime.onConnect.addListener(function(port) { |
23 console.log('connected'); | 23 console.log('connected'); |
24 port.onMessage.addListener(function(msg) { | 24 port.onMessage.addListener(function(msg) { |
25 console.log('got ' + msg); | 25 console.log('got ' + msg); |
26 if (msg.testPostMessage) { | 26 if (msg.testPostMessage) { |
27 port.postMessage({success: true}); | 27 port.postMessage({success: true}); |
28 } else if (msg.testPostMessageFromTab) { | 28 } else if (msg.testPostMessageFromTab) { |
29 testPostMessageFromTab(port); | 29 testPostMessageFromTab(port); |
30 } else if (msg.testSendMessageFromTab) { | 30 } else if (msg.testSendMessageFromTab) { |
31 testSendMessageFromTab(); | 31 testSendMessageFromTab(); |
| 32 } else if (msg.testSendMessageFromFrame) { |
| 33 testSendMessageFromFrame(); |
32 } else if (msg.testDisconnect) { | 34 } else if (msg.testDisconnect) { |
33 port.disconnect(); | 35 port.disconnect(); |
34 } else if (msg.testDisconnectOnClose) { | 36 } else if (msg.testDisconnectOnClose) { |
35 window.location = "about:blank"; | 37 window.location = "about:blank"; |
36 } else if (msg.testPortName) { | 38 } else if (msg.testPortName) { |
37 port.postMessage({portName:port.name}); | 39 port.postMessage({portName:port.name}); |
38 } else if (msg.testSendMessageFromTabError) { | 40 } else if (msg.testSendMessageFromTabError) { |
39 testSendMessageFromTabError(); | 41 testSendMessageFromTabError(); |
40 } else if (msg.testConnectFromTabError) { | 42 } else if (msg.testConnectFromTabError) { |
41 testConnectFromTabError(); | 43 testConnectFromTabError(); |
(...skipping 16 matching lines...) Expand all Loading... |
58 // For test onMessage. | 60 // For test onMessage. |
59 function testSendMessageFromTab() { | 61 function testSendMessageFromTab() { |
60 chrome.runtime.sendMessage({step: 1}, function(response) { | 62 chrome.runtime.sendMessage({step: 1}, function(response) { |
61 if (response.nextStep) { | 63 if (response.nextStep) { |
62 console.log('testSendMessageFromTab sent'); | 64 console.log('testSendMessageFromTab sent'); |
63 chrome.runtime.sendMessage({step: 2}); | 65 chrome.runtime.sendMessage({step: 2}); |
64 } | 66 } |
65 }); | 67 }); |
66 } | 68 } |
67 | 69 |
| 70 function testSendMessageFromFrame() { |
| 71 // Add two frames. The content script declared in manifest.json (frame.js) |
| 72 // runs in frames whose URL matches ?testSendMessageFromFrame. |
| 73 // frame.js sends a message to the background page, which checks that |
| 74 // sender.frameId exists and is different for both frames. |
| 75 for (var i = 0; i < 2; ++i) { |
| 76 var f = document.createElement('iframe'); |
| 77 f.src = '?testSendMessageFromFrame' + i; |
| 78 document.body.appendChild(f); |
| 79 } |
| 80 } |
| 81 |
68 // Tests sendMessage to an invalid extension. | 82 // Tests sendMessage to an invalid extension. |
69 function testSendMessageFromTabError() { | 83 function testSendMessageFromTabError() { |
70 // try sending a request to a bad extension id | 84 // try sending a request to a bad extension id |
71 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { | 85 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { |
72 var success = (response === undefined && chrome.runtime.lastError); | 86 var success = (response === undefined && chrome.runtime.lastError); |
73 chrome.runtime.sendMessage({success: success}); | 87 chrome.runtime.sendMessage({success: success}); |
74 }); | 88 }); |
75 } | 89 } |
76 | 90 |
77 // Tests connecting to an invalid extension. | 91 // Tests connecting to an invalid extension. |
78 function testConnectFromTabError() { | 92 function testConnectFromTabError() { |
79 var port = chrome.runtime.connect("bad-extension-id"); | 93 var port = chrome.runtime.connect("bad-extension-id"); |
80 port.onDisconnect.addListener(function() { | 94 port.onDisconnect.addListener(function() { |
81 var success = (chrome.runtime.lastError ? true : false); | 95 var success = (chrome.runtime.lastError ? true : false); |
82 chrome.runtime.sendMessage({success: success}); | 96 chrome.runtime.sendMessage({success: success}); |
83 }); | 97 }); |
84 } | 98 } |
85 | 99 |
86 // For test sendMessage. | 100 // For test sendMessage. |
87 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { | 101 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { |
88 chrome.test.assertEq({id: chrome.runtime.id}, sender); | 102 chrome.test.assertEq({id: chrome.runtime.id}, sender); |
89 sendResponse({success: (request.step2 == 1)}); | 103 sendResponse({success: (request.step2 == 1)}); |
90 }); | 104 }); |
OLD | NEW |