1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/markupview/test/helper_attributes_test_runner.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 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 add-attributes 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 some text to be entered into the test node's 1.12 + * new-attribute field and check that the given attributes have been created. 1.13 + * After each test has run, the markup-view's undo command will be called and 1.14 + * the test runner will check if all the new attributes are gone. 1.15 + * @param {Array} tests See runAddAttributesTest for the structure 1.16 + * @param {DOMNode|String} nodeOrSelector The node or node selector 1.17 + * corresponding to an element on the current test page that has *no attributes* 1.18 + * when the test starts. It will be used to add and remove attributes. 1.19 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently 1.20 + * opened 1.21 + * @return a promise that resolves when the tests have run 1.22 + */ 1.23 +function runAddAttributesTests(tests, nodeOrSelector, inspector) { 1.24 + info("Running " + tests.length + " add-attributes tests"); 1.25 + return Task.spawn(function*() { 1.26 + info("Selecting the test node"); 1.27 + let div = getNode("div"); 1.28 + yield selectNode(div, inspector); 1.29 + 1.30 + for (let test of tests) { 1.31 + yield runAddAttributesTest(test, div, inspector); 1.32 + } 1.33 + 1.34 + yield inspector.once("inspector-updated"); 1.35 + }); 1.36 +} 1.37 + 1.38 +/** 1.39 + * Run a single add-attribute test. 1.40 + * See runAddAttributesTests for a description. 1.41 + * @param {Object} test A test object should contain the following properties: 1.42 + * - desc {String} a textual description for that test, to help when 1.43 + * reading logs 1.44 + * - text {String} the string to be inserted into the new attribute field 1.45 + * - expectedAttributes {Object} a key/value pair object that will be 1.46 + * used to check the attributes on the test element 1.47 + * - validate {Function} optional extra function that will be called after 1.48 + * the attributes have been added and which should be used to assert some 1.49 + * more things this test runner might not be checking. The function will 1.50 + * be called with the following arguments: 1.51 + * - {DOMNode} The element being tested 1.52 + * - {MarkupContainer} The corresponding container in the markup-view 1.53 + * - {InspectorPanel} The instance of the InspectorPanel opened 1.54 + * @param {DOMNode|String} nodeOrSelector The node or node selector 1.55 + * corresponding to the test element 1.56 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently 1.57 + * opened 1.58 + */ 1.59 +function* runAddAttributesTest(test, nodeOrSelector, inspector) { 1.60 + let element = getNode(nodeOrSelector); 1.61 + 1.62 + info("Starting add-attribute test: " + test.desc); 1.63 + yield addNewAttributes(element, test.text, inspector); 1.64 + 1.65 + info("Assert that the attribute(s) has/have been applied correctly"); 1.66 + assertAttributes(element, test.expectedAttributes); 1.67 + 1.68 + if (test.validate) { 1.69 + test.validate(element, getContainerForRawNode(element, inspector), inspector); 1.70 + } 1.71 + 1.72 + info("Undo the change"); 1.73 + yield undoChange(inspector); 1.74 + 1.75 + info("Assert that the attribute(s) has/have been removed correctly"); 1.76 + assertAttributes(element, {}); 1.77 +} 1.78 + 1.79 +/** 1.80 + * Run a series of edit-attributes tests. 1.81 + * This function will iterate over the provided tests array and run each test. 1.82 + * Each test's goal is to locate a given element on the current test page, assert 1.83 + * its current attributes, then provide the name of one of them and a value to 1.84 + * be set into it, and then check if the new attributes are correct. 1.85 + * After each test has run, the markup-view's undo and redo commands will be 1.86 + * called and the test runner will assert again that the attributes are correct. 1.87 + * @param {Array} tests See runEditAttributesTest for the structure 1.88 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently 1.89 + * opened 1.90 + * @return a promise that resolves when the tests have run 1.91 + */ 1.92 +function runEditAttributesTests(tests, inspector) { 1.93 + info("Running " + tests.length + " edit-attributes tests"); 1.94 + return Task.spawn(function*() { 1.95 + info("Expanding all nodes in the markup-view"); 1.96 + yield inspector.markup.expandAll(); 1.97 + 1.98 + for (let test of tests) { 1.99 + yield runEditAttributesTest(test, inspector); 1.100 + } 1.101 + 1.102 + yield inspector.once("inspector-updated"); 1.103 + }); 1.104 +} 1.105 + 1.106 +/** 1.107 + * Run a single edit-attribute test. 1.108 + * See runEditAttributesTests for a description. 1.109 + * @param {Object} test A test object should contain the following properties: 1.110 + * - desc {String} a textual description for that test, to help when 1.111 + * reading logs 1.112 + * - node {String} a css selector that will be used to select the node 1.113 + * which will be tested during this iteration 1.114 + * - originalAttributes {Object} a key/value pair object that will be 1.115 + * used to check the attributes of the node before the test runs 1.116 + * - name {String} the name of the attribute to focus the editor for 1.117 + * - value {String} the new value to be typed in the focused editor 1.118 + * - expectedAttributes {Object} a key/value pair object that will be 1.119 + * used to check the attributes on the test element 1.120 + * @param {InspectorPanel} inspector The instance of InspectorPanel currently 1.121 + * opened 1.122 + */ 1.123 +function* runEditAttributesTest(test, inspector) { 1.124 + info("Starting edit-attribute test: " + test.desc); 1.125 + 1.126 + info("Selecting the test node " + test.node); 1.127 + yield selectNode(test.node, inspector); 1.128 + 1.129 + info("Asserting that the node has the right attributes to start with"); 1.130 + assertAttributes(test.node, test.originalAttributes); 1.131 + 1.132 + info("Editing attribute " + test.name + " with value " + test.value); 1.133 + 1.134 + let container = getContainerForRawNode(test.node, inspector); 1.135 + ok(container && container.editor, "The markup-container for " + test.node + 1.136 + " was found"); 1.137 + 1.138 + info("Listening for the markupmutation event"); 1.139 + let nodeMutated = inspector.once("markupmutation"); 1.140 + let attr = container.editor.attrs[test.name].querySelector(".editable"); 1.141 + setEditableFieldValue(attr, test.value, inspector); 1.142 + yield nodeMutated; 1.143 + 1.144 + info("Asserting the new attributes after edition"); 1.145 + assertAttributes(test.node, test.expectedAttributes); 1.146 + 1.147 + info("Undo the change and assert that the attributes have been changed back"); 1.148 + yield undoChange(inspector); 1.149 + assertAttributes(test.node, test.originalAttributes); 1.150 + 1.151 + info("Redo the change and assert that the attributes have been changed again"); 1.152 + yield redoChange(inspector); 1.153 + assertAttributes(test.node, test.expectedAttributes); 1.154 +}