browser_inspector_delete-selected-node-02.js (5070B)
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/. */ 4 "use strict"; 5 6 // Test that when nodes are being deleted in the page, the current selection 7 // and therefore the markup view, css rule view, computed view, font view, 8 // box model view, and breadcrumbs, reset accordingly to show the right node 9 10 const TEST_PAGE = URL_ROOT + "doc_inspector_delete-selected-node-02.html"; 11 12 add_task(async function () { 13 const { inspector } = await openInspectorForURL(TEST_PAGE); 14 15 await testManuallyDeleteSelectedNode(); 16 await testAutomaticallyDeleteSelectedNode(); 17 await testDeleteSelectedNodeContainerFrame(); 18 await testDeleteWithNonElementNode(); 19 20 async function testManuallyDeleteSelectedNode() { 21 info( 22 "Selecting a node, deleting it via context menu and checking that " + 23 "its parent node is selected and breadcrumbs are updated." 24 ); 25 26 await selectNode("#deleteManually", inspector); 27 const nodeToBeDeleted = inspector.selection.nodeFront; 28 await deleteNodeWithContextMenu(nodeToBeDeleted, inspector); 29 30 info("Performing checks."); 31 await assertNodeSelectedAndPanelsUpdated( 32 "#selectedAfterDelete", 33 "li#selectedAfterDelete" 34 ); 35 } 36 37 async function testAutomaticallyDeleteSelectedNode() { 38 info( 39 "Selecting a node, deleting it via javascript and checking that " + 40 "its parent node is selected and breadcrumbs are updated." 41 ); 42 43 const div = await getNodeFront("#deleteAutomatically", inspector); 44 await selectNode(div, inspector); 45 46 info("Deleting selected node via javascript."); 47 await inspector.walker.removeNode(div); 48 49 info("Waiting for inspector to update."); 50 await inspector.once("inspector-updated"); 51 52 info("Inspector updated, performing checks."); 53 await assertNodeSelectedAndPanelsUpdated( 54 "#deleteChildren", 55 "ul#deleteChildren" 56 ); 57 } 58 59 async function testDeleteSelectedNodeContainerFrame() { 60 info( 61 "Selecting a node inside iframe, deleting the iframe via javascript " + 62 "and checking the parent node of the iframe is selected and " + 63 "breadcrumbs are updated." 64 ); 65 66 info("Selecting an element inside iframe."); 67 await selectNodeInFrames(["#deleteIframe", "#deleteInIframe"], inspector); 68 69 info("Deleting parent iframe node via javascript."); 70 const onInspectorUpdated = inspector.once("inspector-updated"); 71 72 SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 73 content.document.querySelector("iframe#deleteIframe").remove(); 74 }); 75 76 info("Waiting for inspector to update."); 77 await onInspectorUpdated; 78 79 info("Inspector updated, performing checks."); 80 await assertNodeSelectedAndPanelsUpdated("body", "body"); 81 } 82 83 async function testDeleteWithNonElementNode() { 84 info( 85 "Selecting a node, deleting it via context menu and checking that " + 86 "its parent node is selected and breadcrumbs are updated " + 87 "when the node is followed by a non-element node" 88 ); 89 90 await selectNode("#deleteWithNonElement", inspector); 91 const nodeToBeDeleted = inspector.selection.nodeFront; 92 await deleteNodeWithContextMenu(nodeToBeDeleted, inspector); 93 94 let expectedCrumbs = ["html", "body", "div#deleteToMakeSingleTextNode"]; 95 await assertNodeSelectedAndCrumbsUpdated(expectedCrumbs, Node.TEXT_NODE); 96 97 // Delete node with key, as cannot delete text node with 98 // context menu at this time. 99 inspector.markup._frame.focus(); 100 EventUtils.synthesizeKey("KEY_Delete"); 101 await inspector.once("inspector-updated"); 102 103 expectedCrumbs = ["html", "body", "div#deleteToMakeSingleTextNode"]; 104 await assertNodeSelectedAndCrumbsUpdated(expectedCrumbs, Node.ELEMENT_NODE); 105 } 106 107 function assertNodeSelectedAndCrumbsUpdated( 108 expectedCrumbs, 109 expectedNodeType 110 ) { 111 info("Performing checks"); 112 const actualNodeType = inspector.selection.nodeFront.nodeType; 113 is(actualNodeType, expectedNodeType, "The node has the right type"); 114 115 const breadcrumbs = inspector.panelDoc.querySelectorAll( 116 "#inspector-breadcrumbs .html-arrowscrollbox-inner > *" 117 ); 118 is( 119 breadcrumbs.length, 120 expectedCrumbs.length, 121 "Have the correct number of breadcrumbs" 122 ); 123 for (let i = 0; i < breadcrumbs.length; i++) { 124 is( 125 breadcrumbs[i].textContent, 126 expectedCrumbs[i], 127 "Text content for button " + i + " is correct" 128 ); 129 } 130 } 131 132 async function assertNodeSelectedAndPanelsUpdated(selector, crumbLabel) { 133 const nodeFront = await getNodeFront(selector, inspector); 134 is(inspector.selection.nodeFront, nodeFront, "The right node is selected"); 135 136 const breadcrumbs = inspector.panelDoc.querySelector( 137 "#inspector-breadcrumbs .html-arrowscrollbox-inner" 138 ); 139 is( 140 breadcrumbs.querySelector("button[aria-pressed=true]").textContent, 141 crumbLabel, 142 "The right breadcrumb is selected" 143 ); 144 } 145 });