browser_inspector_menu-06-other.js (7312B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 // Tests for menuitem functionality that doesn't fit into any specific category 6 const TEST_URL = URL_ROOT + "doc_inspector_menu.html"; 7 add_task(async function () { 8 const { inspector, toolbox } = await openInspectorForURL(TEST_URL); 9 await testShowDOMProperties(); 10 await testDuplicateNode(); 11 await testDeleteNode(); 12 await testDeleteTextNode(); 13 await testDeleteRootNode(); 14 await testScrollIntoView(); 15 16 async function testDuplicateNode() { 17 info("Testing 'Duplicate Node' menu item for normal elements."); 18 19 await selectNode(".duplicate", inspector); 20 is( 21 await getNumberOfMatchingElementsInContentPage(".duplicate"), 22 1, 23 "There should initially be 1 .duplicate node" 24 ); 25 26 const allMenuItems = openContextMenuAndGetAllItems(inspector); 27 const menuItem = allMenuItems.find( 28 item => item.id === "node-menu-duplicatenode" 29 ); 30 ok(menuItem, "'Duplicate node' menu item should exist"); 31 32 info("Triggering 'Duplicate Node' and waiting for inspector to update"); 33 const updated = inspector.once("markupmutation"); 34 menuItem.click(); 35 await updated; 36 37 is( 38 await getNumberOfMatchingElementsInContentPage(".duplicate"), 39 2, 40 "The duplicated node should be in the markup." 41 ); 42 43 const container = await getContainerForSelector( 44 ".duplicate + .duplicate", 45 inspector 46 ); 47 ok(container, "A MarkupContainer should be created for the new node"); 48 } 49 50 async function testDeleteNode() { 51 info("Testing 'Delete Node' menu item for normal elements."); 52 await selectNode("#delete", inspector); 53 const allMenuItems = openContextMenuAndGetAllItems(inspector); 54 const deleteNode = allMenuItems.find( 55 item => item.id === "node-menu-delete" 56 ); 57 ok(deleteNode, "the popup menu has a delete menu item"); 58 const updated = inspector.once("inspector-updated"); 59 60 info("Triggering 'Delete Node' and waiting for inspector to update"); 61 deleteNode.click(); 62 await updated; 63 64 ok(!(await hasMatchingElementInContentPage("#delete")), "Node deleted"); 65 } 66 67 async function testDeleteTextNode() { 68 info("Testing 'Delete Node' menu item for text elements."); 69 const { walker } = inspector; 70 const divBefore = await walker.querySelector( 71 walker.rootNode, 72 "#nestedHiddenElement" 73 ); 74 const { nodes } = await walker.children(divBefore); 75 await selectNode(nodes[0], inspector, "test-highlight"); 76 77 const allMenuItems = openContextMenuAndGetAllItems(inspector); 78 const deleteNode = allMenuItems.find( 79 item => item.id === "node-menu-delete" 80 ); 81 ok(deleteNode, "the popup menu has a delete menu item"); 82 ok(!deleteNode.disabled, "the delete menu item is not disabled"); 83 const updated = inspector.once("inspector-updated"); 84 85 info("Triggering 'Delete Node' and waiting for inspector to update"); 86 deleteNode.click(); 87 await updated; 88 89 const divAfter = await walker.querySelector( 90 walker.rootNode, 91 "#nestedHiddenElement" 92 ); 93 const nodesAfter = (await walker.children(divAfter)).nodes; 94 ok(!nodesAfter.length, "the node still had children"); 95 } 96 97 async function testDeleteRootNode() { 98 info("Testing 'Delete Node' menu item does not delete root node."); 99 await selectNode("html", inspector); 100 101 const allMenuItems = openContextMenuAndGetAllItems(inspector); 102 const deleteNode = allMenuItems.find( 103 item => item.id === "node-menu-delete" 104 ); 105 deleteNode.click(); 106 107 await new Promise(resolve => { 108 executeSoon(resolve); 109 }); 110 111 const hasDocumentElement = await SpecialPowers.spawn( 112 gBrowser.selectedBrowser, 113 [], 114 () => !!content.document.documentElement 115 ); 116 ok(hasDocumentElement, "Document element still alive."); 117 } 118 119 async function testShowDOMProperties() { 120 info("Testing 'Show DOM Properties' menu item."); 121 const allMenuItems = openContextMenuAndGetAllItems(inspector); 122 const showDOMPropertiesNode = allMenuItems.find( 123 item => item.id === "node-menu-showdomproperties" 124 ); 125 ok(showDOMPropertiesNode, "the popup menu has a show dom properties item"); 126 127 const consoleOpened = toolbox.once("webconsole-ready"); 128 129 info("Triggering 'Show DOM Properties' and waiting for inspector open"); 130 showDOMPropertiesNode.click(); 131 await consoleOpened; 132 133 const webconsoleUI = toolbox.getPanel("webconsole").hud.ui; 134 135 await poll( 136 () => { 137 const messages = [ 138 ...webconsoleUI.outputNode.querySelectorAll(".message"), 139 ]; 140 const nodeMessage = messages.find(m => m.textContent.includes("body")); 141 // wait for the object to be expanded 142 return ( 143 nodeMessage && 144 nodeMessage.querySelectorAll(".object-inspector .node").length > 10 145 ); 146 }, 147 "Waiting for the element node to be expanded", 148 10, 149 1000 150 ); 151 152 info("Close split console"); 153 await toolbox.toggleSplitConsole(); 154 } 155 156 async function testScrollIntoView() { 157 info("Testing 'Scroll Into View' menu item for normal elements."); 158 159 await selectNode("#scroll-view", inspector); 160 161 let showScrollIntoViewNode = openContextMenuAndGetAllItems(inspector).find( 162 item => item.id === "node-menu-scrollnodeintoview" 163 ); 164 ok(showScrollIntoViewNode, "the popup menu has a scroll into view item"); 165 166 const isElementInViewport = () => 167 SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 168 const rect = content.document 169 .querySelector("#scroll-view") 170 .getBoundingClientRect(); 171 172 return ( 173 rect.top >= 0 && 174 rect.left >= 0 && 175 rect.bottom <= content.innerHeight && 176 rect.right <= content.innerWidth 177 ); 178 }); 179 180 is( 181 await isElementInViewport(), 182 false, 183 "Sanity check: Element is not in viewport" 184 ); 185 186 let onNodeScrolledIntoView = inspector.markup.once( 187 "node-scrolled-into-view" 188 ); 189 showScrollIntoViewNode.click(); 190 await onNodeScrolledIntoView; 191 192 is( 193 await isElementInViewport(), 194 true, 195 "Element is in the viewport after using Scroll into view context menu item" 196 ); 197 198 info("Scroll back up"); 199 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => { 200 content.document.querySelector("h1").scrollIntoView(); 201 }); 202 203 is( 204 await isElementInViewport(), 205 false, 206 "Element is not in viewport anymore after scrolling up" 207 ); 208 209 info("Check that we can scroll to pseudo elements"); 210 const pseudo = await getNodeFront("#scroll-view", inspector); 211 const children = await inspector.walker.children(pseudo); 212 const before = children.nodes[0]; 213 await selectNode(before, inspector); 214 215 showScrollIntoViewNode = openContextMenuAndGetAllItems(inspector).find( 216 item => item.id === "node-menu-scrollnodeintoview" 217 ); 218 onNodeScrolledIntoView = inspector.markup.once("node-scrolled-into-view"); 219 showScrollIntoViewNode.click(); 220 await onNodeScrolledIntoView; 221 222 is( 223 await isElementInViewport(), 224 true, 225 "Element is in the viewport after using Scroll into view context menu item on pseudo element" 226 ); 227 } 228 });