tor-browser

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

helper_outerhtml_test_runner.js (3525B)


      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 /* eslint no-unused-vars: [2, {"vars": "local"}] */
      5 /* import-globals-from head.js */
      6 "use strict";
      7 
      8 /**
      9 * Run a series of edit-outer-html tests.
     10 * This function will iterate over the provided tests array and run each test.
     11 * Each test's goal is to provide a node (a selector) and a new outer-HTML to be
     12 * inserted in place of the current one for that node.
     13 * This test runner will wait for the mutation event to be fired and will check
     14 * a few things. Each test may also provide its own validate function to perform
     15 * assertions and verify that the new outer html is correct.
     16 *
     17 * @param {Array} tests See runEditOuterHTMLTest for the structure
     18 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
     19 * opened
     20 * @return a promise that resolves when the tests have run
     21 */
     22 function runEditOuterHTMLTests(tests, inspector) {
     23  info("Running " + tests.length + " edit-outer-html tests");
     24  return (async function () {
     25    for (const step of tests) {
     26      await runEditOuterHTMLTest(step, inspector);
     27    }
     28  })();
     29 }
     30 
     31 /**
     32 * Run a single edit-outer-html test.
     33 * See runEditOuterHTMLTests for a description.
     34 *
     35 * @param {object} test A test object should contain the following properties:
     36 *        - selector {String} a css selector targeting the node to edit
     37 *        - oldHTML {String}
     38 *        - newHTML {String}
     39 *        - validate {Function} will be executed when the edition test is done,
     40 *        after the new outer-html has been inserted. Should be used to verify
     41 *        the actual DOM, see if it corresponds to the newHTML string provided
     42 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
     43 * opened
     44 */
     45 async function runEditOuterHTMLTest(test, inspector) {
     46  info("Running an edit outerHTML test on '" + test.selector + "'");
     47  await selectNode(test.selector, inspector);
     48 
     49  const onUpdated = inspector.once("inspector-updated");
     50 
     51  info("Listen for reselectedonremoved and edit the outerHTML");
     52  const onReselected = inspector.markup.once("reselectedonremoved");
     53  await inspector.markup.updateNodeOuterHTML(
     54    inspector.selection.nodeFront,
     55    test.newHTML,
     56    test.oldHTML
     57  );
     58  await onReselected;
     59 
     60  // Typically selectedNode will === pageNode, but if a new element has been
     61  // injected in front of it, this will not be the case. If this happens.
     62  const selectedNodeFront = inspector.selection.nodeFront;
     63  const pageNodeFront = await inspector.walker.querySelector(
     64    inspector.walker.rootNode,
     65    test.selector
     66  );
     67 
     68  if (test.validate) {
     69    await test.validate({
     70      pageNodeFront,
     71      selectedNodeFront,
     72      inspector,
     73    });
     74  } else {
     75    is(
     76      pageNodeFront,
     77      selectedNodeFront,
     78      "Original node (grabbed by selector) is selected"
     79    );
     80 
     81    const outerHTML = await getContentPageElementProperty(
     82      test.selector,
     83      "outerHTML"
     84    );
     85    is(outerHTML, test.newHTML, "Outer HTML has been updated");
     86  }
     87 
     88  // Wait for the inspector to be fully updated to avoid causing errors by
     89  // abruptly closing hanging requests when the test ends
     90  await onUpdated;
     91 
     92  const closeTagLine =
     93    inspector.markup.getContainer(pageNodeFront).closeTagLine;
     94  if (closeTagLine) {
     95    is(
     96      closeTagLine.querySelectorAll(".theme-fg-contrast").length,
     97      0,
     98      "No contrast class"
     99    );
    100  }
    101 }