1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "APZCTreeManagerTester.h"
8 #include "APZTestCommon.h"
9 
10 #include "InputUtils.h"
11 #include "mozilla/StaticPrefs_layout.h"
12 
13 class APZCSnappingOnMomentumTesterLayersOnly : public APZCTreeManagerTester {
14  public:
APZCSnappingOnMomentumTesterLayersOnly()15   APZCSnappingOnMomentumTesterLayersOnly() { mLayersOnly = true; }
16 };
17 
TEST_F(APZCSnappingOnMomentumTesterLayersOnly,Snap_On_Momentum)18 TEST_F(APZCSnappingOnMomentumTesterLayersOnly, Snap_On_Momentum) {
19   const char* layerTreeSyntax = "c";
20   nsIntRegion layerVisibleRegion[] = {
21       nsIntRegion(IntRect(0, 0, 100, 100)),
22   };
23   root =
24       CreateLayerTree(layerTreeSyntax, layerVisibleRegion, nullptr, lm, layers);
25   SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID,
26                             CSSRect(0, 0, 100, 500));
27 
28   // Set up some basic scroll snapping
29   ScrollSnapInfo snap;
30   snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory;
31 
32   snap.mSnapPositionY.AppendElement(0 * AppUnitsPerCSSPixel());
33   snap.mSnapPositionY.AppendElement(100 * AppUnitsPerCSSPixel());
34 
35   ScrollMetadata metadata = root->GetScrollMetadata(0);
36   metadata.SetSnapInfo(ScrollSnapInfo(snap));
37   root->SetScrollMetadata(metadata);
38 
39   UniquePtr<ScopedLayerTreeRegistration> registration =
40       MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, root, mcc);
41   UpdateHitTestingTree();
42 
43   RefPtr<TestAsyncPanZoomController> apzc = ApzcOf(root);
44 
45   TimeStamp now = mcc->Time();
46 
47   PanGesture(PanGestureInput::PANGESTURE_START, manager, ScreenIntPoint(50, 80),
48              ScreenPoint(0, 2), now);
49   mcc->AdvanceByMillis(5);
50   apzc->AdvanceAnimations(mcc->GetSampleTime());
51   PanGesture(PanGestureInput::PANGESTURE_PAN, manager, ScreenIntPoint(50, 80),
52              ScreenPoint(0, 25), mcc->Time());
53   mcc->AdvanceByMillis(5);
54   apzc->AdvanceAnimations(mcc->GetSampleTime());
55   PanGesture(PanGestureInput::PANGESTURE_PAN, manager, ScreenIntPoint(50, 80),
56              ScreenPoint(0, 25), mcc->Time());
57 
58   // The velocity should be positive when panning with positive displacement.
59   EXPECT_GT(apzc->GetVelocityVector().y, 3.0);
60 
61   mcc->AdvanceByMillis(5);
62   apzc->AdvanceAnimations(mcc->GetSampleTime());
63   PanGesture(PanGestureInput::PANGESTURE_END, manager, ScreenIntPoint(50, 80),
64              ScreenPoint(0, 0), mcc->Time());
65 
66   // After lifting the fingers, the velocity should still be positive.
67   EXPECT_GT(apzc->GetVelocityVector().y, 3.0);
68 
69   mcc->AdvanceByMillis(5);
70 
71   apzc->AdvanceAnimations(mcc->GetSampleTime());
72   PanGesture(PanGestureInput::PANGESTURE_MOMENTUMSTART, manager,
73              ScreenIntPoint(50, 80), ScreenPoint(0, 200), mcc->Time());
74   mcc->AdvanceByMillis(10);
75   apzc->AdvanceAnimations(mcc->GetSampleTime());
76   PanGesture(PanGestureInput::PANGESTURE_MOMENTUMPAN, manager,
77              ScreenIntPoint(50, 80), ScreenPoint(0, 50), mcc->Time());
78   mcc->AdvanceByMillis(10);
79   apzc->AdvanceAnimations(mcc->GetSampleTime());
80   PanGesture(PanGestureInput::PANGESTURE_MOMENTUMEND, manager,
81              ScreenIntPoint(50, 80), ScreenPoint(0, 0), mcc->Time());
82 
83   apzc->AdvanceAnimationsUntilEnd();
84   EXPECT_EQ(
85       100.0f,
86       apzc->GetCurrentAsyncScrollOffset(
87               AsyncPanZoomController::AsyncTransformConsumer::eForHitTesting)
88           .y);
89 }
90