summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/browser/eventDialog/browser_attachMenu.js
blob: 2a0b2afc4c4e76818f6aa14fd14e2b55314b0823 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests for the attach menu in the event dialog window.
 */

const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
const { cloudFileAccounts } = ChromeUtils.import("resource:///modules/cloudFileAccounts.jsm");
const { MockFilePicker } = ChromeUtils.importESModule(
  "resource://testing-common/MockFilePicker.sys.mjs"
);
var { saveAndCloseItemDialog, setData } = ChromeUtils.import(
  "resource://testing-common/calendar/ItemEditingHelpers.jsm"
);

// Remove the save prompt observer that head.js added. It's causing trouble here.
Services.ww.unregisterNotification(savePromptObserver);

let calendar = CalendarTestUtils.createCalendar("Attachments");
registerCleanupFunction(() => {
  cal.manager.unregisterCalendar(calendar);
  MockFilePicker.cleanup();
});

async function getEventBox(selector) {
  let itemBox;
  await TestUtils.waitForCondition(() => {
    itemBox = document.querySelector(selector);
    return itemBox != null;
  }, "calendar item did not appear in time");
  return itemBox;
}

async function openEventFromBox(eventBox) {
  if (Services.focus.activeWindow != window) {
    await BrowserTestUtils.waitForEvent(window, "focus");
  }
  let promise = CalendarTestUtils.waitForEventDialog();
  EventUtils.synthesizeMouseAtCenter(eventBox, { clickCount: 2 });
  return promise;
}

/**
 * Tests using the "Website" menu item attaches a link to the event.
 */
add_task(async function testAttachWebPage() {
  let startDate = cal.createDateTime("20200101T000001Z");
  await CalendarTestUtils.setCalendarView(window, "month");
  window.goToDate(startDate);

  let { dialogWindow, iframeWindow, dialogDocument, iframeDocument } =
    await CalendarTestUtils.editNewEvent(window);

  await setData(dialogWindow, iframeWindow, {
    title: "Web Link Event",
    startDate,
  });

  // Attach the url.
  let attachButton = dialogWindow.document.querySelector("#button-url");
  Assert.ok(attachButton, "attach menu button found");

  let menu = dialogDocument.querySelector("#button-attach-menupopup");
  let menuShowing = BrowserTestUtils.waitForEvent(menu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(attachButton, {}, dialogWindow);
  await menuShowing;

  let url = "https://thunderbird.net/";
  let urlPrompt = BrowserTestUtils.promiseAlertDialogOpen(
    "",
    "chrome://global/content/commonDialog.xhtml",
    {
      async callback(win) {
        win.document.querySelector("#loginTextbox").value = url;
        EventUtils.synthesizeKey("VK_RETURN", {}, win);
      },
    }
  );
  EventUtils.synthesizeMouseAtCenter(
    dialogDocument.querySelector("#button-attach-url"),
    {},
    dialogWindow
  );
  await urlPrompt;

  // Now check that the url shows in the attachments list.
  EventUtils.synthesizeMouseAtCenter(
    iframeDocument.querySelector("#event-grid-tab-attachments"),
    {},
    iframeWindow
  );

  let listBox = iframeDocument.querySelector("#attachment-link");
  await BrowserTestUtils.waitForCondition(
    () => listBox.itemChildren.length == 1,
    "attachment list did not show in time"
  );

  Assert.equal(listBox.itemChildren[0].tooltipText, url, "url included in attachments list");

  // Save the new event.
  await saveAndCloseItemDialog(dialogWindow);

  // Open the event to verify the attachment is shown in the summary dialog.
  let summaryWin = await openEventFromBox(await getEventBox("calendar-month-day-box-item"));
  let label = summaryWin.document.querySelector(`label[value="${url}"]`);
  Assert.ok(label, "attachment label found on calendar summary dialog");
  await BrowserTestUtils.closeWindow(summaryWin);

  // Clean up.
  let eventBox = await getEventBox("calendar-month-day-box-item");
  eventBox.focus();
  EventUtils.synthesizeKey("VK_DELETE", {});
});

/**
 * Tests selecting a provider from the attach menu works.
 */
add_task(async function testAttachProvider() {
  let fileUrl = "https://path/to/mock/file.pdf";
  let iconURL = "chrome://messenger/content/extension.svg";
  let provider = {
    type: "Mochitest",
    displayName: "Mochitest",
    iconURL,
    initAccount(accountKey) {
      return {
        accountKey,
        type: "Mochitest",
        get displayName() {
          return Services.prefs.getCharPref(
            `mail.cloud_files.accounts.${this.accountKey}.displayName`,
            "Mochitest Account"
          );
        },
        iconURL,
        configured: true,
        managementURL: "",
        uploadFile(window, aFile) {
          return new Promise(resolve =>
            setTimeout(() =>
              resolve({
                id: 1,
                path: aFile.path,
                size: aFile.fileSize,
                url: fileUrl,
                // The uploadFile() function should return serviceIcon, serviceName
                // and serviceUrl - either default or user defined values specified
                // by the onFileUpload event. The item-edit dialog uses only the
                // serviceIcon.
                serviceIcon: "chrome://messenger/skin/icons/globe.svg",
              })
            )
          );
        },
      };
    },
  };

  cloudFileAccounts.registerProvider("Mochitest", provider);
  cloudFileAccounts.createAccount("Mochitest");
  registerCleanupFunction(() => {
    cloudFileAccounts.unregisterProvider("Mochitest");
  });

  let file = new FileUtils.File(getTestFilePath("data/guests.txt"));
  MockFilePicker.init(window);
  MockFilePicker.setFiles([file]);
  MockFilePicker.returnValue = MockFilePicker.returnOk;

  let startDate = cal.createDateTime("20200201T000001Z");
  await CalendarTestUtils.setCalendarView(window, "month");
  window.goToDate(startDate);

  let { dialogWindow, iframeWindow, dialogDocument, iframeDocument } =
    await CalendarTestUtils.editNewEvent(window);

  await setData(dialogWindow, iframeWindow, {
    title: "Provider Attachment Event",
    startDate,
  });

  let attachButton = dialogDocument.querySelector("#button-url");
  Assert.ok(attachButton, "attach menu button found");

  let menu = dialogDocument.querySelector("#button-attach-menupopup");
  let menuItem;

  await BrowserTestUtils.waitForCondition(() => {
    menuItem = menu.querySelector("menuitem[label='File using Mochitest Account']");
    return menuItem;
  });

  Assert.ok(menuItem, "custom provider menuitem found");
  Assert.equal(menuItem.image, iconURL, "provider image src is provider image");

  // Click on the "Attach" menu.
  let menuShowing = BrowserTestUtils.waitForEvent(menu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(attachButton, {}, dialogWindow);
  await menuShowing;

  // Click on the menuitem to attach a file using our provider.
  let menuHidden = BrowserTestUtils.waitForEvent(menu, "popuphidden");
  EventUtils.synthesizeMouseAtCenter(menuItem, {}, dialogWindow);
  await menuHidden;

  // Check if the file dialog was "shown". MockFilePicker.open() is asynchronous
  // but does not return a promise.
  await BrowserTestUtils.waitForCondition(
    () => MockFilePicker.shown,
    "file picker was not shown in time"
  );

  // Click on the attachments tab of the event dialog.
  EventUtils.synthesizeMouseAtCenter(
    iframeDocument.querySelector("#event-grid-tab-attachments"),
    {},
    iframeWindow
  );

  // Wait until the file we attached appears.
  let listBox = iframeDocument.querySelector("#attachment-link");
  await BrowserTestUtils.waitForCondition(
    () => listBox.itemChildren.length == 1,
    "attachment list did not show in time"
  );

  let listItem = listBox.itemChildren[0];

  // XXX: This property is set after an async operation. Unfortunately, that
  // operation is not awaited on in its surrounding code so the assertion
  // after this will occasionally fail if this is not done.
  await BrowserTestUtils.waitForCondition(
    () => listItem.attachCloudFileUpload,
    "attachCloudFileUpload property not set on attachment listitem in time."
  );

  Assert.equal(listItem.attachCloudFileUpload.url, fileUrl, "upload attached to event");

  let listItemImage = listItem.querySelector("img");
  Assert.equal(
    listItemImage.src,
    "chrome://messenger/skin/icons/globe.svg",
    "attachment image is provider image"
  );

  // Save the new event.
  dialogDocument.querySelector("#button-saveandclose").click();

  // Open it and verify the attachment is shown.
  let summaryWin = await openEventFromBox(await getEventBox("calendar-month-day-box-item"));
  let label = summaryWin.document.querySelector(`label[value="${fileUrl}"]`);
  Assert.ok(label, "attachment label found on calendar summary dialog");
  await BrowserTestUtils.closeWindow(summaryWin);

  if (Services.focus.activeWindow != window) {
    await BrowserTestUtils.waitForEvent(window, "focus");
  }

  // Clean up.
  let eventBox = await getEventBox("calendar-month-day-box-item");
  eventBox.focus();
  EventUtils.synthesizeKey("VK_DELETE", {});
});