Chromium Code Reviews| 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 var f = document.createElement('iframe'); | |
| 72 f.src = '?frame_test'; | |
| 73 document.body.appendChild(f); | |
|
not at google - send to devlin
2014/11/11 00:03:04
Ideally you'd test with more than 1 frame.
robwu
2014/11/11 00:34:26
Done.
| |
| 74 // The test will continue in frame.js (a content script that runs at | |
| 75 // ?frame_test, declared in manifest.json). | |
|
not at google - send to devlin
2014/11/11 00:03:04
A more useful comment would explain what the frame
robwu
2014/11/11 00:34:26
Done.
| |
| 76 } | |
| 77 | |
| 68 // Tests sendMessage to an invalid extension. | 78 // Tests sendMessage to an invalid extension. |
| 69 function testSendMessageFromTabError() { | 79 function testSendMessageFromTabError() { |
| 70 // try sending a request to a bad extension id | 80 // try sending a request to a bad extension id |
| 71 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { | 81 chrome.runtime.sendMessage("bad-extension-id", {m: 1}, function(response) { |
| 72 var success = (response === undefined && chrome.runtime.lastError); | 82 var success = (response === undefined && chrome.runtime.lastError); |
| 73 chrome.runtime.sendMessage({success: success}); | 83 chrome.runtime.sendMessage({success: success}); |
| 74 }); | 84 }); |
| 75 } | 85 } |
| 76 | 86 |
| 77 // Tests connecting to an invalid extension. | 87 // Tests connecting to an invalid extension. |
| 78 function testConnectFromTabError() { | 88 function testConnectFromTabError() { |
| 79 var port = chrome.runtime.connect("bad-extension-id"); | 89 var port = chrome.runtime.connect("bad-extension-id"); |
| 80 port.onDisconnect.addListener(function() { | 90 port.onDisconnect.addListener(function() { |
| 81 var success = (chrome.runtime.lastError ? true : false); | 91 var success = (chrome.runtime.lastError ? true : false); |
| 82 chrome.runtime.sendMessage({success: success}); | 92 chrome.runtime.sendMessage({success: success}); |
| 83 }); | 93 }); |
| 84 } | 94 } |
| 85 | 95 |
| 86 // For test sendMessage. | 96 // For test sendMessage. |
| 87 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { | 97 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { |
| 88 chrome.test.assertEq({id: chrome.runtime.id}, sender); | 98 chrome.test.assertEq({id: chrome.runtime.id}, sender); |
| 89 sendResponse({success: (request.step2 == 1)}); | 99 sendResponse({success: (request.step2 == 1)}); |
| 90 }); | 100 }); |
| OLD | NEW |