tor-browser

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

browser_PageActions_overflow.js (9060B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test() {
      7  // We use an extension that shows a page action. We must add this additional
      8  // action because otherwise the meatball menu would not appear as an overflow
      9  // for a single action.
     10  let extension = ExtensionTestUtils.loadExtension({
     11    manifest: {
     12      name: "Test contextMenu",
     13      page_action: { show_matches: ["<all_urls>"] },
     14    },
     15 
     16    useAddonManager: "temporary",
     17  });
     18 
     19  await extension.startup();
     20  let actionId = ExtensionCommon.makeWidgetId(extension.id);
     21 
     22  let win = await BrowserTestUtils.openNewBrowserWindow();
     23  await SimpleTest.promiseFocus(win);
     24  Assert.greater(win.outerWidth, 700, "window is bigger than 700px");
     25  BrowserTestUtils.startLoadingURIString(
     26    win.gBrowser,
     27    "data:text/html,<h1>A Page</h1>"
     28  );
     29  await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
     30 
     31  // The pageAction implementation enables the button at the next animation
     32  // frame, so before we look for the button we should wait one animation frame
     33  // as well.
     34  await promiseAnimationFrame(win);
     35 
     36  info("Check page action buttons are visible, the meatball button is not");
     37  let addonButton =
     38    win.BrowserPageActions.urlbarButtonNodeForActionID(actionId);
     39  Assert.ok(BrowserTestUtils.isVisible(addonButton));
     40  let starButton =
     41    win.BrowserPageActions.urlbarButtonNodeForActionID("bookmark");
     42  Assert.ok(BrowserTestUtils.isVisible(starButton));
     43  let meatballButton = win.document.getElementById("pageActionButton");
     44  Assert.ok(!BrowserTestUtils.isVisible(meatballButton));
     45 
     46  info(
     47    "Shrink the window, check page action buttons are not visible, the meatball menu is visible"
     48  );
     49  let originalOuterWidth = win.outerWidth;
     50  await promiseStableResize(500, win);
     51  Assert.ok(!BrowserTestUtils.isVisible(addonButton));
     52  Assert.ok(!BrowserTestUtils.isVisible(starButton));
     53  Assert.ok(BrowserTestUtils.isVisible(meatballButton));
     54 
     55  info(
     56    "Remove the extension, check the only page action button is visible, the meatball menu is not visible"
     57  );
     58  let promiseUninstalled = promiseAddonUninstalled(extension.id);
     59  await extension.unload();
     60  await promiseUninstalled;
     61  Assert.ok(BrowserTestUtils.isVisible(starButton));
     62  Assert.ok(!BrowserTestUtils.isVisible(meatballButton));
     63  Assert.deepEqual(
     64    win.BrowserPageActions.urlbarButtonNodeForActionID(actionId),
     65    null
     66  );
     67 
     68  await promiseStableResize(originalOuterWidth, win);
     69  await BrowserTestUtils.closeWindow(win);
     70 });
     71 
     72 add_task(async function bookmark() {
     73  // We use an extension that shows a page action. We must add this additional
     74  // action because otherwise the meatball menu would not appear as an overflow
     75  // for a single action.
     76  let extension = ExtensionTestUtils.loadExtension({
     77    manifest: {
     78      name: "Test contextMenu",
     79      page_action: { show_matches: ["<all_urls>"] },
     80    },
     81 
     82    useAddonManager: "temporary",
     83  });
     84 
     85  await extension.startup();
     86  const url = "data:text/html,<h1>A Page</h1>";
     87  let win = await BrowserTestUtils.openNewBrowserWindow();
     88  await SimpleTest.promiseFocus(win);
     89  BrowserTestUtils.startLoadingURIString(win.gBrowser, url);
     90  await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser);
     91 
     92  // The pageAction implementation enables the button at the next animation
     93  // frame, so before we look for the button we should wait one animation frame
     94  // as well.
     95  await promiseAnimationFrame(win);
     96 
     97  info("Shrink the window if necessary, check the meatball menu is visible");
     98  let originalOuterWidth = win.outerWidth;
     99  await promiseStableResize(500, win);
    100 
    101  let meatballButton = win.document.getElementById("pageActionButton");
    102  Assert.ok(BrowserTestUtils.isVisible(meatballButton));
    103 
    104  // Open the panel.
    105  await promisePageActionPanelOpen(win);
    106 
    107  // The bookmark button should read "Bookmark Current Tab…" and not be starred.
    108  let bookmarkButton = win.document.getElementById("pageAction-panel-bookmark");
    109  await TestUtils.waitForCondition(
    110    () => bookmarkButton.label === "Bookmark Current Tab…"
    111  );
    112  Assert.ok(!bookmarkButton.hasAttribute("starred"));
    113 
    114  // Click the button.
    115  let hiddenPromise = promisePageActionPanelHidden(win);
    116  let showPromise = BrowserTestUtils.waitForPopupEvent(
    117    win.StarUI.panel,
    118    "shown"
    119  );
    120  EventUtils.synthesizeMouseAtCenter(bookmarkButton, {}, win);
    121  await hiddenPromise;
    122  await showPromise;
    123  win.StarUI.panel.hidePopup();
    124 
    125  // Open the panel again.
    126  await promisePageActionPanelOpen(win);
    127 
    128  // The bookmark button should now read "Edit This Bookmark…" and be starred.
    129  await TestUtils.waitForCondition(
    130    () => bookmarkButton.label === "Edit This Bookmark…"
    131  );
    132  Assert.ok(bookmarkButton.hasAttribute("starred"));
    133 
    134  // Click it again.
    135  hiddenPromise = promisePageActionPanelHidden(win);
    136  showPromise = BrowserTestUtils.waitForPopupEvent(win.StarUI.panel, "shown");
    137  EventUtils.synthesizeMouseAtCenter(bookmarkButton, {}, win);
    138  await hiddenPromise;
    139  await showPromise;
    140 
    141  let onItemRemovedPromise = PlacesTestUtils.waitForNotification(
    142    "bookmark-removed",
    143    events => events.some(event => event.url == url)
    144  );
    145 
    146  // Click the remove-bookmark button in the panel.
    147  win.StarUI._element("editBookmarkPanelRemoveButton").click();
    148 
    149  // Wait for the bookmark to be removed before continuing.
    150  await onItemRemovedPromise;
    151 
    152  // Open the panel again.
    153  await promisePageActionPanelOpen(win);
    154 
    155  // The bookmark button should read "Bookmark Current Tab…" and not be starred.
    156  await TestUtils.waitForCondition(
    157    () => bookmarkButton.label === "Bookmark Current Tab…"
    158  );
    159  Assert.ok(!bookmarkButton.hasAttribute("starred"));
    160 
    161  // Done.
    162  hiddenPromise = promisePageActionPanelHidden();
    163  win.BrowserPageActions.panelNode.hidePopup();
    164  await hiddenPromise;
    165 
    166  info("Remove the extension");
    167  let promiseUninstalled = promiseAddonUninstalled(extension.id);
    168  await extension.unload();
    169  await promiseUninstalled;
    170 
    171  await promiseStableResize(originalOuterWidth, win);
    172  await BrowserTestUtils.closeWindow(win);
    173 });
    174 
    175 add_task(async function test_disabledPageAction_hidden_in_protonOverflowMenu() {
    176  // Make sure the overflow menu urlbar button is visible (independently from
    177  // the current size of the Firefox window).
    178  BrowserPageActions.mainButtonNode.style.display = "flex";
    179  registerCleanupFunction(() => {
    180    BrowserPageActions.mainButtonNode.style.removeProperty("display");
    181  });
    182 
    183  const extension = ExtensionTestUtils.loadExtension({
    184    manifest: { page_action: {} },
    185    async background() {
    186      const { browser } = this;
    187      const [tab] = await browser.tabs.query({
    188        active: true,
    189        currentWindow: true,
    190      });
    191      browser.test.assertTrue(tab, "Got an active tab as expected");
    192      browser.test.onMessage.addListener(async msg => {
    193        switch (msg) {
    194          case "show-pageAction":
    195            await browser.pageAction.show(tab.id);
    196            break;
    197          case "hide-pageAction":
    198            await browser.pageAction.hide(tab.id);
    199            break;
    200          default:
    201            browser.test.fail(`Unexpected message received: ${msg}`);
    202        }
    203        browser.test.sendMessage(`${msg}:done`);
    204      });
    205    },
    206  });
    207 
    208  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    209  await BrowserTestUtils.withNewTab("http://example.com", async browser => {
    210    const win = browser.ownerGlobal;
    211    const promisePageActionPanelClosed = async () => {
    212      let popupHiddenPromise = promisePageActionPanelHidden(win);
    213      win.BrowserPageActions.panelNode.hidePopup();
    214      await popupHiddenPromise;
    215    };
    216 
    217    await extension.startup();
    218    const widgetId = ExtensionCommon.makeWidgetId(extension.id);
    219 
    220    info(
    221      "Show pageAction and verify it is visible in the urlbar overflow menu"
    222    );
    223    extension.sendMessage("show-pageAction");
    224    await extension.awaitMessage("show-pageAction:done");
    225    await promisePageActionPanelOpen(win);
    226    let pageActionNode =
    227      win.BrowserPageActions.panelButtonNodeForActionID(widgetId);
    228    ok(
    229      pageActionNode && BrowserTestUtils.isVisible(pageActionNode),
    230      "enabled pageAction should be visible in the urlbar overflow menu"
    231    );
    232 
    233    info("Hide pageAction and verify it is hidden in the urlbar overflow menu");
    234    extension.sendMessage("hide-pageAction");
    235    await extension.awaitMessage("hide-pageAction:done");
    236 
    237    await BrowserTestUtils.waitForCondition(
    238      () => !win.BrowserPageActions.panelButtonNodeForActionID(widgetId),
    239      "Wait for the disabled pageAction to be removed from the urlbar overflow menu"
    240    );
    241 
    242    await promisePageActionPanelClosed();
    243 
    244    info("Reopen the urlbar overflow menu");
    245    await promisePageActionPanelOpen(win);
    246    ok(
    247      !win.BrowserPageActions.panelButtonNodeForActionID(widgetId),
    248      "Disabled pageAction is still removed as expected"
    249    );
    250 
    251    await promisePageActionPanelClosed();
    252    await extension.unload();
    253  });
    254 
    255  await SpecialPowers.popPrefEnv();
    256 });