tor-browser

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

browser_taskbarTabs_manifest.js (6220B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 htt*p://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  HttpServer: "resource://testing-common/httpd.sys.mjs",
      8  sinon: "resource://testing-common/Sinon.sys.mjs",
      9  TaskbarTabs: "resource:///modules/taskbartabs/TaskbarTabs.sys.mjs",
     10  TaskbarTabsPin: "resource:///modules/taskbartabs/TaskbarTabsPin.sys.mjs",
     11  TaskbarTabsUtils: "resource:///modules/taskbartabs/TaskbarTabsUtils.sys.mjs",
     12 });
     13 
     14 sinon.stub(TaskbarTabsPin, "pinTaskbarTab");
     15 sinon.stub(TaskbarTabsPin, "unpinTaskbarTab");
     16 
     17 let gHttpServer = null;
     18 let gManifest = null;
     19 
     20 add_setup(function startServer() {
     21  gHttpServer = new HttpServer();
     22  gHttpServer.start(-1);
     23  registerCleanupFunction(() => {
     24    return new Promise(resolve => gHttpServer.stop(resolve));
     25  });
     26 
     27  gHttpServer.registerPathHandler(
     28    "/taskbartabs-manifest.json",
     29    (request, response) => {
     30      response.setStatusLine(
     31        null,
     32        gManifest ? 200 : 404,
     33        gManifest ? "OK" : "Not Found"
     34      );
     35      response.write(
     36        typeof gManifest === "string" ? gManifest : JSON.stringify(gManifest)
     37      );
     38      response.processAsync();
     39      response.finish();
     40    }
     41  );
     42 });
     43 
     44 registerCleanupFunction(async () => {
     45  sinon.restore();
     46  await TaskbarTabs.resetForTests();
     47 });
     48 
     49 function httpUrl(path) {
     50  return `http://localhost:${gHttpServer.identity.primaryPort}${path}`;
     51 }
     52 
     53 const gPageAction = document.getElementById("taskbar-tabs-button");
     54 
     55 add_task(async function test_noManifest() {
     56  gManifest = null;
     57  await usingManifest((aWindow, aTaskbarTab) => {
     58    ok(true, "Created a taskbar tab with no manifest");
     59    // No manifest, so we use TaskbarTabsRegistry's default start URL.
     60    is(aTaskbarTab.startUrl, httpUrl(""), "Default start URL is used");
     61  });
     62 });
     63 
     64 add_task(async function test_emptyManifest() {
     65  gManifest = {};
     66  await usingManifest((aWindow, aTaskbarTab) => {
     67    ok(true, "Created a taskbar tab with an empty manifest");
     68    // The manifest was successfully requested, so ManifestObtainer made
     69    // a default for the missing value. Unlike TaskbarTabsRegistry, this
     70    // default _includes_ a trailing slash, which needs to be accounted
     71    // for.
     72    is(aTaskbarTab.startUrl, httpUrl("/"), "Default start URL is used");
     73  });
     74 });
     75 
     76 add_task(async function test_invalidManifest() {
     77  gManifest = "NOT a valid manifest!!";
     78  await usingManifest((aWindow, aTaskbarTab) => {
     79    ok(true, "Created a taskbar tab with an invalid manifest");
     80    // The manifest could be downloaded, and so (even though the 'JSON' is
     81    // wildly invalid) it uses ManifestObtainer's default.
     82    is(aTaskbarTab.startUrl, httpUrl("/"), "Default start URL is used");
     83  });
     84 });
     85 
     86 add_task(async function test_nameAndStartUrl() {
     87  gManifest = {
     88    name: "Taskbar Tabs test",
     89    start_url: "/example/path/string/here",
     90  };
     91 
     92  await usingManifest((aWindow, aTaskbarTab) => {
     93    is(
     94      aTaskbarTab.name,
     95      "Taskbar Tabs test",
     96      "Page action detected correct name"
     97    );
     98    is(
     99      aTaskbarTab.startUrl,
    100      httpUrl("/example/path/string/here"),
    101      "Page action detected correct start URL"
    102    );
    103  }, "/example/path/string/here");
    104 });
    105 
    106 add_task(async function test_scope() {
    107  gManifest = {
    108    scope: "/example/",
    109  };
    110 
    111  await usingManifest((aWindow, aTaskbarTab) => {
    112    Assert.deepEqual(aTaskbarTab.scopes[0], {
    113      hostname: "localhost",
    114      prefix: "/example/",
    115    });
    116  }, "/example/file");
    117 });
    118 
    119 add_task(async function test_startUrlOutOfScope() {
    120  gManifest = {
    121    scope: "/example/",
    122    start_url: "/example",
    123  };
    124 
    125  await usingManifest((aWindow, aTaskbarTab) => {
    126    Assert.deepEqual(aTaskbarTab.scopes[0], {
    127      hostname: "localhost",
    128      prefix: "/",
    129    });
    130  }, "/example");
    131 });
    132 
    133 add_task(async function test_scopeDistinguishesTaskbarTabs() {
    134  const find = prefix =>
    135    TaskbarTabs.findTaskbarTab(Services.io.newURI(httpUrl(prefix)), 0);
    136 
    137  gManifest = {
    138    scope: "/example/",
    139  };
    140 
    141  await usingManifest(async (aWindowOuter, aTaskbarTabOuter) => {
    142    gManifest = {
    143      scope: "/example/another/",
    144    };
    145 
    146    await usingManifest(async (aWindowInner, aTaskbarTabInner) => {
    147      is(
    148        (await find("/example/another/still")).id,
    149        aTaskbarTabInner.id,
    150        "/example/another/still matches to inner Taskbar Tab"
    151      );
    152      is(
    153        (await find("/example/another/")).id,
    154        aTaskbarTabInner.id,
    155        "/example/another/ matches to inner Taskbar Tab"
    156      );
    157      is(
    158        (await find("/example/another")).id,
    159        aTaskbarTabOuter.id,
    160        "/example/another matches to outer Taskbar Tab"
    161      );
    162      is(
    163        (await find("/example/different")).id,
    164        aTaskbarTabOuter.id,
    165        "/example/different matches to outer Taskbar Tab"
    166      );
    167      is(
    168        (await find("/example/")).id,
    169        aTaskbarTabOuter.id,
    170        "/example/ matches to outer Taskbar Tab"
    171      );
    172 
    173      is(await find("/example"), null, "/example does not match a Taskbar Tab");
    174      is(
    175        await find("/unrelated"),
    176        null,
    177        "/unrelated does not match any Taskbar Tab"
    178      );
    179    }, "/example/another/main");
    180  }, "/example/main");
    181 }).skip(); // TODO bug 2000948
    182 
    183 async function usingManifest(aCallback, aLocation = "/") {
    184  const location = httpUrl("/taskbartabs-manifest.json");
    185 
    186  await BrowserTestUtils.withNewTab(
    187    {
    188      url: httpUrl(aLocation),
    189      gBrowser: window.gBrowser,
    190    },
    191    async browser => {
    192      await SpecialPowers.spawn(browser, [location], async url => {
    193        content.document.body.innerHTML = `<link rel="manifest" href="${url}">`;
    194      });
    195 
    196      const tab = window.gBrowser.getTabForBrowser(browser);
    197      let result = await TaskbarTabs.moveTabIntoTaskbarTab(tab);
    198 
    199      const uri = Services.io.newURI(httpUrl(aLocation));
    200      const tt = await TaskbarTabs.findTaskbarTab(uri, 0);
    201      is(
    202        await TaskbarTabsUtils.getTaskbarTabIdFromWindow(result.window),
    203        tt.id,
    204        "moveTabIntoTaskbarTab created a Taskbar Tab"
    205      );
    206      await aCallback(result.window, tt);
    207 
    208      await TaskbarTabs.removeTaskbarTab(tt.id);
    209      await BrowserTestUtils.closeWindow(result.window);
    210    }
    211  );
    212 }