tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_jsterm_evaluation_context_selector.js (8083B)


      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 http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const FILE_FOLDER = `browser/devtools/client/webconsole/test/browser`;
      8 const TEST_URI = `https://example.com/${FILE_FOLDER}/test-console-evaluation-context-selector.html`;
      9 const IFRAME_PATH = `${FILE_FOLDER}/test-console-evaluation-context-selector-child.html`;
     10 
     11 requestLongerTimeout(2);
     12 
     13 add_task(async function () {
     14  // Force instantiating worker target in order to show them in the context selector
     15  await pushPref("dom.worker.console.dispatch_events_to_main_thread", false);
     16 
     17  const hud = await openNewTabWithIframesAndConsole(TEST_URI, [
     18    `https://example.org/${IFRAME_PATH}?id=iframe-1`,
     19    `https://example.net/${IFRAME_PATH}?id=iframe-2`,
     20  ]);
     21 
     22  const evaluationContextSelectorButton = hud.ui.outputNode.querySelector(
     23    ".webconsole-evaluation-selector-button"
     24  );
     25 
     26  ok(
     27    evaluationContextSelectorButton,
     28    "The evaluation context selector is visible"
     29  );
     30  is(
     31    evaluationContextSelectorButton.innerText,
     32    "Top",
     33    "The button has the expected 'Top' text"
     34  );
     35  is(
     36    evaluationContextSelectorButton.classList.contains("checked"),
     37    false,
     38    "The checked class isn't applied"
     39  );
     40 
     41  const topLevelDocumentMessage = await executeAndWaitForResultMessage(
     42    hud,
     43    "document.location",
     44    "example.com"
     45  );
     46 
     47  setInputValue(hud, "document.location.host");
     48  await waitForEagerEvaluationResult(hud, `"example.com"`);
     49 
     50  info("Check the context selector menu");
     51  const expectedTopItem = {
     52    label: "Top",
     53    tooltip: TEST_URI,
     54  };
     55  const expectedWorkerItem = {
     56    label: "my worker",
     57    tooltip: `data:application/javascript,console.log("worker")`,
     58    indented: true,
     59  };
     60  const expectedSeparatorItem = { separator: true };
     61  const expectedFirstIframeItem = {
     62    label: "iframe-1|example.org",
     63    tooltip: `https://example.org/${IFRAME_PATH}?id=iframe-1`,
     64  };
     65  const expectedSecondIframeItem = {
     66    label: "iframe-2|example.net",
     67    tooltip: `https://example.net/${IFRAME_PATH}?id=iframe-2`,
     68  };
     69 
     70  await checkContextSelectorMenu(hud, [
     71    {
     72      ...expectedTopItem,
     73      checked: true,
     74    },
     75    {
     76      ...expectedWorkerItem,
     77      checked: false,
     78    },
     79    expectedSeparatorItem,
     80    {
     81      ...expectedFirstIframeItem,
     82      checked: false,
     83    },
     84    {
     85      ...expectedSecondIframeItem,
     86      checked: false,
     87    },
     88  ]);
     89 
     90  info("Select the first iframe");
     91  selectTargetInContextSelector(hud, expectedFirstIframeItem.label);
     92 
     93  await waitFor(() =>
     94    evaluationContextSelectorButton.innerText.includes("example.org")
     95  );
     96  ok(true, "The context was set to the selected iframe document");
     97  is(
     98    evaluationContextSelectorButton.classList.contains("checked"),
     99    true,
    100    "The checked class is applied"
    101  );
    102 
    103  await waitForEagerEvaluationResult(hud, `"example.org"`);
    104  ok(true, "The instant evaluation result is updated in the iframe context");
    105 
    106  const iframe1DocumentMessage = await executeAndWaitForResultMessage(
    107    hud,
    108    "document.location",
    109    "example.org"
    110  );
    111  setInputValue(hud, "document.location.host");
    112 
    113  info("Select the second iframe in the context selector menu");
    114  await checkContextSelectorMenu(hud, [
    115    {
    116      ...expectedTopItem,
    117      checked: false,
    118    },
    119    {
    120      ...expectedWorkerItem,
    121      checked: false,
    122    },
    123    expectedSeparatorItem,
    124    {
    125      ...expectedFirstIframeItem,
    126      checked: true,
    127    },
    128    {
    129      ...expectedSecondIframeItem,
    130      checked: false,
    131    },
    132  ]);
    133  selectTargetInContextSelector(hud, expectedSecondIframeItem.label);
    134 
    135  await waitFor(() =>
    136    evaluationContextSelectorButton.innerText.includes("example.net")
    137  );
    138  ok(true, "The context was set to the selected iframe document");
    139  is(
    140    evaluationContextSelectorButton.classList.contains("checked"),
    141    true,
    142    "The checked class is applied"
    143  );
    144 
    145  await waitForEagerEvaluationResult(hud, `"example.net"`);
    146  ok(true, "The instant evaluation result is updated in the iframe context");
    147 
    148  const iframe2DocumentMessage = await executeAndWaitForResultMessage(
    149    hud,
    150    "document.location",
    151    "example.net"
    152  );
    153  setInputValue(hud, "document.location.host");
    154 
    155  info("Select the top frame in the context selector menu");
    156  await checkContextSelectorMenu(hud, [
    157    {
    158      ...expectedTopItem,
    159      checked: false,
    160    },
    161    {
    162      ...expectedWorkerItem,
    163      checked: false,
    164    },
    165    expectedSeparatorItem,
    166    {
    167      ...expectedFirstIframeItem,
    168      checked: false,
    169    },
    170    {
    171      ...expectedSecondIframeItem,
    172      checked: true,
    173    },
    174  ]);
    175  selectTargetInContextSelector(hud, expectedTopItem.label);
    176 
    177  await waitForEagerEvaluationResult(hud, `"example.com"`);
    178  await waitFor(() =>
    179    evaluationContextSelectorButton.innerText.includes("Top")
    180  );
    181  is(
    182    evaluationContextSelectorButton.classList.contains("checked"),
    183    false,
    184    "The checked class isn't applied"
    185  );
    186 
    187  info("Check that 'Store as global variable' selects the right context");
    188  await testStoreAsGlobalVariable(
    189    hud,
    190    iframe1DocumentMessage,
    191    "temp0",
    192    "example.org"
    193  );
    194  await waitForEagerEvaluationResult(
    195    hud,
    196    `Location https://example.org/${IFRAME_PATH}?id=iframe-1`
    197  );
    198  await waitFor(() =>
    199    evaluationContextSelectorButton.innerText.includes("example.org")
    200  );
    201  ok(true, "The context was set to the selected iframe document");
    202 
    203  await testStoreAsGlobalVariable(
    204    hud,
    205    iframe2DocumentMessage,
    206    "temp0",
    207    "example.net"
    208  );
    209  await waitForEagerEvaluationResult(
    210    hud,
    211    `Location https://example.net/${IFRAME_PATH}?id=iframe-2`
    212  );
    213  await waitFor(() =>
    214    evaluationContextSelectorButton.innerText.includes("example.net")
    215  );
    216  ok(true, "The context was set to the selected iframe document");
    217 
    218  await testStoreAsGlobalVariable(
    219    hud,
    220    topLevelDocumentMessage,
    221    "temp0",
    222    "example.com"
    223  );
    224  await waitForEagerEvaluationResult(hud, `Location ${TEST_URI}`);
    225  await waitFor(() =>
    226    evaluationContextSelectorButton.innerText.includes("Top")
    227  );
    228  ok(true, "The context was set to the top document");
    229 
    230  info("Check that autocomplete data are cleared when changing context");
    231  await setInputValueForAutocompletion(hud, "foo");
    232  ok(
    233    hasExactPopupLabels(hud.jsterm.autocompletePopup, ["foobar", "foobaz"]),
    234    "autocomplete has expected items from top level document"
    235  );
    236  checkInputCompletionValue(hud, "bar", `completeNode has expected value`);
    237 
    238  info("Select iframe document");
    239  // We need to hide the popup to be able to select the target in the context selector.
    240  // Don't use `closeAutocompletePopup` as it uses the Escape key, which explicitely hides
    241  // the completion node.
    242  const onPopupHidden = hud.jsterm.autocompletePopup.once("popuphidden");
    243  hud.jsterm.autocompletePopup.hidePopup();
    244  onPopupHidden;
    245 
    246  selectTargetInContextSelector(hud, expectedSecondIframeItem.label);
    247  await waitFor(() => getInputCompletionValue(hud) === "");
    248  ok(true, `completeNode was cleared`);
    249 
    250  const updated = hud.jsterm.once("autocomplete-updated");
    251  EventUtils.sendString("b", hud.iframeWindow);
    252  await updated;
    253 
    254  ok(
    255    hasExactPopupLabels(hud.jsterm.autocompletePopup, []),
    256    "autocomplete data was cleared"
    257  );
    258 });
    259 
    260 async function testStoreAsGlobalVariable(
    261  hud,
    262  msg,
    263  variableName,
    264  expectedTextResult
    265 ) {
    266  const menuPopup = await openContextMenu(
    267    hud,
    268    msg.node.querySelector(".objectBox")
    269  );
    270  const storeMenuItem = menuPopup.querySelector("#console-menu-store");
    271  const onceInputSet = hud.jsterm.once("set-input-value");
    272  storeMenuItem.click();
    273 
    274  info("Wait for console input to be updated with the temp variable");
    275  await onceInputSet;
    276 
    277  info("Wait for context menu to be hidden");
    278  await hideContextMenu(hud);
    279 
    280  is(getInputValue(hud), variableName, "Input was set");
    281 
    282  await executeAndWaitForResultMessage(
    283    hud,
    284    `${variableName}`,
    285    expectedTextResult
    286  );
    287  ok(true, "Correct variable assigned into console.");
    288 }