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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/* 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/. */
/**
* Test dragging of events in the various calendar views.
*/
const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
XPCOMUtils.defineLazyModuleGetters(this, {
CalEvent: "resource:///modules/CalEvent.jsm",
});
const calendar = CalendarTestUtils.createCalendar("Drag Test", "memory");
// Set a low number of hours to reduce pixel -> minute rounding errors.
Services.prefs.setIntPref("calendar.view.visiblehours", 3);
registerCleanupFunction(() => {
CalendarTestUtils.removeCalendar(calendar);
Services.prefs.clearUserPref("calendar.view.visiblehours");
// Reset the spaces toolbar to its default visible state.
window.gSpacesToolbar.toggleToolbar(false);
});
/**
* Ensures that the window is maximised after switching dates.
*
* @param {calIDateTime} date - A date to navigate the view to.
*/
async function resetView(date, view) {
window.goToDate(date);
if (window.windowState != window.STATE_MAXIMIZED) {
// The multi-day views adjust scrolling dynamically when they detect a
// resize. Hook into the resize event and scroll after the adjustment.
let resizePromise = BrowserTestUtils.waitForEvent(window, "resize");
window.maximize();
await resizePromise;
}
}
/**
* End the dragging of the event at the specified location.
*
* @param {number} day - The day to drop into.
* @param {number} hour - The starting hour to drop to.
* @param {number} topOffset - An offset to apply to the mouse position.
*/
function endDrag(day, hour, topOffset) {
let view = window.currentView();
let hourElement;
if (view.id == "day-view") {
hourElement = CalendarTestUtils.dayView.getHourBoxAt(window, hour);
} else {
hourElement = CalendarTestUtils.weekView.getHourBoxAt(window, day, hour);
}
// We scroll to align the *end* of the hour element so we can avoid triggering
// the auto-scroll when we synthesize mousemove below.
// FIXME: Use and test auto scroll by holding mouseover at the view edges.
CalendarTestUtils.scrollViewToTarget(hourElement, false);
let hourRect = hourElement.getBoundingClientRect();
// We drop the event with some offset from the starting edge of the desired
// hourElement.
// NOTE: This may mean that the drop point may not be above the hourElement.
// NOTE: We assume that the drop point is however still above the view.
// Currently event "move" events get cancelled if the pointer leaves the view.
let top = Math.round(hourRect.top + topOffset);
let left = Math.round(hourRect.left + hourRect.width / 2);
EventUtils.synthesizeMouseAtPoint(left, top, { type: "mousemove", shiftKey: true }, window);
EventUtils.synthesizeMouseAtPoint(left, top, { type: "mouseup", shiftKey: true }, window);
}
/**
* Simulates the dragging of an event box in a multi-day view to another
* column, horizontally.
*
* @param {MozCalendarEventBox} eventBox - The event to start moving.
* @param {number} day - The day to drop into.
* @param {number} hour - The starting hour to drop to.
*/
function simulateDragToColumn(eventBox, day, hour) {
// Scroll to align to the top of the view.
CalendarTestUtils.scrollViewToTarget(eventBox, true);
let sourceRect = eventBox.getBoundingClientRect();
// Start dragging from the center of the event box to avoid the gripbars.
// NOTE: We assume that the eventBox's center is in view.
let leftOffset = sourceRect.width / 2;
// We round the mouse position to try and reduce rounding errors when
// scrolling the view.
let sourceTop = Math.round(sourceRect.top + sourceRect.height / 2);
let sourceLeft = sourceRect.left + leftOffset;
// Keep track of the exact offset.
let topOffset = sourceTop - sourceRect.top;
EventUtils.synthesizeMouseAtPoint(
sourceLeft,
sourceTop,
// Hold shift to avoid snapping.
{ type: "mousedown", shiftKey: true },
window
);
EventUtils.synthesizeMouseAtPoint(
// We assume the location of the mouseout event does not matter, just as
// long as the event box receives it.
sourceLeft,
sourceTop,
{ type: "mouseout", shiftKey: true },
window
);
// End drag with the same offset from the starting edge.
endDrag(day, hour, topOffset);
}
/**
* Simulates the dragging of an event box via one of the gripbars.
*
* @param {MozCalendarEventBox} eventBox - The event to resize.
* @param {"start"|"end"} - The side to grab.
* @param {number} day - The day to move into.
* @param {number} hour - The hour to move to.
*/
function simulateGripbarDrag(eventBox, side, day, hour) {
// Scroll the edge of the box into view.
CalendarTestUtils.scrollViewToTarget(eventBox, side == "start");
let gripbar = side == "start" ? eventBox.startGripbar : eventBox.endGripbar;
let sourceRect = gripbar.getBoundingClientRect();
let sourceTop = sourceRect.top + sourceRect.height / 2;
let sourceLeft = sourceRect.left + sourceRect.width / 2;
// Hover to make the gripbar visible.
EventUtils.synthesizeMouseAtPoint(sourceLeft, sourceTop, { type: "mouseover" }, window);
EventUtils.synthesizeMouseAtPoint(
sourceLeft,
sourceTop,
// Hold shift to avoid snapping.
{ type: "mousedown", shiftKey: true },
window
);
// End the drag at the start of the hour.
endDrag(day, hour, 0);
}
/**
* Tests dragging an event item updates the event in the month view.
*/
add_task(async function testMonthViewDragEventItem() {
let event = new CalEvent();
event.id = "1";
event.title = "Month View Event";
event.startDate = cal.createDateTime("20210316T000000Z");
event.endDate = cal.createDateTime("20210316T110000Z");
await CalendarTestUtils.setCalendarView(window, "month");
await calendar.addItem(event);
await resetView(event.startDate);
// Hide the spaces toolbar since it interferes with the calendar
window.gSpacesToolbar.toggleToolbar(true);
let eventItem = await CalendarTestUtils.monthView.waitForItemAt(window, 3, 3, 1);
let dayBox = await CalendarTestUtils.monthView.getDayBox(window, 3, 2);
let dragSession = Cc["@mozilla.org/widget/dragservice;1"].getService(Ci.nsIDragService);
dragSession.startDragSessionForTests(Ci.nsIDragService.DRAGDROP_ACTION_MOVE);
let [result, dataTransfer] = EventUtils.synthesizeDragOver(
eventItem,
dayBox,
undefined,
undefined,
eventItem.ownerGlobal,
dayBox.ownerGlobal
);
EventUtils.synthesizeDropAfterDragOver(result, dataTransfer, dayBox);
dragSession.endDragSession(true);
Assert.ok(
!CalendarTestUtils.monthView.getItemAt(window, 3, 3, 1),
"item removed from initial date"
);
eventItem = await CalendarTestUtils.monthView.waitForItemAt(window, 3, 2, 1);
Assert.ok(eventItem, "item moved to new date");
let { id, title, startDate, endDate } = eventItem.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210315T000000Z", "startDate is correct");
Assert.equal(endDate.icalString, "20210315T110000Z", "endDate is correct");
await calendar.deleteItem(eventItem.occurrence);
});
/**
* Tests dragging an event item updates the event in the multiweek view.
*/
add_task(async function testMultiWeekViewDragEventItem() {
let event = new CalEvent();
event.id = "2";
event.title = "Multiweek View Event";
event.startDate = cal.createDateTime("20210316T000000Z");
event.endDate = cal.createDateTime("20210316T110000Z");
await CalendarTestUtils.setCalendarView(window, "multiweek");
await calendar.addItem(event);
await resetView(event.startDate);
let eventItem = await CalendarTestUtils.multiweekView.waitForItemAt(window, 1, 3, 1);
let dayBox = await CalendarTestUtils.multiweekView.getDayBox(window, 1, 2);
let dragSession = Cc["@mozilla.org/widget/dragservice;1"].getService(Ci.nsIDragService);
dragSession.startDragSessionForTests(Ci.nsIDragService.DRAGDROP_ACTION_MOVE);
let [result, dataTransfer] = EventUtils.synthesizeDragOver(
eventItem,
dayBox,
undefined,
undefined,
eventItem.ownerGlobal,
dayBox.ownerGlobal
);
EventUtils.synthesizeDropAfterDragOver(result, dataTransfer, dayBox);
dragSession.endDragSession(true);
Assert.ok(
!CalendarTestUtils.multiweekView.getItemAt(window, 1, 3, 1),
"item removed from initial date"
);
eventItem = await CalendarTestUtils.multiweekView.waitForItemAt(window, 1, 2, 1);
Assert.ok(eventItem, "item moved to new date");
let { id, title, startDate, endDate } = eventItem.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210315T000000Z", "startDate is correct");
Assert.equal(endDate.icalString, "20210315T110000Z", "endDate is correct");
await calendar.deleteItem(eventItem.occurrence);
});
/**
* Tests dragging an event box to the previous day updates the event in the
* week view.
*/
add_task(async function testWeekViewDragEventBoxToPreviousDay() {
let event = new CalEvent();
event.id = "3";
event.title = "Week View Previous Day";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "week");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
simulateDragToColumn(eventBox, 2, 2);
eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 2, 1);
await TestUtils.waitForCondition(
() => !CalendarTestUtils.weekView.getEventBoxAt(window, 3, 1),
"Old position is empty"
);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210315T020000Z", "startDate is correct");
Assert.equal(endDate.icalString, "20210315T030000Z", "endDate is correct");
await calendar.deleteItem(eventBox.occurrence);
});
/**
* Tests dragging an event box to the following day updates the event in the
* week view.
*/
add_task(async function testWeekViewDragEventBoxToFollowingDay() {
let event = new CalEvent();
event.id = "4";
event.title = "Week View Following Day";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "week");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
simulateDragToColumn(eventBox, 4, 2);
eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 4, 1);
await TestUtils.waitForCondition(
() => !CalendarTestUtils.weekView.getEventBoxAt(window, 3, 1),
"Old position is empty"
);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210317T020000Z", "startDate is correct");
Assert.equal(endDate.icalString, "20210317T030000Z", "endDate is correct");
await calendar.deleteItem(eventBox.occurrence);
});
/**
* Tests dragging the top of an event box updates the start time in the week
* view.
*/
add_task(async function testWeekViewDragEventBoxStartTime() {
let event = new CalEvent();
event.id = "5";
event.title = "Week View Start";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "week");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
simulateGripbarDrag(eventBox, "start", 3, 1);
eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210316T010000Z", "startDate was changed");
Assert.equal(endDate.icalString, "20210316T030000Z", "endDate did not change");
await calendar.deleteItem(eventBox.occurrence);
});
/**
* Tests dragging the end of an event box changes the time in the week view.
*/
add_task(async function testWeekViewDragEventBoxEndTime() {
let event = new CalEvent();
event.id = "6";
event.title = "Week View End";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "week");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
simulateGripbarDrag(eventBox, "end", 3, 6);
eventBox = await CalendarTestUtils.weekView.waitForEventBoxAt(window, 3, 1);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210316T020000Z", "startDate did not change");
Assert.equal(endDate.icalString, "20210316T060000Z", "endDate was changed");
await calendar.deleteItem(eventBox.occurrence);
});
/**
* Tests dragging the top of an event box changes the start time in the day view.
*/
add_task(async function testDayViewDragEventBoxStartTime() {
let event = new CalEvent();
event.id = "7";
event.title = "Day View Start";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "day");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.dayView.waitForEventBoxAt(window, 1);
simulateGripbarDrag(eventBox, "start", 1, 1);
eventBox = await CalendarTestUtils.dayView.waitForEventBoxAt(window, 1);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210316T010000Z", "startDate was changed");
Assert.equal(endDate.icalString, "20210316T030000Z", "endDate did not change");
await calendar.deleteItem(eventBox.occurrence);
});
/**
* Tests dragging the bottom of an event box changes the end time in the day
* view.
*/
add_task(async function testDayViewDragEventBoxEndTime() {
let event = new CalEvent();
event.id = "8";
event.title = "Day View End";
event.startDate = cal.createDateTime("20210316T020000Z");
event.endDate = cal.createDateTime("20210316T030000Z");
await CalendarTestUtils.setCalendarView(window, "day");
await calendar.addItem(event);
await resetView(event.startDate);
let eventBox = await CalendarTestUtils.dayView.waitForEventBoxAt(window, 1);
simulateGripbarDrag(eventBox, "end", 1, 4);
eventBox = await CalendarTestUtils.dayView.waitForEventBoxAt(window, 1);
let { id, title, startDate, endDate } = eventBox.occurrence;
Assert.equal(id, event.id, "id is correct");
Assert.equal(title, event.title, "title is correct");
Assert.equal(startDate.icalString, "20210316T020000Z", "startDate did not change");
Assert.equal(endDate.icalString, "20210316T040000Z", "endDate was changed");
await calendar.deleteItem(eventBox.occurrence);
});
|