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