tor-browser

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

test_TippyTopProvider.js (5427B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  sinon: "resource://testing-common/Sinon.sys.mjs",
      8  TippyTopProvider: "resource:///modules/topsites/TippyTopProvider.sys.mjs",
      9 });
     10 
     11 /**
     12 * Constructs an initialized TippyTopProvider with some prepared site icons
     13 * for testing.
     14 *
     15 * @param {SinonSandbox} sandbox
     16 *   The Sinon sandbox used to create the stubs and ensure cleanup.
     17 * @returns {Promise<TippyTopProvider>}
     18 */
     19 async function getTippyTopProviderForTest(sandbox) {
     20  let instance = new TippyTopProvider();
     21  let fetchStub = sandbox.stub(instance, "fetch");
     22  fetchStub.resolves({
     23    ok: true,
     24    status: 200,
     25    json: () =>
     26      Promise.resolve([
     27        {
     28          domains: ["facebook.com"],
     29          image_url: "images/facebook-com.png",
     30          favicon_url: "images/facebook-com.png",
     31          background_color: "#3b5998",
     32        },
     33        {
     34          domains: ["gmail.com", "mail.google.com"],
     35          image_url: "images/gmail-com.png",
     36          favicon_url: "images/gmail-com.png",
     37          background_color: "#000000",
     38        },
     39      ]),
     40  });
     41 
     42  await instance.init();
     43 
     44  return instance;
     45 }
     46 
     47 add_task(async function test_provide_facebook_icon() {
     48  info("should provide an icon for facebook.com");
     49 
     50  let sandbox = sinon.createSandbox();
     51  let instance = await getTippyTopProviderForTest(sandbox);
     52  let site = instance.processSite({ url: "https://facebook.com" });
     53  Assert.equal(
     54    site.tippyTopIcon,
     55    "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png"
     56  );
     57  Assert.equal(
     58    site.smallFavicon,
     59    "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png"
     60  );
     61  Assert.equal(site.backgroundColor, "#3b5998");
     62 
     63  sandbox.restore();
     64 });
     65 
     66 add_task(async function test_dont_provide_other_facebook_icon() {
     67  info("should not provide an icon for other.facebook.com");
     68 
     69  let sandbox = sinon.createSandbox();
     70  let instance = await getTippyTopProviderForTest(sandbox);
     71  const site = instance.processSite({ url: "https://other.facebook.com" });
     72  Assert.equal(site.tippyTopIcon, undefined);
     73 
     74  sandbox.restore();
     75 });
     76 
     77 add_task(async function test_provide_other_facebook_icon_stripping() {
     78  info("should provide an icon for other.facebook.com with stripping");
     79 
     80  let sandbox = sinon.createSandbox();
     81  let instance = await getTippyTopProviderForTest(sandbox);
     82  let site = instance.processSite({ url: "https://other.facebook.com" }, "*");
     83  Assert.equal(
     84    site.tippyTopIcon,
     85    "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png"
     86  );
     87 
     88  sandbox.restore();
     89 });
     90 
     91 add_task(async function test_provide_facebook_icon_foobar() {
     92  info("should provide an icon for facebook.com/foobar");
     93 
     94  let sandbox = sinon.createSandbox();
     95  let instance = await getTippyTopProviderForTest(sandbox);
     96  let site = instance.processSite({ url: "https://facebook.com/foobar" });
     97  Assert.equal(
     98    site.tippyTopIcon,
     99    "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png"
    100  );
    101  Assert.equal(
    102    site.smallFavicon,
    103    "chrome://activity-stream/content/data/content/tippytop/images/facebook-com.png"
    104  );
    105  Assert.equal(site.backgroundColor, "#3b5998");
    106 
    107  sandbox.restore();
    108 });
    109 
    110 add_task(async function test_provide_gmail_icon() {
    111  info("should provide an icon for gmail.com");
    112 
    113  let sandbox = sinon.createSandbox();
    114  let instance = await getTippyTopProviderForTest(sandbox);
    115  const site = instance.processSite({ url: "https://gmail.com" });
    116  Assert.equal(
    117    site.tippyTopIcon,
    118    "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png"
    119  );
    120  Assert.equal(
    121    site.smallFavicon,
    122    "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png"
    123  );
    124  Assert.equal(site.backgroundColor, "#000000");
    125 
    126  sandbox.restore();
    127 });
    128 
    129 add_task(async function test_provide_mail_google_icon() {
    130  info("should provide an icon for mail.google.com");
    131 
    132  let sandbox = sinon.createSandbox();
    133  let instance = await getTippyTopProviderForTest(sandbox);
    134  const site = instance.processSite({ url: "https://mail.google.com" });
    135  Assert.equal(
    136    site.tippyTopIcon,
    137    "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png"
    138  );
    139  Assert.equal(
    140    site.smallFavicon,
    141    "chrome://activity-stream/content/data/content/tippytop/images/gmail-com.png"
    142  );
    143  Assert.equal(site.backgroundColor, "#000000");
    144 
    145  sandbox.restore();
    146 });
    147 
    148 add_task(async function test_garbage_urls() {
    149  info("should handle garbage URLs gracefully");
    150 
    151  let sandbox = sinon.createSandbox();
    152  let instance = await getTippyTopProviderForTest(sandbox);
    153  const site = instance.processSite({ url: "garbagejlfkdsa" });
    154  Assert.equal(site.tippyTopIcon, undefined);
    155  Assert.equal(site.backgroundColor, undefined);
    156 
    157  sandbox.restore();
    158 });
    159 
    160 add_task(async function test_failed_manifest_parse() {
    161  info("should handle error when fetching and parsing manifest");
    162 
    163  let sandbox = sinon.createSandbox();
    164  let instance = new TippyTopProvider();
    165  let fetchStub = sandbox.stub(instance, "fetch");
    166  fetchStub.rejects("whaaaa");
    167 
    168  await instance.init();
    169  let site = instance.processSite({ url: "https://facebook.com" });
    170  Assert.equal(site.tippyTopIcon, undefined);
    171  Assert.equal(site.backgroundColor, undefined);
    172 
    173  sandbox.restore();
    174 });