summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_downloads_search.js
blob: 2ca18abf86941dae7774779d1edb1d9dbf706e68 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const { Downloads } = ChromeUtils.importESModule(
  "resource://gre/modules/Downloads.sys.mjs"
);

const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));

const BASE = `http://localhost:${server.identity.primaryPort}/data`;
const TXT_FILE = "file_download.txt";
const TXT_URL = BASE + "/" + TXT_FILE;
const TXT_LEN = 46;
const HTML_FILE = "file_download.html";
const HTML_URL = BASE + "/" + HTML_FILE;
const HTML_LEN = 117;
const EMPTY_FILE = "empty_file_download.txt";
const EMPTY_URL = BASE + "/" + EMPTY_FILE;
const EMPTY_LEN = 0;
const BIG_LEN = 1000; // something bigger both TXT_LEN and HTML_LEN

function backgroundScript() {
  let complete = new Map();

  function waitForComplete(id) {
    if (complete.has(id)) {
      return complete.get(id).promise;
    }

    let promise = new Promise(resolve => {
      complete.set(id, { resolve });
    });
    complete.get(id).promise = promise;
    return promise;
  }

  browser.downloads.onChanged.addListener(change => {
    if (change.state && change.state.current == "complete") {
      // Make sure we have a promise.
      waitForComplete(change.id);
      complete.get(change.id).resolve();
    }
  });

  browser.test.onMessage.addListener(async (msg, ...args) => {
    if (msg == "download.request") {
      try {
        let id = await browser.downloads.download(args[0]);
        browser.test.sendMessage("download.done", { status: "success", id });
      } catch (error) {
        browser.test.sendMessage("download.done", {
          status: "error",
          errmsg: error.message,
        });
      }
    } else if (msg == "search.request") {
      try {
        let downloads = await browser.downloads.search(args[0]);
        browser.test.sendMessage("search.done", {
          status: "success",
          downloads,
        });
      } catch (error) {
        browser.test.sendMessage("search.done", {
          status: "error",
          errmsg: error.message,
        });
      }
    } else if (msg == "waitForComplete.request") {
      await waitForComplete(args[0]);
      browser.test.sendMessage("waitForComplete.done");
    }
  });

  browser.test.sendMessage("ready");
}

async function clearDownloads() {
  let list = await Downloads.getList(Downloads.ALL);
  let downloads = await list.getAll();

  await Promise.all(downloads.map(download => list.remove(download)));

  return downloads;
}

add_task(async function test_search() {
  const nsIFile = Ci.nsIFile;
  let downloadDir = FileUtils.getDir("TmpD", ["downloads"]);
  downloadDir.createUnique(nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  info(`downloadDir ${downloadDir.path}`);

  function downloadPath(filename) {
    let path = downloadDir.clone();
    path.append(filename);
    return path.path;
  }

  Services.prefs.setIntPref("browser.download.folderList", 2);
  Services.prefs.setComplexValue("browser.download.dir", nsIFile, downloadDir);
  Services.prefs.setBoolPref("privacy.reduceTimerPrecision", false);

  registerCleanupFunction(async () => {
    Services.prefs.clearUserPref("browser.download.folderList");
    Services.prefs.clearUserPref("browser.download.dir");
    Services.prefs.clearUserPref("privacy.reduceTimerPrecision");
    await cleanupDir(downloadDir);
    await clearDownloads();
  });

  await clearDownloads().then(downloads => {
    info(`removed ${downloads.length} pre-existing downloads from history`);
  });

  let extension = ExtensionTestUtils.loadExtension({
    background: backgroundScript,
    manifest: {
      permissions: ["downloads"],
    },
  });

  async function download(options) {
    extension.sendMessage("download.request", options);
    let result = await extension.awaitMessage("download.done");

    if (result.status == "success") {
      info(`wait for onChanged event to indicate ${result.id} is complete`);
      extension.sendMessage("waitForComplete.request", result.id);

      await extension.awaitMessage("waitForComplete.done");
    }

    return result;
  }

  function search(query) {
    extension.sendMessage("search.request", query);
    return extension.awaitMessage("search.done");
  }

  await extension.startup();
  await extension.awaitMessage("ready");

  // Do some downloads...
  const time1 = new Date();

  let downloadIds = {};
  let msg = await download({ url: TXT_URL });
  equal(msg.status, "success", "download() succeeded");
  downloadIds.txt1 = msg.id;

  const TXT_FILE2 = "NewFile.txt";
  msg = await download({ url: TXT_URL, filename: TXT_FILE2 });
  equal(msg.status, "success", "download() succeeded");
  downloadIds.txt2 = msg.id;

  msg = await download({ url: EMPTY_URL });
  equal(msg.status, "success", "download() succeeded");
  downloadIds.txt3 = msg.id;

  const time2 = new Date();

  msg = await download({ url: HTML_URL });
  equal(msg.status, "success", "download() succeeded");
  downloadIds.html1 = msg.id;

  const HTML_FILE2 = "renamed.html";
  msg = await download({ url: HTML_URL, filename: HTML_FILE2 });
  equal(msg.status, "success", "download() succeeded");
  downloadIds.html2 = msg.id;

  const time3 = new Date();

  // Search for each individual download and check
  // the corresponding DownloadItem.
  async function checkDownloadItem(id, expect) {
    let item = await search({ id });
    equal(item.status, "success", "search() succeeded");
    equal(item.downloads.length, 1, "search() found exactly 1 download");

    Object.keys(expect).forEach(function (field) {
      equal(
        item.downloads[0][field],
        expect[field],
        `DownloadItem.${field} is correct"`
      );
    });
  }
  await checkDownloadItem(downloadIds.txt1, {
    url: TXT_URL,
    filename: downloadPath(TXT_FILE),
    mime: "text/plain",
    state: "complete",
    bytesReceived: TXT_LEN,
    totalBytes: TXT_LEN,
    fileSize: TXT_LEN,
    exists: true,
  });

  await checkDownloadItem(downloadIds.txt2, {
    url: TXT_URL,
    filename: downloadPath(TXT_FILE2),
    mime: "text/plain",
    state: "complete",
    bytesReceived: TXT_LEN,
    totalBytes: TXT_LEN,
    fileSize: TXT_LEN,
    exists: true,
  });

  await checkDownloadItem(downloadIds.txt3, {
    url: EMPTY_URL,
    filename: downloadPath(EMPTY_FILE),
    mime: "text/plain",
    state: "complete",
    bytesReceived: EMPTY_LEN,
    totalBytes: EMPTY_LEN,
    fileSize: EMPTY_LEN,
    exists: true,
  });

  await checkDownloadItem(downloadIds.html1, {
    url: HTML_URL,
    filename: downloadPath(HTML_FILE),
    mime: "text/html",
    state: "complete",
    bytesReceived: HTML_LEN,
    totalBytes: HTML_LEN,
    fileSize: HTML_LEN,
    exists: true,
  });

  await checkDownloadItem(downloadIds.html2, {
    url: HTML_URL,
    filename: downloadPath(HTML_FILE2),
    mime: "text/html",
    state: "complete",
    bytesReceived: HTML_LEN,
    totalBytes: HTML_LEN,
    fileSize: HTML_LEN,
    exists: true,
  });

  async function checkSearch(query, expected, description, exact) {
    let item = await search(query);
    equal(item.status, "success", "search() succeeded");
    equal(
      item.downloads.length,
      expected.length,
      `search() for ${description} found exactly ${expected.length} downloads`
    );

    let receivedIds = item.downloads.map(i => i.id);
    if (exact) {
      receivedIds.forEach((id, idx) => {
        equal(
          id,
          downloadIds[expected[idx]],
          `search() for ${description} returned ${expected[idx]} in position ${idx}`
        );
      });
    } else {
      Object.keys(downloadIds).forEach(key => {
        const id = downloadIds[key];
        const thisExpected = expected.includes(key);
        equal(
          receivedIds.includes(id),
          thisExpected,
          `search() for ${description} ${
            thisExpected ? "includes" : "does not include"
          } ${key}`
        );
      });
    }
  }

  // Check that search with an invalid id returns nothing.
  // NB: for now ids are not persistent and we start numbering them at 1
  //     so a sufficiently large number will be unused.
  const INVALID_ID = 1000;
  await checkSearch({ id: INVALID_ID }, [], "invalid id");

  // Check that search on url works.
  await checkSearch({ url: TXT_URL }, ["txt1", "txt2"], "url");

  // Check that regexp on url works.
  const HTML_REGEX = "[download]{8}.html+$";
  await checkSearch({ urlRegex: HTML_REGEX }, ["html1", "html2"], "url regexp");

  // Check that compatible url+regexp works
  await checkSearch(
    { url: HTML_URL, urlRegex: HTML_REGEX },
    ["html1", "html2"],
    "compatible url+urlRegex"
  );

  // Check that incompatible url+regexp works
  await checkSearch(
    { url: TXT_URL, urlRegex: HTML_REGEX },
    [],
    "incompatible url+urlRegex"
  );

  // Check that search on filename works.
  await checkSearch({ filename: downloadPath(TXT_FILE) }, ["txt1"], "filename");

  // Check that regexp on filename works.
  await checkSearch({ filenameRegex: HTML_REGEX }, ["html1"], "filename regex");

  // Check that compatible filename+regexp works
  await checkSearch(
    { filename: downloadPath(HTML_FILE), filenameRegex: HTML_REGEX },
    ["html1"],
    "compatible filename+filename regex"
  );

  // Check that incompatible filename+regexp works
  await checkSearch(
    { filename: downloadPath(TXT_FILE), filenameRegex: HTML_REGEX },
    [],
    "incompatible filename+filename regex"
  );

  // Check that simple positive search terms work.
  await checkSearch(
    { query: ["file_download"] },
    ["txt1", "txt2", "txt3", "html1", "html2"],
    "term file_download"
  );
  await checkSearch({ query: ["NewFile"] }, ["txt2"], "term NewFile");

  // Check that positive search terms work case-insensitive.
  await checkSearch({ query: ["nEwfILe"] }, ["txt2"], "term nEwfiLe");

  // Check that negative search terms work.
  await checkSearch({ query: ["-txt"] }, ["html1", "html2"], "term -txt");

  // Check that positive and negative search terms together work.
  await checkSearch(
    { query: ["html", "-renamed"] },
    ["html1"],
    "positive and negative terms"
  );

  async function checkSearchWithDate(query, expected, description) {
    const fields = Object.keys(query);
    if (fields.length != 1 || !(query[fields[0]] instanceof Date)) {
      throw new Error("checkSearchWithDate expects exactly one Date field");
    }
    const field = fields[0];
    const date = query[field];

    let newquery = {};

    // Check as a Date
    newquery[field] = date;
    await checkSearch(newquery, expected, `${description} as Date`);

    // Check as numeric milliseconds
    newquery[field] = date.valueOf();
    await checkSearch(newquery, expected, `${description} as numeric ms`);

    // Check as stringified milliseconds
    newquery[field] = date.valueOf().toString();
    await checkSearch(newquery, expected, `${description} as string ms`);

    // Check as ISO string
    newquery[field] = date.toISOString();
    await checkSearch(newquery, expected, `${description} as iso string`);
  }

  // Check startedBefore
  await checkSearchWithDate({ startedBefore: time1 }, [], "before time1");
  await checkSearchWithDate(
    { startedBefore: time2 },
    ["txt1", "txt2", "txt3"],
    "before time2"
  );
  await checkSearchWithDate(
    { startedBefore: time3 },
    ["txt1", "txt2", "txt3", "html1", "html2"],
    "before time3"
  );

  // Check startedAfter
  await checkSearchWithDate(
    { startedAfter: time1 },
    ["txt1", "txt2", "txt3", "html1", "html2"],
    "after time1"
  );
  await checkSearchWithDate(
    { startedAfter: time2 },
    ["html1", "html2"],
    "after time2"
  );
  await checkSearchWithDate({ startedAfter: time3 }, [], "after time3");

  // Check simple search on totalBytes
  await checkSearch({ totalBytes: TXT_LEN }, ["txt1", "txt2"], "totalBytes");
  await checkSearch({ totalBytes: HTML_LEN }, ["html1", "html2"], "totalBytes");

  // Check simple test on totalBytes{Greater,Less}
  // (NB: TXT_LEN < HTML_LEN < BIG_LEN)
  await checkSearch(
    { totalBytesGreater: 0 },
    ["txt1", "txt2", "html1", "html2"],
    "totalBytesGreater than 0"
  );
  await checkSearch(
    { totalBytesGreater: TXT_LEN },
    ["html1", "html2"],
    `totalBytesGreater than ${TXT_LEN}`
  );
  await checkSearch(
    { totalBytesGreater: HTML_LEN },
    [],
    `totalBytesGreater than ${HTML_LEN}`
  );
  await checkSearch(
    { totalBytesLess: TXT_LEN },
    ["txt3"],
    `totalBytesLess than ${TXT_LEN}`
  );
  await checkSearch(
    { totalBytesLess: HTML_LEN },
    ["txt1", "txt2", "txt3"],
    `totalBytesLess than ${HTML_LEN}`
  );
  await checkSearch(
    { totalBytesLess: BIG_LEN },
    ["txt1", "txt2", "txt3", "html1", "html2"],
    `totalBytesLess than ${BIG_LEN}`
  );

  // Bug 1503760 check if 0 byte files with no search query are returned.
  await checkSearch(
    {},
    ["txt1", "txt2", "txt3", "html1", "html2"],
    "totalBytesGreater than -1"
  );

  // Check good combinations of totalBytes*.
  await checkSearch(
    { totalBytes: HTML_LEN, totalBytesGreater: TXT_LEN },
    ["html1", "html2"],
    "totalBytes and totalBytesGreater"
  );
  await checkSearch(
    { totalBytes: TXT_LEN, totalBytesLess: HTML_LEN },
    ["txt1", "txt2"],
    "totalBytes and totalBytesGreater"
  );
  await checkSearch(
    { totalBytes: HTML_LEN, totalBytesLess: BIG_LEN, totalBytesGreater: 0 },
    ["html1", "html2"],
    "totalBytes and totalBytesLess and totalBytesGreater"
  );

  // Check bad combination of totalBytes*.
  await checkSearch(
    { totalBytesLess: TXT_LEN, totalBytesGreater: HTML_LEN },
    [],
    "bad totalBytesLess, totalBytesGreater combination"
  );
  await checkSearch(
    { totalBytes: TXT_LEN, totalBytesGreater: HTML_LEN },
    [],
    "bad totalBytes, totalBytesGreater combination"
  );
  await checkSearch(
    { totalBytes: HTML_LEN, totalBytesLess: TXT_LEN },
    [],
    "bad totalBytes, totalBytesLess combination"
  );

  // Check mime.
  await checkSearch(
    { mime: "text/plain" },
    ["txt1", "txt2", "txt3"],
    "mime text/plain"
  );
  await checkSearch(
    { mime: "text/html" },
    ["html1", "html2"],
    "mime text/htmlplain"
  );
  await checkSearch({ mime: "video/webm" }, [], "mime video/webm");

  // Check fileSize.
  await checkSearch({ fileSize: TXT_LEN }, ["txt1", "txt2"], "fileSize");
  await checkSearch({ fileSize: HTML_LEN }, ["html1", "html2"], "fileSize");

  // Fields like bytesReceived, paused, state, exists are meaningful
  // for downloads that are in progress but have not yet completed.
  // todo: add tests for these when we have better support for in-progress
  // downloads (e.g., after pause(), resume() and cancel() are implemented)

  // Check multiple query properties.
  // We could make this testing arbitrarily complicated...
  // We already tested combining fields with obvious interactions above
  // (e.g., filename and filenameRegex or startTime and startedBefore/After)
  // so now just throw as many fields as we can at a single search and
  // make sure a simple case still works.
  await checkSearch(
    {
      url: TXT_URL,
      urlRegex: "download",
      filename: downloadPath(TXT_FILE),
      filenameRegex: "download",
      query: ["download"],
      startedAfter: time1.valueOf().toString(),
      startedBefore: time2.valueOf().toString(),
      totalBytes: TXT_LEN,
      totalBytesGreater: 0,
      totalBytesLess: BIG_LEN,
      mime: "text/plain",
      fileSize: TXT_LEN,
    },
    ["txt1"],
    "many properties"
  );

  // Check simple orderBy (forward and backward).
  await checkSearch(
    { orderBy: ["startTime"] },
    ["txt1", "txt2", "txt3", "html1", "html2"],
    "orderBy startTime",
    true
  );
  await checkSearch(
    { orderBy: ["-startTime"] },
    ["html2", "html1", "txt3", "txt2", "txt1"],
    "orderBy -startTime",
    true
  );

  // Check orderBy with multiple fields.
  // NB: TXT_URL and HTML_URL differ only in extension and .html precedes .txt
  // EMPTY_URL begins with e which precedes f
  await checkSearch(
    { orderBy: ["url", "-startTime"] },
    ["txt3", "html2", "html1", "txt2", "txt1"],
    "orderBy with multiple fields",
    true
  );

  // Check orderBy with limit.
  await checkSearch(
    { orderBy: ["url"], limit: 1 },
    ["txt3"],
    "orderBy with limit",
    true
  );

  // Check bad arguments.
  async function checkBadSearch(query, pattern, description) {
    let item = await search(query);
    equal(item.status, "error", "search() failed");
    ok(
      pattern.test(item.errmsg),
      `error message for ${description} was correct (${item.errmsg}).`
    );
  }

  await checkBadSearch(
    "myquery",
    /Incorrect argument type/,
    "query is not an object"
  );
  await checkBadSearch(
    { bogus: "boo" },
    /Unexpected property/,
    "query contains an unknown field"
  );
  await checkBadSearch(
    { query: "query string" },
    /Expected array/,
    "query.query is a string"
  );
  await checkBadSearch(
    { startedBefore: "i am not a time" },
    /Type error/,
    "query.startedBefore is not a valid time"
  );
  await checkBadSearch(
    { startedAfter: "i am not a time" },
    /Type error/,
    "query.startedAfter is not a valid time"
  );
  await checkBadSearch(
    { endedBefore: "i am not a time" },
    /Type error/,
    "query.endedBefore is not a valid time"
  );
  await checkBadSearch(
    { endedAfter: "i am not a time" },
    /Type error/,
    "query.endedAfter is not a valid time"
  );
  await checkBadSearch(
    { urlRegex: "[" },
    /Invalid urlRegex/,
    "query.urlRegexp is not a valid regular expression"
  );
  await checkBadSearch(
    { filenameRegex: "[" },
    /Invalid filenameRegex/,
    "query.filenameRegexp is not a valid regular expression"
  );
  await checkBadSearch(
    { orderBy: "startTime" },
    /Expected array/,
    "query.orderBy is not an array"
  );
  await checkBadSearch(
    { orderBy: ["bogus"] },
    /Invalid orderBy field/,
    "query.orderBy references a non-existent field"
  );

  await extension.unload();
});

// Test that downloads with totalBytes of -1 (ie, that have not yet started)
// work properly.  See bug 1519762 for details of a past regression in
// this area.
add_task(async function test_inprogress() {
  let resume,
    resumePromise = new Promise(resolve => {
      resume = resolve;
    });
  let hit = false;
  server.registerPathHandler("/data/slow", async (request, response) => {
    hit = true;
    response.processAsync();
    await resumePromise;
    response.setHeader("Content-type", "text/plain");
    response.write("");
    response.finish();
  });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["downloads"],
    },
    background() {
      browser.test.onMessage.addListener(async (msg, url) => {
        let id = await browser.downloads.download({ url });
        let full = await browser.downloads.search({ id });

        browser.test.assertEq(
          full.length,
          1,
          "Found new download in search results"
        );
        browser.test.assertEq(
          full[0].totalBytes,
          -1,
          "New download still has totalBytes == -1"
        );

        browser.downloads.onChanged.addListener(info => {
          if (info.id == id && info.state && info.state.current == "complete") {
            browser.test.notifyPass("done");
          }
        });

        browser.test.sendMessage("started");
      });
    },
  });

  await extension.startup();
  extension.sendMessage("go", `${BASE}/slow`);
  await extension.awaitMessage("started");
  resume();
  await extension.awaitFinish("done");
  await extension.unload();
  Assert.ok(hit, "slow path was actually hit");
});