tor-browser

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

screenshot-utils.test.js (5577B)


      1 import { GlobalOverrider } from "test/unit/utils";
      2 import { ScreenshotUtils } from "content-src/lib/screenshot-utils";
      3 
      4 const DEFAULT_BLOB_URL = "blob://test";
      5 
      6 describe("ScreenshotUtils", () => {
      7  let globals;
      8  let url;
      9  beforeEach(() => {
     10    globals = new GlobalOverrider();
     11    url = {
     12      createObjectURL: globals.sandbox.stub().returns(DEFAULT_BLOB_URL),
     13      revokeObjectURL: globals.sandbox.spy(),
     14    };
     15    globals.set("URL", url);
     16  });
     17  afterEach(() => globals.restore());
     18  describe("#createLocalImageObject", () => {
     19    it("should return null if no remoteImage is supplied", () => {
     20      let localImageObject = ScreenshotUtils.createLocalImageObject(null);
     21 
     22      assert.notCalled(url.createObjectURL);
     23      assert.equal(localImageObject, null);
     24    });
     25    it("should create a local image object with the correct properties if remoteImage is a blob", () => {
     26      let localImageObject = ScreenshotUtils.createLocalImageObject({
     27        path: "/path1",
     28        data: new Blob([0]),
     29      });
     30 
     31      assert.calledOnce(url.createObjectURL);
     32      assert.deepEqual(localImageObject, {
     33        path: "/path1",
     34        url: DEFAULT_BLOB_URL,
     35      });
     36    });
     37    it("should create a local image object with the correct properties if remoteImage is a normal image", () => {
     38      const imageUrl = "https://test-url";
     39      let localImageObject = ScreenshotUtils.createLocalImageObject(imageUrl);
     40 
     41      assert.notCalled(url.createObjectURL);
     42      assert.deepEqual(localImageObject, { url: imageUrl });
     43    });
     44  });
     45  describe("#maybeRevokeBlobObjectURL", () => {
     46    // Note that we should also ensure that all the tests for #isBlob are green.
     47    it("should call revokeObjectURL if image is a blob", () => {
     48      ScreenshotUtils.maybeRevokeBlobObjectURL({
     49        path: "/path1",
     50        url: "blob://test",
     51      });
     52 
     53      assert.calledOnce(url.revokeObjectURL);
     54    });
     55    it("should not call revokeObjectURL if image is not a blob", () => {
     56      ScreenshotUtils.maybeRevokeBlobObjectURL({ url: "https://test-url" });
     57 
     58      assert.notCalled(url.revokeObjectURL);
     59    });
     60  });
     61  describe("#isRemoteImageLocal", () => {
     62    it("should return true if both propsImage and stateImage are not present", () => {
     63      assert.isTrue(ScreenshotUtils.isRemoteImageLocal(null, null));
     64    });
     65    it("should return false if propsImage is present and stateImage is not present", () => {
     66      assert.isFalse(ScreenshotUtils.isRemoteImageLocal(null, {}));
     67    });
     68    it("should return false if propsImage is not present and stateImage is present", () => {
     69      assert.isFalse(ScreenshotUtils.isRemoteImageLocal({}, null));
     70    });
     71    it("should return true if both propsImage and stateImage are equal blobs", () => {
     72      const blobPath = "/test-blob-path/test.png";
     73      assert.isTrue(
     74        ScreenshotUtils.isRemoteImageLocal(
     75          { path: blobPath, url: "blob://test" }, // state
     76          { path: blobPath, data: new Blob([0]) } // props
     77        )
     78      );
     79    });
     80    it("should return false if both propsImage and stateImage are different blobs", () => {
     81      assert.isFalse(
     82        ScreenshotUtils.isRemoteImageLocal(
     83          { path: "/path1", url: "blob://test" }, // state
     84          { path: "/path2", data: new Blob([0]) } // props
     85        )
     86      );
     87    });
     88    it("should return true if both propsImage and stateImage are equal normal images", () => {
     89      assert.isTrue(
     90        ScreenshotUtils.isRemoteImageLocal(
     91          { url: "test url" }, // state
     92          "test url" // props
     93        )
     94      );
     95    });
     96    it("should return false if both propsImage and stateImage are different normal images", () => {
     97      assert.isFalse(
     98        ScreenshotUtils.isRemoteImageLocal(
     99          { url: "test url 1" }, // state
    100          "test url 2" // props
    101        )
    102      );
    103    });
    104    it("should return false if both propsImage and stateImage are different type of images", () => {
    105      assert.isFalse(
    106        ScreenshotUtils.isRemoteImageLocal(
    107          { path: "/path1", url: "blob://test" }, // state
    108          "test url 2" // props
    109        )
    110      );
    111      assert.isFalse(
    112        ScreenshotUtils.isRemoteImageLocal(
    113          { url: "https://test-url" }, // state
    114          { path: "/path1", data: new Blob([0]) } // props
    115        )
    116      );
    117    });
    118  });
    119  describe("#isBlob", () => {
    120    let state = {
    121      blobImage: { path: "/test", url: "blob://test" },
    122      normalImage: { url: "https://test-url" },
    123    };
    124    let props = {
    125      blobImage: { path: "/test", data: new Blob([0]) },
    126      normalImage: "https://test-url",
    127    };
    128    it("should return false if image is null", () => {
    129      assert.isFalse(ScreenshotUtils.isBlob(true, null));
    130      assert.isFalse(ScreenshotUtils.isBlob(false, null));
    131    });
    132    it("should return true if image is a blob and type matches", () => {
    133      assert.isTrue(ScreenshotUtils.isBlob(true, state.blobImage));
    134      assert.isTrue(ScreenshotUtils.isBlob(false, props.blobImage));
    135    });
    136    it("should return false if image is not a blob and type matches", () => {
    137      assert.isFalse(ScreenshotUtils.isBlob(true, state.normalImage));
    138      assert.isFalse(ScreenshotUtils.isBlob(false, props.normalImage));
    139    });
    140    it("should return false if type does not match", () => {
    141      assert.isFalse(ScreenshotUtils.isBlob(false, state.blobImage));
    142      assert.isFalse(ScreenshotUtils.isBlob(false, state.normalImage));
    143      assert.isFalse(ScreenshotUtils.isBlob(true, props.blobImage));
    144      assert.isFalse(ScreenshotUtils.isBlob(true, props.normalImage));
    145    });
    146  });
    147 });