browser_inspector_highlighter-autohide-config_03.js (2795B)
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 const { getTimeoutMultiplier } = ChromeUtils.importESModule( 8 "chrome://remote/content/shared/AppInfo.sys.mjs" 9 ); 10 11 // Test that configuring a highlighter to autohide twice 12 // will replace the first timer and hide just once. 13 add_task(async function () { 14 info("Loading the test document and opening the inspector"); 15 const { inspector } = await openInspectorForURL( 16 "data:text/html;charset=utf-8,<p id='one'>TEST 1</p>" 17 ); 18 19 // On opt builds, use 500ms and 1000ms for popup timeouts. 20 // On debug builds, multiply the first timeout by the platform multiplier to avoid 21 // an extra `hidden` event between the `showHighlighterTypeForNode` commands. 22 const FIRST_POPUP_TIMEOUT = getTimeoutMultiplier() * 500; 23 const SECOND_POPUP_TIMEOUT = 1000; 24 25 const nodeFront = await getNodeFront("#one", inspector); 26 27 const waitForShowEvents = waitForNEvents( 28 inspector.highlighters, 29 "highlighter-shown", 30 2 31 ); 32 const waitForHideEvents = waitForNEvents( 33 inspector.highlighters, 34 "highlighter-hidden", 35 1 36 ); 37 38 info("Show Box Model Highlighter, then hide after half a second"); 39 await inspector.highlighters.showHighlighterTypeForNode( 40 inspector.highlighters.TYPES.BOXMODEL, 41 nodeFront, 42 { duration: FIRST_POPUP_TIMEOUT } 43 ); 44 45 info("Show Box Model Highlighter again, then hide after one second"); 46 await inspector.highlighters.showHighlighterTypeForNode( 47 inspector.highlighters.TYPES.BOXMODEL, 48 nodeFront, 49 { duration: SECOND_POPUP_TIMEOUT } 50 ); 51 52 info("Waiting for 2 highlighter-shown and 1 highlighter-hidden event"); 53 await Promise.all([waitForShowEvents, waitForHideEvents]); 54 55 /* 56 Since the second duration passed is longer than the first and is supposed to overwrite 57 the first, it is reasonable to expect that the "highlighter-hidden" event was emitted 58 after the second (longer) duration expired. As an added check, we naively wait for an 59 additional time amounting to the sum of both durations to check if the first timer was 60 somehow not overwritten and fires another "highlighter-hidden" event. 61 */ 62 let wasEmitted = false; 63 const waitForExtraEvent = new Promise(resolve => { 64 const _handler = () => { 65 wasEmitted = true; 66 resolve(); 67 }; 68 69 inspector.highlighters.on("highlighter-hidden", _handler, { once: true }); 70 }); 71 72 info("Wait to see if another highlighter-hidden event is emitted"); 73 await Promise.race([ 74 waitForExtraEvent, 75 wait(FIRST_POPUP_TIMEOUT + SECOND_POPUP_TIMEOUT), 76 ]); 77 78 is(wasEmitted, false, "An extra highlighter-hidden event was not emitted"); 79 });