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 var pass = chrome.test.callbackPass; | 5 var pass = chrome.test.callbackPass; |
6 var fail = chrome.test.callbackFail; | 6 var fail = chrome.test.callbackFail; |
7 var assertEq = chrome.test.assertEq; | 7 var assertEq = chrome.test.assertEq; |
8 var assertTrue = chrome.test.assertTrue; | 8 var assertTrue = chrome.test.assertTrue; |
9 var relativePath = | 9 var relativePath = |
10 '/extensions/api_test/executescript/in_frame/test_executescript.html'; | 10 '/extensions/api_test/executescript/in_frame/test_executescript.html'; |
11 var testUrl = 'http://a.com:PORT' + relativePath; | 11 var testUrl = 'http://a.com:PORT' + relativePath; |
12 | 12 |
13 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { | 13 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { |
14 if (changeInfo.status != 'complete') | 14 if (changeInfo.status != 'complete') |
15 return; | 15 return; |
16 | 16 |
17 chrome.test.runTests([ | 17 chrome.test.runTests([ |
18 function executeJavaScriptCodeInAllFramesShouldSucceed() { | 18 function executeJavaScriptCodeInAllFramesShouldSucceed() { |
19 var script_file = {}; | 19 var script_file = {}; |
20 script_file.code = "var extensionPort = chrome.runtime.connect();"; | 20 script_file.code = "var extensionPort = chrome.runtime.connect();"; |
21 script_file.code = script_file.code + | 21 script_file.code = script_file.code + |
22 "extensionPort.postMessage({message: document.title});"; | 22 "extensionPort.postMessage({message: document.title});"; |
23 script_file.allFrames = true; | 23 script_file.allFrames = true; |
24 var counter = 0; | 24 var counter = 0; |
25 var totalTitles = ''; | 25 var totalTitles = ''; |
| 26 var done = pass(); |
| 27 function verifyAndFinish() { |
| 28 assertEq(counter, 5); |
| 29 assertEq(totalTitles, 'frametest0test1test2test3'); |
| 30 chrome.runtime.onConnect.removeListener(eventListener); |
| 31 done(); |
| 32 } |
26 function eventListener(port) { | 33 function eventListener(port) { |
27 port.onMessage.addListener(function(data) { | 34 port.onMessage.addListener(function(data) { |
28 counter++; | 35 counter++; |
| 36 assertTrue(counter <= 5); |
29 totalTitles += data.message; | 37 totalTitles += data.message; |
| 38 if (counter == 5) |
| 39 verifyAndFinish(); |
30 }); | 40 }); |
31 }; | 41 }; |
32 chrome.runtime.onConnect.addListener(eventListener); | 42 chrome.runtime.onConnect.addListener(eventListener); |
33 chrome.tabs.executeScript(tabId, script_file, pass(function() { | 43 chrome.tabs.executeScript(tabId, script_file); |
34 assertEq(counter, 5); | |
35 assertEq(totalTitles, 'frametest0test1test2test3'); | |
36 chrome.runtime.onConnect.removeListener(eventListener); | |
37 })); | |
38 }, | 44 }, |
39 | 45 |
40 function insertCSSTextInAllFramesShouldSucceed() { | 46 function insertCSSTextInAllFramesShouldSucceed() { |
41 var css_file = {}; | 47 var css_file = {}; |
42 css_file.code = "p {display:none;}"; | 48 css_file.code = "p {display:none;}"; |
43 css_file.allFrames = true; | 49 css_file.allFrames = true; |
44 var newStyle = ''; | 50 var newStyle = ''; |
45 var counter = 0; | 51 var counter = 0; |
| 52 var done = pass(); |
| 53 function verifyAndFinish() { |
| 54 assertEq(newStyle, 'nonenonenonenone'); |
| 55 assertEq(counter, 4); |
| 56 chrome.runtime.onConnect.removeListener(eventListener); |
| 57 done(); |
| 58 } |
46 function eventListener(port) { | 59 function eventListener(port) { |
47 port.onMessage.addListener(function(data) { | 60 port.onMessage.addListener(function(data) { |
48 counter++; | 61 counter++; |
49 newStyle += data.message; | 62 newStyle += data.message; |
| 63 if (counter == 4) |
| 64 verifyAndFinish(); |
50 }); | 65 }); |
51 }; | 66 }; |
52 chrome.runtime.onConnect.addListener(eventListener); | 67 chrome.runtime.onConnect.addListener(eventListener); |
53 chrome.tabs.insertCSS(tabId, css_file, function() { | 68 chrome.tabs.insertCSS(tabId, css_file, function() { |
54 var script_file = {}; | 69 var script_file = {}; |
55 script_file.file = 'script.js'; | 70 script_file.file = 'script.js'; |
56 script_file.allFrames = true; | 71 script_file.allFrames = true; |
57 chrome.tabs.executeScript(tabId, script_file, | 72 chrome.tabs.executeScript(tabId, script_file); |
58 pass(function() { | |
59 assertEq(newStyle, 'nonenonenonenone'); | |
60 assertEq(counter, 4); | |
61 chrome.runtime.onConnect.removeListener(eventListener); | |
62 })); | |
63 }); | 73 }); |
64 } | 74 } |
65 ]); | 75 ]); |
66 }); | 76 }); |
67 | 77 |
68 chrome.test.getConfig(function(config) { | 78 chrome.test.getConfig(function(config) { |
69 testUrl = testUrl.replace(/PORT/, config.testServer.port); | 79 testUrl = testUrl.replace(/PORT/, config.testServer.port); |
70 chrome.tabs.create({ url: testUrl }); | 80 chrome.tabs.create({ url: testUrl }); |
71 }); | 81 }); |
OLD | NEW |