summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/test/gtest/TestSnapping.cpp
blob: 13bb1e55918a10a0b5ea1377665c8f4c3172fce2 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "APZCTreeManagerTester.h"
#include "APZTestCommon.h"

#include "InputUtils.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPrefs_mousewheel.h"

class APZCSnappingTesterMock : public APZCTreeManagerTester {
 public:
  APZCSnappingTesterMock() { CreateMockHitTester(); }
};

TEST_F(APZCSnappingTesterMock, Bug1265510) {
  const char* treeShape = "x(x)";
  LayerIntRegion layerVisibleRegion[] = {LayerIntRect(0, 0, 100, 100),
                                         LayerIntRect(0, 100, 100, 100)};
  CreateScrollData(treeShape, layerVisibleRegion);
  SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID,
                            CSSRect(0, 0, 100, 200));
  SetScrollableFrameMetrics(layers[1], ScrollableLayerGuid::START_SCROLL_ID + 1,
                            CSSRect(0, 0, 100, 200));
  SetScrollHandoff(layers[1], root);

  ScrollSnapInfo snap;
  snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory;
  snap.mSnapportSize = CSSSize::ToAppUnits(
      layerVisibleRegion[0].GetBounds().Size() * LayerToCSSScale(1.0));

  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(0 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 0, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{1}));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(100 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 100, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{2}));

  ModifyFrameMetrics(root, [&](ScrollMetadata& aSm, FrameMetrics&) {
    aSm.SetSnapInfo(ScrollSnapInfo(snap));
  });

  UniquePtr<ScopedLayerTreeRegistration> registration =
      MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, mcc);
  UpdateHitTestingTree();

  TestAsyncPanZoomController* outer = ApzcOf(layers[0]);
  TestAsyncPanZoomController* inner = ApzcOf(layers[1]);

  // Position the mouse near the bottom of the outer frame and scroll by 60px.
  // (6 lines of 10px each). APZC will actually scroll to y=100 because of the
  // mandatory snap coordinate there.
  TimeStamp now = mcc->Time();
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  SmoothWheel(manager, ScreenIntPoint(50, 80), ScreenPoint(0, 6), now);
  // Advance in 5ms increments until we've scrolled by 70px. At this point, the
  // closest snap point is y=100, and the inner frame should be under the mouse
  // cursor.
  while (outer
             ->GetCurrentAsyncScrollOffset(
                 AsyncPanZoomController::AsyncTransformConsumer::eForHitTesting)
             .y < 70) {
    mcc->AdvanceByMillis(5);
    outer->AdvanceAnimations(mcc->GetSampleTime());
  }
  // Now do another wheel in a new transaction. This should start scrolling the
  // inner frame; we verify that it does by checking the inner scroll position.
  TimeStamp newTransactionTime =
      now + TimeDuration::FromMilliseconds(
                StaticPrefs::mousewheel_transaction_timeout() + 100);
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID + 1);
  SmoothWheel(manager, ScreenIntPoint(50, 80), ScreenPoint(0, 6),
              newTransactionTime);
  inner->AdvanceAnimationsUntilEnd();
  EXPECT_LT(
      0.0f,
      inner
          ->GetCurrentAsyncScrollOffset(
              AsyncPanZoomController::AsyncTransformConsumer::eForHitTesting)
          .y);

  // However, the outer frame should also continue to the snap point, otherwise
  // it is demonstrating incorrect behaviour by violating the mandatory
  // snapping.
  outer->AdvanceAnimationsUntilEnd();
  EXPECT_EQ(
      100.0f,
      outer
          ->GetCurrentAsyncScrollOffset(
              AsyncPanZoomController::AsyncTransformConsumer::eForHitTesting)
          .y);
}

TEST_F(APZCSnappingTesterMock, Snap_After_Pinch) {
  const char* treeShape = "x";
  LayerIntRegion layerVisibleRegion[] = {
      LayerIntRect(0, 0, 100, 100),
  };
  CreateScrollData(treeShape, layerVisibleRegion);
  SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID,
                            CSSRect(0, 0, 100, 200));

  // Set up some basic scroll snapping
  ScrollSnapInfo snap;
  snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory;
  snap.mSnapportSize = CSSSize::ToAppUnits(
      layerVisibleRegion[0].GetBounds().Size() * LayerToCSSScale(1.0));

  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(0 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 0, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{1}));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(100 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 100, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{2}));

  // Save the scroll snap info on the root APZC.
  // Also mark the root APZC as "root content", since APZC only allows
  // zooming on the root content APZC.
  ModifyFrameMetrics(root, [&](ScrollMetadata& aSm, FrameMetrics& aMetrics) {
    aSm.SetSnapInfo(ScrollSnapInfo(snap));
    aMetrics.SetIsRootContent(true);
  });

  UniquePtr<ScopedLayerTreeRegistration> registration =
      MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, mcc);
  UpdateHitTestingTree();

  RefPtr<TestAsyncPanZoomController> apzc = ApzcOf(root);

  // Allow zooming
  apzc->UpdateZoomConstraints(ZoomConstraints(
      true, true, CSSToParentLayerScale(0.25f), CSSToParentLayerScale(4.0f)));

  PinchWithPinchInput(apzc, ScreenIntPoint(50, 50), ScreenIntPoint(50, 50),
                      1.2f);

  apzc->AssertStateIsSmoothMsdScroll();
}

// Currently fails on Android because on the platform we have a different
// VelocityTracker.
#ifndef MOZ_WIDGET_ANDROID
TEST_F(APZCSnappingTesterMock, SnapOnPanEndWithZeroVelocity) {
  // Use pref values for desktop everywhere.
  SCOPED_GFX_PREF_FLOAT("apz.fling_friction", 0.002);
  SCOPED_GFX_PREF_FLOAT("apz.fling_stopped_threshold", 0.01);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_x1", 0.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_x2", 1.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_y1", 0.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_y2", 1.0);
  SCOPED_GFX_PREF_INT("apz.velocity_relevance_time_ms", 100);

  const char* treeShape = "x";
  LayerIntRegion layerVisibleRegion[] = {
      LayerIntRect(0, 0, 100, 100),
  };
  CreateScrollData(treeShape, layerVisibleRegion);
  SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID,
                            CSSRect(0, 0, 100, 400));

  // Set up two snap points, 30 and 100.
  ScrollSnapInfo snap;
  snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory;
  snap.mSnapportSize = CSSSize::ToAppUnits(
      layerVisibleRegion[0].GetBounds().Size() * LayerToCSSScale(1.0));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(30 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 30, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{1}));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(100 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 100, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{2}));

  // Save the scroll snap info on the root APZC.
  ModifyFrameMetrics(root, [&](ScrollMetadata& aSm, FrameMetrics& aMetrics) {
    aSm.SetSnapInfo(ScrollSnapInfo(snap));
  });

  UniquePtr<ScopedLayerTreeRegistration> registration =
      MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, mcc);
  UpdateHitTestingTree();

  RefPtr<TestAsyncPanZoomController> apzc = ApzcOf(root);

  // Send a series of pan gestures to scroll to position at 50.
  const ScreenIntPoint position = ScreenIntPoint(50, 30);
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_START, manager, position,
             ScreenPoint(0, 10), mcc->Time());
  mcc->AdvanceByMillis(5);
  apzc->AdvanceAnimations(mcc->GetSampleTime());
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_PAN, manager, position,
             ScreenPoint(0, 40), mcc->Time());
  mcc->AdvanceByMillis(5);
  apzc->AdvanceAnimations(mcc->GetSampleTime());

  // Make sure the velocity just before sending a pan-end is zero.
  EXPECT_EQ(apzc->GetVelocityVector().y, 0);

  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_END, manager, position,
             ScreenPoint(0, 0), mcc->Time());

  // Now a smooth animation has been triggered for snapping to 30.
  apzc->AssertStateIsSmoothMsdScroll();

  apzc->AdvanceAnimationsUntilEnd();
  // The snapped position should be 30 rather than 100 because it's the nearest
  // snap point.
  EXPECT_EQ(
      apzc->GetCurrentAsyncScrollOffset(AsyncPanZoomController::eForHitTesting)
          .y,
      30);
}

// Smililar to above SnapOnPanEndWithZeroVelocity but with positive velocity so
// that the snap position would be the one in the scrolling direction.
TEST_F(APZCSnappingTesterMock, SnapOnPanEndWithPositiveVelocity) {
  // Use pref values for desktop everywhere.
  SCOPED_GFX_PREF_FLOAT("apz.fling_friction", 0.002);
  SCOPED_GFX_PREF_FLOAT("apz.fling_stopped_threshold", 0.01);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_x1", 0.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_x2", 1.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_y1", 0.0);
  SCOPED_GFX_PREF_FLOAT("apz.fling_curve_function_y2", 1.0);
  SCOPED_GFX_PREF_INT("apz.velocity_relevance_time_ms", 100);

  const char* treeShape = "x";
  LayerIntRegion layerVisibleRegion[] = {
      LayerIntRect(0, 0, 100, 100),
  };
  CreateScrollData(treeShape, layerVisibleRegion);
  SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID,
                            CSSRect(0, 0, 100, 400));

  // Set up two snap points, 30 and 100.
  ScrollSnapInfo snap;
  snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory;
  snap.mSnapportSize = CSSSize::ToAppUnits(
      layerVisibleRegion[0].GetBounds().Size() * LayerToCSSScale(1.0));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(30 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 30, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{1}));
  snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget(
      Nothing(), Some(100 * AppUnitsPerCSSPixel()),
      CSSRect::ToAppUnits(CSSRect(0, 100, 10, 10)), StyleScrollSnapStop::Normal,
      ScrollSnapTargetId{2}));

  // Save the scroll snap info on the root APZC.
  ModifyFrameMetrics(root, [&](ScrollMetadata& aSm, FrameMetrics& aMetrics) {
    aSm.SetSnapInfo(ScrollSnapInfo(snap));
  });

  UniquePtr<ScopedLayerTreeRegistration> registration =
      MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, mcc);
  UpdateHitTestingTree();

  RefPtr<TestAsyncPanZoomController> apzc = ApzcOf(root);

  // Send a series of pan gestures that a pan-end event happens at 65
  const ScreenIntPoint position = ScreenIntPoint(50, 30);
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_START, manager, position,
             ScreenPoint(0, 10), mcc->Time());
  mcc->AdvanceByMillis(5);
  apzc->AdvanceAnimations(mcc->GetSampleTime());
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_PAN, manager, position,
             ScreenPoint(0, 35), mcc->Time());
  mcc->AdvanceByMillis(5);
  apzc->AdvanceAnimations(mcc->GetSampleTime());
  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_PAN, manager, position,
             ScreenPoint(0, 20), mcc->Time());
  mcc->AdvanceByMillis(5);
  apzc->AdvanceAnimations(mcc->GetSampleTime());

  // There should be positive velocity in this case.
  EXPECT_GT(apzc->GetVelocityVector().y, 0);

  QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID);
  PanGesture(PanGestureInput::PANGESTURE_END, manager, position,
             ScreenPoint(0, 0), mcc->Time());
  mcc->AdvanceByMillis(5);

  // A smooth animation has been triggered by the pan-end event above.
  apzc->AssertStateIsSmoothMsdScroll();

  apzc->AdvanceAnimationsUntilEnd();
  EXPECT_EQ(
      apzc->GetCurrentAsyncScrollOffset(AsyncPanZoomController::eForHitTesting)
          .y,
      100);
}
#endif