summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/browser/browser_menulist.js
blob: 39a6a840d2c72aca4dbc7f0b5212b5423b204dd0 (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
/* import-globals-from ../../content/utilityOverlay.js */

add_task(async () => {
  let TEST_DOCUMENT_URL = getRootDirectory(gTestPath) + "files/menulist.xhtml";
  let testDocument = await new Promise(resolve => {
    Services.obs.addObserver(function documentLoaded(subject) {
      if (subject.URL == TEST_DOCUMENT_URL) {
        Services.obs.removeObserver(documentLoaded, "chrome-document-loaded");
        resolve(subject);
      }
    }, "chrome-document-loaded");
    openContentTab(TEST_DOCUMENT_URL);
  });
  ok(testDocument.URL == TEST_DOCUMENT_URL);
  let testWindow = testDocument.ownerGlobal;
  let MENULIST_CLASS = testWindow.customElements.get("menulist");
  let MENULIST_EDITABLE_CLASS =
    testWindow.customElements.get("menulist-editable");

  let menulists = testDocument.querySelectorAll("menulist");
  is(menulists.length, 3);

  // Menulist 0 is an ordinary, non-editable menulist.
  ok(menulists[0] instanceof MENULIST_CLASS);
  ok(!(menulists[0] instanceof MENULIST_EDITABLE_CLASS));
  ok(!("editable" in menulists[0]));

  // Menulist 1 is an editable menulist, but not in editing mode.
  ok(menulists[1] instanceof MENULIST_CLASS);
  ok(menulists[1] instanceof MENULIST_EDITABLE_CLASS);
  ok("editable" in menulists[1]);
  ok(!menulists[1].editable);

  // Menulist 2 is an editable menulist, in editing mode.
  ok(menulists[2] instanceof MENULIST_CLASS);
  ok(menulists[2] instanceof MENULIST_EDITABLE_CLASS);
  ok("editable" in menulists[2]);
  ok(menulists[2].editable);

  // Okay, let's check the focus order.
  let testBrowser = document.getElementById("tabmail").currentTabInfo.browser;
  EventUtils.synthesizeMouseAtCenter(testBrowser, { clickCount: 1 });
  await new Promise(resolve => setTimeout(resolve));

  let beforeButton = testDocument.querySelector("button#before");
  beforeButton.focus();
  is(testDocument.activeElement, beforeButton);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  is(testDocument.activeElement, menulists[0]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  is(testDocument.activeElement, menulists[1]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  is(testDocument.activeElement, menulists[2]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  is(testDocument.activeElement, menulists[2]);
  is(menulists[2].shadowRoot.activeElement, menulists[2]._inputField);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  is(testDocument.activeElement, testDocument.querySelector("button#after"));

  // Now go back again.
  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, testWindow);
  is(testDocument.activeElement, menulists[2]);
  is(menulists[2].shadowRoot.activeElement, menulists[2]._inputField);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, testWindow);
  is(testDocument.activeElement, menulists[2]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, testWindow);
  is(testDocument.activeElement, menulists[1]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, testWindow);
  is(testDocument.activeElement, menulists[0]);

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, testWindow);
  is(testDocument.activeElement, beforeButton, "focus back to the start");

  let popup = menulists[2].menupopup;
  // The dropmarker should open and close the popup.
  let openEvent = BrowserTestUtils.waitForEvent(popup, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    menulists[2]._dropmarker,
    { clickCount: 1 },
    testWindow
  );
  await openEvent;
  ok(menulists[2].hasAttribute("open"), "popup open");

  let hiddenEvent = BrowserTestUtils.waitForEvent(popup, "popuphidden");
  popup.hidePopup();
  await hiddenEvent;
  ok(!menulists[2].hasAttribute("open"), "closed again");

  // Open the popup and choose an item.
  EventUtils.synthesizeMouseAtCenter(
    menulists[2]._dropmarker,
    { clickCount: 1 },
    testWindow
  );
  await BrowserTestUtils.waitForEvent(popup, "popupshown");
  ok(menulists[2].hasAttribute("open"), "open for item click");

  await new Promise(resolve => {
    menulists[2].addEventListener("select", () => setTimeout(resolve), {
      once: true,
    });
    popup.activateItem(menulists[2].querySelectorAll("menuitem")[0]);
  });
  ok(!menulists[2].hasAttribute("open"));
  is(testDocument.activeElement, menulists[2]);
  is(menulists[2].shadowRoot.activeElement, menulists[2]._inputField);
  is(menulists[2]._inputField.value, "foo");
  is(menulists[2].value, "foo");
  is(menulists[2].getAttribute("value"), "foo");

  // Again.
  EventUtils.synthesizeMouseAtCenter(
    menulists[2]._dropmarker,
    { clickCount: 1 },
    testWindow
  );
  await BrowserTestUtils.waitForEvent(popup, "popupshown");
  ok(menulists[2].hasAttribute("open"));

  await new Promise(resolve => {
    menulists[2].addEventListener("select", () => setTimeout(resolve), {
      once: true,
    });
    popup.activateItem(menulists[2].querySelectorAll("menuitem")[1]);
  });
  ok(!menulists[2].hasAttribute("open"));
  is(testDocument.activeElement, menulists[2]);
  is(menulists[2].shadowRoot.activeElement, menulists[2]._inputField);
  is(menulists[2]._inputField.value, "bar");
  is(menulists[2].value, "bar");
  is(menulists[2].getAttribute("value"), "bar");

  // Type in a value.
  is(menulists[2]._inputField.selectionStart, 0);
  is(menulists[2]._inputField.selectionEnd, 3);
  EventUtils.sendString("quux", testWindow);
  await new Promise(resolve => {
    menulists[2].addEventListener(
      "change",
      event => {
        is(event.target, menulists[2]);
        resolve();
      },
      { once: true }
    );
    EventUtils.synthesizeKey("VK_TAB", { shiftKey: false }, testWindow);
  });
  is(menulists[2].value, "quux");
  is(menulists[2].getAttribute("value"), "quux");

  // Open the popup and choose an item.
  EventUtils.synthesizeMouseAtCenter(
    menulists[2]._dropmarker,
    { clickCount: 1 },
    testWindow
  );
  await BrowserTestUtils.waitForEvent(popup, "popupshown");
  ok(menulists[2].hasAttribute("open"));

  await new Promise(resolve => {
    menulists[2].addEventListener("select", () => setTimeout(resolve), {
      once: true,
    });
    popup.activateItem(menulists[2].querySelectorAll("menuitem")[0]);
  });
  ok(!menulists[2].hasAttribute("open"));
  is(testDocument.activeElement, menulists[2]);
  is(menulists[2].shadowRoot.activeElement, menulists[2]._inputField);
  is(menulists[2]._inputField.value, "foo");
  is(menulists[2].value, "foo");
  is(menulists[2].getAttribute("value"), "foo");

  document.getElementById("tabmail").closeOtherTabs(0);
});