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

Unified Diff: chrome/test/data/extensions/platform_apps/web_view/shim/main.js

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up in web_view_internal_api.cc. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/extensions/platform_apps/web_view/shim/main.js
diff --git a/chrome/test/data/extensions/platform_apps/web_view/shim/main.js b/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
index 1ef5637d869eca9ceada3d7b371cc770e1000795..9f86a2bbff6a422a666b40a7cf94a9fcd2b575fa 100644
--- a/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
+++ b/chrome/test/data/extensions/platform_apps/web_view/shim/main.js
@@ -13,6 +13,11 @@ embedder.redirectGuestURLDest = '';
embedder.closeSocketURL = '';
embedder.tests = {};
+var request_to_comm_channel_1 = 'connect';
+var request_to_comm_channel_2 = 'connect_request';
+var response_from_comm_channel_1 = 'connected';
+var response_from_comm_channel_2 = 'connected_response';
+
embedder.setUp_ = function(config) {
if (!config || !config.testServer) {
return;
@@ -22,6 +27,8 @@ embedder.setUp_ = function(config) {
'/extensions/platform_apps/web_view/shim/empty_guest.html';
embedder.windowOpenGuestURL = embedder.baseGuestURL +
'/extensions/platform_apps/web_view/shim/guest.html';
+ embedder.windowOpenGuestFromSameURL = embedder.baseGuestURL +
+ '/extensions/platform_apps/web_view/shim/guest_from_opener.html';
embedder.noReferrerGuestURL = embedder.baseGuestURL +
'/extensions/platform_apps/web_view/shim/guest_noreferrer.html';
embedder.detectUserAgentURL = embedder.baseGuestURL + '/detect-user-agent';
@@ -749,6 +756,404 @@ function testPartitionRemovalAfterNavigationFails() {
webview.setAttribute('src', 'data:text/html,<html><body>guest</body></html>');
}
+// This test verifies that a content script will be injected to the webview when
+// the webview is navigated to a page that matches the URL pattern defined in
+// the content sript.
+function testAddContentScript() {
+ var webview = document.createElement('webview');
+
+ console.log("Step 1: call <webview>.addContentScripts.");
+ webview.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+
+ webview.addEventListener('loadstop', function() {
+ var msg = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*');
+ });
+
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1) {
+ console.log(
+ 'Step 2: A communication channel has been established with webview.');
+ embedder.test.succeed();
+ return;
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
+// Adds two content scripts with the same URL pattern to <webview> at the same
+// time. This test verifies that both scripts are injected when the <webview>
+// navigates to a URL that matches the URL pattern.
+function testAddMultipleContentScripts() {
+ var webview = document.createElement('webview');
+
+ console.log("Step 1: call <webview>.addContentScripts(myrule1 & myrule2)");
+ webview.addContentScripts(
+ [{"name": 'myrule1',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"},
+ {"name": 'myrule2',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel_2.js"],
+ "run_at": "document_start"}]);
+
+ webview.addEventListener('loadstop', function() {
+ var msg1 = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg1), '*');
+ var msg2 = [request_to_comm_channel_2];
+ webview.contentWindow.postMessage(JSON.stringify(msg2), '*');
+ });
+
+ var response_1 = false;
+ var response_2 = false;
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1) {
+ console.log(
+ 'Step 2: A communication channel has been established with webview.');
+ response_1 = true;
+ if (response_1 && response_2)
+ embedder.test.succeed();
+ return;
+ } else if (data == response_from_comm_channel_2) {
+ console.log(
+ 'Step 3: A communication channel has been established with webview.');
+ response_2 = true;
+ if (response_1 && response_2)
+ embedder.test.succeed();
+ return;
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
+// Adds a content script to <webview> and navigates. After seeing the script is
+// injected, we add another content script with the same name to the <webview>.
+// This test verifies that the second script will replace the first one and be
+// injected after navigating the <webview>. Meanwhile, the <webview> shouldn't
+// get any message from the first script anymore.
+function testAddContentScriptWithSameNameShouldOverwriteTheExistingOne() {
+ var webview = document.createElement('webview');
+
+ console.log("Step 1: call <webview>.addContentScripts(myrule1)");
+ webview.addContentScripts(
+ [{"name": 'myrule1',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+ var connect_script_1 = true;
+ var connect_script_2 = false;
+
+ webview.addEventListener('loadstop', function() {
+ if (connect_script_1) {
+ var msg1 = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg1), '*');
+ connect_script_1 = false;
+ }
+ if (connect_script_2) {
+ var msg2 = [request_to_comm_channel_2];
+ webview.contentWindow.postMessage(JSON.stringify(msg2), '*');
+ connect_script_2 = false;
+ }
+ });
+
+ var should_get_response_from_script_1 = true;
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1) {
+ if (should_get_response_from_script_1) {
+ console.log(
+ 'Step 2: A communication channel has been established with webview.'
+ );
+ webview.addContentScripts(
+ [{"name": 'myrule1',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel_2.js"],
+ "run_at": "document_start"}]);
+ connect_script_2 = true;
+ should_get_response_from_script_1 = false;
+ webview.src = embedder.emptyGuestURL;
+ } else {
+ embedder.test.fail();
+ }
+ return;
+ } else if (data == response_from_comm_channel_2) {
+ console.log(
+ 'Step 3: Another communication channel has been established ' +
+ 'with webview.');
+ setTimeout(function() {
+ embedder.test.succeed();
+ }, 0);
+ return;
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
+// There are two <webview>s are added to the DOM, and we add a content script
+// to one of them. This test verifies that the script won't be injected in
+// the other <webview>.
+function testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView() {
+ var webview1 = document.createElement('webview');
+ var webview2 = document.createElement('webview');
+
+ console.log("Step 1: call <webview1>.addContentScripts.");
+ webview1.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+
+ webview2.addEventListener('loadstop', function() {
+ console.log("Step 2: webview2 requests to build communication channel.");
+ var msg = [request_to_comm_channel_1];
+ webview2.contentWindow.postMessage(JSON.stringify(msg), '*');
+ setTimeout(function() {
+ embedder.test.succeed();
+ }, 0);
+ });
+
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1) {
+ embedder.test.fail();
+ return;
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview1.src = embedder.emptyGuestURL;
+ webview2.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview1);
+ document.body.appendChild(webview2);
+}
+
+
+// Adds a content script to <webview> and navigates to a URL that matches the
+// URL pattern defined in the script. After the first navigation, we remove this
+// script from the <webview> and navigates to the same URL. This test verifies
+// taht the script is injected during the first navigation, but isn't injected
+// after removing it.
+function testAddAndRemoveContentScripts() {
+ var webview = document.createElement('webview');
+
+ console.log("Step 1: call <webview>.addContentScripts.");
+ webview.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+
+ var count = 0;
+ webview.addEventListener('loadstop', function() {
+ if (count == 0) {
+ console.log('Step 2: post message to build connect.');
+ var msg = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*');
+ ++count;
+ } else if (count == 1) {
+ console.log(
+ 'Step 4: call <webview>.removeContentScripts and navigate.');
+ webview.removeContentScripts();
+ webview.src = embedder.emptyGuestURL;
+ ++count;
+ } else if (count == 2) {
+ console.log('Step 5: post message to build connect again.');
+ var msg = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*');
+ setTimeout(function() {
+ embedder.test.succeed();
+ }, 0);
+ }
+ });
+
+ var replyCount = 0;
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data[0] == response_from_comm_channel_1) {
+ console.log(
+ 'Step 3: A communication channel has been established with webview.');
+ if (replyCount == 0) {
+ webview.setAttribute('src', 'about:blank');
+ ++replyCount;
+ return;
+ } else if (replyCount == 1) {
+ embedder.test.fail();
+ return;
+ }
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
+// This test verifies that the addContentScripts API works with the new window
+// API.
+function testAddContentScriptsWithNewWindowAPI() {
+ var webview = document.createElement('webview');
+
+ var newwebview;
+ webview.addEventListener('newwindow', function(e) {
+ e.preventDefault();
+ newwebview = document.createElement('webview');
+
+ console.log('Step 2: call newwebview.addContentScripts.');
+ newwebview.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+
+ newwebview.addEventListener('loadstop', function(evt) {
+ var msg = [request_to_comm_channel_1];
+ console.log('Step 4: new webview postmessage to build communication ' +
+ 'channel.');
+ newwebview.contentWindow.postMessage(JSON.stringify(msg), '*');
+ });
+
+ document.body.appendChild(newwebview);
+ // attach the new window to the new <webview>.
+ console.log("Step 3: attaches the new webview.");
+ e.window.attach(newwebview);
+ });
+
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1 &&
+ e.source == newwebview.contentWindow) {
+ console.log('Step 5: a communication channel has been established ' +
+ 'with the new webview.');
+ embedder.test.succeed();
+ return;
+ } else {
+ embedder.test.fail();
+ return;
+ }
+ console.log('unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ console.log('Step 1: navigates the webview to window open guest URL.');
+ webview.setAttribute('src', embedder.windowOpenGuestFromSameURL);
+ document.body.appendChild(webview);
+}
+
+// Adds a content script to <webview>. This test verifies that the script is
+// injected after terminate and reload <webview>.
+function testContentScriptIsInjectedAfterTerminateAndReloadWebView() {
+ var webview = document.createElement('webview');
+
+ console.log('Step 1: call <webview>.addContentScripts.');
+ webview.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["inject_comm_channel.js"],
+ "run_at": "document_start"}]);
+
+ var count = 0;
+ webview.addEventListener('loadstop', function() {
+ if (count == 0) {
+ console.log('Step 2: call webview.terminate().');
+ webview.terminate();
+ ++count;
+ return;
+ } else if (count == 1) {
+ console.log('Step 4: postMessage to build communication.');
+ var msg = [request_to_comm_channel_1];
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*');
+ ++count;
+ }
+ });
+
+ webview.addEventListener('exit', function() {
+ console.log('Step 3: call webview.reload().');
+ webview.reload();
+ });
+
+ window.addEventListener('message', function(e) {
+ var data = JSON.parse(e.data);
+ if (data == response_from_comm_channel_1) {
+ console.log(
+ 'Step 5: A communication channel has been established with webview.');
+ embedder.test.succeed();
+ return;
+ }
+ console.log('Unexpected message: \'' + data[0] + '\'');
+ embedder.test.fail();
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
+// This test verifies the content script won't be removed when the guest is
+// destroyed, i.e., removed <webview> from the DOM.
+function testContentScriptExistsAsLongAsWebViewTagExists() {
+ var webview = document.createElement('webview');
+
+ console.log('Step 1: call <webview>.addContentScripts.');
+ webview.addContentScripts(
+ [{"name": 'myrule',
+ "matches": ["http://*/extensions/*"],
+ "js": ["simple_script.js"],
+ "run_at": "document_end"}]);
+
+ var count = 0;
+ webview.addEventListener('loadstop', function() {
+ if (count == 0) {
+ console.log('Step 2: check the result of content script injected.');
+ webview.executeScript({
+ code: 'document.body.style.backgroundColor;'
+ }, function(results) {
+ embedder.test.assertEq(1, results.length);
+ embedder.test.assertEq('red', results[0]);
+ });
+
+ console.log('Step 3: remove webview from the DOM.');
+ document.body.removeChild(webview);
+
+ console.log('Step 4: add webview back to the DOM.');
+ document.body.appendChild(webview);
+ ++count;
+ } else if (count == 1) {
+ webview.executeScript({
+ code: 'document.body.style.backgroundColor;'
+ }, function(results) {
+ console.log('Step 5: check the result of content script injected' +
+ ' again.');
+ embedder.test.assertEq(1, results.length);
+ embedder.test.assertEq('red', results[0]);
+ embedder.test.succeed();
+ });
+ }
+ });
+
+ webview.src = embedder.emptyGuestURL;
+ document.body.appendChild(webview);
+}
+
function testExecuteScriptFail() {
var webview = document.createElement('webview');
document.body.appendChild(webview);
@@ -2229,6 +2634,19 @@ embedder.test.testList = {
'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation,
'testPartitionRemovalAfterNavigationFails':
testPartitionRemovalAfterNavigationFails,
+ 'testAddContentScript': testAddContentScript,
+ 'testAddMultipleContentScripts': testAddMultipleContentScripts,
+ 'testAddContentScriptWithSameNameShouldOverwriteTheExistingOne':
+ testAddContentScriptWithSameNameShouldOverwriteTheExistingOne,
+ 'testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView':
+ testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView,
+ 'testAddAndRemoveContentScripts': testAddAndRemoveContentScripts,
+ 'testAddContentScriptsWithNewWindowAPI':
+ testAddContentScriptsWithNewWindowAPI,
+ 'testContentScriptIsInjectedAfterTerminateAndReloadWebView':
+ testContentScriptIsInjectedAfterTerminateAndReloadWebView,
+ 'testContentScriptExistsAsLongAsWebViewTagExists':
+ testContentScriptExistsAsLongAsWebViewTagExists,
'testExecuteScriptFail': testExecuteScriptFail,
'testExecuteScript': testExecuteScript,
'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged':

Powered by Google App Engine
This is Rietveld 408576698