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

"use strict";

const TEST_URI = "http://mochi.test:8888/browser/devtools/client/" +
                 "responsivedesign/test/touch.html";
const layoutReflowSynthMouseMove = "layout.reflow.synthMouseMove";
const domViewportEnabled = "dom.meta-viewport.enabled";

add_task(function* () {
  let tab = yield addTab(TEST_URI);
  let {rdm} = yield openRDM(tab);
  yield pushPrefs([layoutReflowSynthMouseMove, false]);
  yield testWithNoTouch();
  yield rdm.enableTouch();
  yield testWithTouch();
  yield rdm.disableTouch();
  yield testWithNoTouch();
  yield closeRDM(rdm);
});

function* testWithNoTouch() {
  let div = content.document.querySelector("div");
  let x = 0, y = 0;

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

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

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

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

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

function* testWithTouch() {
  let div = content.document.querySelector("div");
  let x = 0, y = 0;

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

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

  info("testWithTouch: Drag the div element");
  yield BrowserTestUtils.synthesizeMouseAtCenter("div", { type: "mousedown", isSynthesized: false },
        gBrowser.selectedBrowser);
  x = 100; y = 100;
  yield BrowserTestUtils.synthesizeMouse("div", x, y,
        { type: "mousemove", isSynthesized: false }, gBrowser.selectedBrowser);
  isnot(div.style.transform, "none", "touchmove should work");
  yield BrowserTestUtils.synthesizeMouse("div", x, y,
        { type: "mouseup", isSynthesized: false }, gBrowser.selectedBrowser);

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

  yield testWithMetaViewportEnabled();
  yield testWithMetaViewportDisabled();
}

function* testWithMetaViewportEnabled() {
  yield pushPrefs([domViewportEnabled, true]);
  let meta = content.document.querySelector("meta[name=viewport]");
  let div = content.document.querySelector("div");
  div.dataset.isDelay = "false";

  info("testWithMetaViewportEnabled: click the div element with <meta name='viewport'>");
  meta.content = "";
  yield synthesizeClick(div);
  is(div.dataset.isDelay, "true", "300ms delay between touch events and mouse events should work");

  info("testWithMetaViewportEnabled: click the div element with <meta name='viewport' content='user-scalable=no'>");
  meta.content = "user-scalable=no";
  yield synthesizeClick(div);
  is(div.dataset.isDelay, "false", "300ms delay between touch events and mouse events should not work");

  info("testWithMetaViewportEnabled: click the div element with <meta name='viewport' content='minimum-scale=maximum-scale'>");
  meta.content = "minimum-scale=maximum-scale";
  yield synthesizeClick(div);
  is(div.dataset.isDelay, "false", "300ms delay between touch events and mouse events should not work");

  info("testWithMetaViewportEnabled: click the div element with <meta name='viewport' content='width=device-width'>");
  meta.content = "width=device-width";
  yield synthesizeClick(div);
  is(div.dataset.isDelay, "false", "300ms delay between touch events and mouse events should not work");
}

function* testWithMetaViewportDisabled() {
  yield pushPrefs([domViewportEnabled, false]);
  let meta = content.document.querySelector("meta[name=viewport]");
  let div = content.document.querySelector("div");
  div.dataset.isDelay = "false";

  info("testWithMetaViewportDisabled: click the div element with <meta name='viewport'>");
  meta.content = "";
  yield synthesizeClick(div);
  is(div.dataset.isDelay, "true", "300ms delay between touch events and mouse events should work");
}

function synthesizeClick(element) {
  let waitForClickEvent = BrowserTestUtils.waitForEvent(element, "click");
  BrowserTestUtils.synthesizeMouseAtCenter(element, { type: "mousedown", isSynthesized: false },
        gBrowser.selectedBrowser);
  BrowserTestUtils.synthesizeMouseAtCenter(element, { type: "mouseup", isSynthesized: false },
        gBrowser.selectedBrowser);
  return waitForClickEvent;
}

function pushPrefs(...aPrefs) {
  let deferred = promise.defer();
  SpecialPowers.pushPrefEnv({"set": aPrefs}, deferred.resolve);
  return deferred.promise;
}