tor-browser

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

browser_rules_class_panel_add.js (3128B)


      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 classes can be added in the class panel
      7 
      8 // This array contains the list of test cases. Each test case contains these properties:
      9 // - {String} textEntered The text to be entered in the field
     10 // - {Boolean} expectNoMutation Set to true if we shouldn't wait for a DOM mutation
     11 // - {Array} expectedClasses The expected list of classes to be applied to the DOM and to
     12 //   be found in the class panel
     13 const TEST_ARRAY = [
     14  {
     15    textEntered: "",
     16    expectNoMutation: true,
     17    expectedClasses: [],
     18  },
     19  {
     20    textEntered: "class",
     21    expectedClasses: ["class"],
     22  },
     23  {
     24    textEntered: "class",
     25    expectNoMutation: true,
     26    expectedClasses: ["class"],
     27  },
     28  {
     29    textEntered: "a a a a a a a a a a",
     30    expectedClasses: ["class", "a"],
     31  },
     32  {
     33    textEntered: "class2 class3",
     34    expectedClasses: ["class", "a", "class2", "class3"],
     35  },
     36  {
     37    textEntered: "                       ",
     38    expectNoMutation: true,
     39    expectedClasses: ["class", "a", "class2", "class3"],
     40  },
     41  {
     42    textEntered: "          class4",
     43    expectedClasses: ["class", "a", "class2", "class3", "class4"],
     44  },
     45  {
     46    textEntered: "    \t      class5      \t \t\t             ",
     47    expectedClasses: ["class", "a", "class2", "class3", "class4", "class5"],
     48  },
     49 ];
     50 
     51 add_task(async function () {
     52  await addTab("data:text/html;charset=utf-8,");
     53  const { inspector, view } = await openRuleView();
     54 
     55  info("Open the class panel");
     56  view.showClassPanel();
     57 
     58  const textField = inspector.panelDoc.querySelector(
     59    "#ruleview-class-panel .add-class"
     60  );
     61  ok(textField, "The input field exists in the class panel");
     62 
     63  textField.focus();
     64 
     65  let onMutation;
     66  for (const { textEntered, expectNoMutation, expectedClasses } of TEST_ARRAY) {
     67    if (!expectNoMutation) {
     68      onMutation = inspector.once("markupmutation");
     69    }
     70 
     71    info(`Enter the test string in the field: ${textEntered}`);
     72    for (const key of textEntered.split("")) {
     73      const onPreviewMutation = inspector.once("markupmutation");
     74      EventUtils.synthesizeKey(key, {}, view.styleWindow);
     75      await onPreviewMutation;
     76    }
     77 
     78    info("Submit the change and wait for the textfield to become empty");
     79    const onEmpty = waitForFieldToBeEmpty(textField);
     80    EventUtils.synthesizeKey("VK_RETURN", {}, view.styleWindow);
     81 
     82    if (!expectNoMutation) {
     83      info("Wait for the DOM to change");
     84      await onMutation;
     85    }
     86 
     87    await onEmpty;
     88 
     89    info("Check the state of the DOM node");
     90    const className = await getContentPageElementAttribute("body", "class");
     91    const expectedClassName = expectedClasses.length
     92      ? expectedClasses.join(" ")
     93      : null;
     94    is(className, expectedClassName, "The DOM node has the right className");
     95 
     96    info("Check the content of the class panel");
     97    checkClassPanelContent(
     98      view,
     99      expectedClasses.map(name => {
    100        return { name, state: true };
    101      })
    102    );
    103  }
    104 });
    105 
    106 function waitForFieldToBeEmpty(textField) {
    107  return waitForSuccess(() => !textField.value);
    108 }