summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/gesture/browser_gesture_navigation.js
blob: 667a1f07b66f22ed33619ba9f4bcb3aefc5da0ce (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_setup(async () => {
  // Disable window occlusion. See bug 1733955 / bug 1779559.
  if (navigator.platform.indexOf("Win") == 0) {
    await SpecialPowers.pushPrefEnv({
      set: [["widget.windows.window_occlusion_tracking.enabled", false]],
    });
  }
});

add_task(async () => {
  // Open a new browser window to make sure there is no navigation history.
  const newBrowser = await BrowserTestUtils.openNewBrowserWindow({});

  let event = {
    direction: SimpleGestureEvent.DIRECTION_LEFT,
  };
  ok(!newBrowser.gGestureSupport._shouldDoSwipeGesture(event));

  event = {
    direction: SimpleGestureEvent.DIRECTION_RIGHT,
  };
  ok(!newBrowser.gGestureSupport._shouldDoSwipeGesture(event));

  await BrowserTestUtils.closeWindow(newBrowser);
});

function createSimpleGestureEvent(type, direction) {
  let event = document.createEvent("SimpleGestureEvent");
  event.initSimpleGestureEvent(
    type,
    false /* canBubble */,
    false /* cancelableArg */,
    window,
    0 /* detail */,
    0 /* screenX */,
    0 /* screenY */,
    0 /* clientX */,
    0 /* clientY */,
    false /* ctrlKey */,
    false /* altKey */,
    false /* shiftKey */,
    false /* metaKey */,
    0 /* button */,
    null /* relatedTarget */,
    0 /* allowedDirections */,
    direction,
    1 /* delta */
  );
  return event;
}

add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [["ui.swipeAnimationEnabled", false]],
  });

  // Open a new browser window and load two pages so that the browser can go
  // back but can't go forward.
  const newWindow = await BrowserTestUtils.openNewBrowserWindow({});

  // gHistroySwipeAnimation gets initialized in a requestIdleCallback so we need
  // to wait for the initialization.
  await TestUtils.waitForCondition(() => {
    return (
      // There's no explicit notification for the initialization, so we wait
      // until `isLTR` matches the browser locale state.
      newWindow.gHistorySwipeAnimation.isLTR != Services.locale.isAppLocaleRTL
    );
  });

  BrowserTestUtils.loadURIString(
    newWindow.gBrowser.selectedBrowser,
    "about:mozilla"
  );
  await BrowserTestUtils.browserLoaded(
    newWindow.gBrowser.selectedBrowser,
    false,
    "about:mozilla"
  );
  BrowserTestUtils.loadURIString(
    newWindow.gBrowser.selectedBrowser,
    "about:about"
  );
  await BrowserTestUtils.browserLoaded(
    newWindow.gBrowser.selectedBrowser,
    false,
    "about:about"
  );

  let event = createSimpleGestureEvent(
    "MozSwipeGestureMayStart",
    SimpleGestureEvent.DIRECTION_LEFT
  );
  newWindow.gGestureSupport._shouldDoSwipeGesture(event);

  // Assuming we are on LTR environment.
  is(
    event.allowedDirections,
    SimpleGestureEvent.DIRECTION_LEFT,
    "Allows only swiping to left, i.e. backward"
  );

  event = createSimpleGestureEvent(
    "MozSwipeGestureMayStart",
    SimpleGestureEvent.DIRECTION_RIGHT
  );
  newWindow.gGestureSupport._shouldDoSwipeGesture(event);
  is(
    event.allowedDirections,
    SimpleGestureEvent.DIRECTION_LEFT,
    "Allows only swiping to left, i.e. backward"
  );

  await BrowserTestUtils.closeWindow(newWindow);
});

add_task(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [["ui.swipeAnimationEnabled", true]],
  });

  // Open a new browser window and load two pages so that the browser can go
  // back but can't go forward.
  const newWindow = await BrowserTestUtils.openNewBrowserWindow({});

  if (!newWindow.gHistorySwipeAnimation._isSupported()) {
    await BrowserTestUtils.closeWindow(newWindow);
    return;
  }

  function sendSwipeSequence(sendEnd) {
    let event = createSimpleGestureEvent(
      "MozSwipeGestureMayStart",
      SimpleGestureEvent.DIRECTION_LEFT
    );
    newWindow.gGestureSupport.handleEvent(event);

    event = createSimpleGestureEvent(
      "MozSwipeGestureStart",
      SimpleGestureEvent.DIRECTION_LEFT
    );
    newWindow.gGestureSupport.handleEvent(event);

    event = createSimpleGestureEvent(
      "MozSwipeGestureUpdate",
      SimpleGestureEvent.DIRECTION_LEFT
    );
    newWindow.gGestureSupport.handleEvent(event);

    event = createSimpleGestureEvent(
      "MozSwipeGestureUpdate",
      SimpleGestureEvent.DIRECTION_LEFT
    );
    newWindow.gGestureSupport.handleEvent(event);

    if (sendEnd) {
      sendSwipeEnd();
    }
  }
  function sendSwipeEnd() {
    let event = createSimpleGestureEvent(
      "MozSwipeGestureEnd",
      SimpleGestureEvent.DIRECTION_LEFT
    );
    newWindow.gGestureSupport.handleEvent(event);
  }

  // gHistroySwipeAnimation gets initialized in a requestIdleCallback so we need
  // to wait for the initialization.
  await TestUtils.waitForCondition(() => {
    return (
      // There's no explicit notification for the initialization, so we wait
      // until `isLTR` matches the browser locale state.
      newWindow.gHistorySwipeAnimation.isLTR != Services.locale.isAppLocaleRTL
    );
  });

  BrowserTestUtils.loadURIString(
    newWindow.gBrowser.selectedBrowser,
    "about:mozilla"
  );
  await BrowserTestUtils.browserLoaded(
    newWindow.gBrowser.selectedBrowser,
    false,
    "about:mozilla"
  );
  BrowserTestUtils.loadURIString(
    newWindow.gBrowser.selectedBrowser,
    "about:about"
  );
  await BrowserTestUtils.browserLoaded(
    newWindow.gBrowser.selectedBrowser,
    false,
    "about:about"
  );

  // Start a swipe that's not enough to navigate
  sendSwipeSequence(/* sendEnd = */ true);

  // Wait two frames
  await new Promise(r =>
    window.requestAnimationFrame(() => window.requestAnimationFrame(r))
  );

  // The transition to fully stopped shouldn't have had enough time yet to
  // become fully stopped.
  ok(
    newWindow.gHistorySwipeAnimation._isStoppingAnimation,
    "should be stopping anim"
  );

  // Start another swipe.
  sendSwipeSequence(/* sendEnd = */ false);

  // Wait two frames
  await new Promise(r =>
    window.requestAnimationFrame(() => window.requestAnimationFrame(r))
  );

  // We should have started a new swipe, ie we shouldn't be stopping.
  ok(
    !newWindow.gHistorySwipeAnimation._isStoppingAnimation,
    "should not be stopping anim"
  );

  sendSwipeEnd();

  await BrowserTestUtils.closeWindow(newWindow);
});