summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/emulation/browser_setTouchEmulationEnabled.js
blob: 48315ed0e944a3adbdd9a4122e97d48b90ca911e (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const DOC_TOUCH = toDataURL("<div>Hello world");

add_task(async function invalidEnabledType({ client }) {
  const { Emulation } = client;

  for (const enabled of [null, "", 1, [], {}]) {
    let errorThrown = "";
    try {
      await Emulation.setTouchEmulationEnabled({ enabled });
    } catch (e) {
      errorThrown = e.message;
    }
    ok(
      errorThrown.match(/enabled: boolean value expected/),
      `Fails with invalid type: ${enabled}`
    );
  }
});

add_task(async function enableAndDisable({ client }) {
  const { Emulation, Runtime } = client;
  const url = toDataURL("<p>foo");

  await enableRuntime(client);

  let contextCreated = Runtime.executionContextCreated();
  await loadURL(url);
  let result = await contextCreated;
  await assertTouchEnabled(client, result.context, false);

  await Emulation.setTouchEmulationEnabled({ enabled: true });
  contextCreated = Runtime.executionContextCreated();
  await loadURL(url);
  result = await contextCreated;
  await assertTouchEnabled(client, result.context, true);

  await Emulation.setTouchEmulationEnabled({ enabled: false });
  contextCreated = Runtime.executionContextCreated();
  await loadURL(url);
  result = await contextCreated;
  await assertTouchEnabled(client, result.context, false);
});

add_task(async function receiveTouchEventsWhenEnabled({ client }) {
  const { Emulation, Runtime } = client;

  await enableRuntime(client);

  await Emulation.setTouchEmulationEnabled({ enabled: true });
  const contextCreated = Runtime.executionContextCreated();
  await loadURL(DOC_TOUCH);
  const { context } = await contextCreated;

  await assertTouchEnabled(client, context, true);

  const { result } = await evaluate(client, context.id, () => {
    return new Promise(resolve => {
      window.ontouchstart = () => {
        resolve(true);
      };
      window.dispatchEvent(new Event("touchstart"));
      resolve(false);
    });
  });
  is(result.value, true, "Received touch event");
});

async function assertTouchEnabled(client, context, expectedStatus) {
  const { result } = await evaluate(client, context.id, () => {
    return "ontouchstart" in window;
  });

  if (expectedStatus) {
    ok(result.value, "Touch emulation enabled");
  } else {
    ok(!result.value, "Touch emulation disabled");
  }
}