tor-browser

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

Screenshots.test.js (6805B)


      1 "use strict";
      2 import { GlobalOverrider } from "test/unit/utils";
      3 import { Screenshots } from "lib/Screenshots.sys.mjs";
      4 
      5 const URL = "foo.com";
      6 const FAKE_THUMBNAIL_PATH = "fake/path/thumb.jpg";
      7 const FAKE_THUMBNAIL_THUMB =
      8  "moz-page-thumb://thumbnail?url=http%3A%2F%2Ffoo.com%2F";
      9 
     10 describe("Screenshots", () => {
     11  let globals;
     12  let sandbox;
     13  let fakeServices;
     14  let testFile;
     15 
     16  beforeEach(() => {
     17    globals = new GlobalOverrider();
     18    sandbox = globals.sandbox;
     19    fakeServices = {
     20      wm: {
     21        getEnumerator() {
     22          return Array(10);
     23        },
     24      },
     25    };
     26    globals.set("BackgroundPageThumbs", {
     27      captureIfMissing: sandbox.spy(() => Promise.resolve()),
     28    });
     29    globals.set("PageThumbs", {
     30      _store: sandbox.stub(),
     31      getThumbnailPath: sandbox.spy(() => FAKE_THUMBNAIL_PATH),
     32      getThumbnailURL: sandbox.spy(() => FAKE_THUMBNAIL_THUMB),
     33    });
     34    globals.set("PrivateBrowsingUtils", {
     35      isWindowPrivate: sandbox.spy(() => false),
     36    });
     37    testFile = { size: 1 };
     38    globals.set("Services", fakeServices);
     39    globals.set(
     40      "fetch",
     41      sandbox.spy(() =>
     42        Promise.resolve({ blob: () => Promise.resolve(testFile) })
     43      )
     44    );
     45  });
     46  afterEach(() => {
     47    globals.restore();
     48  });
     49 
     50  describe("#getScreenshotForURL", () => {
     51    it("should call BackgroundPageThumbs.captureIfMissing with the correct url", async () => {
     52      await Screenshots.getScreenshotForURL(URL);
     53      assert.calledWith(global.BackgroundPageThumbs.captureIfMissing, URL);
     54    });
     55    it("should call PageThumbs.getThumbnailPath with the correct url", async () => {
     56      globals.set("gPrivilegedAboutProcessEnabled", false);
     57      await Screenshots.getScreenshotForURL(URL);
     58      assert.calledWith(global.PageThumbs.getThumbnailPath, URL);
     59    });
     60    it("should call fetch", async () => {
     61      await Screenshots.getScreenshotForURL(URL);
     62      assert.calledOnce(global.fetch);
     63    });
     64    it("should have the necessary keys in the response object", async () => {
     65      const screenshot = await Screenshots.getScreenshotForURL(URL);
     66 
     67      assert.notEqual(screenshot.path, undefined);
     68      assert.notEqual(screenshot.data, undefined);
     69    });
     70    it("should get null if something goes wrong", async () => {
     71      globals.set("BackgroundPageThumbs", {
     72        captureIfMissing: () =>
     73          Promise.reject(new Error("Cannot capture thumbnail")),
     74      });
     75 
     76      const screenshot = await Screenshots.getScreenshotForURL(URL);
     77 
     78      assert.calledOnce(global.PageThumbs._store);
     79      assert.equal(screenshot, null);
     80    });
     81    it("should get direct thumbnail url for privileged process", async () => {
     82      globals.set("gPrivilegedAboutProcessEnabled", true);
     83      await Screenshots.getScreenshotForURL(URL);
     84      assert.calledWith(global.PageThumbs.getThumbnailURL, URL);
     85    });
     86    it("should get null without storing if existing thumbnail is empty", async () => {
     87      testFile.size = 0;
     88 
     89      const screenshot = await Screenshots.getScreenshotForURL(URL);
     90 
     91      assert.notCalled(global.PageThumbs._store);
     92      assert.equal(screenshot, null);
     93    });
     94  });
     95 
     96  describe("#maybeCacheScreenshot", () => {
     97    let link;
     98    beforeEach(() => {
     99      link = {
    100        __sharedCache: {
    101          updateLink: (prop, val) => {
    102            link[prop] = val;
    103          },
    104        },
    105      };
    106    });
    107    it("should call getScreenshotForURL", () => {
    108      sandbox.stub(Screenshots, "getScreenshotForURL");
    109      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
    110      Screenshots.maybeCacheScreenshot(
    111        link,
    112        "mozilla.com",
    113        "image",
    114        sinon.stub()
    115      );
    116 
    117      assert.calledOnce(Screenshots.getScreenshotForURL);
    118      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    119    });
    120    it("should not call getScreenshotForURL twice if a fetch is in progress", () => {
    121      sandbox
    122        .stub(Screenshots, "getScreenshotForURL")
    123        .returns(new Promise(() => {}));
    124      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
    125      Screenshots.maybeCacheScreenshot(
    126        link,
    127        "mozilla.com",
    128        "image",
    129        sinon.stub()
    130      );
    131      Screenshots.maybeCacheScreenshot(
    132        link,
    133        "mozilla.org",
    134        "image",
    135        sinon.stub()
    136      );
    137 
    138      assert.calledOnce(Screenshots.getScreenshotForURL);
    139      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    140    });
    141    it("should not call getScreenshotsForURL if property !== undefined", async () => {
    142      sandbox
    143        .stub(Screenshots, "getScreenshotForURL")
    144        .returns(Promise.resolve(null));
    145      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
    146      await Screenshots.maybeCacheScreenshot(
    147        link,
    148        "mozilla.com",
    149        "image",
    150        sinon.stub()
    151      );
    152      await Screenshots.maybeCacheScreenshot(
    153        link,
    154        "mozilla.org",
    155        "image",
    156        sinon.stub()
    157      );
    158 
    159      assert.calledOnce(Screenshots.getScreenshotForURL);
    160      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    161    });
    162    it("should check if we are in private browsing before getting screenshots", async () => {
    163      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
    164      await Screenshots.maybeCacheScreenshot(
    165        link,
    166        "mozilla.com",
    167        "image",
    168        sinon.stub()
    169      );
    170 
    171      assert.calledOnce(Screenshots._shouldGetScreenshots);
    172    });
    173    it("should not get a screenshot if we are in private browsing", async () => {
    174      sandbox.stub(Screenshots, "getScreenshotForURL");
    175      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(false);
    176      await Screenshots.maybeCacheScreenshot(
    177        link,
    178        "mozilla.com",
    179        "image",
    180        sinon.stub()
    181      );
    182 
    183      assert.notCalled(Screenshots.getScreenshotForURL);
    184    });
    185  });
    186 
    187  describe("#_shouldGetScreenshots", () => {
    188    beforeEach(() => {
    189      let more = 2;
    190      sandbox
    191        .stub(global.Services.wm, "getEnumerator")
    192        .callsFake(() => Array(Math.max(more--, 0)));
    193    });
    194    it("should use private browsing utils to determine if a window is private", () => {
    195      Screenshots._shouldGetScreenshots();
    196      assert.calledOnce(global.PrivateBrowsingUtils.isWindowPrivate);
    197    });
    198    it("should return true if there exists at least 1 non-private window", () => {
    199      assert.isTrue(Screenshots._shouldGetScreenshots());
    200    });
    201    it("should return false if there exists private windows", () => {
    202      global.PrivateBrowsingUtils = {
    203        isWindowPrivate: sandbox.spy(() => true),
    204      };
    205      assert.isFalse(Screenshots._shouldGetScreenshots());
    206      assert.calledTwice(global.PrivateBrowsingUtils.isWindowPrivate);
    207    });
    208  });
    209 });