summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/browser/browser_eventDisplay_dayView.js
blob: 5f0941cac4ee9809cff99ff8a5606f0770b1c3f8 (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
/* 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/. */

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

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

/**
 * Create an event item in the calendar.
 *
 * @param {string} name - The name of the event.
 * @param {string} start - The date time string for the start of the event.
 * @param {string} end - The date time string for the end of the event.
 *
 * @returns {CalEvent} - The created event.
 */
async function createEvent(name, start, end) {
  let event = new CalEvent();
  event.title = name;
  event.startDate = cal.createDateTime(start);
  event.endDate = cal.createDateTime(end);
  return calendar.addItem(event);
}

/**
 * Assert that there is an event shown on the given date in the day-view.
 *
 * @param {object} date - The date to move to.
 * @param {number} date.day - The day.
 * @param {number} date.week - The week.
 * @param {number} date.year - The year.
 * @param {object} expect - Details about the expected event.
 * @param {string} expect.name - The event name.
 * @param {boolean} expect.startInView - Whether the event starts within the
 *   view on the given date.
 * @param {boolean} expect.endInView - Whether the event ends within the view
 *   on the given date.
 * @param {string} message - A message to use in assertions.
 */
async function assertDayEvent(date, expect, message) {
  await CalendarTestUtils.goToDate(window, date.year, date.month, date.day);
  let element = await CalendarTestUtils.dayView.waitForEventBoxAt(window, 1);
  Assert.equal(
    element.querySelector(".event-name-label").textContent,
    expect.name,
    `Event name should match: ${message}`
  );
  await CalendarTestUtils.assertEventBoxDraggable(
    element,
    expect.startInView,
    expect.endInView,
    message
  );
}

/**
 * Test an event that occurs within one day, in the day view.
 */
add_task(async function testInsideDayView() {
  let event = await createEvent("Test Event", "20190403T123400", "20190403T234500");
  await CalendarTestUtils.setCalendarView(window, "day");
  Assert.equal(
    document.querySelectorAll("#day-view calendar-event-column").length,
    1,
    "1 day column in the day view"
  );

  // This event is fully within this view.
  await assertDayEvent(
    { day: 3, month: 4, year: 2019 },
    { name: "Test Event", startInView: true, endInView: true },
    "Single day event"
  );

  await CalendarTestUtils.closeCalendarTab(window);
  await calendar.deleteItem(event);
});

/**
 * Test an event that starts and ends at midnight, in the day view.
 */
add_task(async function testMidnightDayView() {
  let event = await createEvent("Test Event", "20190403T000000", "20190404T000000");
  await CalendarTestUtils.setCalendarView(window, "day");

  // This event is fully within this view.
  await assertDayEvent(
    { day: 3, month: 4, year: 2019 },
    { name: "Test Event", startInView: true, endInView: true },
    "Single midnight event"
  );

  await CalendarTestUtils.closeCalendarTab(window);
  await calendar.deleteItem(event);
});

/**
 * Test an event that spans multiple days, in the day view.
 */
add_task(async function testOutsideDayView() {
  let event = await createEvent("Test Event", "20190402T123400", "20190404T234500");
  await CalendarTestUtils.setCalendarView(window, "day");

  // Go to the start of the event. The end of the event is beyond the current view.
  await assertDayEvent(
    { day: 2, month: 4, year: 2019 },
    { name: "Test Event", startInView: true, endInView: false },
    "First day"
  );

  // Go to the middle of the event. Both ends of the event are beyond the current view.
  await assertDayEvent(
    { day: 3, month: 4, year: 2019 },
    { name: "Test Event", startInView: false, endInView: false },
    "Middle day"
  );

  // Go to the end of the event. The start of the event is beyond the current view.
  await assertDayEvent(
    { day: 4, month: 4, year: 2019 },
    { name: "Test Event", startInView: false, endInView: true },
    "Last day"
  );

  await CalendarTestUtils.closeCalendarTab(window);
  await calendar.deleteItem(event);
});