tor-browser

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

browser_taskbarTabs_chromeTest.js (7201B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 XPCOMUtils.defineLazyServiceGetters(this, {
      6  Favicons: ["@mozilla.org/browser/favicon-service;1", Ci.nsIFaviconService],
      7 });
      8 
      9 const gAudioPage =
     10  "https://example.com/browser/dom/base/test/file_audioLoop.html";
     11 
     12 // Given a window, check if it meets all requirements
     13 // of the taskbar tab chrome UI
     14 function checkWindowChrome(win) {
     15  let document = win.document.documentElement;
     16 
     17  ok(
     18    document.hasAttribute("taskbartab"),
     19    "The window HTML should have a taskbartab attribute"
     20  );
     21 
     22  ok(win.gURLBar.readOnly, "The URL bar should be read-only");
     23 
     24  ok(
     25    win.document.getElementById("TabsToolbar").collapsed,
     26    "The tab bar should be collapsed"
     27  );
     28 
     29  is(
     30    document.getAttribute("chromehidden"),
     31    "menubar directories extrachrome ",
     32    "The correct chrome hidden attributes should be populated"
     33  );
     34 
     35  ok(!win.menubar.visible, "menubar barprop should not be visible");
     36  ok(!win.personalbar.visible, "personalbar barprop should not be visible");
     37 
     38  let starButton = win.document.querySelector("#star-button-box");
     39  is(
     40    win.getComputedStyle(starButton).display,
     41    "none",
     42    "Bookmark button should not be visible"
     43  );
     44 
     45  ok(
     46    !document.hasAttribute("fxatoolbarmenu"),
     47    "Firefox accounts menu should not be displayed"
     48  );
     49 
     50  ok(
     51    document.hasAttribute("fxadisabled"),
     52    "fxadisabled attribute should exist"
     53  );
     54 
     55  let sideBarElement = win.document.getElementById("sidebar-main");
     56  ok(BrowserTestUtils.isHidden(sideBarElement), "The sidebar should be hidden");
     57 }
     58 
     59 // Given a window, check if hamburger menu
     60 // buttons that aren't relevant to taskbar tabs
     61 // are hidden
     62 async function checkHamburgerMenu(win) {
     63  win.document.getElementById("PanelUI-menu-button").click();
     64 
     65  // Set up a MutationObserver to await for the hamburger menu
     66  // DOM element and CSS to be loaded & applied.
     67  // The observer itself verifies that the "new tab" button is hidden
     68  await new Promise(resolve => {
     69    const observer = new MutationObserver(() => {
     70      const newTabButton = win.document.querySelector(
     71        "#appMenu-new-tab-button2"
     72      );
     73      if (
     74        newTabButton &&
     75        win.getComputedStyle(newTabButton).display == "none"
     76      ) {
     77        observer.disconnect();
     78        resolve();
     79      }
     80    });
     81 
     82    observer.observe(win.document, { childList: true, subtree: true });
     83  });
     84 
     85  is(
     86    win.getComputedStyle(
     87      win.document.querySelector("#appMenu-new-window-button2")
     88    ).display,
     89    "none",
     90    "New window button in hamburger menu should not be visible"
     91  );
     92 
     93  is(
     94    win.getComputedStyle(
     95      win.document.querySelector("#appMenu-new-private-window-button2")
     96    ).display,
     97    "none",
     98    "New private window button in hamburger menu should not be visible"
     99  );
    100 
    101  is(
    102    win.getComputedStyle(
    103      win.document.querySelector("#appMenu-bookmarks-button")
    104    ).display,
    105    "none",
    106    "Bookmarks button in hamburger menu should not be visible"
    107  );
    108 }
    109 
    110 // Creates a Taskbar Tab window and verifies UI elements match expectations.
    111 add_task(async function testOpenWindowChrome() {
    112  const win = await openTaskbarTabWindow();
    113 
    114  checkWindowChrome(win);
    115  await checkHamburgerMenu(win);
    116 
    117  await BrowserTestUtils.closeWindow(win);
    118 });
    119 
    120 add_task(async function testFaviconUpdates() {
    121  const win = await openTaskbarTabWindow();
    122  const favicon = win.document.getElementById("taskbar-tabs-favicon");
    123  const tab = win.gBrowser.selectedTab;
    124 
    125  is(favicon.src, Favicons.defaultFavicon.spec, "starts with default favicon");
    126 
    127  let promise = Promise.all([
    128    waitForTabAttributeChange(tab, "image"),
    129    BrowserTestUtils.browserLoaded(tab.linkedBrowser),
    130  ]);
    131  BrowserTestUtils.startLoadingURIString(
    132    tab.linkedBrowser,
    133    "data:text/html,<link rel='shortcut icon' href='https://example.com/favicon.ico'>"
    134  );
    135  await promise;
    136 
    137  is(favicon.src, win.gBrowser.getIcon(tab), "updates favicon when changed");
    138 
    139  promise = Promise.all([
    140    waitForTabAttributeChange(tab, "busy").then(() =>
    141      waitForTabAttributeChange(tab, "busy")
    142    ),
    143    BrowserTestUtils.browserLoaded(tab.linkedBrowser),
    144  ]);
    145  BrowserTestUtils.startLoadingURIString(
    146    tab.linkedBrowser,
    147    "data:text/html,<meta charset='utf-8'>"
    148  );
    149  await promise;
    150 
    151  is(favicon.src, Favicons.defaultFavicon.spec, "ends with default favicon");
    152 
    153  await BrowserTestUtils.closeWindow(win);
    154 });
    155 
    156 add_task(async function test_muteAttributesMatchState() {
    157  const win = await openTaskbarTabWindow();
    158  const mute = win.document.getElementById("taskbar-tabs-audio");
    159  const tab = win.gBrowser.selectedTab;
    160 
    161  function checkAttributesMatch(when) {
    162    is(tab.muted, mute.hasAttribute("muted"), `${when}: 'muted' matches`);
    163    is(
    164      tab.soundPlaying,
    165      mute.hasAttribute("soundplaying"),
    166      `${when}: 'soundplaying' matches`
    167    );
    168    is(
    169      mute.getAttribute("data-l10n-id"),
    170      tab.muted ? "taskbar-tab-audio-unmute" : "taskbar-tab-audio-mute",
    171      `${when}: tooltip is relevant`
    172    );
    173  }
    174 
    175  let promise = waitForTabAttributeChange(tab, "soundplaying");
    176  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, gAudioPage);
    177  await promise;
    178 
    179  ok(tab.soundPlaying, "Tab is now playing sound");
    180  checkAttributesMatch("after starting playback");
    181 
    182  promise = waitForTabAttributeChange(tab, "muted");
    183  tab.toggleMuteAudio();
    184  await promise;
    185  ok(tab.muted, "Tab is now muted during playback");
    186  checkAttributesMatch("after muting during playback");
    187 
    188  promise = waitForTabAttributeChange(tab, "soundplaying");
    189  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    190    content.document.querySelector("audio").pause();
    191  });
    192  await promise;
    193  ok(!tab.soundPlaying, "Tab is no longer playing sound");
    194  checkAttributesMatch("after playback stops");
    195 
    196  promise = waitForTabAttributeChange(tab, "muted");
    197  tab.toggleMuteAudio();
    198  await promise;
    199  ok(!tab.muted, "Tab is unmuted after stopping playback");
    200  checkAttributesMatch("after unmuting and stopping");
    201 
    202  await BrowserTestUtils.closeWindow(win);
    203 });
    204 
    205 add_task(async function test_muteTogglesTabMute() {
    206  const win = await openTaskbarTabWindow();
    207  const mute = win.document.getElementById("taskbar-tabs-audio");
    208  const tab = win.gBrowser.selectedTab;
    209 
    210  let promise = waitForTabAttributeChange(tab, "soundplaying");
    211  BrowserTestUtils.startLoadingURIString(tab.linkedBrowser, gAudioPage);
    212  await promise;
    213  ok(!tab.muted, "Tab is not muted to start");
    214 
    215  promise = waitForTabAttributeChange(tab, "muted");
    216  mute.dispatchEvent(new PointerEvent("click"));
    217  await promise;
    218  ok(tab.muted, "Tab is now muted");
    219 
    220  promise = waitForTabAttributeChange(tab, "muted");
    221  mute.dispatchEvent(new PointerEvent("click"));
    222  await promise;
    223  ok(!tab.muted, "Tab is now unmuted");
    224 
    225  await BrowserTestUtils.closeWindow(win);
    226 });
    227 
    228 async function waitForTabAttributeChange(aTab, aEvent) {
    229  return await new Promise(resolve => {
    230    const callback = e => {
    231      if (e.detail.changed.includes(aEvent)) {
    232        aTab.removeEventListener("TabAttrModified", callback);
    233        resolve();
    234      }
    235    };
    236    aTab.addEventListener("TabAttrModified", callback);
    237  });
    238 }