summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_library_clearall.js
blob: 022d1b69779f6fc9c643bffb58fee2246c6fec35 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

ChromeUtils.defineESModuleGetters(this, {
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
});

let win;

function waitForChildren(element, callback) {
  let MutationObserver = element.ownerGlobal.MutationObserver;
  return new Promise(resolve => {
    let observer = new MutationObserver(() => {
      if (callback()) {
        observer.disconnect();
        resolve();
      }
    });
    observer.observe(element, { childList: true });
  });
}

async function waitForChildrenLength(element, length, callback) {
  if (element.childElementCount != length) {
    await waitForChildren(element, () => element.childElementCount == length);
  }
}

registerCleanupFunction(async function () {
  await task_resetState();
  await PlacesUtils.history.clear();
});

async function testClearingDownloads(clearCallback) {
  const DOWNLOAD_DATA = [
    httpUrl("file1.txt"),
    httpUrl("file2.txt"),
    httpUrl("file3.txt"),
  ];

  let listbox = win.document.getElementById("downloadsListBox");
  ok(listbox, "download list box present");

  let promiseLength = waitForChildrenLength(listbox, DOWNLOAD_DATA.length);
  await simulateDropAndCheck(win, listbox, DOWNLOAD_DATA);
  await promiseLength;

  let receivedNotifications = [];
  const promiseNotification = PlacesTestUtils.waitForNotification(
    "page-removed",
    events => {
      for (const { url, isRemovedFromStore } of events) {
        Assert.ok(isRemovedFromStore);

        if (DOWNLOAD_DATA.includes(url)) {
          receivedNotifications.push(url);
        }
      }
      return receivedNotifications.length == DOWNLOAD_DATA.length;
    }
  );

  promiseLength = waitForChildrenLength(listbox, 0);
  await clearCallback(listbox);
  await promiseLength;

  await promiseNotification;

  Assert.deepEqual(
    receivedNotifications.sort(),
    DOWNLOAD_DATA.sort(),
    "Should have received notifications for each URL"
  );
}

add_setup(async function () {
  // Ensure that state is reset in case previous tests didn't finish.
  await task_resetState();

  await setDownloadDir();

  startServer();

  win = await openLibrary("Downloads");
  registerCleanupFunction(function () {
    win.close();
  });
});

add_task(async function test_clear_downloads_toolbar() {
  await testClearingDownloads(async () => {
    win.document.getElementById("clearDownloadsButton").click();
  });
});

add_task(async function test_clear_downloads_context_menu() {
  await testClearingDownloads(async listbox => {
    // Select one of the downloads.
    listbox.itemChildren[0].click();

    let contextMenu = win.document.getElementById("downloadsContextMenu");

    let popupShownPromise = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popupshown"
    );
    EventUtils.synthesizeMouseAtCenter(
      listbox.itemChildren[0],
      { type: "contextmenu", button: 2 },
      win
    );
    await popupShownPromise;

    // Find the clear context item.
    let clearDownloadsButton = [...contextMenu.children].find(
      child => child.command == "downloadsCmd_clearDownloads"
    );
    contextMenu.activateItem(clearDownloadsButton);
  });
});