summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_downloads_context_menu_selection.js
blob: a4a8eacf36c57e87b72439698e776fc5a5b9e00f (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test that the context menu refers to the triggering item, even if the
 * selection was not set preemptively.
 */

async function createDownloadFiles() {
  let dir = await setDownloadDir();
  let downloads = [];
  downloads.push({
    state: DownloadsCommon.DOWNLOAD_FAILED,
    contentType: "text/plain",
    target: new FileUtils.File(PathUtils.join(dir, "does-not-exist.txt")),
  });
  downloads.push({
    state: DownloadsCommon.DOWNLOAD_FINISHED,
    contentType: "text/plain",
    target: await createDownloadedFile(PathUtils.join(dir, "file.txt"), "file"),
  });
  return downloads;
}

add_setup(async function setup() {
  await PlacesUtils.history.clear();
  await startServer();

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

add_task(async function test() {
  // remove download files, empty out collections
  let downloadList = await Downloads.getList(Downloads.ALL);
  let downloadCount = (await downloadList.getAll()).length;
  Assert.equal(downloadCount, 0, "There should be 0 downloads");
  await task_resetState();
  let downloads = await createDownloadFiles();
  await task_addDownloads(downloads);
  await task_openPanel();
  let downloadsListBox = document.getElementById("downloadsListBox");
  await TestUtils.waitForCondition(() => {
    downloadsListBox.removeAttribute("disabled");
    return downloadsListBox.childElementCount == downloads.length;
  });

  // Note we're not doing anything to set the selectedItem here, exactly to
  // check the context menu doesn't depend on some selection prerequisite.

  let first = downloadsListBox.querySelector("richlistitem");
  let second = downloadsListBox.querySelector("richlistitem:nth-child(2)");

  info("Check first item");
  let firstDownload = DownloadsView.itemForElement(first).download;
  is(
    DownloadsCommon.stateOfDownload(firstDownload),
    DownloadsCommon.DOWNLOAD_FINISHED,
    "Download states match up"
  );
  // mousemove to the _other_ download, to ensure it doesn't confuse code.
  EventUtils.synthesizeMouse(second, -5, -5, { type: "mousemove" });
  await checkCommandsWithContextMenu(first, {
    downloadsCmd_show: true,
    cmd_delete: true,
  });

  info("Check second item");
  let secondDownload = DownloadsView.itemForElement(second).download;
  is(
    DownloadsCommon.stateOfDownload(secondDownload),
    DownloadsCommon.DOWNLOAD_FAILED,
    "Download states match up"
  );
  // mousemove to the _other_ download, to ensure it doesn't confuse code.
  EventUtils.synthesizeMouse(first, -5, -5, { type: "mousemove" });
  await checkCommandsWithContextMenu(second, {
    downloadsCmd_show: false,
    cmd_delete: true,
  });

  info("Check we don't open a context menu between items.");
  function listener() {
    Assert.ok(false, "Should not open a context menu");
  }
  document.addEventListener("popupshown", listener);
  let listRect = downloadsListBox.getBoundingClientRect();
  let firstRect = first.getBoundingClientRect();
  let secondRect = second.getBoundingClientRect();
  let x = parseInt(firstRect.width / 2);
  Assert.greater(
    secondRect.y - firstRect.y - firstRect.height,
    1,
    "There should be a gap of at least 1 px for this test"
  );
  let y = parseInt(firstRect.y - listRect.y + firstRect.height + 1);
  info(`Right click at (${x}, ${y})`);
  EventUtils.synthesizeMouse(downloadsListBox, x, y, {
    type: "contextmenu",
    button: 2,
  });
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(r => setTimeout(r, 100));
  document.removeEventListener("popupshown", listener);

  let hiddenPromise = BrowserTestUtils.waitForEvent(
    DownloadsPanel.panel,
    "popuphidden"
  );
  DownloadsPanel.hidePanel();
  await hiddenPromise;
});

async function checkCommandsWithContextMenu(element, commands) {
  let contextMenu = await openContextMenu(element);
  for (let command in commands) {
    let enabled = commands[command];
    let commandStatus = enabled ? "enabled" : "disabled";
    info(`Checking command ${command} is ${commandStatus}`);

    let commandElt = contextMenu.querySelector(`[command="${command}"]`);
    Assert.equal(
      !BrowserTestUtils.is_hidden(commandElt),
      enabled,
      `${command} should be ${enabled ? "visible" : "hidden"}`
    );

    Assert.strictEqual(
      DownloadsView.richListBox.selectedItem._shell.isCommandEnabled(command),
      enabled,
      `${command} should be ${commandStatus}`
    );
  }
  contextMenu.hidePopup();
}