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 add-attributes 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 some text to be entered into the test node's michael@0: * new-attribute field and check that the given attributes have been created. michael@0: * After each test has run, the markup-view's undo command will be called and michael@0: * the test runner will check if all the new attributes are gone. michael@0: * @param {Array} tests See runAddAttributesTest for the structure michael@0: * @param {DOMNode|String} nodeOrSelector The node or node selector michael@0: * corresponding to an element on the current test page that has *no attributes* michael@0: * when the test starts. It will be used to add and remove attributes. 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 runAddAttributesTests(tests, nodeOrSelector, inspector) { michael@0: info("Running " + tests.length + " add-attributes tests"); michael@0: return Task.spawn(function*() { michael@0: info("Selecting the test node"); michael@0: let div = getNode("div"); michael@0: yield selectNode(div, inspector); michael@0: michael@0: for (let test of tests) { michael@0: yield runAddAttributesTest(test, div, inspector); michael@0: } michael@0: michael@0: yield inspector.once("inspector-updated"); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Run a single add-attribute test. michael@0: * See runAddAttributesTests for a description. michael@0: * @param {Object} test A test object should contain the following properties: michael@0: * - desc {String} a textual description for that test, to help when michael@0: * reading logs michael@0: * - text {String} the string to be inserted into the new attribute field michael@0: * - expectedAttributes {Object} a key/value pair object that will be michael@0: * used to check the attributes on the test element michael@0: * - validate {Function} optional extra function that will be called after michael@0: * the attributes have been added and which should be used to assert some michael@0: * more things this test runner might not be checking. The function will michael@0: * be called with the following arguments: michael@0: * - {DOMNode} The element being tested michael@0: * - {MarkupContainer} The corresponding container in the markup-view michael@0: * - {InspectorPanel} The instance of the InspectorPanel opened michael@0: * @param {DOMNode|String} nodeOrSelector The node or node selector michael@0: * corresponding to the test element michael@0: * @param {InspectorPanel} inspector The instance of InspectorPanel currently michael@0: * opened michael@0: */ michael@0: function* runAddAttributesTest(test, nodeOrSelector, inspector) { michael@0: let element = getNode(nodeOrSelector); michael@0: michael@0: info("Starting add-attribute test: " + test.desc); michael@0: yield addNewAttributes(element, test.text, inspector); michael@0: michael@0: info("Assert that the attribute(s) has/have been applied correctly"); michael@0: assertAttributes(element, test.expectedAttributes); michael@0: michael@0: if (test.validate) { michael@0: test.validate(element, getContainerForRawNode(element, inspector), inspector); michael@0: } michael@0: michael@0: info("Undo the change"); michael@0: yield undoChange(inspector); michael@0: michael@0: info("Assert that the attribute(s) has/have been removed correctly"); michael@0: assertAttributes(element, {}); michael@0: } michael@0: michael@0: /** michael@0: * Run a series of edit-attributes tests. michael@0: * This function will iterate over the provided tests array and run each test. michael@0: * Each test's goal is to locate a given element on the current test page, assert michael@0: * its current attributes, then provide the name of one of them and a value to michael@0: * be set into it, and then check if the new attributes are correct. michael@0: * After each test has run, the markup-view's undo and redo commands will be michael@0: * called and the test runner will assert again that the attributes are correct. michael@0: * @param {Array} tests See runEditAttributesTest 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 runEditAttributesTests(tests, inspector) { michael@0: info("Running " + tests.length + " edit-attributes tests"); michael@0: return Task.spawn(function*() { michael@0: info("Expanding all nodes in the markup-view"); michael@0: yield inspector.markup.expandAll(); michael@0: michael@0: for (let test of tests) { michael@0: yield runEditAttributesTest(test, inspector); michael@0: } michael@0: michael@0: yield inspector.once("inspector-updated"); michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Run a single edit-attribute test. michael@0: * See runEditAttributesTests for a description. michael@0: * @param {Object} test A test object should contain the following properties: michael@0: * - desc {String} a textual description for that test, to help when michael@0: * reading logs michael@0: * - node {String} a css selector that will be used to select the node michael@0: * which will be tested during this iteration michael@0: * - originalAttributes {Object} a key/value pair object that will be michael@0: * used to check the attributes of the node before the test runs michael@0: * - name {String} the name of the attribute to focus the editor for michael@0: * - value {String} the new value to be typed in the focused editor michael@0: * - expectedAttributes {Object} a key/value pair object that will be michael@0: * used to check the attributes on the test element michael@0: * @param {InspectorPanel} inspector The instance of InspectorPanel currently michael@0: * opened michael@0: */ michael@0: function* runEditAttributesTest(test, inspector) { michael@0: info("Starting edit-attribute test: " + test.desc); michael@0: michael@0: info("Selecting the test node " + test.node); michael@0: yield selectNode(test.node, inspector); michael@0: michael@0: info("Asserting that the node has the right attributes to start with"); michael@0: assertAttributes(test.node, test.originalAttributes); michael@0: michael@0: info("Editing attribute " + test.name + " with value " + test.value); michael@0: michael@0: let container = getContainerForRawNode(test.node, inspector); michael@0: ok(container && container.editor, "The markup-container for " + test.node + michael@0: " was found"); michael@0: michael@0: info("Listening for the markupmutation event"); michael@0: let nodeMutated = inspector.once("markupmutation"); michael@0: let attr = container.editor.attrs[test.name].querySelector(".editable"); michael@0: setEditableFieldValue(attr, test.value, inspector); michael@0: yield nodeMutated; michael@0: michael@0: info("Asserting the new attributes after edition"); michael@0: assertAttributes(test.node, test.expectedAttributes); michael@0: michael@0: info("Undo the change and assert that the attributes have been changed back"); michael@0: yield undoChange(inspector); michael@0: assertAttributes(test.node, test.originalAttributes); michael@0: michael@0: info("Redo the change and assert that the attributes have been changed again"); michael@0: yield redoChange(inspector); michael@0: assertAttributes(test.node, test.expectedAttributes); michael@0: }