Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: chrome/test/data/webui/webview_execute_script_test.js

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

Powered by Google App Engine
This is Rietveld 408576698