tor-browser

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

browser_rules_original-source-link.js (3769B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the stylesheet links in the rule view are correct when source maps
      7 // are involved.
      8 
      9 const TESTCASE_URI = URL_ROOT_SSL + "doc_sourcemaps.html";
     10 const PREF = "devtools.source-map.client-service.enabled";
     11 const SCSS_FILENAME = "doc_sourcemaps.scss";
     12 const SCSS_LOC_LINE = 4;
     13 const CSS_FILENAME = "doc_sourcemaps.css";
     14 const CSS_LOC_LINE = 1;
     15 
     16 add_task(async function () {
     17  info("Setting the " + PREF + " pref to true");
     18  Services.prefs.setBoolPref(PREF, true);
     19 
     20  await addTab(TESTCASE_URI);
     21  const { toolbox, inspector, view } = await openRuleView();
     22 
     23  info("Selecting the test node");
     24  await selectNode("div", inspector);
     25 
     26  await verifyStyleSheetLink(view, SCSS_FILENAME, SCSS_LOC_LINE);
     27 
     28  info("Setting the " + PREF + " pref to false");
     29  Services.prefs.setBoolPref(PREF, false);
     30  await verifyStyleSheetLink(view, CSS_FILENAME, CSS_LOC_LINE);
     31 
     32  info("Setting the " + PREF + " pref to true again");
     33  Services.prefs.setBoolPref(PREF, true);
     34 
     35  await testClickingLink(toolbox, view);
     36  const selectedEditor =
     37    await waitForOriginalStyleSheetEditorSelection(toolbox);
     38 
     39  const href = selectedEditor.styleSheet.href;
     40  ok(
     41    href.endsWith("doc_sourcemaps.scss"),
     42    "selected stylesheet is correct one"
     43  );
     44 
     45  await selectedEditor.getSourceEditor();
     46  const { line } = selectedEditor.sourceEditor.getCursor();
     47  is(line, 3, "cursor is at correct line number in original source");
     48 
     49  info("Clearing the " + PREF + " pref");
     50  Services.prefs.clearUserPref(PREF);
     51 });
     52 
     53 async function testClickingLink(toolbox, view) {
     54  info("Listening for switch to the style editor");
     55  const onStyleEditorReady = toolbox.once("styleeditor-selected");
     56 
     57  info("Finding the stylesheet link and clicking it");
     58  const link = getRuleViewLinkByIndex(view, 1);
     59  link.scrollIntoView();
     60  link.click();
     61  await onStyleEditorReady;
     62 }
     63 
     64 function waitForOriginalStyleSheetEditorSelection(toolbox) {
     65  const panel = toolbox.getCurrentPanel();
     66  return new Promise(resolve => {
     67    const maybeContinue = editor => {
     68      // The style editor selects the first sheet at first load before
     69      // selecting the desired sheet.
     70      if (editor.styleSheet.href.endsWith("scss")) {
     71        info("Original source editor selected");
     72        off();
     73        resolve(editor);
     74      }
     75    };
     76    const off = panel.UI.on("editor-selected", maybeContinue);
     77    if (panel.UI.selectedEditor) {
     78      maybeContinue(panel.UI.selectedEditor);
     79    }
     80  });
     81 }
     82 
     83 async function verifyStyleSheetLink(view, fileName, lineNumber) {
     84  const expectedLocation = `${fileName}:${lineNumber}`;
     85  const expectedUrl = URL_ROOT_SSL + fileName;
     86  const expectedTitle = `View source in Style Editor → ${URL_ROOT_SSL}${expectedLocation}`;
     87 
     88  info("Verifying that the rule-view stylesheet link is " + expectedLocation);
     89  const label = getRuleViewLinkByIndex(view, 1).querySelector(
     90    ".ruleview-rule-source-label"
     91  );
     92  await waitForSuccess(function () {
     93    return (
     94      label.textContent == expectedLocation &&
     95      label.getAttribute("title") === expectedTitle
     96    );
     97  }, "Link text changed to display correct location: " + expectedLocation);
     98 
     99  const copyLocationMenuItem = openStyleContextMenuAndGetAllItems(
    100    view,
    101    label
    102  ).find(
    103    item =>
    104      item.label ===
    105      STYLE_INSPECTOR_L10N.getStr("styleinspector.contextmenu.copyLocation")
    106  );
    107 
    108  try {
    109    await waitForClipboardPromise(
    110      () => copyLocationMenuItem.click(),
    111      () => SpecialPowers.getClipboardData("text/plain") === expectedUrl
    112    );
    113    ok(true, "Expected URL was copied to clipboard");
    114  } catch (e) {
    115    ok(false, `Clipboard text does not match expected "${expectedUrl}" url`);
    116  }
    117 }