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

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 sync_IPC handled in IO thread. 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 (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 testAddContentScriptWithSameNameShouldOverwriteTheExistingOne() {
832 var webview = document.createElement('webview');
833
834 console.log("Step 1: call <webview>.addContentScripts(myrule1)");
835 webview.addContentScripts(
836 [{"name": 'myrule1',
837 "matches": ["http://*/extensions/*"],
838 "js": ["inject_comm_channel.js"],
839 "run_at": "document_start"}]);
840 var connect_script_1 = true;
841 var connect_script_2 = false;
842
843 webview.addEventListener('loadstop', function() {
844 if (connect_script_1) {
845 var msg1 = ['connect'];
846 webview.contentWindow.postMessage(JSON.stringify(msg1), '*');
847 connect_script_1 = false;
848 }
849 if (connect_script_2) {
850 var msg2 = ['connect_request'];
851 webview.contentWindow.postMessage(JSON.stringify(msg2), '*');
852 connect_script_2 = false;
853 }
854 });
855
856 var should_get_response_from_script_1 = true;
857 window.addEventListener('message', function(e) {
858 var data = JSON.parse(e.data);
859 if (data == 'connected') {
860 if (should_get_response_from_script_1) {
861 console.log(
862 'Step 2: A communication channel has been established with webview.'
863 );
864 webview.addContentScripts(
865 [{"name": 'myrule1',
866 "matches": ["http://*/extensions/*"],
867 "js": ["inject_comm_channel_2.js"],
868 "run_at": "document_start"}]);
869 connect_script_2 = true;
870 should_get_response_from_script_1 = false;
871 webview.src = embedder.emptyGuestURL;
872 } else {
873 embedder.test.fail();
874 }
875 return;
876 } else if (data == 'connected_response') {
877 console.log(
878 'Step 3: Another communication channel has been established ' +
879 'with webview.');
880 setTimeout(function() {
881 embedder.test.succeed();
882 }, 1000);
883 return;
884 }
885 console.log('Unexpected message: \'' + data[0] + '\'');
886 embedder.test.fail();
887 });
888
889 webview.src = embedder.emptyGuestURL;
890 document.body.appendChild(webview);
891 }
892
893 function testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView() {
894 var webview1 = document.createElement('webview');
895 var webview2 = document.createElement('webview');
896
897 console.log("Step 1: call <webview1>.addContentScripts.");
898 webview1.addContentScripts(
899 [{"name": 'myrule',
900 "matches": ["http://*/extensions/*"],
901 "js": ["inject_comm_channel.js"],
902 "run_at": "document_start"}]);
903
904 webview2.addEventListener('loadstop', function() {
905 console.log("Step 2: webview2 requests to build communication channel.");
906 var msg = ['connect'];
907 webview2.contentWindow.postMessage(JSON.stringify(msg), '*');
908 setTimeout(function() {
909 embedder.test.succeed();
910 }, 2000);
911 });
912
913 window.addEventListener('message', function(e) {
914 var data = JSON.parse(e.data);
915 if (data == 'connected') {
916 embedder.test.fail();
917 return;
918 }
919 console.log('Unexpected message: \'' + data[0] + '\'');
920 embedder.test.fail();
921 });
922
923 webview1.src = embedder.emptyGuestURL;
924 webview2.src = embedder.emptyGuestURL;
925 document.body.appendChild(webview1);
926 document.body.appendChild(webview2);
927 }
928
929 function testAddAndRemoveContentScripts() {
930 var webview = document.createElement('webview');
931
932 console.log("Step 1: call <webview>.addContentScripts.");
933 webview.addContentScripts(
934 [{"name": 'myrule',
935 "matches": ["http://*/extensions/*"],
936 "js": ["inject_comm_channel.js"],
937 "run_at": "document_start"}]);
938
939 var count = 0;
940 webview.addEventListener('loadstop', function() {
941 if (count == 0) {
942 console.log('Step 2: post message to build connect.');
943 var msg = ['connect'];
944 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
945 count++;
946 } else if (count == 1) {
947 console.log(
948 'Step 4: call <webview>.removeContentScripts and navigate.');
949 webview.removeContentScripts();
950 webview.src = embedder.emptyGuestURL;
951 count++;
952 } else if (count == 2) {
953 console.log('Step 5: post message to build connect again.');
954 var msg = ['connect'];
955 webview.contentWindow.postMessage(JSON.stringify(msg), '*');
956 setTimeout(function() {
957 embedder.test.succeed();
958 }, 2000);
959 }
960 });
961
962 var replyCount = 0;
963 window.addEventListener('message', function(e) {
964 var data = JSON.parse(e.data);
965 if (data[0] == 'connected') {
966 console.log(
967 'Step 3: A communication channel has been established with webview.');
968 if (replyCount == 0) {
969 webview.setAttribute('src', 'about:blank');
970 replyCount++;
971 return;
972 } else if (replyCount == 1) {
973 embedder.test.fail();
974 return;
975 }
976 }
977 console.log('Unexpected message: \'' + data[0] + '\'');
978 embedder.test.fail();
979 });
980
981 webview.src = embedder.emptyGuestURL;
982 document.body.appendChild(webview);
983 }
984
752 function testExecuteScriptFail() { 985 function testExecuteScriptFail() {
753 var webview = document.createElement('webview'); 986 var webview = document.createElement('webview');
754 document.body.appendChild(webview); 987 document.body.appendChild(webview);
755 setTimeout(function() { 988 setTimeout(function() {
756 webview.executeScript( 989 webview.executeScript(
757 {code:'document.body.style.backgroundColor = "red";'}, 990 {code:'document.body.style.backgroundColor = "red";'},
758 function(results) { 991 function(results) {
759 embedder.test.fail(); 992 embedder.test.fail();
760 }); 993 });
761 setTimeout(function() { 994 setTimeout(function() {
(...skipping 1359 matching lines...) Expand 10 before | Expand all | Expand 10 after
2121 'testInvalidChromeExtensionURL': testInvalidChromeExtensionURL, 2354 'testInvalidChromeExtensionURL': testInvalidChromeExtensionURL,
2122 'testWebRequestAPIExistence': testWebRequestAPIExistence, 2355 'testWebRequestAPIExistence': testWebRequestAPIExistence,
2123 'testEventName': testEventName, 2356 'testEventName': testEventName,
2124 'testOnEventProperties': testOnEventProperties, 2357 'testOnEventProperties': testOnEventProperties,
2125 'testLoadProgressEvent': testLoadProgressEvent, 2358 'testLoadProgressEvent': testLoadProgressEvent,
2126 'testDestroyOnEventListener': testDestroyOnEventListener, 2359 'testDestroyOnEventListener': testDestroyOnEventListener,
2127 'testCannotMutateEventName': testCannotMutateEventName, 2360 'testCannotMutateEventName': testCannotMutateEventName,
2128 'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation, 2361 'testPartitionChangeAfterNavigation': testPartitionChangeAfterNavigation,
2129 'testPartitionRemovalAfterNavigationFails': 2362 'testPartitionRemovalAfterNavigationFails':
2130 testPartitionRemovalAfterNavigationFails, 2363 testPartitionRemovalAfterNavigationFails,
2364 'testAddContentScript': testAddContentScript,
2365 'testAddMultiContentScripts': testAddMultiContentScripts,
2366 'testAddContentScriptWithSameNameShouldOverwriteTheExistingOne':
2367 testAddContentScriptWithSameNameShouldOverwriteTheExistingOne,
2368 'testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView':
2369 testAddContentScriptToOneWebViewShouldNotInjectToTheOtherWebView,
2370 'testAddAndRemoveContentScripts': testAddAndRemoveContentScripts,
2131 'testExecuteScriptFail': testExecuteScriptFail, 2371 'testExecuteScriptFail': testExecuteScriptFail,
2132 'testExecuteScript': testExecuteScript, 2372 'testExecuteScript': testExecuteScript,
2133 'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged': 2373 'testExecuteScriptIsAbortedWhenWebViewSourceIsChanged':
2134 testExecuteScriptIsAbortedWhenWebViewSourceIsChanged, 2374 testExecuteScriptIsAbortedWhenWebViewSourceIsChanged,
2135 'testTerminateAfterExit': testTerminateAfterExit, 2375 'testTerminateAfterExit': testTerminateAfterExit,
2136 'testAssignSrcAfterCrash': testAssignSrcAfterCrash, 2376 'testAssignSrcAfterCrash': testAssignSrcAfterCrash,
2137 'testNavOnConsecutiveSrcAttributeChanges': 2377 'testNavOnConsecutiveSrcAttributeChanges':
2138 testNavOnConsecutiveSrcAttributeChanges, 2378 testNavOnConsecutiveSrcAttributeChanges,
2139 'testNavOnSrcAttributeChange': testNavOnSrcAttributeChange, 2379 'testNavOnSrcAttributeChange': testNavOnSrcAttributeChange,
2140 'testReassignSrcAttribute': testReassignSrcAttribute, 2380 'testReassignSrcAttribute': testReassignSrcAttribute,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 'testLoadDataAPI': testLoadDataAPI, 2424 'testLoadDataAPI': testLoadDataAPI,
2185 'testResizeEvents': testResizeEvents 2425 'testResizeEvents': testResizeEvents
2186 }; 2426 };
2187 2427
2188 onload = function() { 2428 onload = function() {
2189 chrome.test.getConfig(function(config) { 2429 chrome.test.getConfig(function(config) {
2190 embedder.setUp_(config); 2430 embedder.setUp_(config);
2191 chrome.test.sendMessage("Launched"); 2431 chrome.test.sendMessage("Launched");
2192 }); 2432 });
2193 }; 2433 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698