summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/browser/eventDialog/browser_eventDialog.js
blob: 44d75d7169a96f1da372620979fbbf830a6b2829 (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
/* 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/. */

var { TIMEOUT_MODAL_DIALOG, checkMonthAlarmIcon, handleDeleteOccurrencePrompt } =
  ChromeUtils.import("resource://testing-common/calendar/CalendarUtils.jsm");
var { cancelItemDialog, formatTime, saveAndCloseItemDialog, setData } = ChromeUtils.import(
  "resource://testing-common/calendar/ItemEditingHelpers.jsm"
);
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

XPCOMUtils.defineLazyModuleGetters(this, {
  CalEvent: "resource:///modules/CalEvent.jsm",
});

const EVENTTITLE = "Event";
const EVENTLOCATION = "Location";
const EVENTDESCRIPTION = "Event Description";
const EVENTATTENDEE = "foo@example.com";
const EVENTURL = "https://mozilla.org/";
const EVENT_ORGANIZER_EMAIL = "pillow@example.com";
var firstDay;

var { dayView, monthView } = CalendarTestUtils;

let calendar = CalendarTestUtils.createCalendar();
// This is done so that calItemBase#isInvitation returns true.
calendar.setProperty("organizerId", `mailto:${EVENT_ORGANIZER_EMAIL}`);
registerCleanupFunction(() => {
  CalendarTestUtils.removeCalendar(calendar);
});

add_task(async function testEventDialog() {
  let now = new Date();

  // Since from other tests we may be elsewhere, make sure we start today.
  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(
    window,
    now.getUTCFullYear(),
    now.getUTCMonth() + 1,
    now.getUTCDate()
  );
  await CalendarTestUtils.calendarViewBackward(window, 1);

  // Open month view.
  await CalendarTestUtils.setCalendarView(window, "month");
  firstDay = window.currentView().startDay;
  dump(`First day in view is: ${firstDay.year}-${firstDay.month + 1}-${firstDay.day}\n`);

  // Setup start- & endTime.
  // Next full hour except last hour of the day.
  let hour = now.getUTCHours();
  let startHour = hour == 23 ? hour : (hour + 1) % 24;

  let nextHour = cal.dtz.now();
  nextHour.resetTo(firstDay.year, firstDay.month, firstDay.day, startHour, 0, 0, cal.dtz.UTC);
  let startTime = formatTime(nextHour);
  nextHour.resetTo(
    firstDay.year,
    firstDay.month,
    firstDay.day,
    (startHour + 1) % 24,
    0,
    0,
    cal.dtz.UTC
  );
  let endTime = formatTime(nextHour);

  // Create new event on first day in view.
  EventUtils.synthesizeMouseAtCenter(monthView.getDayBox(window, 1, 1), {}, window);

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

  // First check all standard-values are set correctly.
  let startPicker = iframeDocument.getElementById("event-starttime");
  Assert.equal(startPicker._timepicker._inputField.value, startTime);

  // Check selected calendar.
  Assert.equal(iframeDocument.getElementById("item-calendar").value, "Test");

  // Check standard title.
  let defTitle = cal.l10n.getAnyString("calendar", "calendar", "newEvent");
  Assert.equal(iframeDocument.getElementById("item-title").placeholder, defTitle);

  // Prepare category.
  let categories = cal.l10n.getAnyString("calendar", "categories", "categories2");
  // Pick 4th value in a comma-separated list.
  let category = categories.split(",")[4];
  // Calculate date to repeat until.
  let untildate = firstDay.clone();
  untildate.addDuration(cal.createDuration("P20D"));

  // Fill in the rest of the values.
  await setData(dialogWindow, iframeWindow, {
    title: EVENTTITLE,
    location: EVENTLOCATION,
    description: EVENTDESCRIPTION,
    categories: [category],
    repeat: "daily",
    repeatuntil: untildate,
    reminder: "5minutes",
    privacy: "private",
    attachment: { add: EVENTURL },
    attendees: { add: EVENTATTENDEE },
  });

  // Verify attendee added.
  EventUtils.synthesizeMouseAtCenter(
    iframeDocument.getElementById("event-grid-tab-attendees"),
    {},
    dialogWindow
  );

  let attendeesTab = iframeDocument.getElementById("event-grid-tabpanel-attendees");
  let attendeeNameElements = attendeesTab.querySelectorAll(".attendee-list .attendee-name");
  Assert.equal(attendeeNameElements.length, 2, "there should be two attendees after save");
  Assert.equal(attendeeNameElements[0].textContent, EVENT_ORGANIZER_EMAIL);
  Assert.equal(attendeeNameElements[1].textContent, EVENTATTENDEE);
  Assert.ok(!iframeDocument.getElementById("notify-attendees-checkbox").checked);

  // Verify private label visible.
  await TestUtils.waitForCondition(
    () => !dialogDocument.getElementById("status-privacy-private-box").hasAttribute("collapsed")
  );
  dialogDocument.getElementById("event-privacy-menupopup").hidePopup();

  // Add attachment and verify added.
  EventUtils.synthesizeMouseAtCenter(
    iframeDocument.getElementById("event-grid-tab-attachments"),
    {},
    iframeWindow
  );

  let attachmentsTab = iframeDocument.getElementById("event-grid-tabpanel-attachments");
  Assert.equal(attachmentsTab.querySelectorAll("richlistitem").length, 1);

  let alarmPromise = BrowserTestUtils.promiseAlertDialog(
    undefined,
    "chrome://calendar/content/calendar-alarm-dialog.xhtml",
    {
      callback(alarmWindow) {
        let dismissAllButton = alarmWindow.document.getElementById("alarm-dismiss-all-button");
        EventUtils.synthesizeMouseAtCenter(dismissAllButton, {}, alarmWindow);
      },
    }
  );

  // save
  await saveAndCloseItemDialog(dialogWindow);

  // Catch and dismiss alarm.
  await alarmPromise;

  // Verify event and alarm icon visible until endDate (3 full rows) and check tooltip.
  for (let row = 1; row <= 3; row++) {
    for (let col = 1; col <= 7; col++) {
      await monthView.waitForItemAt(window, row, col, 1);
      checkMonthAlarmIcon(window, row, col);
      checkTooltip(row, col, startTime, endTime);
    }
  }
  Assert.ok(!monthView.getItemAt(window, 4, 1, 1));

  // Delete and verify deleted 6th col in row 1.
  EventUtils.synthesizeMouseAtCenter(monthView.getItemAt(window, 1, 6, 1), {}, window);
  let elemToDelete = document.getElementById("month-view");
  await handleDeleteOccurrencePrompt(window, elemToDelete, false);

  await monthView.waitForNoItemAt(window, 1, 6, 1);

  // Verify all others still exist.
  for (let col = 1; col <= 5; col++) {
    Assert.ok(monthView.getItemAt(window, 1, col, 1));
  }
  Assert.ok(monthView.getItemAt(window, 1, 7, 1));

  for (let row = 2; row <= 3; row++) {
    for (let col = 1; col <= 7; col++) {
      Assert.ok(monthView.getItemAt(window, row, col, 1));
    }
  }

  // Delete series by deleting last item in row 1 and confirming to delete all.
  EventUtils.synthesizeMouseAtCenter(monthView.getItemAt(window, 1, 7, 1), {}, window);
  elemToDelete = document.getElementById("month-view");
  await handleDeleteOccurrencePrompt(window, elemToDelete, true);

  // Verify all deleted.
  await monthView.waitForNoItemAt(window, 1, 5, 1);
  await monthView.waitForNoItemAt(window, 1, 6, 1);
  await monthView.waitForNoItemAt(window, 1, 7, 1);

  for (let row = 2; row <= 3; row++) {
    for (let col = 1; col <= 7; col++) {
      await monthView.waitForNoItemAt(window, row, col, 1);
    }
  }
});

add_task(async function testOpenExistingEventDialog() {
  let now = new Date();

  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(
    window,
    now.getUTCFullYear(),
    now.getUTCMonth() + 1,
    now.getUTCDate()
  );

  let createBox = dayView.getHourBoxAt(window, 8);

  // Create a new event.
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, createBox);
  await setData(dialogWindow, iframeWindow, {
    title: EVENTTITLE,
    location: EVENTLOCATION,
    description: EVENTDESCRIPTION,
  });
  await saveAndCloseItemDialog(dialogWindow);

  let eventBox = await dayView.waitForEventBoxAt(window, 1);

  // Open the event in the summary dialog, it will fail if otherwise.
  let eventWin = await CalendarTestUtils.viewItem(window, eventBox);
  Assert.equal(
    eventWin.document.querySelector("calendar-item-summary .item-title").textContent,
    EVENTTITLE
  );
  Assert.equal(
    eventWin.document.querySelector("calendar-item-summary .item-location").textContent,
    EVENTLOCATION
  );
  Assert.equal(
    eventWin.document.querySelector("calendar-item-summary .item-description").contentDocument.body
      .innerText,
    EVENTDESCRIPTION
  );
  EventUtils.synthesizeKey("VK_ESCAPE", {}, eventWin);

  eventBox.focus();
  EventUtils.synthesizeKey("VK_DELETE", {}, window);
  await dayView.waitForNoEventBoxAt(window, 1);
});

add_task(async function testEventReminderDisplay() {
  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(window, 2020, 1, 1);

  let createBox = dayView.getHourBoxAt(window, 8);

  // Create an event without a reminder.
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, createBox);
  await setData(dialogWindow, iframeWindow, {
    title: EVENTTITLE,
    location: EVENTLOCATION,
    description: EVENTDESCRIPTION,
  });
  await saveAndCloseItemDialog(dialogWindow);

  let eventBox = await dayView.waitForEventBoxAt(window, 1);

  let eventWindow = await CalendarTestUtils.viewItem(window, eventBox);
  let doc = eventWindow.document;
  let row = doc.querySelector(".reminder-row");
  Assert.ok(row.hidden, "reminder dropdown is not displayed");
  EventUtils.synthesizeKey("VK_ESCAPE", {}, eventWindow);

  await CalendarTestUtils.goToDate(window, 2020, 2, 1);
  createBox = dayView.getHourBoxAt(window, 8);

  // Create an event with a reminder.
  ({ dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, createBox));
  await setData(dialogWindow, iframeWindow, {
    title: EVENTTITLE,
    location: EVENTLOCATION,
    description: EVENTDESCRIPTION,
    reminder: "1week",
  });
  await saveAndCloseItemDialog(dialogWindow);

  eventBox = await dayView.waitForEventBoxAt(window, 1);
  eventWindow = await CalendarTestUtils.viewItem(window, eventBox);
  doc = eventWindow.document;
  row = doc.querySelector(".reminder-row");

  Assert.ok(
    row.textContent.includes("7 days before"),
    "the details are shown when a reminder is set"
  );
  EventUtils.synthesizeKey("VK_ESCAPE", {}, eventWindow);

  // Create an invitation.
  let icalString =
    "BEGIN:VCALENDAR\r\n" +
    "PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN\r\n" +
    "VERSION:2.0\r\n" +
    "BEGIN:VEVENT\r\n" +
    "CREATED:20200301T152601Z\r\n" +
    "DTSTAMP:20200301T192729Z\r\n" +
    "UID:x137e\r\n" +
    "SUMMARY:Nap Time\r\n" +
    "ORGANIZER;CN=Papa Bois:mailto:papabois@example.com\r\n" +
    "ATTENDEE;RSVP=TRUE;CN=pillow@example.com;PARTSTAT=NEEDS-ACTION;CUTY\r\n" +
    " PE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;X-NUM-GUESTS=0:mailto:pillow@example.com\r\n" +
    "DTSTART:20200301T153000Z\r\n" +
    "DTEND:20200301T163000Z\r\n" +
    "DESCRIPTION:Slumber In Lumber\r\n" +
    "SEQUENCE:0\r\n" +
    "TRANSP:OPAQUE\r\n" +
    "BEGIN:VALARM\r\n" +
    "TRIGGER:-PT30M\r\n" +
    "REPEAT:2\r\n" +
    "DURATION:PT15M\r\n" +
    "ACTION:DISPLAY\r\n" +
    "END:VALARM\r\n" +
    "END:VEVENT\r\n" +
    "END:VCALENDAR\r\n";

  let calendarEvent = await calendar.addItem(new CalEvent(icalString));
  await CalendarTestUtils.goToDate(window, 2020, 3, 1);
  eventBox = await dayView.waitForEventBoxAt(window, 1);

  eventWindow = await CalendarTestUtils.viewItem(window, eventBox);
  doc = eventWindow.document;
  row = doc.querySelector(".reminder-row");

  Assert.ok(!row.hidden, "reminder row is displayed");
  Assert.ok(row.querySelector("menulist") != null, "reminder dropdown is available");
  EventUtils.synthesizeKey("VK_ESCAPE", {}, eventWindow);

  // Delete directly, as using the UI causes a prompt to appear.
  calendar.deleteItem(calendarEvent);
  await dayView.waitForNoEventBoxAt(window, 1);
});

/**
 * Test that using CTRL+Enter does not result in two events being created.
 * This only happens in the dialog window. See bug 1668478.
 */
add_task(async function testCtrlEnterShortcut() {
  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(window, 2020, 9, 1);

  let createBox = dayView.getHourBoxAt(window, 8);
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, createBox);
  await setData(dialogWindow, iframeWindow, {
    title: EVENTTITLE,
    location: EVENTLOCATION,
    description: EVENTDESCRIPTION,
  });
  EventUtils.synthesizeKey("VK_RETURN", { ctrlKey: true }, dialogWindow);

  await CalendarTestUtils.setCalendarView(window, "month");

  // Give the event boxes enough time to appear before checking for duplicates.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 2000));

  let events = document.querySelectorAll("calendar-month-day-box-item");
  Assert.equal(events.length, 1, "event was created once");

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

  events[0].focus();
  EventUtils.synthesizeKey("VK_DELETE", {}, window);
});

function checkTooltip(row, col, startTime, endTime) {
  let item = monthView.getItemAt(window, row, col, 1);

  let toolTipNode = document.getElementById("itemTooltip");
  toolTipNode.ownerGlobal.onMouseOverItem({ currentTarget: item });

  function getDescription(index) {
    return toolTipNode.querySelector(
      `.tooltipHeaderTable > tr:nth-of-type(${index}) > .tooltipHeaderDescription`
    ).textContent;
  }

  // Check title.
  Assert.equal(getDescription(1), EVENTTITLE);

  // Check date and time.
  let dateTime = getDescription(3);

  let currDate = firstDay.clone();
  currDate.addDuration(cal.createDuration(`P${7 * (row - 1) + (col - 1)}D`));
  let startDate = cal.dtz.formatter.formatDate(currDate);

  Assert.ok(dateTime.includes(`${startDate} ${startTime} – `));

  // This could be on the next day if it is 00:00.
  Assert.ok(dateTime.endsWith(endTime));
}