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

Side by Side 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: Make the API work before the first navigation of guest. Created 5 years, 9 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 (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 util = {}; 5 var util = {};
6 var embedder = {}; 6 var embedder = {};
7 embedder.baseGuestURL = ''; 7 embedder.baseGuestURL = '';
8 embedder.emptyGuestURL = ''; 8 embedder.emptyGuestURL = '';
9 embedder.windowOpenGuestURL = ''; 9 embedder.windowOpenGuestURL = '';
10 embedder.noReferrerGuestURL = ''; 10 embedder.noReferrerGuestURL = '';
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 // Removing after navigation should not change the partition. 742 // Removing after navigation should not change the partition.
743 webview.removeAttribute('partition'); 743 webview.removeAttribute('partition');
744 embedder.test.assertEq('testme', webview.partition); 744 embedder.test.assertEq('testme', webview.partition);
745 embedder.test.succeed(); 745 embedder.test.succeed();
746 }; 746 };
747 webview.addEventListener('loadstop', loadstopHandler); 747 webview.addEventListener('loadstop', loadstopHandler);
748 748
749 webview.setAttribute('src', 'data:text/html,<html><body>guest</body></html>'); 749 webview.setAttribute('src', 'data:text/html,<html><body>guest</body></html>');
750 } 750 }
751 751
752 function testAddContentScript() {
753 var webview = document.createElement('webview');
754
755 console.log("Step 1: call <webview>.addContentScripts.");
756 webview.addContentScripts(
757 [{"name": 'myrule',
758 "matches": ["http://*/extensions/*"],
759 "js": ["inject_comm_channel.js"],
760 "run_at": "document_start"}]);
761
762 webview.addEventListener('loadstop', function() {
763 var msg = ['connect'];
764 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
765 });
766
767 window.addEventListener('message', function(e) {
768 var data = JSON.parse(e.data);
769 if (data == 'connected') {
770 console.log(
771 'Step 2: A communication channel has been established with webview.');
772 embedder.test.succeed();
773 return;
774 }
775 console.log('Unexpected message: \'' + data[0] + '\'');
776 embedder.test.fail();
777 });
778
779 webview.src = embedder.emptyGuestURL;
780 document.body.appendChild(webview);
781 }
782
783 function testAddMultiContentScripts() {
784 var webview = document.createElement('webview');
785
786 console.log("Step 1: call <webview>.addContentScripts(myrule1 & myrule2)");
787 webview.addContentScripts(
788 [{"name": 'myrule1',
789 "matches": ["http://*/extensions/*"],
790 "js": ["inject_comm_channel.js"],
791 "run_at": "document_start"},
792 {"name": 'myrule2',
793 "matches": ["http://*/extensions/*"],
794 "js": ["inject_comm_channel_2.js"],
795 "run_at": "document_start"}]);
796
797 webview.addEventListener('loadstop', function() {
798 var msg1 = ['connect'];
799 webview.contentWindow.postMessage(JSON.stringify(msg1), '*');
800 var msg2 = ['connect_request'];
801 webview.contentWindow.postMessage(JSON.stringify(msg2), '*');
802 });
803
804 var response_1 = false;
805 var response_2 = false;
806 window.addEventListener('message', function(e) {
807 var data = JSON.parse(e.data);
808 if (data == 'connected') {
809 console.log(
810 'Step 2: A communication channel has been established with webview.');
811 response_1 = true;
812 if (response_1 && response_2)
813 embedder.test.succeed();
814 return;
815 } else if (data == 'connected_response') {
816 console.log(
817 'Step 3: A communication channel has been established with webview.');
818 response_2 = true;
819 if (response_1 && response_2)
820 embedder.test.succeed();
821 return;
822 }
823 console.log('Unexpected message: \'' + data[0] + '\'');
824 embedder.test.fail();
825 });
826
827 webview.src = embedder.emptyGuestURL;
828 document.body.appendChild(webview);
829 }
830
831 function testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView() {
832 var webview1 = document.createElement('webview');
833 var webview2 = document.createElement('webview');
834
835 console.log("Step 1: call <webview1>.addContentScripts.");
836 webview1.addContentScripts(
837 [{"name": 'myrule',
838 "matches": ["http://*/extensions/*"],
839 "js": ["inject_comm_channel.js"],
840 "run_at": "document_start"}]);
841
842 webview2.addEventListener('loadstop', function() {
843 console.log("Step 2: webview2 requests to build communication channel.");
844 var msg = ['connect'];
845 webview2.contentWindow.postMessage(JSON.stringify(msg), '*');
846 setTimeout(function() {
847 embedder.test.succeed();
848 }, 2000);
849 });
850
851 window.addEventListener('message', function(e) {
852 var data = JSON.parse(e.data);
853 if (data == 'connected') {
854 embedder.test.fail();
855 return;
856 }
857 console.log('Unexpected message: \'' + data[0] + '\'');
858 embedder.test.fail();
859 });
860
861 webview1.src = embedder.emptyGuestURL;
862 webview2.src = embedder.emptyGuestURL;
863 document.body.appendChild(webview1);
864 document.body.appendChild(webview2);
865 }
866
867 function testAddAndRemoveContentScripts() {
868 var webview = document.createElement('webview');
869
870 console.log("Step 1: call <webview>.addContentScripts.");
871 webview.addContentScripts(
872 [{"name": 'myrule',
873 "matches": ["http://*/extensions/*"],
874 "js": ["inject_comm_channel.js"],
875 "run_at": "document_start"}]);
876
877 var count = 0;
878 webview.addEventListener('loadstop', function() {
879 if (count == 0) {
880 console.log('Step 2: post message to build connect.');
881 var msg = ['connect'];
882 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
883 count++;
884 } else if (count == 1) {
885 console.log(
886 'Step 4: call <webview>.removeContentScripts and navigate.');
887 webview.removeContentScripts();
888 webview.src = embedder.emptyGuestURL;
889 count++;
890 } else if (count == 2) {
891 console.log('Step 5: post message to build connect again.');
892 var msg = ['connect'];
893 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
894 setTimeout(function() {
895 embedder.test.succeed();
896 }, 2000);
897 }
898 });
899
900 var replyCount = 0;
901 window.addEventListener('message', function(e) {
902 var data = JSON.parse(e.data);
903 if (data[0] == 'connected') {
904 console.log(
905 'Step 3: A communication channel has been established with webview.');
906 if (replyCount == 0) {
907 webview.setAttribute('src', 'about:blank');
908 replyCount++;
909 return;
910 } else if (replyCount == 1) {
911 embedder.test.fail();
912 return;
913 }
914 }
915 console.log('Unexpected message: \'' + data[0] + '\'');
916 embedder.test.fail();
917 });
918
919 webview.src = embedder.emptyGuestURL;
920 document.body.appendChild(webview);
921 }
922
752 function testExecuteScriptFail() { 923 function testExecuteScriptFail() {
753 var webview = document.createElement('webview'); 924 var webview = document.createElement('webview');
754 document.body.appendChild(webview); 925 document.body.appendChild(webview);
755 setTimeout(function() { 926 setTimeout(function() {
756 webview.executeScript( 927 webview.executeScript(
757 {code:'document.body.style.backgroundColor = "red";'}, 928 {code:'document.body.style.backgroundColor = "red";'},
758 function(results) { 929 function(results) {
759 embedder.test.fail(); 930 embedder.test.fail();
760 }); 931 });
761 setTimeout(function() { 932 setTimeout(function() {
(...skipping 1359 matching lines...) Expand 10 before | Expand all | Expand 10 after
2121 'testInvalidChromeExtensionURL': testInvalidChromeExtensionURL, 2292 'testInvalidChromeExtensionURL': testInvalidChromeExtensionURL,
2122 'testWebRequestAPIExistence': testWebRequestAPIExistence, 2293 'testWebRequestAPIExistence': testWebRequestAPIExistence,
2123 'testEventName': testEventName, 2294 'testEventName': testEventName,
2124 'testOnEventProperties': testOnEventProperties, 2295 'testOnEventProperties': testOnEventProperties,
2125 'testLoadProgressEvent': testLoadProgressEvent, 2296 'testLoadProgressEvent': testLoadProgressEvent,
2126 'testDestroyOnEventListener': testDestroyOnEventListener, 2297 'testDestroyOnEventListener': testDestroyOnEventListener,
2127 'testCannotMutateEventName': testCannotMutateEventName, 2298 'testCannotMutateEventName': testCannotMutateEventName,
2128 'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation, 2299 'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation,
2129 'testPartitionRemovalAfterNavigationFails': 2300 'testPartitionRemovalAfterNavigationFails':
2130 testPartitionRemovalAfterNavigationFails, 2301 testPartitionRemovalAfterNavigationFails,
2302 'testAddContentScript': testAddContentScript,
2303 'testAddMultiContentScripts': testAddMultiContentScripts,
2304 'testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView':
2305 testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView,
2306 'testAddAndRemoveContentScripts': testAddAndRemoveContentScripts,
2131 'testExecuteScriptFail': testExecuteScriptFail, 2307 'testExecuteScriptFail': testExecuteScriptFail,
2132 'testExecuteScript': testExecuteScript, 2308 'testExecuteScript': testExecuteScript,
2133 'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged': 2309 'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged':
2134 testExecuteScriptIsAbortedWhenWebViewSourceIsChanged, 2310 testExecuteScriptIsAbortedWhenWebViewSourceIsChanged,
2135 'testTerminateAfterExit': testTerminateAfterExit, 2311 'testTerminateAfterExit': testTerminateAfterExit,
2136 'testAssignSrcAfterCrash': testAssignSrcAfterCrash, 2312 'testAssignSrcAfterCrash': testAssignSrcAfterCrash,
2137 'testNavOnConsecutiveSrcAttributeChanges': 2313 'testNavOnConsecutiveSrcAttributeChanges':
2138 testNavOnConsecutiveSrcAttributeChanges, 2314 testNavOnConsecutiveSrcAttributeChanges,
2139 'testNavOnSrcAttributeChange': testNavOnSrcAttributeChange, 2315 'testNavOnSrcAttributeChange': testNavOnSrcAttributeChange,
2140 'testReassignSrcAttribute': testReassignSrcAttribute, 2316 'testReassignSrcAttribute': testReassignSrcAttribute,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 'testLoadDataAPI': testLoadDataAPI, 2360 'testLoadDataAPI': testLoadDataAPI,
2185 'testResizeEvents': testResizeEvents 2361 'testResizeEvents': testResizeEvents
2186 }; 2362 };
2187 2363
2188 onload = function() { 2364 onload = function() {
2189 chrome.test.getConfig(function(config) { 2365 chrome.test.getConfig(function(config) {
2190 embedder.setUp_(config); 2366 embedder.setUp_(config);
2191 chrome.test.sendMessage("Launched"); 2367 chrome.test.sendMessage("Launched");
2192 }); 2368 });
2193 }; 2369 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698