summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/DownloadsManager.test.js
blob: 23ee9ffa34a452415dadbe54fe556d8f48ffbdf2 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { actionTypes as at } from "common/Actions.mjs";
import { DownloadsManager } from "lib/DownloadsManager.sys.mjs";
import { GlobalOverrider } from "test/unit/utils";

describe("Downloads Manager", () => {
  let downloadsManager;
  let globals;
  const DOWNLOAD_URL = "https://site.com/download.mov";

  beforeEach(() => {
    globals = new GlobalOverrider();
    global.Cc["@mozilla.org/timer;1"] = {
      createInstance() {
        return {
          initWithCallback: sinon.stub().callsFake(callback => callback()),
          cancel: sinon.spy(),
        };
      },
    };

    globals.set("DownloadsCommon", {
      getData: sinon.stub().returns({
        addView: sinon.stub(),
        removeView: sinon.stub(),
      }),
      copyDownloadLink: sinon.stub(),
      deleteDownload: sinon.stub().returns(Promise.resolve()),
      openDownload: sinon.stub(),
      showDownloadedFile: sinon.stub(),
    });

    globals.set("BrowserUtils", {
      whereToOpenLink: sinon.stub().returns("current"),
    });

    downloadsManager = new DownloadsManager();
    downloadsManager.init({ dispatch() {} });
    downloadsManager.onDownloadAdded({
      source: { url: DOWNLOAD_URL },
      endTime: Date.now(),
      target: { path: "/path/to/download.mov", exists: true },
      succeeded: true,
      refresh: async () => {},
    });
    assert.ok(downloadsManager._downloadItems.has(DOWNLOAD_URL));

    globals.set("NewTabUtils", { blockedLinks: { isBlocked() {} } });
  });
  afterEach(() => {
    downloadsManager._downloadItems.clear();
    globals.restore();
  });
  describe("#init", () => {
    it("should add a DownloadsCommon view on init", () => {
      downloadsManager.init({ dispatch() {} });
      assert.calledTwice(global.DownloadsCommon.getData().addView);
    });
  });
  describe("#onAction", () => {
    it("should copy the file on COPY_DOWNLOAD_LINK", () => {
      downloadsManager.onAction({
        type: at.COPY_DOWNLOAD_LINK,
        data: { url: DOWNLOAD_URL },
      });
      assert.calledOnce(global.DownloadsCommon.copyDownloadLink);
    });
    it("should remove the file on REMOVE_DOWNLOAD_FILE", () => {
      downloadsManager.onAction({
        type: at.REMOVE_DOWNLOAD_FILE,
        data: { url: DOWNLOAD_URL },
      });
      assert.calledOnce(global.DownloadsCommon.deleteDownload);
    });
    it("should show the file on SHOW_DOWNLOAD_FILE", () => {
      downloadsManager.onAction({
        type: at.SHOW_DOWNLOAD_FILE,
        data: { url: DOWNLOAD_URL },
      });
      assert.calledOnce(global.DownloadsCommon.showDownloadedFile);
    });
    it("should open the file on OPEN_DOWNLOAD_FILE if the type is download", () => {
      downloadsManager.onAction({
        type: at.OPEN_DOWNLOAD_FILE,
        data: { url: DOWNLOAD_URL, type: "download" },
        _target: { browser: {} },
      });
      assert.calledOnce(global.DownloadsCommon.openDownload);
    });
    it("should copy the file on UNINIT", () => {
      // DownloadsManager._downloadData needs to exist first
      downloadsManager.onAction({ type: at.UNINIT });
      assert.calledOnce(global.DownloadsCommon.getData().removeView);
    });
    it("should not execute a download command if we do not have the correct url", () => {
      downloadsManager.onAction({
        type: at.SHOW_DOWNLOAD_FILE,
        data: { url: "unknown_url" },
      });
      assert.notCalled(global.DownloadsCommon.showDownloadedFile);
    });
  });
  describe("#onDownloadAdded", () => {
    let newDownload;
    beforeEach(() => {
      downloadsManager._downloadItems.clear();
      newDownload = {
        source: { url: "https://site.com/newDownload.mov" },
        endTime: Date.now(),
        target: { path: "/path/to/newDownload.mov", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
    });
    afterEach(() => {
      downloadsManager._downloadItems.clear();
    });
    it("should add a download on onDownloadAdded", () => {
      downloadsManager.onDownloadAdded(newDownload);
      assert.ok(
        downloadsManager._downloadItems.has("https://site.com/newDownload.mov")
      );
    });
    it("should not add a download if it already exists", () => {
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(newDownload);
      const results = downloadsManager._downloadItems;
      assert.equal(results.size, 1);
    });
    it("should not return any downloads if no threshold is provided", async () => {
      downloadsManager.onDownloadAdded(newDownload);
      const results = await downloadsManager.getDownloads(null, {});
      assert.equal(results.length, 0);
    });
    it("should stop at numItems when it found one it's looking for", async () => {
      const aDownload = {
        source: { url: "https://site.com/aDownload.pdf" },
        endTime: Date.now(),
        target: { path: "/path/to/aDownload.pdf", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(aDownload);
      downloadsManager.onDownloadAdded(newDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 1,
        onlySucceeded: true,
        onlyExists: true,
      });
      assert.equal(results.length, 1);
      assert.equal(results[0].url, aDownload.source.url);
    });
    it("should get all the downloads younger than the threshold provided", async () => {
      const oldDownload = {
        source: { url: "https://site.com/oldDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/oldDownload.pdf", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
      // Add an old download (older than 36 hours in this case)
      downloadsManager.onDownloadAdded(oldDownload);
      downloadsManager.onDownloadAdded(newDownload);
      const RECENT_DOWNLOAD_THRESHOLD = 36 * 60 * 60 * 1000;
      const results = await downloadsManager.getDownloads(
        RECENT_DOWNLOAD_THRESHOLD,
        { numItems: 5, onlySucceeded: true, onlyExists: true }
      );
      assert.equal(results.length, 1);
      assert.equal(results[0].url, newDownload.source.url);
    });
    it("should dispatch DOWNLOAD_CHANGED when adding a download", () => {
      downloadsManager._store.dispatch = sinon.spy();
      downloadsManager._downloadTimer = null; // Nuke the timer
      downloadsManager.onDownloadAdded(newDownload);
      assert.calledOnce(downloadsManager._store.dispatch);
    });
    it("should refresh the downloads if onlyExists is true", async () => {
      const aDownload = {
        source: { url: "https://site.com/aDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/aDownload.pdf", exists: true },
        succeeded: true,
        refresh: () => {},
      };
      sinon.stub(aDownload, "refresh").returns(Promise.resolve());
      downloadsManager.onDownloadAdded(aDownload);
      await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
        onlyExists: true,
      });
      assert.calledOnce(aDownload.refresh);
    });
    it("should not refresh the downloads if onlyExists is false (by default)", async () => {
      const aDownload = {
        source: { url: "https://site.com/aDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/aDownload.pdf", exists: true },
        succeeded: true,
        refresh: () => {},
      };
      sinon.stub(aDownload, "refresh").returns(Promise.resolve());
      downloadsManager.onDownloadAdded(aDownload);
      await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
      });
      assert.notCalled(aDownload.refresh);
    });
    it("should only return downloads that exist if specified", async () => {
      const nonExistantDownload = {
        source: { url: "https://site.com/nonExistantDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/nonExistantDownload.pdf", exists: false },
        succeeded: true,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(nonExistantDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
        onlyExists: true,
      });
      assert.equal(results.length, 1);
      assert.equal(results[0].url, newDownload.source.url);
    });
    it("should return all downloads that either exist or don't exist if not specified", async () => {
      const nonExistantDownload = {
        source: { url: "https://site.com/nonExistantDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/nonExistantDownload.pdf", exists: false },
        succeeded: true,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(nonExistantDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
      });
      assert.equal(results.length, 2);
      assert.equal(results[0].url, newDownload.source.url);
      assert.equal(results[1].url, nonExistantDownload.source.url);
    });
    it("should return only unblocked downloads", async () => {
      const nonExistantDownload = {
        source: { url: "https://site.com/nonExistantDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/nonExistantDownload.pdf", exists: false },
        succeeded: true,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(nonExistantDownload);
      globals.set("NewTabUtils", {
        blockedLinks: {
          isBlocked: item => item.url === nonExistantDownload.source.url,
        },
      });

      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
      });

      assert.equal(results.length, 1);
      assert.propertyVal(results[0], "url", newDownload.source.url);
    });
    it("should only return downloads that were successful if specified", async () => {
      const nonSuccessfulDownload = {
        source: { url: "https://site.com/nonSuccessfulDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/nonSuccessfulDownload.pdf", exists: false },
        succeeded: false,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(nonSuccessfulDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
      });
      assert.equal(results.length, 1);
      assert.equal(results[0].url, newDownload.source.url);
    });
    it("should return all downloads that were either successful or not if not specified", async () => {
      const nonExistantDownload = {
        source: { url: "https://site.com/nonExistantDownload.pdf" },
        endTime: Date.now() - 40 * 60 * 60 * 1000,
        target: { path: "/path/to/nonExistantDownload.pdf", exists: true },
        succeeded: false,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
      downloadsManager.onDownloadAdded(nonExistantDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
      });
      assert.equal(results.length, 2);
      assert.equal(results[0].url, newDownload.source.url);
      assert.equal(results[1].url, nonExistantDownload.source.url);
    });
    it("should sort the downloads by recency", async () => {
      const olderDownload1 = {
        source: { url: "https://site.com/oldDownload1.pdf" },
        endTime: Date.now() - 2 * 60 * 60 * 1000, // 2 hours ago
        target: { path: "/path/to/oldDownload1.pdf", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
      const olderDownload2 = {
        source: { url: "https://site.com/oldDownload2.pdf" },
        endTime: Date.now() - 60 * 60 * 1000, // 1 hour ago
        target: { path: "/path/to/oldDownload2.pdf", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
      // Add some older downloads and check that they are in order
      downloadsManager.onDownloadAdded(olderDownload1);
      downloadsManager.onDownloadAdded(olderDownload2);
      downloadsManager.onDownloadAdded(newDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
        onlyExists: true,
      });
      assert.equal(results.length, 3);
      assert.equal(results[0].url, newDownload.source.url);
      assert.equal(results[1].url, olderDownload2.source.url);
      assert.equal(results[2].url, olderDownload1.source.url);
    });
    it("should format the description properly if there is no file type", async () => {
      newDownload.target.path = null;
      downloadsManager.onDownloadAdded(newDownload);
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
        onlySucceeded: true,
        onlyExists: true,
      });
      assert.equal(results.length, 1);
      assert.equal(results[0].description, "1.5 MB"); // see unit-entry.js to see where this comes from
    });
  });
  describe("#onDownloadRemoved", () => {
    let newDownload;
    beforeEach(() => {
      downloadsManager._downloadItems.clear();
      newDownload = {
        source: { url: "https://site.com/removeMe.mov" },
        endTime: Date.now(),
        target: { path: "/path/to/removeMe.mov", exists: true },
        succeeded: true,
        refresh: async () => {},
      };
      downloadsManager.onDownloadAdded(newDownload);
    });
    it("should remove a download if it exists on onDownloadRemoved", async () => {
      downloadsManager.onDownloadRemoved({
        source: { url: "https://site.com/removeMe.mov" },
      });
      const results = await downloadsManager.getDownloads(Infinity, {
        numItems: 5,
      });
      assert.deepEqual(results, []);
    });
    it("should dispatch DOWNLOAD_CHANGED when removing a download", () => {
      downloadsManager._store.dispatch = sinon.spy();
      downloadsManager.onDownloadRemoved({
        source: { url: "https://site.com/removeMe.mov" },
      });
      assert.calledOnce(downloadsManager._store.dispatch);
    });
  });
});