browser_inspector_menu-05-attribute-items.js (4434B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 "use strict"; 4 5 // Test that attribute items work in the context menu 6 7 const TEST_URL = URL_ROOT + "doc_inspector_menu.html"; 8 9 add_task(async function () { 10 const { inspector, toolbox } = await openInspectorForURL(TEST_URL); 11 await selectNode("#attributes", inspector); 12 13 await testAddAttribute(); 14 await testCopyAttributeValue(); 15 await testCopyLongAttributeValue(); 16 await testEditAttribute(); 17 await testRemoveAttribute(); 18 19 async function testAddAttribute() { 20 info("Triggering 'Add Attribute' and waiting for mutation to occur"); 21 const addAttribute = await getMenuItem("node-menu-add-attribute"); 22 23 addAttribute.click(); 24 25 EventUtils.sendString('class="u-hidden"'); 26 const onMutation = inspector.once("markupmutation"); 27 EventUtils.synthesizeKey("KEY_Enter"); 28 await onMutation; 29 30 const hasAttribute = await hasMatchingElementInContentPage( 31 "#attributes.u-hidden" 32 ); 33 ok(hasAttribute, "attribute was successfully added"); 34 } 35 36 async function testCopyAttributeValue() { 37 info( 38 "Testing 'Copy Attribute Value' and waiting for clipboard promise to resolve" 39 ); 40 const copyAttributeValue = await getMenuItem("node-menu-copy-attribute"); 41 42 info( 43 "Triggering 'Copy Attribute Value' and waiting for clipboard to copy the value" 44 ); 45 inspector.markup.contextMenu.nodeMenuTriggerInfo = { 46 type: "attribute", 47 name: "data-edit", 48 value: "the", 49 }; 50 51 await waitForClipboardPromise(() => copyAttributeValue.click(), "the"); 52 } 53 54 async function testCopyLongAttributeValue() { 55 info("Testing 'Copy Attribute Value' copies very long attribute values"); 56 const copyAttributeValue = await getMenuItem("node-menu-copy-attribute"); 57 const longAttribute = 58 "#01234567890123456789012345678901234567890123456789" + 59 "12345678901234567890123456789012345678901234567890123456789012345678901" + 60 "23456789012345678901234567890123456789012345678901234567890123456789012" + 61 "34567890123456789012345678901234567890123456789012345678901234567890123"; 62 63 inspector.markup.contextMenu.nodeMenuTriggerInfo = { 64 type: "attribute", 65 name: "data-edit", 66 value: longAttribute, 67 }; 68 69 await waitForClipboardPromise( 70 () => copyAttributeValue.click(), 71 longAttribute 72 ); 73 } 74 75 async function testEditAttribute() { 76 info("Testing 'Edit Attribute' menu item"); 77 const editAttribute = await getMenuItem("node-menu-edit-attribute"); 78 79 info("Triggering 'Edit Attribute' and waiting for mutation to occur"); 80 inspector.markup.contextMenu.nodeMenuTriggerInfo = { 81 type: "attribute", 82 name: "data-edit", 83 }; 84 editAttribute.click(); 85 EventUtils.sendString("data-edit='edited'"); 86 const onMutation = inspector.once("markupmutation"); 87 EventUtils.synthesizeKey("KEY_Enter"); 88 await onMutation; 89 90 const isAttributeChanged = await hasMatchingElementInContentPage( 91 "#attributes[data-edit='edited']" 92 ); 93 ok(isAttributeChanged, "attribute was successfully edited"); 94 } 95 96 async function testRemoveAttribute() { 97 info("Testing 'Remove Attribute' menu item"); 98 const removeAttribute = await getMenuItem("node-menu-remove-attribute"); 99 100 info("Triggering 'Remove Attribute' and waiting for mutation to occur"); 101 inspector.markup.contextMenu.nodeMenuTriggerInfo = { 102 type: "attribute", 103 name: "data-remove", 104 }; 105 const onMutation = inspector.once("markupmutation"); 106 removeAttribute.click(); 107 await onMutation; 108 109 const hasAttribute = await hasMatchingElementInContentPage( 110 "#attributes[data-remove]" 111 ); 112 ok(!hasAttribute, "attribute was successfully removed"); 113 } 114 115 async function getMenuItem(id) { 116 const allMenuItems = openContextMenuAndGetAllItems(inspector, { 117 target: getContainerForSelector("#attributes", inspector).tagLine, 118 }); 119 const menuItem = allMenuItems.find(i => i.id === id); 120 ok(menuItem, "Menu item '" + id + "' found"); 121 122 // Close the menu so synthesizing future keys won't select menu items. 123 const contextMenu = toolbox.topDoc.querySelector("#markup-context-menu"); 124 const popupHiddenPromise = BrowserTestUtils.waitForEvent( 125 contextMenu, 126 "popuphidden" 127 ); 128 129 contextMenu.hidePopup(); 130 await popupHiddenPromise; 131 132 return menuItem; 133 } 134 });