Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /** |
michael@0 | 6 | * Run a series of add-attributes tests. |
michael@0 | 7 | * This function will iterate over the provided tests array and run each test. |
michael@0 | 8 | * Each test's goal is to provide some text to be entered into the test node's |
michael@0 | 9 | * new-attribute field and check that the given attributes have been created. |
michael@0 | 10 | * After each test has run, the markup-view's undo command will be called and |
michael@0 | 11 | * the test runner will check if all the new attributes are gone. |
michael@0 | 12 | * @param {Array} tests See runAddAttributesTest for the structure |
michael@0 | 13 | * @param {DOMNode|String} nodeOrSelector The node or node selector |
michael@0 | 14 | * corresponding to an element on the current test page that has *no attributes* |
michael@0 | 15 | * when the test starts. It will be used to add and remove attributes. |
michael@0 | 16 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 17 | * opened |
michael@0 | 18 | * @return a promise that resolves when the tests have run |
michael@0 | 19 | */ |
michael@0 | 20 | function runAddAttributesTests(tests, nodeOrSelector, inspector) { |
michael@0 | 21 | info("Running " + tests.length + " add-attributes tests"); |
michael@0 | 22 | return Task.spawn(function*() { |
michael@0 | 23 | info("Selecting the test node"); |
michael@0 | 24 | let div = getNode("div"); |
michael@0 | 25 | yield selectNode(div, inspector); |
michael@0 | 26 | |
michael@0 | 27 | for (let test of tests) { |
michael@0 | 28 | yield runAddAttributesTest(test, div, inspector); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | yield inspector.once("inspector-updated"); |
michael@0 | 32 | }); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | /** |
michael@0 | 36 | * Run a single add-attribute test. |
michael@0 | 37 | * See runAddAttributesTests for a description. |
michael@0 | 38 | * @param {Object} test A test object should contain the following properties: |
michael@0 | 39 | * - desc {String} a textual description for that test, to help when |
michael@0 | 40 | * reading logs |
michael@0 | 41 | * - text {String} the string to be inserted into the new attribute field |
michael@0 | 42 | * - expectedAttributes {Object} a key/value pair object that will be |
michael@0 | 43 | * used to check the attributes on the test element |
michael@0 | 44 | * - validate {Function} optional extra function that will be called after |
michael@0 | 45 | * the attributes have been added and which should be used to assert some |
michael@0 | 46 | * more things this test runner might not be checking. The function will |
michael@0 | 47 | * be called with the following arguments: |
michael@0 | 48 | * - {DOMNode} The element being tested |
michael@0 | 49 | * - {MarkupContainer} The corresponding container in the markup-view |
michael@0 | 50 | * - {InspectorPanel} The instance of the InspectorPanel opened |
michael@0 | 51 | * @param {DOMNode|String} nodeOrSelector The node or node selector |
michael@0 | 52 | * corresponding to the test element |
michael@0 | 53 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 54 | * opened |
michael@0 | 55 | */ |
michael@0 | 56 | function* runAddAttributesTest(test, nodeOrSelector, inspector) { |
michael@0 | 57 | let element = getNode(nodeOrSelector); |
michael@0 | 58 | |
michael@0 | 59 | info("Starting add-attribute test: " + test.desc); |
michael@0 | 60 | yield addNewAttributes(element, test.text, inspector); |
michael@0 | 61 | |
michael@0 | 62 | info("Assert that the attribute(s) has/have been applied correctly"); |
michael@0 | 63 | assertAttributes(element, test.expectedAttributes); |
michael@0 | 64 | |
michael@0 | 65 | if (test.validate) { |
michael@0 | 66 | test.validate(element, getContainerForRawNode(element, inspector), inspector); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | info("Undo the change"); |
michael@0 | 70 | yield undoChange(inspector); |
michael@0 | 71 | |
michael@0 | 72 | info("Assert that the attribute(s) has/have been removed correctly"); |
michael@0 | 73 | assertAttributes(element, {}); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Run a series of edit-attributes tests. |
michael@0 | 78 | * This function will iterate over the provided tests array and run each test. |
michael@0 | 79 | * Each test's goal is to locate a given element on the current test page, assert |
michael@0 | 80 | * its current attributes, then provide the name of one of them and a value to |
michael@0 | 81 | * be set into it, and then check if the new attributes are correct. |
michael@0 | 82 | * After each test has run, the markup-view's undo and redo commands will be |
michael@0 | 83 | * called and the test runner will assert again that the attributes are correct. |
michael@0 | 84 | * @param {Array} tests See runEditAttributesTest for the structure |
michael@0 | 85 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 86 | * opened |
michael@0 | 87 | * @return a promise that resolves when the tests have run |
michael@0 | 88 | */ |
michael@0 | 89 | function runEditAttributesTests(tests, inspector) { |
michael@0 | 90 | info("Running " + tests.length + " edit-attributes tests"); |
michael@0 | 91 | return Task.spawn(function*() { |
michael@0 | 92 | info("Expanding all nodes in the markup-view"); |
michael@0 | 93 | yield inspector.markup.expandAll(); |
michael@0 | 94 | |
michael@0 | 95 | for (let test of tests) { |
michael@0 | 96 | yield runEditAttributesTest(test, inspector); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | yield inspector.once("inspector-updated"); |
michael@0 | 100 | }); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Run a single edit-attribute test. |
michael@0 | 105 | * See runEditAttributesTests for a description. |
michael@0 | 106 | * @param {Object} test A test object should contain the following properties: |
michael@0 | 107 | * - desc {String} a textual description for that test, to help when |
michael@0 | 108 | * reading logs |
michael@0 | 109 | * - node {String} a css selector that will be used to select the node |
michael@0 | 110 | * which will be tested during this iteration |
michael@0 | 111 | * - originalAttributes {Object} a key/value pair object that will be |
michael@0 | 112 | * used to check the attributes of the node before the test runs |
michael@0 | 113 | * - name {String} the name of the attribute to focus the editor for |
michael@0 | 114 | * - value {String} the new value to be typed in the focused editor |
michael@0 | 115 | * - expectedAttributes {Object} a key/value pair object that will be |
michael@0 | 116 | * used to check the attributes on the test element |
michael@0 | 117 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 118 | * opened |
michael@0 | 119 | */ |
michael@0 | 120 | function* runEditAttributesTest(test, inspector) { |
michael@0 | 121 | info("Starting edit-attribute test: " + test.desc); |
michael@0 | 122 | |
michael@0 | 123 | info("Selecting the test node " + test.node); |
michael@0 | 124 | yield selectNode(test.node, inspector); |
michael@0 | 125 | |
michael@0 | 126 | info("Asserting that the node has the right attributes to start with"); |
michael@0 | 127 | assertAttributes(test.node, test.originalAttributes); |
michael@0 | 128 | |
michael@0 | 129 | info("Editing attribute " + test.name + " with value " + test.value); |
michael@0 | 130 | |
michael@0 | 131 | let container = getContainerForRawNode(test.node, inspector); |
michael@0 | 132 | ok(container && container.editor, "The markup-container for " + test.node + |
michael@0 | 133 | " was found"); |
michael@0 | 134 | |
michael@0 | 135 | info("Listening for the markupmutation event"); |
michael@0 | 136 | let nodeMutated = inspector.once("markupmutation"); |
michael@0 | 137 | let attr = container.editor.attrs[test.name].querySelector(".editable"); |
michael@0 | 138 | setEditableFieldValue(attr, test.value, inspector); |
michael@0 | 139 | yield nodeMutated; |
michael@0 | 140 | |
michael@0 | 141 | info("Asserting the new attributes after edition"); |
michael@0 | 142 | assertAttributes(test.node, test.expectedAttributes); |
michael@0 | 143 | |
michael@0 | 144 | info("Undo the change and assert that the attributes have been changed back"); |
michael@0 | 145 | yield undoChange(inspector); |
michael@0 | 146 | assertAttributes(test.node, test.originalAttributes); |
michael@0 | 147 | |
michael@0 | 148 | info("Redo the change and assert that the attributes have been changed again"); |
michael@0 | 149 | yield redoChange(inspector); |
michael@0 | 150 | assertAttributes(test.node, test.expectedAttributes); |
michael@0 | 151 | } |