summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/Screenshots.test.js
blob: 272c7ff7d3b785bc1573771477217b27d948ed17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
import { GlobalOverrider } from "test/unit/utils";
import { Screenshots } from "lib/Screenshots.jsm";

const URL = "foo.com";
const FAKE_THUMBNAIL_PATH = "fake/path/thumb.jpg";
const FAKE_THUMBNAIL_THUMB =
  "moz-page-thumb://thumbnail?url=http%3A%2F%2Ffoo.com%2F";

describe("Screenshots", () => {
  let globals;
  let sandbox;
  let fakeServices;
  let testFile;

  beforeEach(() => {
    globals = new GlobalOverrider();
    sandbox = globals.sandbox;
    fakeServices = {
      wm: {
        getEnumerator() {
          return Array(10);
        },
      },
    };
    globals.set("BackgroundPageThumbs", {
      captureIfMissing: sandbox.spy(() => Promise.resolve()),
    });
    globals.set("PageThumbs", {
      _store: sandbox.stub(),
      getThumbnailPath: sandbox.spy(() => FAKE_THUMBNAIL_PATH),
      getThumbnailURL: sandbox.spy(() => FAKE_THUMBNAIL_THUMB),
    });
    globals.set("PrivateBrowsingUtils", {
      isWindowPrivate: sandbox.spy(() => false),
    });
    testFile = { size: 1 };
    globals.set("Services", fakeServices);
    globals.set(
      "fetch",
      sandbox.spy(() =>
        Promise.resolve({ blob: () => Promise.resolve(testFile) })
      )
    );
  });
  afterEach(() => {
    globals.restore();
  });

  describe("#getScreenshotForURL", () => {
    it("should call BackgroundPageThumbs.captureIfMissing with the correct url", async () => {
      await Screenshots.getScreenshotForURL(URL);
      assert.calledWith(global.BackgroundPageThumbs.captureIfMissing, URL);
    });
    it("should call PageThumbs.getThumbnailPath with the correct url", async () => {
      globals.set("gPrivilegedAboutProcessEnabled", false);
      await Screenshots.getScreenshotForURL(URL);
      assert.calledWith(global.PageThumbs.getThumbnailPath, URL);
    });
    it("should call fetch", async () => {
      await Screenshots.getScreenshotForURL(URL);
      assert.calledOnce(global.fetch);
    });
    it("should have the necessary keys in the response object", async () => {
      const screenshot = await Screenshots.getScreenshotForURL(URL);

      assert.notEqual(screenshot.path, undefined);
      assert.notEqual(screenshot.data, undefined);
    });
    it("should get null if something goes wrong", async () => {
      globals.set("BackgroundPageThumbs", {
        captureIfMissing: () =>
          Promise.reject(new Error("Cannot capture thumbnail")),
      });

      const screenshot = await Screenshots.getScreenshotForURL(URL);

      assert.calledOnce(global.PageThumbs._store);
      assert.equal(screenshot, null);
    });
    it("should get direct thumbnail url for privileged process", async () => {
      globals.set("gPrivilegedAboutProcessEnabled", true);
      await Screenshots.getScreenshotForURL(URL);
      assert.calledWith(global.PageThumbs.getThumbnailURL, URL);
    });
    it("should get null without storing if existing thumbnail is empty", async () => {
      testFile.size = 0;

      const screenshot = await Screenshots.getScreenshotForURL(URL);

      assert.notCalled(global.PageThumbs._store);
      assert.equal(screenshot, null);
    });
  });

  describe("#maybeCacheScreenshot", () => {
    let link;
    beforeEach(() => {
      link = {
        __sharedCache: {
          updateLink: (prop, val) => {
            link[prop] = val;
          },
        },
      };
    });
    it("should call getScreenshotForURL", () => {
      sandbox.stub(Screenshots, "getScreenshotForURL");
      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
      Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.com",
        "image",
        sinon.stub()
      );

      assert.calledOnce(Screenshots.getScreenshotForURL);
      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    });
    it("should not call getScreenshotForURL twice if a fetch is in progress", () => {
      sandbox
        .stub(Screenshots, "getScreenshotForURL")
        .returns(new Promise(() => {}));
      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
      Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.com",
        "image",
        sinon.stub()
      );
      Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.org",
        "image",
        sinon.stub()
      );

      assert.calledOnce(Screenshots.getScreenshotForURL);
      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    });
    it("should not call getScreenshotsForURL if property !== undefined", async () => {
      sandbox
        .stub(Screenshots, "getScreenshotForURL")
        .returns(Promise.resolve(null));
      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
      await Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.com",
        "image",
        sinon.stub()
      );
      await Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.org",
        "image",
        sinon.stub()
      );

      assert.calledOnce(Screenshots.getScreenshotForURL);
      assert.calledWithExactly(Screenshots.getScreenshotForURL, "mozilla.com");
    });
    it("should check if we are in private browsing before getting screenshots", async () => {
      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(true);
      await Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.com",
        "image",
        sinon.stub()
      );

      assert.calledOnce(Screenshots._shouldGetScreenshots);
    });
    it("should not get a screenshot if we are in private browsing", async () => {
      sandbox.stub(Screenshots, "getScreenshotForURL");
      sandbox.stub(Screenshots, "_shouldGetScreenshots").returns(false);
      await Screenshots.maybeCacheScreenshot(
        link,
        "mozilla.com",
        "image",
        sinon.stub()
      );

      assert.notCalled(Screenshots.getScreenshotForURL);
    });
  });

  describe("#_shouldGetScreenshots", () => {
    beforeEach(() => {
      let more = 2;
      sandbox
        .stub(global.Services.wm, "getEnumerator")
        .callsFake(() => Array(Math.max(more--, 0)));
    });
    it("should use private browsing utils to determine if a window is private", () => {
      Screenshots._shouldGetScreenshots();
      assert.calledOnce(global.PrivateBrowsingUtils.isWindowPrivate);
    });
    it("should return true if there exists at least 1 non-private window", () => {
      assert.isTrue(Screenshots._shouldGetScreenshots());
    });
    it("should return false if there exists private windows", () => {
      global.PrivateBrowsingUtils = {
        isWindowPrivate: sandbox.spy(() => true),
      };
      assert.isFalse(Screenshots._shouldGetScreenshots());
      assert.calledTwice(global.PrivateBrowsingUtils.isWindowPrivate);
    });
  });
});