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 31d1c37ffd0a7bf020f2b00eb9c6700df50f7e15..c2da8974c14f0eeafd121b6196bea20eb56a2017 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 |
@@ -749,6 +749,239 @@ function testPartitionRemovalAfterNavigationFails() { |
webview.setAttribute('src', 'data:text/html,<html><body>guest</body></html>'); |
} |
+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 = ['connect']; |
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
+ }); |
+ |
+ window.addEventListener('message', function(e) { |
+ var data = JSON.parse(e.data); |
+ if (data == 'connected') { |
+ 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); |
+} |
+ |
+function testAddMultiContentScripts() { |
+ 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 = ['connect']; |
+ webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); |
+ var msg2 = ['connect_request']; |
+ 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 == 'connected') { |
+ 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 == 'connected_response') { |
+ 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); |
+} |
+ |
+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 = ['connect']; |
+ webview.contentWindow.postMessage(JSON.stringify(msg1), '*'); |
+ connect_script_1 = false; |
+ } |
+ if (connect_script_2) { |
+ var msg2 = ['connect_request']; |
+ 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 == 'connected') { |
+ 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 == 'connected_response') { |
+ console.log( |
+ 'Step 3: Another communication channel has been established ' + |
+ 'with webview.'); |
+ setTimeout(function() { |
+ embedder.test.succeed(); |
+ }, 1000); |
+ return; |
+ } |
+ console.log('Unexpected message: \'' + data[0] + '\''); |
+ embedder.test.fail(); |
+ }); |
+ |
+ webview.src = embedder.emptyGuestURL; |
+ document.body.appendChild(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 = ['connect']; |
+ webview2.contentWindow.postMessage(JSON.stringify(msg), '*'); |
+ setTimeout(function() { |
+ embedder.test.succeed(); |
+ }, 2000); |
+ }); |
+ |
+ window.addEventListener('message', function(e) { |
+ var data = JSON.parse(e.data); |
+ if (data == 'connected') { |
+ 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); |
+} |
+ |
+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 = ['connect']; |
+ 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 = ['connect']; |
+ webview.contentWindow.postMessage(JSON.stringify(msg), '*'); |
+ setTimeout(function() { |
+ embedder.test.succeed(); |
+ }, 2000); |
+ } |
+ }); |
+ |
+ var replyCount = 0; |
+ window.addEventListener('message', function(e) { |
+ var data = JSON.parse(e.data); |
+ if (data[0] == 'connected') { |
+ 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); |
+} |
+ |
function testExecuteScriptFail() { |
var webview = document.createElement('webview'); |
document.body.appendChild(webview); |
@@ -2128,6 +2361,13 @@ embedder.test.testList = { |
'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation, |
'testPartitionRemovalAfterNavigationFails': |
testPartitionRemovalAfterNavigationFails, |
+ 'testAddContentScript': testAddContentScript, |
+ 'testAddMultiContentScripts': testAddMultiContentScripts, |
+ 'testAddContentScriptWithSameNameShouldOverwriteTheExistingOne': |
+ testAddContentScriptWithSameNameShouldOverwriteTheExistingOne, |
+ 'testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView': |
+ testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView, |
+ 'testAddAndRemoveContentScripts': testAddAndRemoveContentScripts, |
'testExecuteScriptFail': testExecuteScriptFail, |
'testExecuteScript': testExecuteScript, |
'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged': |