summaryrefslogtreecommitdiffstats
path: root/devtools/client/responsive/test/browser/browser_touch_simulation.js
blob: b74c9f53c432ef1a9b4866855aca41dfd6ce50b8 (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
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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test global touch simulation button

const TEST_URL = `${URL_ROOT_SSL}touch.html`;
const PREF_DOM_META_VIEWPORT_ENABLED = "dom.meta-viewport.enabled";

// A 300ms delay between a `touchend` and `click` event is added whenever double-tap zoom
// is allowed.
const DELAY_MIN = 250;

addRDMTask(TEST_URL, async function ({ ui }) {
  reloadOnTouchChange(true);

  await waitBootstrap(ui);
  await testWithNoTouch(ui);
  await toggleTouchSimulation(ui);
  await promiseContentReflow(ui);
  await testWithTouch(ui);
  await testWithMetaViewportEnabled(ui);
  await testWithMetaViewportDisabled(ui);
  testTouchButton(ui);

  reloadOnTouchChange(false);
});

async function testWithNoTouch(ui) {
  await SpecialPowers.spawn(ui.getViewportBrowser(), [], async function () {
    const div = content.document.querySelector("div");
    let x = 0,
      y = 0;

    info("testWithNoTouch: Initial test parameter and mouse mouse outside div");
    x = -1;
    y = -1;
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    div.style.transform = "none";
    div.style.backgroundColor = "";

    info("testWithNoTouch: Move mouse into the div element");
    await EventUtils.synthesizeMouseAtCenter(
      div,
      { type: "mousemove", isSynthesized: false },
      content
    );
    is(div.style.backgroundColor, "red", "mouseenter or mouseover should work");

    info("testWithNoTouch: Drag the div element");
    await EventUtils.synthesizeMouseAtCenter(
      div,
      { type: "mousedown", isSynthesized: false },
      content
    );
    x = 100;
    y = 100;
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    is(div.style.transform, "none", "touchmove shouldn't work");
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mouseup", isSynthesized: false },
      content
    );

    info("testWithNoTouch: Move mouse out of the div element");
    x = -1;
    y = -1;
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    is(div.style.backgroundColor, "blue", "mouseout or mouseleave should work");

    info("testWithNoTouch: Click the div element");
    await EventUtils.synthesizeClick(div);
    is(
      div.dataset.isDelay,
      "false",
      "300ms delay between touch events and mouse events should not work"
    );

    // Assuming that this test runs on devices having no touch screen device.
    ok(
      !content.document.defaultView.matchMedia("(pointer: coarse)").matches,
      "pointer: coarse shouldn't be matched"
    );
    ok(
      !content.document.defaultView.matchMedia("(hover: none)").matches,
      "hover: none shouldn't be matched"
    );
    ok(
      !content.document.defaultView.matchMedia("(any-pointer: coarse)").matches,
      "any-pointer: coarse shouldn't be matched"
    );
    ok(
      !content.document.defaultView.matchMedia("(any-hover: none)").matches,
      "any-hover: none shouldn't be matched"
    );
  });
}

async function testWithTouch(ui) {
  await SpecialPowers.spawn(ui.getViewportBrowser(), [], async function () {
    const div = content.document.querySelector("div");
    let x = 0,
      y = 0;

    info("testWithTouch: Initial test parameter and mouse mouse outside div");
    x = -1;
    y = -1;
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    div.style.transform = "none";
    div.style.backgroundColor = "";

    info("testWithTouch: Move mouse into the div element");
    await EventUtils.synthesizeMouseAtCenter(
      div,
      { type: "mousemove", isSynthesized: false },
      content
    );
    isnot(
      div.style.backgroundColor,
      "red",
      "mouseenter or mouseover should not work"
    );

    info("testWithTouch: Drag the div element");
    await EventUtils.synthesizeMouseAtCenter(
      div,
      { type: "mousedown", isSynthesized: false },
      content
    );
    x = 100;
    y = 100;
    const touchMovePromise = ContentTaskUtils.waitForEvent(div, "touchmove");
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    await touchMovePromise;
    isnot(div.style.transform, "none", "touchmove should work");
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mouseup", isSynthesized: false },
      content
    );

    info("testWithTouch: Move mouse out of the div element");
    x = -1;
    y = -1;
    await EventUtils.synthesizeMouse(
      div,
      x,
      y,
      { type: "mousemove", isSynthesized: false },
      content
    );
    isnot(
      div.style.backgroundColor,
      "blue",
      "mouseout or mouseleave should not work"
    );

    ok(
      content.document.defaultView.matchMedia("(pointer: coarse)").matches,
      "pointer: coarse should be matched"
    );
    ok(
      content.document.defaultView.matchMedia("(hover: none)").matches,
      "hover: none should be matched"
    );
    ok(
      content.document.defaultView.matchMedia("(any-pointer: coarse)").matches,
      "any-pointer: coarse should be matched"
    );
    ok(
      content.document.defaultView.matchMedia("(any-hover: none)").matches,
      "any-hover: none should be matched"
    );
  });

  // Capturing touch events with the content window as a registered listener causes the
  // "changedTouches" field to be undefined when using deprecated TouchEvent APIs.
  // See Bug 1549220 and Bug 1588438 for more information on this issue.
  info("Test that changed touches captured on the content window are defined.");
  await SpecialPowers.spawn(ui.getViewportBrowser(), [], async function () {
    const div = content.document.querySelector("div");

    content.addEventListener(
      "touchstart",
      event => {
        const changedTouch = event.changedTouches[0];
        ok(changedTouch, "Changed touch is defined.");
      },
      { once: true }
    );
    await EventUtils.synthesizeClick(div);
  });
}

async function testWithMetaViewportEnabled(ui) {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_DOM_META_VIEWPORT_ENABLED, true]],
  });

  await SpecialPowers.spawn(
    ui.getViewportBrowser(),
    [{ delay_min: DELAY_MIN }],
    async function ({ delay_min }) {
      // A helper for testing the delay between touchend and click events.
      async function testDelay(mvc, el) {
        const touchendPromise = ContentTaskUtils.waitForEvent(el, "touchend");
        const clickPromise = ContentTaskUtils.waitForEvent(el, "click");
        await EventUtils.synthesizeClick(el);
        const { timeStamp: touchendTimestamp } = await touchendPromise;
        const { timeStamp: clickTimeStamp } = await clickPromise;
        const delay = clickTimeStamp - touchendTimestamp;

        const expected = delay >= delay_min;

        ok(
          expected,
          `${mvc}: There should be greater than a ${delay_min}ms delay between touch events and mouse events. Got delay of ${delay}ms`
        );
      }

      // A helper function for waiting for reflow to complete.
      const promiseReflow = () => {
        return new Promise(resolve => {
          content.window.requestAnimationFrame(() => {
            content.window.requestAnimationFrame(resolve);
          });
        });
      };

      const meta = content.document.querySelector("meta[name=viewport]");
      const div = content.document.querySelector("div");

      info(
        "testWithMetaViewportEnabled: " +
          "click the div element with <meta name='viewport'>"
      );
      meta.content = "";
      await promiseReflow();
      await testDelay("(empty)", div);
    }
  );

  await SpecialPowers.popPrefEnv();
}

async function testWithMetaViewportDisabled(ui) {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_DOM_META_VIEWPORT_ENABLED, false]],
  });

  await SpecialPowers.spawn(
    ui.getViewportBrowser(),
    [{ delay_min: DELAY_MIN }],
    async function ({ delay_min }) {
      const meta = content.document.querySelector("meta[name=viewport]");
      const div = content.document.querySelector("div");

      info(
        "testWithMetaViewportDisabled: click the div with <meta name='viewport'>"
      );
      meta.content = "";
      const touchendPromise = ContentTaskUtils.waitForEvent(div, "touchend");
      const clickPromise = ContentTaskUtils.waitForEvent(div, "click");
      await EventUtils.synthesizeClick(div);
      const { timeStamp: touchendTimestamp } = await touchendPromise;
      const { timeStamp: clickTimeStamp } = await clickPromise;
      const delay = clickTimeStamp - touchendTimestamp;

      const expected = delay >= delay_min;

      ok(
        expected,
        `There should be greater than a ${delay_min}ms delay between touch events and mouse events. Got delay of ${delay}ms`
      );
    }
  );
}

function testTouchButton(ui) {
  const { document } = ui.toolWindow;
  const touchButton = document.getElementById("touch-simulation-button");

  ok(
    touchButton.classList.contains("checked"),
    "Touch simulation is active at end of test."
  );

  touchButton.click();

  ok(
    !touchButton.classList.contains("checked"),
    "Touch simulation is stopped on click."
  );

  touchButton.click();

  ok(
    touchButton.classList.contains("checked"),
    "Touch simulation is started on click."
  );
}

async function waitBootstrap(ui) {
  await waitForFrameLoad(ui, TEST_URL);
}