summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/browser/recurrence/browser_weeklyN.js
blob: e32ab470f1e4d93db8901253675f67d03de578aa (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
/* 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 { handleDeleteOccurrencePrompt } = ChromeUtils.import(
  "resource://testing-common/calendar/CalendarUtils.jsm"
);

var { menulistSelect, saveAndCloseItemDialog, setData } = ChromeUtils.import(
  "resource://testing-common/calendar/ItemEditingHelpers.jsm"
);

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

var { dayView, weekView, multiweekView, monthView } = CalendarTestUtils;

const HOUR = 8;

/*
 * This test is intended to verify that events recurring on a weekly basis are
 * correctly created and displayed. The event should recur on multiple days in
 * the week, skip days, and be limited to a certain number of recurrences in
 * order to verify that these parameters are respected. Deletion should delete
 * all event occurrences when appropriate.
 */
add_task(async function testWeeklyNRecurrence() {
  async function setRecurrence(recurrenceWindow) {
    let recurrenceDocument = recurrenceWindow.document;

    // Select weekly recurrence
    await menulistSelect(recurrenceDocument.getElementById("period-list"), "1");

    let monLabel = cal.l10n.getDateFmtString("day.2.Mmm");
    let tueLabel = cal.l10n.getDateFmtString("day.3.Mmm");
    let wedLabel = cal.l10n.getDateFmtString("day.4.Mmm");
    let friLabel = cal.l10n.getDateFmtString("day.6.Mmm");
    let satLabel = cal.l10n.getDateFmtString("day.7.Mmm");

    let dayPicker = recurrenceDocument.getElementById("daypicker-weekday");

    // Selected date is a Monday, so it should already be selected
    Assert.ok(
      dayPicker.querySelector(`[label="${monLabel}"]`).checked,
      "Monday should already be selected"
    );

    // Select Tuesday, Wednesday, Friday, and Saturday as additional days for
    // event occurrences
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${tueLabel}"]`),
      {},
      recurrenceWindow
    );
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${wedLabel}"]`),
      {},
      recurrenceWindow
    );
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${friLabel}"]`),
      {},
      recurrenceWindow
    );
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${satLabel}"]`),
      {},
      recurrenceWindow
    );

    // Create a total of four events
    EventUtils.synthesizeMouseAtCenter(
      recurrenceDocument.getElementById("recurrence-range-for"),
      {},
      recurrenceWindow
    );
    recurrenceDocument.getElementById("repeat-ntimes-count").value = "4";

    let button = recurrenceDocument.querySelector("dialog").getButton("accept");
    button.scrollIntoView();
    // Close dialog
    EventUtils.synthesizeMouseAtCenter(button, {}, recurrenceWindow);
  }

  let calendar = CalendarTestUtils.createCalendar();
  registerCleanupFunction(() => {
    CalendarTestUtils.removeCalendar(calendar);
  });

  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(window, 2009, 1, 5);

  // Create event recurring on a weekly basis
  let eventBox = dayView.getHourBoxAt(window, HOUR);
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, eventBox);
  await setData(dialogWindow, iframeWindow, { title: "Event", repeat: setRecurrence });
  await saveAndCloseItemDialog(dialogWindow);

  // Verify in the day view that events were created for Monday through Wednesday
  for (let i = 0; i < 3; i++) {
    await dayView.waitForEventBoxAt(window, 1);
    await CalendarTestUtils.calendarViewForward(window, 1);
  }

  // No event should have been created on Thursday because it was not selected
  await dayView.waitForNoEventBoxAt(window, 1);
  await CalendarTestUtils.calendarViewForward(window, 1);

  // An event should have been created for Friday because it was selected
  await dayView.waitForEventBoxAt(window, 1);
  await CalendarTestUtils.calendarViewForward(window, 1);

  // No event should have been created on Saturday due to four event limit
  await dayView.waitForNoEventBoxAt(window, 1);

  // Validate event creation and lack of Saturday event in week view
  await CalendarTestUtils.setCalendarView(window, "week");

  for (let i = 2; i < 5; i++) {
    await weekView.waitForEventBoxAt(window, i, 1);
  }

  // No event Thursday or Saturday, event on Friday
  await weekView.waitForNoEventBoxAt(window, 5, 1);
  await weekView.waitForEventBoxAt(window, 6, 1);
  await weekView.waitForNoEventBoxAt(window, 7, 1);

  // Validate event creation and lack of Saturday event in multiweek view
  await CalendarTestUtils.setCalendarView(window, "multiweek");

  for (let i = 2; i < 5; i++) {
    await multiweekView.waitForItemAt(window, 1, i, 1);
  }

  // No event Thursday or Saturday, event on Friday
  await multiweekView.waitForNoItemAt(window, 1, 5, 1);
  await multiweekView.waitForItemAt(window, 1, 6, 1);
  await multiweekView.waitForNoItemAt(window, 1, 7, 1);

  // Validate event creation and lack of Saturday event in month view
  await CalendarTestUtils.setCalendarView(window, "month");

  for (let i = 2; i < 5; i++) {
    // This should be the second week in the month
    await monthView.waitForItemAt(window, 2, i, 1);
  }

  // No event Thursday or Saturday, event on Friday
  await monthView.waitForNoItemAt(window, 2, 5, 1);
  await monthView.waitForItemAt(window, 2, 6, 1);
  await monthView.waitForNoItemAt(window, 2, 7, 1);

  // Delete event
  let box = await monthView.waitForItemAt(window, 2, 2, 1);
  EventUtils.synthesizeMouseAtCenter(box, {}, window);
  await handleDeleteOccurrencePrompt(window, box, true);

  // All occurrences should have been deleted
  for (let i = 2; i < 5; i++) {
    await monthView.waitForNoItemAt(window, 2, i, 1);
  }

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

/*
 * This test is intended to catch instances in which we aren't correctly setting
 * the week start value of recurrences. For example, if the user has set their
 * week to start on Saturday, then creates a recurring event running every other
 * Saturday, Sunday, and Monday, they expect to see events on the initial
 * Saturday, Sunday, Monday, skip a week, repeat. However, week start defaults
 * to Monday, so if it is not correctly set, they would see events on the
 * initial Saturday and Sunday, nothing on Monday, but an event on the following
 * Monday.
 */
add_task(async function testRecurrenceAcrossWeekStart() {
  // Sanity check that we're not testing against a default value
  const initialWeekStart = Services.prefs.getIntPref("calendar.week.start", 0);
  Assert.notEqual(initialWeekStart, 6, "week start should not be Saturday");

  // Set week start to Saturday
  Services.prefs.setIntPref("calendar.week.start", 6);
  registerCleanupFunction(() => {
    Services.prefs.setIntPref("calendar.week.start", initialWeekStart);
  });

  async function setRecurrence(recurrenceWindow) {
    let recurrenceDocument = recurrenceWindow.document;

    // Select weekly recurrence
    await menulistSelect(recurrenceDocument.getElementById("period-list"), "1");

    // Recur every two weeks
    recurrenceDocument.getElementById("weekly-weeks").value = "2";

    let satLabel = cal.l10n.getDateFmtString("day.7.Mmm");
    let sunLabel = cal.l10n.getDateFmtString("day.1.Mmm");
    let monLabel = cal.l10n.getDateFmtString("day.2.Mmm");

    let dayPicker = recurrenceDocument.getElementById("daypicker-weekday");

    // Selected date is a Saturday, so it should already be selected
    Assert.ok(
      dayPicker.querySelector(`[label="${satLabel}"]`).checked,
      "Saturday should already be checked"
    );

    // Select Sunday and Monday as additional days for event occurrences
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${sunLabel}"]`),
      {},
      recurrenceWindow
    );
    EventUtils.synthesizeMouseAtCenter(
      dayPicker.querySelector(`[label="${monLabel}"]`),
      {},
      recurrenceWindow
    );

    // Create a total of six events
    EventUtils.synthesizeMouseAtCenter(
      recurrenceDocument.getElementById("recurrence-range-for"),
      {},
      recurrenceWindow
    );
    recurrenceDocument.getElementById("repeat-ntimes-count").value = "6";

    let button = recurrenceDocument.querySelector("dialog").getButton("accept");
    button.scrollIntoView();
    // Close dialog
    EventUtils.synthesizeMouseAtCenter(button, {}, recurrenceWindow);
  }

  let calendar = CalendarTestUtils.createCalendar();
  registerCleanupFunction(() => {
    CalendarTestUtils.removeCalendar(calendar);
  });

  await CalendarTestUtils.setCalendarView(window, "day");
  await CalendarTestUtils.goToDate(window, 2022, 10, 15);

  // Create event recurring every other week
  let eventBox = dayView.getHourBoxAt(window, HOUR);
  let { dialogWindow, iframeWindow } = await CalendarTestUtils.editNewEvent(window, eventBox);
  await setData(dialogWindow, iframeWindow, { title: "Event", repeat: setRecurrence });
  await saveAndCloseItemDialog(dialogWindow);

  // Open week view
  await CalendarTestUtils.setCalendarView(window, "week");

  // Verify events created on Saturday, Sunday, Monday of first week
  for (let i = 1; i < 4; i++) {
    await weekView.waitForEventBoxAt(window, i, 1);
  }

  // Verify no events created on Saturday, Sunday, Monday of second week
  await CalendarTestUtils.calendarViewForward(window, 1);

  for (let i = 1; i < 4; i++) {
    await weekView.waitForNoEventBoxAt(window, i, 1);
  }

  // Verify events created on Saturday, Sunday, Monday of third week
  await CalendarTestUtils.calendarViewForward(window, 1);

  for (let i = 1; i < 4; i++) {
    await weekView.waitForEventBoxAt(window, i, 1);
  }
});