OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 var request_to_comm_channel_1 = 'connect'; | |
6 var request_to_comm_channel_2 = 'connect_request'; | |
7 var response_from_comm_channel_1 = 'connected'; | |
8 var response_from_comm_channel_2 = 'connected_response'; | |
9 | |
10 function createWebview() { | |
11 var webview = document.createElement('webview'); | |
12 document.body.appendChild(webview); | |
13 return webview; | |
14 } | |
15 | |
16 function onGetBackgroundExecuted(results) { | |
17 chrome.send('testResult', [results.length == 1 && results[0] == 'red']); | |
18 }; | |
19 | |
20 function testExecuteScriptCode(url) { | |
21 var webview = createWebview(); | |
22 | |
23 var onSetBackgroundExecuted = function() { | |
24 webview.executeScript({ | |
25 code: 'document.body.style.backgroundColor;' | |
26 }, onGetBackgroundExecuted); | |
27 }; | |
28 | |
29 var onLoadStop = function() { | |
30 webview.executeScript({ | |
31 code: 'document.body.style.backgroundColor = \'red\';' | |
32 }, onSetBackgroundExecuted); | |
33 }; | |
34 | |
35 webview.addEventListener('loadstop', onLoadStop); | |
36 webview.src = url; | |
37 } | |
38 | |
39 function testExecuteScriptCodeFromFile(url) { | |
40 var webview = createWebview(); | |
41 | |
42 var onSetBackgroundExecuted = function() { | |
43 webview.executeScript({ | |
44 code: 'document.body.style.backgroundColor;' | |
45 }, onGetBackgroundExecuted); | |
46 }; | |
47 | |
48 var onLoadStop = function() { | |
49 webview.executeScript({ | |
50 file: 'test/webview_execute_script.js' | |
51 }, onSetBackgroundExecuted); | |
52 }; | |
53 | |
54 webview.addEventListener('loadstop', onLoadStop); | |
55 webview.src = url; | |
56 } | |
57 | |
58 // This test verifies that a content script will be injected to the webview when | |
59 // the webview is navigated to a page that matches the URL pattern defined in | |
60 // the content sript. | |
61 function testAddContentScript(url) { | |
62 var webview = document.createElement('webview'); | |
63 | |
64 console.log('Step 1: call <webview>.addContentScripts.'); | |
65 webview.addContentScripts( | |
66 [{"name": 'myrule', | |
67 "matches": ['http://*/empty*'], | |
68 "js": ['test/inject_comm_channel.js', 'test/inject_comm_channel_2.js'], | |
69 "run_at": 'document_start'}]); | |
70 | |
71 webview.addEventListener('loadstop', function() { | |
72 console.log('Step 2: postMessage to build connection.'); | |
73 var msg = [request_to_comm_channel_1]; | |
74 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
75 }); | |
76 | |
77 window.addEventListener('message', function(e) { | |
78 var data = JSON.parse(e.data); | |
79 if (data == response_from_comm_channel_1) { | |
80 console.log( | |
81 'Step 3: A communication channel has been established with webview.'); | |
82 chrome.send('testResult', [true]); | |
83 return; | |
84 } | |
85 console.log('Unexpected message: \'' + data[0] + '\''); | |
86 chrome.send('testResult', [false]); | |
87 }); | |
88 | |
89 webview.src = url; | |
90 document.body.appendChild(webview); | |
91 } | |
92 | |
93 // Adds two content scripts with the same URL pattern to <webview> at the same | |
94 // time. This test verifies that both scripts are injected when the <webview> | |
95 // navigates to a URL that matches the URL pattern. | |
96 function testAddMultiContentScripts(url) { | |
97 var webview = document.createElement('webview'); | |
98 | |
99 console.log('Step 1: call <webview>.addContentScripts(myrule1 & myrule2)'); | |
100 webview.addContentScripts( | |
101 [{"name": 'myrule1', | |
102 "matches": ['http://*/empty*'], | |
103 "js": ['test/inject_comm_channel.js'], | |
104 "run_at": 'document_start'}, | |
105 {"name": 'myrule2', | |
106 "matches": ['http://*/empty*'], | |
107 "js": ['test/inject_comm_channel_2.js'], | |
108 "run_at": 'document_start'}]); | |
109 | |
110 webview.addEventListener('loadstop', function() { | |
111 console.log('Step 2: postMessage to build connection.'); | |
112 var msg1 = [request_to_comm_channel_1]; | |
113 webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); | |
114 console.log('Step 3: postMessage to build connection to the other script.'); | |
115 var msg2 = [request_to_comm_channel_2]; | |
116 webview.contentWindow.postMessage(JSON.stringify(msg2), '*'); | |
117 }); | |
118 | |
119 var response_1 = false; | |
120 var response_2 = false; | |
121 window.addEventListener('message', function(e) { | |
122 var data = JSON.parse(e.data); | |
123 if (data == response_from_comm_channel_1) { | |
124 console.log( | |
125 'Step 4: A communication channel has been established with webview.'); | |
126 response_1 = true; | |
127 if (response_1 && response_2) | |
128 chrome.send('testResult', [true]); | |
129 return; | |
130 } else if (data == response_from_comm_channel_2) { | |
131 console.log( | |
132 'Step 5: A communication channel has been established with webview.'); | |
133 response_2 = true; | |
134 if (response_1 && response_2) | |
135 chrome.send('testResult', [true]); | |
136 return; | |
137 } | |
138 console.log('Unexpected message: \'' + data[0] + '\''); | |
139 chrome.send('testResult', [false]); | |
140 }); | |
141 | |
142 webview.src = url; | |
143 document.body.appendChild(webview); | |
144 } | |
145 | |
146 // Adds a content script to <webview> and navigates. After seeing the script is | |
147 // injected, we add another content script with the same name to the <webview>. | |
148 // This test verifies that the second script will replace the first one and be | |
149 // injected after navigating the <webview>. Meanwhile, the <webview> shouldn't | |
150 // get any message from the first script anymore. | |
151 function testAddContentScriptWithSameNameShouldOverwriteTheExistingOne(url) { | |
152 var webview = document.createElement('webview'); | |
153 | |
154 console.log('Step 1: call <webview>.addContentScripts(myrule1)'); | |
155 webview.addContentScripts( | |
156 [{"name": 'myrule1', | |
157 "matches": ['http://*/empty*'], | |
158 "js": ['test/inject_comm_channel.js'], | |
159 "run_at": 'document_start'}]); | |
160 var connect_script_1 = true; | |
161 var connect_script_2 = false; | |
162 | |
163 webview.addEventListener('loadstop', function() { | |
164 if (connect_script_1) { | |
165 var msg1 = [request_to_comm_channel_1]; | |
166 webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); | |
167 connect_script_1 = false; | |
168 } | |
169 if (connect_script_2) { | |
170 var msg2 = [request_to_comm_channel_2]; | |
171 webview.contentWindow.postMessage(JSON.stringify(msg2), '*'); | |
172 connect_script_2 = false; | |
173 } | |
174 }); | |
175 | |
176 var should_get_response_from_script_1 = true; | |
177 window.addEventListener('message', function(e) { | |
178 var data = JSON.parse(e.data); | |
179 if (data == response_from_comm_channel_1) { | |
180 if (should_get_response_from_script_1) { | |
181 console.log( | |
182 'Step 2: A communication channel has been established with webview.' | |
183 ); | |
184 webview.addContentScripts( | |
185 [{"name": 'myrule1', | |
186 "matches": ['http://*/empty*'], | |
187 "js": ['test/inject_comm_channel_2.js'], | |
188 "run_at": 'document_start'}]); | |
189 connect_script_2 = true; | |
190 should_get_response_from_script_1 = false; | |
191 webview.src = url; | |
192 } else { | |
193 chrome.send('testResult', [false]); | |
194 } | |
195 return; | |
196 } else if (data == response_from_comm_channel_2) { | |
197 console.log( | |
198 'Step 3: Another communication channel has been established ' + | |
199 'with webview.'); | |
200 setTimeout(function() { | |
201 chrome.send('testResult', [true]); | |
202 }, 0); | |
203 return; | |
204 } | |
205 console.log('Unexpected message: \'' + data[0] + '\''); | |
206 chrome.send('testResult', [false]); | |
207 }); | |
208 | |
209 webview.src = url; | |
210 document.body.appendChild(webview); | |
211 } | |
212 | |
213 // There are two <webview>s are added to the DOM, and we add a content script | |
214 // to one of them. This test verifies that the script won't be injected in | |
215 // the other <webview>. | |
216 function testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView(url) { | |
217 var webview1 = document.createElement('webview'); | |
218 var webview2 = document.createElement('webview'); | |
219 | |
220 console.log('Step 1: call <webview1>.addContentScripts.'); | |
221 webview1.addContentScripts( | |
222 [{"name": 'myrule', | |
223 "matches": ['http://*/empty*'], | |
224 "js": ['test/inject_comm_channel.js'], | |
225 "run_at": 'document_start'}]); | |
226 | |
227 webview2.addEventListener('loadstop', function() { | |
228 console.log('Step 2: webview2 requests to build communication channel.'); | |
229 var msg = [request_to_comm_channel_1]; | |
230 webview2.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
231 setTimeout(function() { | |
232 chrome.send('testResult', [true]); | |
233 }, 0); | |
234 }); | |
235 | |
236 window.addEventListener('message', function(e) { | |
237 var data = JSON.parse(e.data); | |
238 if (data == response_from_comm_channel_1) { | |
239 chrome.send('testResult', [false]); | |
240 return; | |
241 } | |
242 console.log('Unexpected message: \'' + data[0] + '\''); | |
243 chrome.send('testResult', [false]); | |
244 }); | |
245 | |
246 webview1.src = url; | |
247 webview2.src = url; | |
248 document.body.appendChild(webview1); | |
249 document.body.appendChild(webview2); | |
250 } | |
251 | |
252 // Adds a content script to <webview> and navigates to a URL that matches the | |
253 // URL pattern defined in the script. After the first navigation, we remove this | |
254 // script from the <webview> and navigates to the same URL. This test verifies | |
255 // taht the script is injected during the first navigation, but isn't injected | |
256 // after removing it. | |
257 function testAddAndRemoveContentScripts(url) { | |
258 var webview = document.createElement('webview'); | |
259 | |
260 console.log('Step 1: call <webview>.addContentScripts.'); | |
261 webview.addContentScripts( | |
262 [{"name": 'myrule', | |
263 "matches": ['http://*/empty*'], | |
264 "js": ['test/inject_comm_channel.js'], | |
265 "run_at": 'document_start'}]); | |
266 | |
267 var count = 0; | |
268 webview.addEventListener('loadstop', function() { | |
269 if (count == 0) { | |
270 console.log('Step 2: post message to build connect.'); | |
271 var msg = [request_to_comm_channel_1]; | |
272 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
273 ++count; | |
274 } else if (count == 1) { | |
275 console.log( | |
276 'Step 4: call <webview>.removeContentScripts and navigate.'); | |
277 webview.removeContentScripts(); | |
278 webview.src = url; | |
279 ++count; | |
280 } else if (count == 2) { | |
281 console.log('Step 5: post message to build connect again.'); | |
282 var msg = [request_to_comm_channel_1]; | |
283 webview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
284 setTimeout(function() { | |
285 chrome.send('testResult', [true]); | |
286 }, 0); | |
287 } | |
288 }); | |
289 | |
290 var replyCount = 0; | |
291 window.addEventListener('message', function(e) { | |
292 var data = JSON.parse(e.data); | |
293 if (data[0] == response_from_comm_channel_1) { | |
294 console.log( | |
295 'Step 3: A communication channel has been established with webview.'); | |
296 if (replyCount == 0) { | |
297 webview.setAttribute('src', 'about:blank'); | |
298 ++replyCount; | |
299 return; | |
300 } else if (replyCount == 1) { | |
301 chrome.send('testResult', [false]); | |
302 return; | |
303 } | |
304 } | |
305 console.log('Unexpected message: \'' + data[0] + '\''); | |
306 chrome.send('testResult', [false]); | |
307 }); | |
308 | |
309 webview.src = url; | |
310 document.body.appendChild(webview); | |
311 } | |
312 | |
313 // This test verifies that the addContentScripts API works with the new window | |
314 // API. | |
315 function testAddContentScriptsWithNewWindowAPI(url) { | |
316 var webview = document.createElement('webview'); | |
317 | |
318 var newwebview; | |
319 webview.addEventListener('newwindow', function(e) { | |
320 e.preventDefault(); | |
321 newwebview = document.createElement('webview'); | |
322 | |
323 console.log('Step 2: call newwebview.addContentScripts.'); | |
324 newwebview.addContentScripts( | |
325 [{"name": 'myrule', | |
326 "matches": ['http://*/guest_from_opener*'], | |
327 "js": ['test/inject_comm_channel.js'], | |
328 "run_at": 'document_start'}]); | |
329 | |
330 newwebview.addEventListener('loadstop', function(evt) { | |
331 var msg = [request_to_comm_channel_1]; | |
332 console.log('Step 4: new webview postmessage to build communication ' + | |
333 'channel.'); | |
334 newwebview.contentWindow.postMessage(JSON.stringify(msg), '*'); | |
335 }); | |
336 | |
337 document.body.appendChild(newwebview); | |
338 // attach the new window to the new <webview>. | |
339 console.log('Step 3: attaches the new webview.'); | |
340 e.window.attach(newwebview); | |
341 }); | |
342 | |
343 window.addEventListener('message', function(e) { | |
344 var data = JSON.parse(e.data); | |
345 if (data == response_from_comm_channel_1 && | |
346 e.source == newwebview.contentWindow) { | |
347 console.log('Step 5: a communication channel has been established ' + | |
348 'with the new webview.'); | |
349 chrome.send('testResult', [true]); | |
350 return; | |
351 } else { | |
352 chrome.send('testResult', [false]); | |
353 return; | |
354 } | |
355 console.log('Unexpected message: \'' + data[0] + '\''); | |
356 chrome.send('testResult', [false]); | |
357 }); | |
358 | |
359 console.log('Step 1: navigates the webview to window open guest URL.'); | |
360 webview.setAttribute('src', url); | |
361 document.body.appendChild(webview); | |
362 } | |
363 | |
364 // Adds a content script to <webview>. This test verifies that the script is | |
365 // injected after terminate and reload <webview>. | |
366 function testContentScriptIsInjectedAfterTerminateAndReloadWebView(url) { | |
367 var webview = document.createElement('webview'); | |
368 | |
369 console.log('Step 1: call <webview>.addContentScripts.'); | |
370 webview.addContentScripts( | |
371 [{"name": 'myrule', | |
372 "matches": ['http://*/empty*'], | |
373 "js": ['test/webview_execute_script.js'], | |
374 "run_at": 'document_end'}]); | |
375 | |
376 var count = 0; | |
377 webview.addEventListener('loadstop', function() { | |
378 if (count == 0) { | |
379 console.log('Step 2: call webview.terminate().'); | |
380 webview.terminate(); | |
381 ++count; | |
382 return; | |
383 } else if (count == 1) { | |
384 console.log('Step 4: call <webview>.executeScript to check result.'); | |
385 webview.executeScript({ | |
386 code: 'document.body.style.backgroundColor;' | |
387 }, onGetBackgroundExecuted); | |
388 } | |
389 }); | |
390 | |
391 webview.addEventListener('exit', function() { | |
392 console.log('Step 3: call webview.reload().'); | |
393 webview.reload(); | |
394 }); | |
395 | |
396 webview.src = url; | |
397 document.body.appendChild(webview); | |
398 } | |
399 | |
400 // This test verifies the content script won't be removed when the guest is | |
401 // destroyed, i.e., removed <webview> from the DOM. | |
402 function testContentScriptExistsAsLongAsWebViewTagExists(url) { | |
403 var webview = document.createElement('webview'); | |
404 | |
405 console.log('Step 1: call <webview>.addContentScripts.'); | |
406 webview.addContentScripts( | |
407 [{"name": 'myrule', | |
408 "matches": ["http://*/empty*"], | |
409 "js": ["test/webview_execute_script.js"], | |
410 "run_at": "document_end"}]); | |
411 | |
412 var count = 0; | |
413 webview.addEventListener('loadstop', function() { | |
414 if (count == 0) { | |
415 console.log('Step 2: check the result of content script injected.'); | |
416 webview.executeScript({ | |
417 code: 'document.body.style.backgroundColor;' | |
418 }, function(results) { | |
419 assertEquals(1, results.length); | |
420 assertEquals('red', results[0]); | |
421 | |
422 console.log('Step 3: remove webview from the DOM.'); | |
423 document.body.removeChild(webview); | |
424 console.log('Step 4: add webview back to the DOM.'); | |
425 document.body.appendChild(webview); | |
426 ++count; | |
427 }); | |
428 } else if (count == 1) { | |
429 console.log('Step 5: check the result of content script injected again.'); | |
430 webview.executeScript({ | |
431 code: 'document.body.style.backgroundColor;' | |
432 }, onGetBackgroundExecuted); | |
433 } | |
434 }); | |
435 | |
436 webview.src = url; | |
437 document.body.appendChild(webview); | |
438 } | |
OLD | NEW |