tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_sidebar_adopt.js (2736B)


      1 /* This test checks that the SidebarFocused event doesn't fire in adopted
      2 * windows when the sidebar gets opened during window opening, to make sure
      3 * that sidebars don't steal focus from the page in this case (Bug 1394207).
      4 * There's another case not covered here that has the same expected behavior -
      5 * during the initial browser startup - but it would be hard to do with a mochitest. */
      6 
      7 registerCleanupFunction(() => {
      8  SidebarController.hide();
      9 });
     10 
     11 function failIfSidebarFocusedFires() {
     12  ok(false, "This event shouldn't have fired");
     13 }
     14 
     15 add_setup(function () {
     16  CustomizableUI.addWidgetToArea("sidebar-button", "nav-bar");
     17  registerCleanupFunction(() =>
     18    CustomizableUI.removeWidgetFromArea("sidebar-button")
     19  );
     20 });
     21 
     22 add_task(async function testAdoptedTwoWindows() {
     23  // First open a new window, show the sidebar in that window, and close it.
     24  // Then, open another new window and confirm that the sidebar is closed since it is
     25  // being adopted from the main window which doesn't have a shown sidebar. See Bug 1407737.
     26  info("Ensure that sidebar state is adopted only from the opener");
     27 
     28  let win1 = await BrowserTestUtils.openNewBrowserWindow();
     29  await win1.SidebarController.show("viewBookmarksSidebar");
     30  await BrowserTestUtils.closeWindow(win1);
     31 
     32  let win2 = await BrowserTestUtils.openNewBrowserWindow();
     33  ok(
     34    !win2.document.getElementById("sidebar-button").hasAttribute("checked"),
     35    "Sidebar button isn't checked"
     36  );
     37  ok(!win2.SidebarController.isOpen, "Sidebar is closed");
     38  await BrowserTestUtils.closeWindow(win2);
     39 });
     40 
     41 add_task(async function testEventsReceivedInMainWindow() {
     42  info(
     43    "Opening the sidebar and expecting both SidebarShown and SidebarFocused events"
     44  );
     45 
     46  let initialShown = BrowserTestUtils.waitForEvent(window, "SidebarShown");
     47  let initialFocus = BrowserTestUtils.waitForEvent(window, "SidebarFocused");
     48 
     49  await SidebarController.show("viewBookmarksSidebar");
     50  await initialShown;
     51  await initialFocus;
     52 
     53  ok(true, "SidebarShown and SidebarFocused events fired on a new window");
     54 });
     55 
     56 add_task(async function testEventReceivedInNewWindow() {
     57  info(
     58    "Opening a new window and expecting the SidebarFocused event to not fire"
     59  );
     60 
     61  let promiseNewWindow = BrowserTestUtils.waitForNewWindow();
     62  let win = OpenBrowserWindow();
     63 
     64  let adoptedShown = BrowserTestUtils.waitForEvent(win, "SidebarShown");
     65  win.addEventListener("SidebarFocused", failIfSidebarFocusedFires);
     66  registerCleanupFunction(async function () {
     67    win.removeEventListener("SidebarFocused", failIfSidebarFocusedFires);
     68    await BrowserTestUtils.closeWindow(win);
     69  });
     70 
     71  await promiseNewWindow;
     72  await adoptedShown;
     73  ok(true, "SidebarShown event fired on an adopted window");
     74 });