browser/devtools/markupview/test/helper_outerhtml_test_runner.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/markupview/test/helper_outerhtml_test_runner.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Run a series of edit-outer-html tests.
    1.10 + * This function will iterate over the provided tests array and run each test.
    1.11 + * Each test's goal is to provide a node (a selector) and a new outer-HTML to be
    1.12 + * inserted in place of the current one for that node.
    1.13 + * This test runner will wait for the mutation event to be fired and will check
    1.14 + * a few things. Each test may also provide its own validate function to perform
    1.15 + * assertions and verify that the new outer html is correct.
    1.16 + * @param {Array} tests See runEditOuterHTMLTest for the structure
    1.17 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently
    1.18 + * opened
    1.19 + * @return a promise that resolves when the tests have run
    1.20 + */
    1.21 +function runEditOuterHTMLTests(tests, inspector) {
    1.22 +  info("Running " + tests.length + " edit-outer-html tests");
    1.23 +  return Task.spawn(function* () {
    1.24 +    for (let step of TEST_DATA) {
    1.25 +      yield runEditOuterHTMLTest(step, inspector);
    1.26 +    }
    1.27 +  });
    1.28 +}
    1.29 +
    1.30 +/**
    1.31 + * Run a single edit-outer-html test.
    1.32 + * See runEditOuterHTMLTests for a description.
    1.33 + * @param {Object} test A test object should contain the following properties:
    1.34 + *        - selector {String} a css selector targeting the node to edit
    1.35 + *        - oldHTML {String}
    1.36 + *        - newHTML {String}
    1.37 + *        - validate {Function} will be executed when the edition test is done,
    1.38 + *        after the new outer-html has been inserted. Should be used to verify
    1.39 + *        the actual DOM, see if it corresponds to the newHTML string provided
    1.40 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently
    1.41 + * opened
    1.42 + */
    1.43 +function* runEditOuterHTMLTest(test, inspector) {
    1.44 +  info("Running an edit outerHTML test on '" + test.selector + "'");
    1.45 +  yield selectNode(test.selector, inspector);
    1.46 +  let oldNodeFront = inspector.selection.nodeFront;
    1.47 +
    1.48 +  info("Listening for the markupmutation event");
    1.49 +  // This event fires once the outerHTML is set, with a target as the parent node and a type of "childList".
    1.50 +  let mutated = inspector.once("markupmutation");
    1.51 +  info("Editing the outerHTML");
    1.52 +  inspector.markup.updateNodeOuterHTML(inspector.selection.nodeFront, test.newHTML, test.oldHTML);
    1.53 +  let mutations = yield mutated;
    1.54 +  ok(true, "The markupmutation event has fired, mutation done");
    1.55 +
    1.56 +  info("Check to make the sure the correct mutation event was fired, and that the parent is selected");
    1.57 +  let nodeFront = inspector.selection.nodeFront;
    1.58 +  let mutation = mutations[0];
    1.59 +  let isFromOuterHTML = mutation.removed.some(n => n === oldNodeFront);
    1.60 +
    1.61 +  ok(isFromOuterHTML, "The node is in the 'removed' list of the mutation");
    1.62 +  is(mutation.type, "childList", "Mutation is a childList after updating outerHTML");
    1.63 +  is(mutation.target, nodeFront, "Parent node is selected immediately after setting outerHTML");
    1.64 +
    1.65 +  // Wait for node to be reselected after outerHTML has been set
    1.66 +  yield inspector.selection.once("new-node");
    1.67 +
    1.68 +  // Typically selectedNode will === pageNode, but if a new element has been injected in front
    1.69 +  // of it, this will not be the case.  If this happens.
    1.70 +  let selectedNode = inspector.selection.node;
    1.71 +  let nodeFront = inspector.selection.nodeFront;
    1.72 +  let pageNode = getNode(test.selector);
    1.73 +
    1.74 +  if (test.validate) {
    1.75 +    test.validate(pageNode, selectedNode);
    1.76 +  } else {
    1.77 +    is(pageNode, selectedNode, "Original node (grabbed by selector) is selected");
    1.78 +    is(pageNode.outerHTML, test.newHTML, "Outer HTML has been updated");
    1.79 +  }
    1.80 +
    1.81 +  // Wait for the inspector to be fully updated to avoid causing errors by
    1.82 +  // abruptly closing hanging requests when the test ends
    1.83 +  yield inspector.once("inspector-updated");
    1.84 +}

mercurial