tor-browser

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

helper_attributes_test_runner.js (6754B)


      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 add-attributes tests.
     10 * This function will iterate over the provided tests array and run each test.
     11 * Each test's goal is to provide some text to be entered into the test node's
     12 * new-attribute field and check that the given attributes have been created.
     13 * After each test has run, the markup-view's undo command will be called and
     14 * the test runner will check if all the new attributes are gone.
     15 *
     16 * @param {Array} tests See runAddAttributesTest for the structure
     17 * @param {DOMNode | string} nodeOrSelector The node or node selector
     18 * corresponding to an element on the current test page that has *no attributes*
     19 * when the test starts. It will be used to add and remove attributes.
     20 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
     21 * opened
     22 * @return a promise that resolves when the tests have run
     23 */
     24 function runAddAttributesTests(tests, nodeOrSelector, inspector) {
     25  info("Running " + tests.length + " add-attributes tests");
     26  return (async function () {
     27    info("Selecting the test node");
     28    await selectNode("div", inspector);
     29 
     30    for (const test of tests) {
     31      await runAddAttributesTest(test, "div", inspector);
     32    }
     33  })();
     34 }
     35 
     36 /**
     37 * Run a single add-attribute test.
     38 * See runAddAttributesTests for a description.
     39 *
     40 * @param {object} test A test object should contain the following properties:
     41 *        - desc {string} a textual description for that test, to help when
     42 *        reading logs
     43 *        - text {string} the string to be inserted into the new attribute field
     44 *        - expectedAttributes {Object} a key/value pair object that will be
     45 *        used to check the attributes on the test element
     46 *        - validate {Function} optional extra function that will be called
     47 *        after the attributes have been added and which should be used to
     48 *        assert some more things this test runner might not be checking. The
     49 *        function will be called with the following arguments:
     50 *          - {DOMNode} The element being tested
     51 *          - {MarkupContainer} The corresponding container in the markup-view
     52 *          - {InspectorPanel} The instance of the InspectorPanel opened
     53 * @param {string} selector The node selector corresponding to the test element
     54 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
     55 * opened
     56 */
     57 async function runAddAttributesTest(test, selector, inspector) {
     58  if (test.setUp) {
     59    test.setUp(inspector);
     60  }
     61 
     62  info("Starting add-attribute test: " + test.desc);
     63  await addNewAttributes(selector, test.text, inspector);
     64 
     65  info("Assert that the attribute(s) has/have been applied correctly");
     66  await assertAttributes(selector, test.expectedAttributes);
     67 
     68  if (test.validate) {
     69    const container = await getContainerForSelector(selector, inspector);
     70    test.validate(container, inspector);
     71  }
     72 
     73  info("Undo the change");
     74  await undoChange(inspector);
     75 
     76  info("Assert that the attribute(s) has/have been removed correctly");
     77  await assertAttributes(selector, {});
     78  if (test.tearDown) {
     79    test.tearDown(inspector);
     80  }
     81 }
     82 
     83 /**
     84 * Run a series of edit-attributes tests.
     85 * This function will iterate over the provided tests array and run each test.
     86 * Each test's goal is to locate a given element on the current test page,
     87 * assert its current attributes, then provide the name of one of them and a
     88 * value to be set into it, and then check if the new attributes are correct.
     89 * After each test has run, the markup-view's undo and redo commands will be
     90 * called and the test runner will assert again that the attributes are correct.
     91 *
     92 * @param {Array} tests See runEditAttributesTest for the structure
     93 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
     94 * opened
     95 * @return a promise that resolves when the tests have run
     96 */
     97 function runEditAttributesTests(tests, inspector) {
     98  info("Running " + tests.length + " edit-attributes tests");
     99  return (async function () {
    100    info("Expanding all nodes in the markup-view");
    101    await inspector.markup.expandAll();
    102 
    103    for (const test of tests) {
    104      await runEditAttributesTest(test, inspector);
    105    }
    106  })();
    107 }
    108 
    109 /**
    110 * Run a single edit-attribute test.
    111 * See runEditAttributesTests for a description.
    112 *
    113 * @param {object} test A test object should contain the following properties:
    114 *        - desc {String} a textual description for that test, to help when
    115 *        reading logs
    116 *        - node {String} a css selector that will be used to select the node
    117 *        which will be tested during this iteration
    118 *        - originalAttributes {Object} a key/value pair object that will be
    119 *        used to check the attributes of the node before the test runs
    120 *        - name {String} the name of the attribute to focus the editor for
    121 *        - value {String} the new value to be typed in the focused editor
    122 *        - expectedAttributes {Object} a key/value pair object that will be
    123 *        used to check the attributes on the test element
    124 * @param {InspectorPanel} inspector The instance of InspectorPanel currently
    125 * opened
    126 */
    127 async function runEditAttributesTest(test, inspector) {
    128  info("Starting edit-attribute test: " + test.desc);
    129 
    130  info("Selecting the test node " + test.node);
    131  await selectNode(test.node, inspector);
    132 
    133  info("Asserting that the node has the right attributes to start with");
    134  await assertAttributes(test.node, test.originalAttributes);
    135 
    136  info("Editing attribute " + test.name + " with value " + test.value);
    137 
    138  const container = await focusNode(test.node, inspector);
    139  ok(
    140    container && container.editor,
    141    "The markup-container for " + test.node + " was found"
    142  );
    143 
    144  info("Listening for the markupmutation event");
    145  const nodeMutated = inspector.once("markupmutation");
    146  const attr = container.editor.attrElements
    147    .get(test.name)
    148    .querySelector(".editable");
    149  setEditableFieldValue(attr, test.value, inspector);
    150  await nodeMutated;
    151 
    152  info("Asserting the new attributes after edition");
    153  await assertAttributes(test.node, test.expectedAttributes);
    154 
    155  info("Undo the change and assert that the attributes have been changed back");
    156  await undoChange(inspector);
    157  await assertAttributes(test.node, test.originalAttributes);
    158 
    159  info(
    160    "Redo the change and assert that the attributes have been changed " +
    161      "again"
    162  );
    163  await redoChange(inspector);
    164  await assertAttributes(test.node, test.expectedAttributes);
    165 }