TestSnappingOnMomentum.cpp (4306B)
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 APZCSnappingOnMomentumTesterMock : public APZCTreeManagerTester { 14 public: 15 APZCSnappingOnMomentumTesterMock() { CreateMockHitTester(); } 16 }; 17 18 TEST_F(APZCSnappingOnMomentumTesterMock, Snap_On_Momentum) { 19 const char* treeShape = "x"; 20 LayerIntRect layerVisibleRect[] = { 21 LayerIntRect(0, 0, 100, 100), 22 }; 23 CreateScrollData(treeShape, layerVisibleRect); 24 SetScrollableFrameMetrics(root, ScrollableLayerGuid::START_SCROLL_ID, 25 CSSRect(0, 0, 100, 500)); 26 27 // Set up some basic scroll snapping 28 ScrollSnapInfo snap; 29 snap.mScrollSnapStrictnessY = StyleScrollSnapStrictness::Mandatory; 30 snap.mSnapportSize = 31 CSSSize::ToAppUnits(layerVisibleRect[0].Size() * LayerToCSSScale(1.0)); 32 snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget( 33 Nothing(), Some(0 * AppUnitsPerCSSPixel()), 34 CSSRect::ToAppUnits(CSSRect(0, 0, 10, 10)), StyleScrollSnapStop::Normal, 35 ScrollSnapTargetId{1})); 36 snap.mSnapTargets.AppendElement(ScrollSnapInfo::SnapTarget( 37 Nothing(), Some(100 * AppUnitsPerCSSPixel()), 38 CSSRect::ToAppUnits(CSSRect(0, 100, 10, 10)), StyleScrollSnapStop::Normal, 39 ScrollSnapTargetId{2})); 40 41 ModifyFrameMetrics(root, [&](ScrollMetadata& aSm, FrameMetrics&) { 42 aSm.SetSnapInfo(ScrollSnapInfo(snap)); 43 }); 44 45 UniquePtr<ScopedLayerTreeRegistration> registration = 46 MakeUnique<ScopedLayerTreeRegistration>(LayersId{0}, mcc); 47 UpdateHitTestingTree(); 48 49 RefPtr<TestAsyncPanZoomController> apzc = ApzcOf(root); 50 51 TimeStamp now = mcc->Time(); 52 53 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 54 PanGesture(PanGestureInput::PANGESTURE_START, manager, ScreenIntPoint(50, 80), 55 ScreenPoint(0, 2), now); 56 mcc->AdvanceByMillis(5); 57 apzc->AdvanceAnimations(mcc->GetSampleTime()); 58 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 59 PanGesture(PanGestureInput::PANGESTURE_PAN, manager, ScreenIntPoint(50, 80), 60 ScreenPoint(0, 25), mcc->Time()); 61 mcc->AdvanceByMillis(5); 62 apzc->AdvanceAnimations(mcc->GetSampleTime()); 63 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 64 PanGesture(PanGestureInput::PANGESTURE_PAN, manager, ScreenIntPoint(50, 80), 65 ScreenPoint(0, 25), mcc->Time()); 66 67 // The velocity should be positive when panning with positive displacement. 68 EXPECT_GT(apzc->GetVelocityVector().y, 3.0); 69 70 mcc->AdvanceByMillis(5); 71 apzc->AdvanceAnimations(mcc->GetSampleTime()); 72 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 73 PanGesture(PanGestureInput::PANGESTURE_END, manager, ScreenIntPoint(50, 80), 74 ScreenPoint(0, 0), mcc->Time()); 75 76 // After lifting the fingers, the velocity should be zero and a smooth 77 // animation should have been triggered for scroll snap. 78 EXPECT_EQ(apzc->GetVelocityVector().y, 0); 79 apzc->AssertInSmoothMsdScroll(); 80 81 mcc->AdvanceByMillis(5); 82 83 apzc->AdvanceAnimations(mcc->GetSampleTime()); 84 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 85 PanGesture(PanGestureInput::PANGESTURE_MOMENTUMSTART, manager, 86 ScreenIntPoint(50, 80), ScreenPoint(0, 200), mcc->Time()); 87 mcc->AdvanceByMillis(10); 88 apzc->AdvanceAnimations(mcc->GetSampleTime()); 89 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 90 PanGesture(PanGestureInput::PANGESTURE_MOMENTUMPAN, manager, 91 ScreenIntPoint(50, 80), ScreenPoint(0, 50), mcc->Time()); 92 mcc->AdvanceByMillis(10); 93 apzc->AdvanceAnimations(mcc->GetSampleTime()); 94 QueueMockHitResult(ScrollableLayerGuid::START_SCROLL_ID); 95 PanGesture(PanGestureInput::PANGESTURE_MOMENTUMEND, manager, 96 ScreenIntPoint(50, 80), ScreenPoint(0, 0), mcc->Time()); 97 98 apzc->AdvanceAnimationsUntilEnd(); 99 EXPECT_EQ(100.0f, apzc->GetCurrentAsyncScrollOffset( 100 AsyncTransformConsumer::eForEventHandling) 101 .y); 102 }