summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/browser/browser_BrowserTestUtils.js
blob: 8be0b5f3489aea9d8de203534ad92a9dde5d93c2 (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
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
function getLastEventDetails(browser) {
  return SpecialPowers.spawn(browser, [], async function() {
    return content.document.getElementById("out").textContent;
  });
}

add_task(async function() {
  let onClickEvt =
    'document.getElementById("out").textContent = event.target.localName + "," + event.clientX + "," + event.clientY;';
  const url =
    "<body onclick='" +
    onClickEvt +
    "' style='margin: 0'>" +
    "<button id='one' style='margin: 0; margin-left: 16px; margin-top: 14px; width: 80px; height: 40px;'>Test</button>" +
    "<div onmousedown='event.preventDefault()' style='margin: 0; width: 80px; height: 60px;'>Other</div>" +
    "<span id='out'></span></body>";
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html," + url
  );

  let browser = tab.linkedBrowser;
  await BrowserTestUtils.synthesizeMouseAtCenter("#one", {}, browser);
  let details = await getLastEventDetails(browser);

  is(details, "button,56,34", "synthesizeMouseAtCenter");

  await BrowserTestUtils.synthesizeMouse("#one", 4, 9, {}, browser);
  details = await getLastEventDetails(browser);
  is(details, "button,20,23", "synthesizeMouse");

  await BrowserTestUtils.synthesizeMouseAtPoint(15, 6, {}, browser);
  details = await getLastEventDetails(browser);
  is(details, "body,15,6", "synthesizeMouseAtPoint on body");

  await BrowserTestUtils.synthesizeMouseAtPoint(
    20,
    22,
    {},
    browser.browsingContext
  );
  details = await getLastEventDetails(browser);
  is(details, "button,20,22", "synthesizeMouseAtPoint on button");

  await BrowserTestUtils.synthesizeMouseAtCenter("body > div", {}, browser);
  details = await getLastEventDetails(browser);
  is(details, "div,40,84", "synthesizeMouseAtCenter with complex selector");

  let cancelled = await BrowserTestUtils.synthesizeMouseAtCenter(
    "body > div",
    { type: "mousedown" },
    browser
  );
  details = await getLastEventDetails(browser);
  is(
    details,
    "div,40,84",
    "synthesizeMouseAtCenter mousedown with complex selector"
  );
  ok(
    cancelled,
    "synthesizeMouseAtCenter mousedown with complex selector not cancelled"
  );

  cancelled = await BrowserTestUtils.synthesizeMouseAtCenter(
    "body > div",
    { type: "mouseup" },
    browser
  );
  details = await getLastEventDetails(browser);
  is(
    details,
    "div,40,84",
    "synthesizeMouseAtCenter mouseup with complex selector"
  );
  ok(
    !cancelled,
    "synthesizeMouseAtCenter mouseup with complex selector cancelled"
  );

  gBrowser.removeTab(tab);
});

add_task(async function testSynthesizeMouseAtPointsButtons() {
  let onMouseEvt =
    'document.getElementById("mouselog").textContent += "/" + [event.type,event.clientX,event.clientY,event.button,event.buttons].join(",");';

  let getLastMouseEventDetails = browser => {
    return SpecialPowers.spawn(browser, [], async () => {
      let log = content.document.getElementById("mouselog").textContent;
      content.document.getElementById("mouselog").textContent = "";
      return log;
    });
  };

  const url =
    "<body" +
    "' onmousedown='" +
    onMouseEvt +
    "' onmousemove='" +
    onMouseEvt +
    "' onmouseup='" +
    onMouseEvt +
    "' style='margin: 0'>" +
    "<div style='margin: 0; width: 80px; height: 60px;'>Mouse area</div>" +
    "<span id='mouselog'></span>" +
    "</body>";
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html," + url
  );

  let browser = tab.linkedBrowser;
  let details;

  await BrowserTestUtils.synthesizeMouseAtPoint(
    21,
    22,
    {
      type: "mousemove",
    },
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(details, "/mousemove,21,22,0,0", "synthesizeMouseAtPoint mousemove");

  await BrowserTestUtils.synthesizeMouseAtPoint(
    22,
    23,
    {},
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(
    details,
    "/mousedown,22,23,0,1/mouseup,22,23,0,0",
    "synthesizeMouseAtPoint default action includes buttons on mousedown only"
  );

  await BrowserTestUtils.synthesizeMouseAtPoint(
    20,
    22,
    {
      type: "mousedown",
    },
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(
    details,
    "/mousedown,20,22,0,1",
    "synthesizeMouseAtPoint mousedown includes buttons"
  );

  await BrowserTestUtils.synthesizeMouseAtPoint(
    21,
    20,
    {
      type: "mouseup",
    },
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(details, "/mouseup,21,20,0,0", "synthesizeMouseAtPoint mouseup");

  await BrowserTestUtils.synthesizeMouseAtPoint(
    20,
    22,
    {
      type: "mousedown",
      button: 2,
    },
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(
    details,
    "/mousedown,20,22,2,2",

    "synthesizeMouseAtPoint mousedown respects specified button 2"
  );

  await BrowserTestUtils.synthesizeMouseAtPoint(
    21,
    20,
    {
      type: "mouseup",
      button: 2,
    },
    browser.browsingContext
  );
  details = await getLastMouseEventDetails(browser);
  is(
    details,
    "/mouseup,21,20,2,0",
    "synthesizeMouseAtPoint mouseup with button 2"
  );

  gBrowser.removeTab(tab);
});

add_task(async function mouse_in_iframe() {
  let onClickEvt = "document.body.lastChild.textContent = event.target.id;";
  const url = `<iframe style='margin: 30px;' src='data:text/html,<body onclick="${onClickEvt}">
     <p><button>One</button></p><p><button id="two">Two</button></p><p id="out"></p></body>'></iframe>
     <iframe style='margin: 10px;' src='data:text/html,<body onclick="${onClickEvt}">
     <p><button>Three</button></p><p><button id="four">Four</button></p><p id="out"></p></body>'></iframe>`;
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "data:text/html," + url
  );

  let browser = tab.linkedBrowser;

  await BrowserTestUtils.synthesizeMouse(
    "#two",
    5,
    10,
    {},
    browser.browsingContext.children[0]
  );

  let details = await getLastEventDetails(browser.browsingContext.children[0]);
  is(details, "two", "synthesizeMouse");

  await BrowserTestUtils.synthesizeMouse(
    "#four",
    5,
    10,
    {},
    browser.browsingContext.children[1]
  );
  details = await getLastEventDetails(browser.browsingContext.children[1]);
  is(details, "four", "synthesizeMouse");

  gBrowser.removeTab(tab);
});

add_task(async function() {
  await BrowserTestUtils.registerAboutPage(
    registerCleanupFunction,
    "about-pages-are-cool",
    getRootDirectory(gTestPath) + "dummy.html",
    0
  );
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:about-pages-are-cool",
    true
  );
  ok(tab, "Successfully created an about: page and loaded it.");
  BrowserTestUtils.removeTab(tab);
  try {
    await BrowserTestUtils.unregisterAboutPage("about-pages-are-cool");
    ok(true, "Successfully unregistered the about page.");
  } catch (ex) {
    ok(false, "Should not throw unregistering a known about: page");
  }
  await BrowserTestUtils.unregisterAboutPage("random-other-about-page").then(
    () => {
      ok(
        false,
        "Should not have succeeded unregistering an unknown about: page."
      );
    },
    () => {
      ok(
        true,
        "Should have returned a rejected promise trying to unregister an unknown about page"
      );
    }
  );
});

add_task(async function testWaitForEvent() {
  // A promise returned by BrowserTestUtils.waitForEvent should not be resolved
  // in the same event tick as the event listener is called.
  let eventListenerCalled = false;
  let waitForEventResolved = false;
  // Use capturing phase to make sure the event listener added by
  // BrowserTestUtils.waitForEvent is called before the normal event listener
  // below.
  let eventPromise = BrowserTestUtils.waitForEvent(
    gBrowser,
    "dummyevent",
    true
  );
  eventPromise.then(() => {
    waitForEventResolved = true;
  });
  // Add normal event listener that is called after the event listener added by
  // BrowserTestUtils.waitForEvent.
  gBrowser.addEventListener(
    "dummyevent",
    () => {
      eventListenerCalled = true;
      is(
        waitForEventResolved,
        false,
        "BrowserTestUtils.waitForEvent promise resolution handler shouldn't be called at this point."
      );
    },
    { once: true }
  );

  var event = new CustomEvent("dummyevent");
  gBrowser.dispatchEvent(event);

  await eventPromise;

  is(eventListenerCalled, true, "dummyevent listener should be called");
});