browser_inspector_search-06.js (3232B)
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 5 "use strict"; 6 7 // Check that searching again for nodes after they are removed or added from the 8 // DOM works correctly. 9 10 const TEST_URL = URL_ROOT + "doc_inspector_search.html"; 11 12 add_task(async function () { 13 const { inspector } = await openInspectorForURL(TEST_URL); 14 15 info("Searching for test node #d1"); 16 await focusSearchBoxUsingShortcut(inspector.panelWin); 17 await synthesizeKeys(["#", "d", "1", "VK_RETURN"], inspector); 18 19 await inspector.search.once("search-result"); 20 assertHasResult(inspector, true); 21 22 info("Removing node #d1"); 23 // Expect an inspector-updated event here, because removing #d1 causes the 24 // breadcrumbs to update (since #d1 is displayed in it). 25 const onUpdated = inspector.once("inspector-updated"); 26 await mutatePage(inspector, () => 27 content.document.getElementById("d1").remove() 28 ); 29 await onUpdated; 30 31 info("Pressing return button to search again for node #d1."); 32 await synthesizeKeys("VK_RETURN", inspector); 33 34 await inspector.search.once("search-result"); 35 assertHasResult(inspector, false); 36 37 info("Emptying the field and searching for a node that doesn't exist: #d3"); 38 const keys = [ 39 "VK_BACK_SPACE", 40 "VK_BACK_SPACE", 41 "VK_BACK_SPACE", 42 "#", 43 "d", 44 "3", 45 "VK_RETURN", 46 ]; 47 await synthesizeKeys(keys, inspector); 48 49 await inspector.search.once("search-result"); 50 assertHasResult(inspector, false); 51 52 info("Create the #d3 node in the page"); 53 // No need to expect an inspector-updated event here, Creating #d3 isn't going 54 // to update the breadcrumbs in any ways. 55 await mutatePage(inspector, () => 56 content.document 57 .getElementById("d2") 58 .insertAdjacentHTML("afterend", "<div id=d3></div>") 59 ); 60 61 info("Pressing return button to search again for node #d3."); 62 await synthesizeKeys("VK_RETURN", inspector); 63 64 await inspector.search.once("search-result"); 65 assertHasResult(inspector, true); 66 67 // Catch-all event for remaining server requests when searching for the new 68 // node. 69 await inspector.once("inspector-updated"); 70 }); 71 72 async function synthesizeKeys(keys, inspector) { 73 if (typeof keys === "string") { 74 keys = [keys]; 75 } 76 77 for (const key of keys) { 78 info("Synthesizing key " + key + " in the search box"); 79 const eventHandled = once(inspector.searchBox, "keypress", true); 80 const onSearchProcessingDone = 81 inspector.searchSuggestions.once("processing-done"); 82 EventUtils.synthesizeKey(key, {}, inspector.panelWin); 83 await eventHandled; 84 info("Waiting for the search query to complete"); 85 await onSearchProcessingDone; 86 } 87 } 88 89 function assertHasResult(inspector, expectResult) { 90 is( 91 inspector.searchBox.parentNode.classList.contains( 92 "devtools-searchbox-no-match" 93 ), 94 !expectResult, 95 "There are" + (expectResult ? "" : " no") + " search results" 96 ); 97 } 98 99 async function mutatePage(inspector, mutationFn) { 100 const onMutation = inspector.once("markupmutation"); 101 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], mutationFn); 102 await onMutation; 103 }