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 edit-outer-html 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 a node (a selector) and a new outer-HTML to be |
michael@0 | 9 | * inserted in place of the current one for that node. |
michael@0 | 10 | * This test runner will wait for the mutation event to be fired and will check |
michael@0 | 11 | * a few things. Each test may also provide its own validate function to perform |
michael@0 | 12 | * assertions and verify that the new outer html is correct. |
michael@0 | 13 | * @param {Array} tests See runEditOuterHTMLTest for the structure |
michael@0 | 14 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 15 | * opened |
michael@0 | 16 | * @return a promise that resolves when the tests have run |
michael@0 | 17 | */ |
michael@0 | 18 | function runEditOuterHTMLTests(tests, inspector) { |
michael@0 | 19 | info("Running " + tests.length + " edit-outer-html tests"); |
michael@0 | 20 | return Task.spawn(function* () { |
michael@0 | 21 | for (let step of TEST_DATA) { |
michael@0 | 22 | yield runEditOuterHTMLTest(step, inspector); |
michael@0 | 23 | } |
michael@0 | 24 | }); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * Run a single edit-outer-html test. |
michael@0 | 29 | * See runEditOuterHTMLTests for a description. |
michael@0 | 30 | * @param {Object} test A test object should contain the following properties: |
michael@0 | 31 | * - selector {String} a css selector targeting the node to edit |
michael@0 | 32 | * - oldHTML {String} |
michael@0 | 33 | * - newHTML {String} |
michael@0 | 34 | * - validate {Function} will be executed when the edition test is done, |
michael@0 | 35 | * after the new outer-html has been inserted. Should be used to verify |
michael@0 | 36 | * the actual DOM, see if it corresponds to the newHTML string provided |
michael@0 | 37 | * @param {InspectorPanel} inspector The instance of InspectorPanel currently |
michael@0 | 38 | * opened |
michael@0 | 39 | */ |
michael@0 | 40 | function* runEditOuterHTMLTest(test, inspector) { |
michael@0 | 41 | info("Running an edit outerHTML test on '" + test.selector + "'"); |
michael@0 | 42 | yield selectNode(test.selector, inspector); |
michael@0 | 43 | let oldNodeFront = inspector.selection.nodeFront; |
michael@0 | 44 | |
michael@0 | 45 | info("Listening for the markupmutation event"); |
michael@0 | 46 | // This event fires once the outerHTML is set, with a target as the parent node and a type of "childList". |
michael@0 | 47 | let mutated = inspector.once("markupmutation"); |
michael@0 | 48 | info("Editing the outerHTML"); |
michael@0 | 49 | inspector.markup.updateNodeOuterHTML(inspector.selection.nodeFront, test.newHTML, test.oldHTML); |
michael@0 | 50 | let mutations = yield mutated; |
michael@0 | 51 | ok(true, "The markupmutation event has fired, mutation done"); |
michael@0 | 52 | |
michael@0 | 53 | info("Check to make the sure the correct mutation event was fired, and that the parent is selected"); |
michael@0 | 54 | let nodeFront = inspector.selection.nodeFront; |
michael@0 | 55 | let mutation = mutations[0]; |
michael@0 | 56 | let isFromOuterHTML = mutation.removed.some(n => n === oldNodeFront); |
michael@0 | 57 | |
michael@0 | 58 | ok(isFromOuterHTML, "The node is in the 'removed' list of the mutation"); |
michael@0 | 59 | is(mutation.type, "childList", "Mutation is a childList after updating outerHTML"); |
michael@0 | 60 | is(mutation.target, nodeFront, "Parent node is selected immediately after setting outerHTML"); |
michael@0 | 61 | |
michael@0 | 62 | // Wait for node to be reselected after outerHTML has been set |
michael@0 | 63 | yield inspector.selection.once("new-node"); |
michael@0 | 64 | |
michael@0 | 65 | // Typically selectedNode will === pageNode, but if a new element has been injected in front |
michael@0 | 66 | // of it, this will not be the case. If this happens. |
michael@0 | 67 | let selectedNode = inspector.selection.node; |
michael@0 | 68 | let nodeFront = inspector.selection.nodeFront; |
michael@0 | 69 | let pageNode = getNode(test.selector); |
michael@0 | 70 | |
michael@0 | 71 | if (test.validate) { |
michael@0 | 72 | test.validate(pageNode, selectedNode); |
michael@0 | 73 | } else { |
michael@0 | 74 | is(pageNode, selectedNode, "Original node (grabbed by selector) is selected"); |
michael@0 | 75 | is(pageNode.outerHTML, test.newHTML, "Outer HTML has been updated"); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | // Wait for the inspector to be fully updated to avoid causing errors by |
michael@0 | 79 | // abruptly closing hanging requests when the test ends |
michael@0 | 80 | yield inspector.once("inspector-updated"); |
michael@0 | 81 | } |