browser_dbg_multiple-windows.js (3671B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Make sure that the debugger attaches to the right tab when multiple windows 8 * are open. 9 */ 10 11 var { 12 DevToolsServer, 13 } = require("resource://devtools/server/devtools-server.js"); 14 var { 15 DevToolsClient, 16 } = require("resource://devtools/client/devtools-client.js"); 17 18 const TAB1_URL = "data:text/html;charset=utf-8,first-tab"; 19 const TAB2_URL = "data:text/html;charset=utf-8,second-tab"; 20 21 add_task(async function () { 22 DevToolsServer.init(); 23 DevToolsServer.registerAllActors(); 24 25 const transport = DevToolsServer.connectPipe(); 26 const client = new DevToolsClient(transport); 27 const [type] = await client.connect(); 28 is(type, "browser", "Root actor should identify itself as a browser."); 29 30 const tab = await addTab(TAB1_URL); 31 await testFirstTab(client, tab); 32 const win = await addWindow(TAB2_URL); 33 await testNewWindow(client, win); 34 testFocusFirst(client); 35 await testRemoveTab(client, win, tab); 36 await client.close(); 37 }); 38 39 async function testFirstTab(client, tab) { 40 ok(!!tab, "Second tab created."); 41 42 const tabs = await client.mainRoot.listTabs(); 43 const targetFront = tabs.find(grip => grip.url == TAB1_URL); 44 ok(targetFront, "Should find a target actor for the first tab."); 45 46 ok(!tabs[0].selected, "The previously opened tab isn't selected."); 47 ok(tabs[1].selected, "The first tab is selected."); 48 } 49 50 async function testNewWindow(client, win) { 51 ok(!!win, "Second window created."); 52 53 win.focus(); 54 55 const topWindow = Services.wm.getMostRecentBrowserWindow(); 56 is(topWindow, win, "The second window is on top."); 57 58 if (Services.focus.activeWindow != win) { 59 await new Promise(resolve => { 60 win.addEventListener( 61 "activate", 62 function onActivate(event) { 63 if (event.target != win) { 64 return; 65 } 66 win.removeEventListener("activate", onActivate, true); 67 resolve(); 68 }, 69 true 70 ); 71 }); 72 } 73 74 const tabs = await client.mainRoot.listTabs(); 75 ok(!tabs[0].selected, "The previously opened tab isn't selected."); 76 ok(!tabs[1].selected, "The first tab isn't selected."); 77 ok(tabs[2].selected, "The second tab is selected."); 78 } 79 80 async function testFocusFirst(client) { 81 const tab = window.gBrowser.selectedTab; 82 await ContentTask.spawn(tab.linkedBrowser, null, async function () { 83 const onFocus = new Promise(resolve => { 84 content.addEventListener("focus", resolve, { once: true }); 85 }); 86 await onFocus; 87 }); 88 89 const tabs = await client.mainRoot.listTabs(); 90 ok(!tabs[0].selected, "The previously opened tab isn't selected."); 91 ok(!tabs[1].selected, "The first tab is selected after focusing on i."); 92 ok(tabs[2].selected, "The second tab isn't selected."); 93 } 94 95 async function testRemoveTab(client, win, tab) { 96 win.close(); 97 98 // give it time to close 99 await new Promise(resolve => executeSoon(resolve)); 100 await continue_remove_tab(client, tab); 101 } 102 103 async function continue_remove_tab(client, tab) { 104 removeTab(tab); 105 106 const tabs = await client.mainRoot.listTabs(); 107 108 // Verify that tabs are no longer included in listTabs. 109 const foundTab1 = tabs.some(grip => grip.url == TAB1_URL); 110 const foundTab2 = tabs.some(grip => grip.url == TAB2_URL); 111 ok(!foundTab1, "Tab1 should be gone."); 112 ok(!foundTab2, "Tab2 should be gone."); 113 114 ok(tabs[0].selected, "The previously opened tab is selected."); 115 } 116 117 async function addWindow(url) { 118 info("Adding window: " + url); 119 const onNewWindow = BrowserTestUtils.waitForNewWindow({ url }); 120 window.open(url, "_blank", "noopener"); 121 return onNewWindow; 122 }