browser_net_block-draganddrop.js (4821B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Test blocking and unblocking a request. 8 */ 9 10 add_task(async function () { 11 class DataTransfer { 12 constructor() { 13 this.BLOCKING_URL = 14 "https://example.com/browser/devtools/client/netmonitor/test/html_simple-test-page.html"; 15 this.getDataTrigger = false; 16 this.setDataTrigger = false; 17 this.data = ""; 18 } 19 20 setData(format, data) { 21 this.setDataTrigger = true; 22 Assert.strictEqual( 23 format, 24 "text/plain", 25 'setData passed valid "text/plain" format' 26 ); 27 Assert.strictEqual( 28 data, 29 this.BLOCKING_URL, 30 "Data matches the expected URL" 31 ); 32 this.data = data; 33 } 34 35 getData(format) { 36 this.getDataTrigger = true; 37 Assert.strictEqual( 38 format, 39 "text/plain", 40 'getData passed valid "text/plain" format' 41 ); 42 return this.data; 43 } 44 45 wasGetDataTriggered() { 46 return this.getDataTrigger; 47 } 48 49 wasSetDataTriggered() { 50 return this.setDataTrigger; 51 } 52 } 53 54 const dataTransfer = new DataTransfer(); 55 56 const { tab, monitor } = await initNetMonitor(HTTPS_SIMPLE_URL, { 57 requestCount: 1, 58 }); 59 info("Starting test... "); 60 61 const { document, store, windowRequire } = monitor.panelWin; 62 const { getSelectedRequest } = windowRequire( 63 "devtools/client/netmonitor/src/selectors/index" 64 ); 65 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 66 store.dispatch(Actions.batchEnable(false)); 67 68 // Open the request blocking panel 69 store.dispatch(Actions.toggleRequestBlockingPanel()); 70 71 // Reload to have one request in the list 72 let waitForEvents = waitForNetworkEvents(monitor, 1); 73 await navigateTo(HTTPS_SIMPLE_URL); 74 await waitForEvents; 75 76 // Capture normal request 77 let normalRequestState; 78 let normalRequestSize; 79 { 80 const requestBlockingPanel = document.querySelector( 81 ".request-blocking-panel" 82 ); 83 const firstRequest = document.querySelectorAll(".request-list-item")[0]; 84 const waitForHeaders = waitUntil(() => 85 document.querySelector(".headers-overview") 86 ); 87 EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest); 88 await waitForHeaders; 89 normalRequestState = getSelectedRequest(store.getState()); 90 normalRequestSize = firstRequest.querySelector( 91 ".requests-list-transferred" 92 ).textContent; 93 info("Captured normal request"); 94 95 // Drag and drop the list item 96 const createBubbledEvent = (type, props = {}) => { 97 const event = new Event(type, { bubbles: true }); 98 Object.assign(event, props); 99 return event; 100 }; 101 102 info('Dispatching "dragstart" event on first item of request list'); 103 firstRequest.dispatchEvent( 104 createBubbledEvent("dragstart", { 105 clientX: 0, 106 clientY: 0, 107 dataTransfer, 108 }) 109 ); 110 111 Assert.strictEqual( 112 dataTransfer.wasSetDataTriggered(), 113 true, 114 'setData() was called during the "dragstart" event' 115 ); 116 117 info('Dispatching "drop" event on request blocking list'); 118 requestBlockingPanel.dispatchEvent( 119 createBubbledEvent("drop", { 120 clientX: 0, 121 clientY: 1, 122 dataTransfer, 123 }) 124 ); 125 126 Assert.strictEqual( 127 dataTransfer.wasGetDataTriggered(), 128 true, 129 'getData() was called during the "drop" event' 130 ); 131 132 const onRequestBlocked = waitForDispatch( 133 store, 134 "REQUEST_BLOCKING_UPDATE_COMPLETE" 135 ); 136 137 info("Wait for dropped request to be blocked"); 138 await onRequestBlocked; 139 info("Dropped request is now blocked"); 140 } 141 142 // Reload to have one request in the list 143 info("Reloading to check block"); 144 // We can't use the normal waiting methods because a canceled request won't send all 145 // the extra update packets. 146 waitForEvents = waitForNetworkEvents(monitor, 1); 147 tab.linkedBrowser.reload(); 148 await waitForEvents; 149 150 // Capture blocked request, then unblock 151 let blockedRequestState; 152 let blockedRequestSize; 153 { 154 const firstRequest = document.querySelectorAll(".request-list-item")[0]; 155 blockedRequestSize = firstRequest.querySelector( 156 ".requests-list-transferred" 157 ).textContent; 158 EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest); 159 blockedRequestState = getSelectedRequest(store.getState()); 160 info("Captured blocked request"); 161 } 162 163 ok(!normalRequestState.blockedReason, "Normal request is not blocked"); 164 ok(!normalRequestSize.includes("Blocked"), "Normal request has a size"); 165 166 ok(blockedRequestState.blockedReason, "Blocked request is blocked"); 167 ok( 168 blockedRequestSize.includes("Blocked"), 169 "Blocked request shows reason as size" 170 ); 171 172 return teardown(monitor); 173 });