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 JSON.parse = function() { | 5 JSON.parse = function() { |
6 return "JSON.parse clobbered by extension."; | 6 return "JSON.parse clobbered by extension."; |
7 }; | 7 }; |
8 | 8 |
9 JSON.stringify = function() { | 9 JSON.stringify = function() { |
10 return "JSON.stringify clobbered by extension."; | 10 return "JSON.stringify clobbered by extension."; |
(...skipping 10 matching lines...) Expand all Loading... |
21 // Keep track of the tab that we're running tests in, for simplicity. | 21 // Keep track of the tab that we're running tests in, for simplicity. |
22 var testTab = null; | 22 var testTab = null; |
23 | 23 |
24 chrome.test.getConfig(function(config) { | 24 chrome.test.getConfig(function(config) { |
25 chrome.test.runTests([ | 25 chrome.test.runTests([ |
26 function setupTestTab() { | 26 function setupTestTab() { |
27 chrome.test.log("Creating tab..."); | 27 chrome.test.log("Creating tab..."); |
28 chrome.tabs.create({ | 28 chrome.tabs.create({ |
29 url: "http://localhost:PORT/files/extensions/test_file.html" | 29 url: "http://localhost:PORT/files/extensions/test_file.html" |
30 .replace(/PORT/, config.testServer.port) | 30 .replace(/PORT/, config.testServer.port) |
31 }, function(tab) { | 31 }, function(newTab) { |
32 chrome.tabs.onUpdated.addListener(function listener(tabid, info) { | 32 chrome.tabs.onUpdated.addListener(function listener(_, info, tab) { |
33 if (tab.id == tabid && info.status == 'complete') { | 33 if (tab.id == newTab.id && info.status == 'complete') { |
34 chrome.test.log("Created tab: " + tab.url); | 34 chrome.test.log("Created tab: " + tab.url); |
35 chrome.tabs.onUpdated.removeListener(listener); | 35 chrome.tabs.onUpdated.removeListener(listener); |
36 testTab = tab; | 36 testTab = tab; |
37 chrome.test.succeed(); | 37 chrome.test.succeed(); |
38 } | 38 } |
39 }); | 39 }); |
40 }); | 40 }); |
41 }, | 41 }, |
42 | 42 |
43 // Tests that postMessage to the tab and its response works. | 43 // Tests that postMessage to the tab and its response works. |
(...skipping 14 matching lines...) Expand all Loading... |
58 port.onMessage.addListener(function(msg) { | 58 port.onMessage.addListener(function(msg) { |
59 chrome.test.assertEq(msg.portName, portName); | 59 chrome.test.assertEq(msg.portName, portName); |
60 port.disconnect(); | 60 port.disconnect(); |
61 chrome.test.succeed(); | 61 chrome.test.succeed(); |
62 }); | 62 }); |
63 }, | 63 }, |
64 | 64 |
65 // Tests that postMessage from the tab and its response works. | 65 // Tests that postMessage from the tab and its response works. |
66 function postMessageFromTab() { | 66 function postMessageFromTab() { |
67 chrome.runtime.onConnect.addListener(function(port) { | 67 chrome.runtime.onConnect.addListener(function(port) { |
68 chrome.test.assertTrue(Boolean(port.sender.tab.url)); | 68 chrome.test.assertEq({ |
69 chrome.test.assertTrue(Boolean(port.sender.tab.title)); | 69 tab: testTab, |
70 chrome.test.assertTrue(Boolean(port.sender.tab.id)); | 70 url: testTab.url, |
| 71 id: chrome.runtime.id |
| 72 }, port.sender); |
71 port.onMessage.addListener(function(msg) { | 73 port.onMessage.addListener(function(msg) { |
72 chrome.test.assertTrue(msg.testPostMessageFromTab); | 74 chrome.test.assertTrue(msg.testPostMessageFromTab); |
73 port.postMessage({success: true, portName: port.name}); | 75 port.postMessage({success: true, portName: port.name}); |
74 chrome.test.log("postMessageFromTab: got message from tab"); | 76 chrome.test.log("postMessageFromTab: got message from tab"); |
75 }); | 77 }); |
76 }); | 78 }); |
77 | 79 |
78 var port = chrome.tabs.connect(testTab.id); | 80 var port = chrome.tabs.connect(testTab.id); |
79 port.postMessage({testPostMessageFromTab: true}); | 81 port.postMessage({testPostMessageFromTab: true}); |
80 chrome.test.log("postMessageFromTab: sent first message to tab"); | 82 chrome.test.log("postMessageFromTab: sent first message to tab"); |
81 port.onMessage.addListener(function(msg) { | 83 port.onMessage.addListener(function(msg) { |
82 port.disconnect(); | 84 port.disconnect(); |
83 chrome.test.succeed(); | 85 chrome.test.succeed(); |
84 }); | 86 }); |
85 }, | 87 }, |
86 | 88 |
87 // Tests receiving a request from a content script and responding. | 89 // Tests receiving a request from a content script and responding. |
88 function sendMessageFromTab() { | 90 function sendMessageFromTab() { |
89 var doneListening = chrome.test.listenForever( | 91 var doneListening = chrome.test.listenForever( |
90 chrome.runtime.onMessage, | 92 chrome.runtime.onMessage, |
91 function(request, sender, sendResponse) { | 93 function(request, sender, sendResponse) { |
92 chrome.test.assertTrue("url" in sender.tab, "no tab available."); | 94 chrome.test.assertEq({ |
93 chrome.test.assertEq(sender.id, location.host); | 95 tab: testTab, |
| 96 url: testTab.url, |
| 97 id: chrome.runtime.id |
| 98 }, sender); |
94 if (request.step == 1) { | 99 if (request.step == 1) { |
95 // Step 1: Page should send another request for step 2. | 100 // Step 1: Page should send another request for step 2. |
96 chrome.test.log("sendMessageFromTab: got step 1"); | 101 chrome.test.log("sendMessageFromTab: got step 1"); |
97 sendResponse({nextStep: true}); | 102 sendResponse({nextStep: true}); |
98 } else { | 103 } else { |
99 // Step 2. | 104 // Step 2. |
100 chrome.test.assertEq(request.step, 2); | 105 chrome.test.assertEq(request.step, 2); |
101 sendResponse(); | 106 sendResponse(); |
102 doneListening(); | 107 doneListening(); |
103 } | 108 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } catch(e) { | 191 } catch(e) { |
187 error = e; | 192 error = e; |
188 } | 193 } |
189 chrome.test.assertTrue(error != undefined); | 194 chrome.test.assertTrue(error != undefined); |
190 | 195 |
191 chrome.test.succeed(); | 196 chrome.test.succeed(); |
192 }, | 197 }, |
193 | 198 |
194 ]); | 199 ]); |
195 }); | 200 }); |
OLD | NEW |