michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Run a series of edit-outer-html tests. michael@0: * This function will iterate over the provided tests array and run each test. michael@0: * Each test's goal is to provide a node (a selector) and a new outer-HTML to be michael@0: * inserted in place of the current one for that node. michael@0: * This test runner will wait for the mutation event to be fired and will check michael@0: * a few things. Each test may also provide its own validate function to perform michael@0: * assertions and verify that the new outer html is correct. michael@0: * @param {Array} tests See runEditOuterHTMLTest for the structure michael@0: * @param {InspectorPanel} inspector The instance of InspectorPanel currently michael@0: * opened michael@0: * @return a promise that resolves when the tests have run michael@0: */ michael@0: function runEditOuterHTMLTests(tests, inspector) { michael@0: info("Running " + tests.length + " edit-outer-html tests"); michael@0: return Task.spawn(function* () { michael@0: for (let step of TEST_DATA) { michael@0: yield runEditOuterHTMLTest(step, inspector); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Run a single edit-outer-html test. michael@0: * See runEditOuterHTMLTests for a description. michael@0: * @param {Object} test A test object should contain the following properties: michael@0: * - selector {String} a css selector targeting the node to edit michael@0: * - oldHTML {String} michael@0: * - newHTML {String} michael@0: * - validate {Function} will be executed when the edition test is done, michael@0: * after the new outer-html has been inserted. Should be used to verify michael@0: * the actual DOM, see if it corresponds to the newHTML string provided michael@0: * @param {InspectorPanel} inspector The instance of InspectorPanel currently michael@0: * opened michael@0: */ michael@0: function* runEditOuterHTMLTest(test, inspector) { michael@0: info("Running an edit outerHTML test on '" + test.selector + "'"); michael@0: yield selectNode(test.selector, inspector); michael@0: let oldNodeFront = inspector.selection.nodeFront; michael@0: michael@0: info("Listening for the markupmutation event"); michael@0: // This event fires once the outerHTML is set, with a target as the parent node and a type of "childList". michael@0: let mutated = inspector.once("markupmutation"); michael@0: info("Editing the outerHTML"); michael@0: inspector.markup.updateNodeOuterHTML(inspector.selection.nodeFront, test.newHTML, test.oldHTML); michael@0: let mutations = yield mutated; michael@0: ok(true, "The markupmutation event has fired, mutation done"); michael@0: michael@0: info("Check to make the sure the correct mutation event was fired, and that the parent is selected"); michael@0: let nodeFront = inspector.selection.nodeFront; michael@0: let mutation = mutations[0]; michael@0: let isFromOuterHTML = mutation.removed.some(n => n === oldNodeFront); michael@0: michael@0: ok(isFromOuterHTML, "The node is in the 'removed' list of the mutation"); michael@0: is(mutation.type, "childList", "Mutation is a childList after updating outerHTML"); michael@0: is(mutation.target, nodeFront, "Parent node is selected immediately after setting outerHTML"); michael@0: michael@0: // Wait for node to be reselected after outerHTML has been set michael@0: yield inspector.selection.once("new-node"); michael@0: michael@0: // Typically selectedNode will === pageNode, but if a new element has been injected in front michael@0: // of it, this will not be the case. If this happens. michael@0: let selectedNode = inspector.selection.node; michael@0: let nodeFront = inspector.selection.nodeFront; michael@0: let pageNode = getNode(test.selector); michael@0: michael@0: if (test.validate) { michael@0: test.validate(pageNode, selectedNode); michael@0: } else { michael@0: is(pageNode, selectedNode, "Original node (grabbed by selector) is selected"); michael@0: is(pageNode.outerHTML, test.newHTML, "Outer HTML has been updated"); michael@0: } michael@0: michael@0: // Wait for the inspector to be fully updated to avoid causing errors by michael@0: // abruptly closing hanging requests when the test ends michael@0: yield inspector.once("inspector-updated"); michael@0: }