browser_console_evaluation_context_selector.js (6343B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 add_task(async function () { 8 await pushPref("devtools.chrome.enabled", true); 9 await pushPref("devtools.browsertoolbox.scope", "everything"); 10 11 const hud = await BrowserConsoleManager.toggleBrowserConsole(); 12 13 const evaluationContextSelectorButton = await waitFor(() => 14 hud.ui.outputNode.querySelector(".webconsole-evaluation-selector-button") 15 ); 16 17 ok( 18 evaluationContextSelectorButton, 19 "The evaluation context selector is visible" 20 ); 21 is( 22 evaluationContextSelectorButton.innerText, 23 "Top", 24 "The button has the expected 'Top' text" 25 ); 26 is( 27 evaluationContextSelectorButton.classList.contains("checked"), 28 false, 29 "The checked class isn't applied" 30 ); 31 32 await executeAndWaitForResultMessage( 33 hud, 34 "document.location", 35 "chrome://browser/content/browser.xhtml" 36 ); 37 ok(true, "The evaluation was done in the top context"); 38 39 setInputValue(hud, "document.location.host"); 40 await waitForEagerEvaluationResult(hud, `"browser"`); 41 42 info("Check the context selector menu"); 43 checkContextSelectorMenuItemAt(hud, 0, { 44 label: "Top", 45 tooltip: "chrome://browser/content/browser.xhtml", 46 checked: true, 47 }); 48 checkContextSelectorMenuItemAt(hud, 1, { 49 separator: true, 50 }); 51 52 info( 53 "Add a tab with a worker and check both the document and the worker are displayed in the context selector" 54 ); 55 const documentFile = "test-evaluate-worker.html"; 56 const documentWithWorkerUrl = 57 "https://example.com/browser/devtools/client/webconsole/test/browser/" + 58 documentFile; 59 const tab = await addTab(documentWithWorkerUrl); 60 61 const documentIndex = await waitFor(() => { 62 const i = getContextSelectorItems(hud).findIndex(el => 63 el.querySelector(".label")?.innerText?.endsWith(documentFile) 64 ); 65 return i == -1 ? null : i; 66 }); 67 68 info("Select the document target"); 69 selectTargetInContextSelector(hud, documentWithWorkerUrl); 70 71 await waitFor(() => 72 evaluationContextSelectorButton.innerText.includes(documentFile) 73 ); 74 ok(true, "The context was set to the selected document"); 75 is( 76 evaluationContextSelectorButton.classList.contains("checked"), 77 true, 78 "The checked class is applied" 79 ); 80 81 checkContextSelectorMenuItemAt(hud, documentIndex, { 82 label: documentWithWorkerUrl, 83 tooltip: documentWithWorkerUrl, 84 checked: true, 85 }); 86 87 await waitForEagerEvaluationResult(hud, `"example.com"`); 88 ok(true, "The instant evaluation result is updated in the document context"); 89 90 await executeAndWaitForResultMessage( 91 hud, 92 "document.location", 93 documentWithWorkerUrl 94 ); 95 ok(true, "The evaluation is done in the document context"); 96 97 info("Check that autocomplete is done in the tab document context"); 98 await setInputValueForAutocompletion(hud, "p"); 99 // `pauseInWorker` is defined in test-evaluate-worker.html 100 ok( 101 getAutocompletePopupLabels(hud.jsterm.autocompletePopup).includes( 102 "pauseInWorker" 103 ), 104 "autocomplete happened in the tab document context" 105 ); 106 107 // set input text so we can watch for instant evaluation result update 108 setInputValue(hud, "globalThis.location.href"); 109 await waitForEagerEvaluationResult(hud, `"${documentWithWorkerUrl}"`); 110 111 info("Select the worker target"); 112 const workerFile = "test-evaluate-worker.js"; 113 const workerUrl = 114 "https://example.com/browser/devtools/client/webconsole/test/browser/" + 115 workerFile; 116 // Wait for the worker target to be displayed 117 await waitFor(() => 118 getContextSelectorItems(hud).find(el => 119 el.querySelector(".label")?.innerText?.endsWith(workerFile) 120 ) 121 ); 122 selectTargetInContextSelector(hud, workerFile); 123 124 await waitFor(() => 125 evaluationContextSelectorButton.innerText.includes(workerFile) 126 ); 127 ok(true, "The context was set to the selected worker"); 128 129 await waitForEagerEvaluationResult(hud, `"${workerUrl}"`); 130 ok(true, "The instant evaluation result is updated in the worker context"); 131 132 const workerIndex = await waitFor(() => { 133 const i = getContextSelectorItems(hud).findIndex(el => 134 el.querySelector(".label")?.innerText?.endsWith(workerFile) 135 ); 136 return i == -1 ? null : i; 137 }); 138 is( 139 workerIndex, 140 documentIndex + 1, 141 "The worker is displayed right after its related document target" 142 ); 143 checkContextSelectorMenuItemAt(hud, workerIndex, { 144 label: workerFile, 145 tooltip: workerUrl, 146 checked: true, 147 indented: true, 148 }); 149 150 await executeAndWaitForResultMessage( 151 hud, 152 "globalThis.location", 153 `WorkerLocation` 154 ); 155 ok(true, "The evaluation is done in the worker context"); 156 157 info("Check that autocomplete is done in the worker context"); 158 await setInputValueForAutocompletion(hud, "f"); 159 // `foo` is defined in test-evaluate-worker.js 160 ok( 161 getAutocompletePopupLabels(hud.jsterm.autocompletePopup).includes("foo"), 162 "autocomplete happened in the worker context" 163 ); 164 165 // set input text so we can watch for instant evaluation result update 166 setInputValue(hud, "document.location.host"); 167 await waitForEagerEvaluationResult( 168 hud, 169 `ReferenceError: document is not defined` 170 ); 171 172 info( 173 "Remove the tab and make sure both the document and worker target are removed from the context selector" 174 ); 175 await removeTab(tab); 176 177 await waitFor(() => evaluationContextSelectorButton.innerText == "Top"); 178 ok(true, "The context is set back to Top"); 179 180 checkContextSelectorMenuItemAt(hud, 0, { 181 label: "Top", 182 tooltip: "chrome://browser/content/browser.xhtml", 183 checked: true, 184 }); 185 186 is( 187 getContextSelectorItems(hud).every(el => { 188 const label = el.querySelector(".label")?.innerText; 189 return ( 190 !label || 191 (label !== "test-evaluate-worker.html" && label !== workerFile) 192 ); 193 }), 194 true, 195 "the document and worker targets were removed" 196 ); 197 198 await waitForEagerEvaluationResult(hud, `"browser"`); 199 ok(true, "The instant evaluation was done in the top context"); 200 201 await executeAndWaitForResultMessage( 202 hud, 203 "document.location", 204 "chrome://browser/content/browser.xhtml" 205 ); 206 ok(true, "The evaluation was done in the top context"); 207 });