summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/browser/eventDialog/head.js
blob: 0646cd709c9a856e00c819ea2659b0586d0e4fc0 (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
/* 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 { CalendarTestUtils } = ChromeUtils.import(
  "resource://testing-common/calendar/CalendarTestUtils.jsm"
);

// If the "do you want to save the event?" prompt appears, the test failed.
// Listen for all windows opening, and if one is the save prompt, fail.
var savePromptObserver = {
  async observe(win, topic) {
    if (topic == "domwindowopened") {
      await BrowserTestUtils.waitForEvent(win, "load");
      // Make sure this is a prompt window.
      if (win.location.href == "chrome://global/content/commonDialog.xhtml") {
        let doc = win.document;
        // Adding attachments also shows a prompt, but we can tell which one
        // this is by checking whether the textbox is visible.
        if (doc.querySelector("#loginContainer").hasAttribute("hidden")) {
          Assert.report(true, undefined, undefined, "Unexpected save prompt appeared");
          doc.querySelector("dialog").getButton("cancel").click();
        }
      }
    }
  },
};
Services.ww.registerNotification(savePromptObserver);

const calendarViewsInitialState = CalendarTestUtils.saveCalendarViewsState(window);

registerCleanupFunction(async () => {
  Services.ww.unregisterNotification(savePromptObserver);
  await CalendarTestUtils.restoreCalendarViewsState(window, calendarViewsInitialState);
});

function openAttendeesWindow(eventWindowOrArgs) {
  let attendeesWindowPromise = BrowserTestUtils.promiseAlertDialogOpen(
    null,
    "chrome://calendar/content/calendar-event-dialog-attendees.xhtml",
    {
      async callback(win) {
        await new Promise(resolve => win.setTimeout(resolve));
      },
    }
  );

  if (Window.isInstance(eventWindowOrArgs)) {
    EventUtils.synthesizeMouseAtCenter(
      eventWindowOrArgs.document.getElementById("button-attendees"),
      {},
      eventWindowOrArgs
    );
  } else {
    openDialog(
      "chrome://calendar/content/calendar-event-dialog-attendees.xhtml",
      "_blank",
      "chrome,titlebar,resizable",
      eventWindowOrArgs
    );
  }
  return attendeesWindowPromise;
}

async function closeAttendeesWindow(attendeesWindow, buttonAction = "accept") {
  let closedPromise = BrowserTestUtils.domWindowClosed(attendeesWindow);
  let dialog = attendeesWindow.document.querySelector("dialog");
  dialog.getButton(buttonAction).click();
  await closedPromise;

  await new Promise(resolve => setTimeout(resolve));
}

function findAndFocusMatchingRow(attendeesWindow, message, matchFunction) {
  // Get the attendee row for which the input matches.
  const attendeeList = attendeesWindow.document.getElementById("attendee-list");
  const attendeeInput = Array.from(attendeeList.children)
    .map(child => child.querySelector("input"))
    .find(input => {
      return input ? matchFunction(input.value) : false;
    });
  Assert.ok(attendeeInput, message);

  attendeeInput.focus();

  return attendeeInput;
}

function findAndEditMatchingRow(attendeesWindow, newValue, message, matchFunction) {
  // Get the attendee row we wish to edit.
  const attendeeInput = findAndFocusMatchingRow(attendeesWindow, message, matchFunction);

  // Set the new value of the row. We set the input value directly due to issues
  // experienced trying to use simulated keystrokes.
  attendeeInput.value = newValue;
  EventUtils.synthesizeKey("VK_RETURN", {}, attendeesWindow);
}