browser_toolbox_select_event.js (4707B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const PAGE_URL = "data:text/html;charset=utf-8,test select events"; 7 8 requestLongerTimeout(2); 9 10 add_task(async function () { 11 const tab = await addTab(PAGE_URL); 12 13 let toolbox = await openToolboxForTab(tab, "webconsole", "bottom"); 14 await testSelectEvent("inspector"); 15 await testSelectEvent("webconsole"); 16 await testSelectEvent("styleeditor"); 17 await testSelectEvent("inspector"); 18 await testSelectEvent("webconsole"); 19 await testSelectEvent("styleeditor"); 20 21 await testToolSelectEvent("inspector"); 22 await testToolSelectEvent("webconsole"); 23 await testToolSelectEvent("styleeditor"); 24 await toolbox.destroy(); 25 26 toolbox = await openToolboxForTab(tab, "webconsole", "right"); 27 await testSelectEvent("inspector"); 28 await testSelectEvent("webconsole"); 29 await testSelectEvent("styleeditor"); 30 await testSelectEvent("inspector"); 31 await testSelectEvent("webconsole"); 32 await testSelectEvent("styleeditor"); 33 await toolbox.destroy(); 34 35 toolbox = await openToolboxForTab(tab, "webconsole", "window"); 36 await testSelectEvent("inspector"); 37 await testSelectEvent("webconsole"); 38 await testSelectEvent("styleeditor"); 39 await testSelectEvent("inspector"); 40 await testSelectEvent("webconsole"); 41 await testSelectEvent("styleeditor"); 42 await toolbox.destroy(); 43 44 await testSelectToolSamePanelRace(); 45 await testSelectToolDistinctPanelRace(); 46 47 /** 48 * Assert that selecting the given toolId raises a select event 49 * 50 * @param {toolId} Id of the tool to test 51 */ 52 async function testSelectEvent(toolId) { 53 const onSelect = toolbox.once("select"); 54 toolbox.selectTool(toolId); 55 const id = await onSelect; 56 is(id, toolId, toolId + " selected"); 57 } 58 59 /** 60 * Assert that selecting the given toolId raises its corresponding 61 * selected event 62 * 63 * @param {toolId} Id of the tool to test 64 */ 65 async function testToolSelectEvent(toolId) { 66 const onSelected = toolbox.once(toolId + "-selected"); 67 toolbox.selectTool(toolId); 68 await onSelected; 69 is(toolbox.currentToolId, toolId, toolId + " tool selected"); 70 } 71 72 /** 73 * Assert that two calls to selectTool won't race 74 */ 75 async function testSelectToolSamePanelRace() { 76 const toolbox = await openToolboxForTab(tab, "webconsole"); 77 let selected = false; 78 const onSelect = () => { 79 if (selected) { 80 ok(false, "Got more than one 'select' event"); 81 } else { 82 selected = true; 83 } 84 }; 85 toolbox.on("select", onSelect); 86 const p1 = toolbox.selectTool("inspector"); 87 const p2 = toolbox.selectTool("inspector"); 88 // Check that both promises don't resolve too early 89 const checkSelectToolResolution = panel => { 90 ok(selected, "selectTool resolves only after 'select' event is fired"); 91 const inspector = toolbox.getPanel("inspector"); 92 is(panel, inspector, "selecTool resolves to the panel instance"); 93 }; 94 p1.then(checkSelectToolResolution); 95 p2.then(checkSelectToolResolution); 96 await p1; 97 await p2; 98 const selectedPanels = toolbox.doc.querySelectorAll( 99 `.toolbox-panel[aria-selected="true"]` 100 ); 101 is(selectedPanels.length, 1); 102 is( 103 selectedPanels[0].id, 104 "toolbox-panel-inspector", 105 "Ensure that the inspector is actually selected" 106 ); 107 toolbox.off("select", onSelect); 108 109 await toolbox.destroy(); 110 } 111 112 /** 113 * Assert that two calls to selectTool won't race 114 */ 115 async function testSelectToolDistinctPanelRace() { 116 const toolbox = await openToolboxForTab(tab, "inspector"); 117 let selected = false; 118 const onSelect = () => { 119 if (selected) { 120 ok(false, "Got more than one 'select' event"); 121 } else { 122 selected = true; 123 } 124 }; 125 toolbox.on("select", onSelect); 126 // The load of the debugger will take some time, while the load of the inspector will be immediate 127 const p1 = toolbox.selectTool("jsdebugger"); 128 const p2 = toolbox.selectTool("inspector"); 129 // Check that both promises don't resolve too early 130 const checkSelectToolResolution = () => { 131 ok(selected, "selectTool resolves only after 'select' event is fired"); 132 }; 133 p1.then(checkSelectToolResolution); 134 p2.then(checkSelectToolResolution); 135 await p1; 136 await p2; 137 const selectedPanels = toolbox.doc.querySelectorAll( 138 `.toolbox-panel[aria-selected="true"]` 139 ); 140 is(selectedPanels.length, 1); 141 is( 142 selectedPanels[0].id, 143 "toolbox-panel-inspector", 144 "Ensure that the inspector is actually selected" 145 ); 146 toolbox.off("select", onSelect); 147 148 await toolbox.destroy(); 149 } 150 });