tor-browser

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

browser_verify_remotetabs_notif.js (4230B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const { Observers } = ChromeUtils.importESModule(
      6  "resource://services-common/observers.sys.mjs"
      7 );
      8 
      9 // Since we show the brand name (Nightly, Beta) in the notification
     10 // we need to fetch the localization so the test doesn't break when going
     11 // through the trains
     12 const l10n = new Localization(["branding/brand.ftl", "browser/accounts.ftl"]);
     13 
     14 // URL that opens up when a user clicks the "close tab" notification
     15 const NOTIFICATION_CLICKED_URL = "about:firefoxview#recentlyclosed";
     16 
     17 add_task(async function test_closetab_notification() {
     18  const URL_TO_CLOSE = "about:mozilla";
     19  let payload = [
     20    {
     21      urls: [URL_TO_CLOSE],
     22      sender: {
     23        deviceName: "device-1",
     24      },
     25    },
     26  ];
     27  info("Test verify receiving a close tab command will show a notification");
     28 
     29  // Get the expected notification text we'll show the user
     30  const [expectedTitle, expectedBody] = await l10n.formatValues([
     31    {
     32      id: "account-tabs-closed-remotely",
     33      args: { closedCount: 1 },
     34    },
     35    { id: "account-view-recently-closed-tabs" },
     36  ]);
     37 
     38  // This will also immediately invoke the "alertclickcallback" in addition to
     39  // the usual alertshow and alertfinished
     40  setupMockAlertsService({
     41    title: expectedTitle,
     42    body: expectedBody,
     43  });
     44 
     45  // Open a tab with the same url we'll be expecting from the close tab command payload
     46  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL_TO_CLOSE);
     47  let tabClosedPromise = BrowserTestUtils.waitForTabClosing(tab);
     48  let waitForTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
     49 
     50  // Send the notify, which will kick off the closing of the tab and notification
     51  Observers.notify("fxaccounts:commands:close-uri", payload);
     52  Assert.ok("Close notification sent");
     53 
     54  // Wait for the tab to close
     55  await tabClosedPromise;
     56  Assert.ok("Tab successfully closed");
     57 
     58  // Test the tab that opened for "clicking" the notification shows
     59  // the recently closed list
     60  let notifTab = await waitForTabPromise;
     61  Assert.equal(
     62    notifTab.linkedBrowser.currentURI.spec,
     63    NOTIFICATION_CLICKED_URL
     64  );
     65 
     66  // Cleanup the tab
     67  BrowserTestUtils.removeTab(notifTab);
     68 });
     69 
     70 add_task(async function test_closetab_multiple_urls_notification() {
     71  const URLS_TO_CLOSE = ["about:mozilla", "about:about"];
     72  let payload = [
     73    {
     74      urls: URLS_TO_CLOSE,
     75      sender: {
     76        deviceName: "device-1",
     77      },
     78    },
     79  ];
     80  info(
     81    "Test verify receiving multiple close tabs command will show the proper notification"
     82  );
     83 
     84  // Get the expected notification text we'll show the user
     85  const [expectedTitle, expectedBody] = await l10n.formatValues([
     86    {
     87      id: "account-tabs-closed-remotely",
     88      args: { closedCount: 2 },
     89    },
     90    { id: "account-view-recently-closed-tabs" },
     91  ]);
     92 
     93  // This will also immediately invoke the "alertclickcallback" in addition to
     94  // the usual alertshow and alertfinished
     95  setupMockAlertsService({
     96    title: expectedTitle,
     97    body: expectedBody,
     98  });
     99  // Open multiple tabs to test we can close both and have the correct
    100  // notification text
    101  let tab1 = await BrowserTestUtils.openNewForegroundTab(
    102    gBrowser,
    103    URLS_TO_CLOSE[0]
    104  );
    105  let tab2 = await BrowserTestUtils.openNewForegroundTab(
    106    gBrowser,
    107    URLS_TO_CLOSE[1]
    108  );
    109  // We want to make sure multiple tabs were closed
    110  let tabClosedPromise = Promise.all([
    111    BrowserTestUtils.waitForTabClosing(tab1),
    112    BrowserTestUtils.waitForTabClosing(tab2),
    113  ]);
    114  let waitForTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
    115 
    116  // Send the notify, which will kick off the closing of the tab and notification
    117  Observers.notify("fxaccounts:commands:close-uri", payload);
    118  Assert.ok("Close notification sent");
    119 
    120  await tabClosedPromise;
    121  Assert.ok("Multiple tabs successfully closed");
    122 
    123  // Test the click after the notification
    124  let notifTab = await waitForTabPromise;
    125  Assert.equal(
    126    notifTab.linkedBrowser.currentURI.spec,
    127    NOTIFICATION_CLICKED_URL
    128  );
    129 
    130  // Cleanup the tab
    131  BrowserTestUtils.removeTab(notifTab);
    132 });