InputUtils.h (6741B)
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 #ifndef mozilla_layers_InputUtils_h 8 #define mozilla_layers_InputUtils_h 9 10 /** 11 * Defines a set of utility functions for generating input events 12 * to an APZC/APZCTM during APZ gtests. 13 */ 14 15 #include "APZTestCommon.h" 16 17 /* The InputReceiver template parameter used in the helper functions below needs 18 * to be a class that implements functions with the signatures: 19 * APZEventResult ReceiveInputEvent(const InputData& aEvent); 20 * void SetAllowedTouchBehavior(uint64_t aInputBlockId, 21 * const nsTArray<uint32_t>& aBehaviours); 22 * The classes that currently implement these are APZCTreeManager and 23 * TestAsyncPanZoomController. Using this template allows us to test individual 24 * APZC instances in isolation and also an entire APZ tree, while using the same 25 * code to dispatch input events. 26 */ 27 28 template <class InputReceiver> 29 void SetDefaultAllowedTouchBehavior(const RefPtr<InputReceiver>& aTarget, 30 uint64_t aInputBlockId, 31 int touchPoints = 1) { 32 nsTArray<uint32_t> defaultBehaviors; 33 // use the default value where everything is allowed 34 for (int i = 0; i < touchPoints; i++) { 35 defaultBehaviors.AppendElement( 36 mozilla::layers::AllowedTouchBehavior::HORIZONTAL_PAN | 37 mozilla::layers::AllowedTouchBehavior::VERTICAL_PAN | 38 mozilla::layers::AllowedTouchBehavior::PINCH_ZOOM | 39 mozilla::layers::AllowedTouchBehavior::ANIMATING_ZOOM); 40 } 41 aTarget->SetAllowedTouchBehavior(aInputBlockId, defaultBehaviors); 42 } 43 44 inline MultiTouchInput CreateMultiTouchInput( 45 MultiTouchInput::MultiTouchType aType, TimeStamp aTime) { 46 return MultiTouchInput(aType, MillisecondsSinceStartup(aTime), aTime, 0); 47 } 48 49 template <class InputReceiver> 50 APZEventResult TouchDown(const RefPtr<InputReceiver>& aTarget, 51 const ScreenIntPoint& aPoint, TimeStamp aTime) { 52 MultiTouchInput mti = 53 CreateMultiTouchInput(MultiTouchInput::MULTITOUCH_START, aTime); 54 mti.mTouches.AppendElement(CreateSingleTouchData(0, aPoint)); 55 return aTarget->ReceiveInputEvent(mti); 56 } 57 58 template <class InputReceiver> 59 APZEventResult TouchMove(const RefPtr<InputReceiver>& aTarget, 60 const ScreenIntPoint& aPoint, TimeStamp aTime) { 61 MultiTouchInput mti = 62 CreateMultiTouchInput(MultiTouchInput::MULTITOUCH_MOVE, aTime); 63 mti.mTouches.AppendElement(CreateSingleTouchData(0, aPoint)); 64 return aTarget->ReceiveInputEvent(mti); 65 } 66 67 template <class InputReceiver> 68 APZEventResult TouchUp(const RefPtr<InputReceiver>& aTarget, 69 const ScreenIntPoint& aPoint, TimeStamp aTime) { 70 MultiTouchInput mti = 71 CreateMultiTouchInput(MultiTouchInput::MULTITOUCH_END, aTime); 72 mti.mTouches.AppendElement(CreateSingleTouchData(0, aPoint)); 73 return aTarget->ReceiveInputEvent(mti); 74 } 75 76 template <class InputReceiver> 77 APZEventResult Wheel(const RefPtr<InputReceiver>& aTarget, 78 const ScreenIntPoint& aPoint, const ScreenPoint& aDelta, 79 TimeStamp aTime) { 80 ScrollWheelInput input(aTime, 0, ScrollWheelInput::SCROLLMODE_INSTANT, 81 ScrollWheelInput::SCROLLDELTA_PIXEL, aPoint, aDelta.x, 82 aDelta.y, false, WheelDeltaAdjustmentStrategy::eNone); 83 return aTarget->ReceiveInputEvent(input); 84 } 85 86 // Tests that use this function should set general.smoothScroll=true, otherwise 87 // the smooth scroll animation code will set the animation duration to 0. 88 template <class InputReceiver> 89 APZEventResult SmoothWheel(const RefPtr<InputReceiver>& aTarget, 90 const ScreenIntPoint& aPoint, 91 const ScreenPoint& aDelta, TimeStamp aTime) { 92 ScrollWheelInput input(aTime, 0, ScrollWheelInput::SCROLLMODE_SMOOTH, 93 ScrollWheelInput::SCROLLDELTA_LINE, aPoint, aDelta.x, 94 aDelta.y, false, WheelDeltaAdjustmentStrategy::eNone); 95 return aTarget->ReceiveInputEvent(input); 96 } 97 98 template <class InputReceiver> 99 APZEventResult MouseDown(const RefPtr<InputReceiver>& aTarget, 100 const ScreenIntPoint& aPoint, TimeStamp aTime) { 101 MouseInput input(MouseInput::MOUSE_DOWN, 102 MouseInput::ButtonType::PRIMARY_BUTTON, 0, 0, aPoint, aTime, 103 0); 104 return aTarget->ReceiveInputEvent(input); 105 } 106 107 template <class InputReceiver> 108 APZEventResult MouseMove(const RefPtr<InputReceiver>& aTarget, 109 const ScreenIntPoint& aPoint, TimeStamp aTime) { 110 MouseInput input(MouseInput::MOUSE_MOVE, 111 MouseInput::ButtonType::PRIMARY_BUTTON, 0, 0, aPoint, aTime, 112 0); 113 return aTarget->ReceiveInputEvent(input); 114 } 115 116 template <class InputReceiver> 117 APZEventResult MouseUp(const RefPtr<InputReceiver>& aTarget, 118 const ScreenIntPoint& aPoint, TimeStamp aTime) { 119 MouseInput input(MouseInput::MOUSE_UP, MouseInput::ButtonType::PRIMARY_BUTTON, 120 0, 0, aPoint, aTime, 0); 121 return aTarget->ReceiveInputEvent(input); 122 } 123 124 template <class InputReceiver> 125 APZEventResult PanGesture(PanGestureInput::PanGestureType aType, 126 const RefPtr<InputReceiver>& aTarget, 127 const ScreenIntPoint& aPoint, 128 const ScreenPoint& aDelta, TimeStamp aTime, 129 Modifiers aModifiers = MODIFIER_NONE, 130 bool aSimulateMomentum = false) { 131 PanGestureInput input(aType, aTime, aPoint, aDelta, aModifiers); 132 input.mSimulateMomentum = aSimulateMomentum; 133 if constexpr (std::is_same_v<InputReceiver, TestAsyncPanZoomController>) { 134 // In the case of TestAsyncPanZoomController we know for sure that the 135 // event will be handled by APZ so set it explicitly. 136 input.mHandledByAPZ = true; 137 } 138 return aTarget->ReceiveInputEvent(input); 139 } 140 141 template <class InputReceiver> 142 APZEventResult PanGestureWithModifiers(PanGestureInput::PanGestureType aType, 143 Modifiers aModifiers, 144 const RefPtr<InputReceiver>& aTarget, 145 const ScreenIntPoint& aPoint, 146 const ScreenPoint& aDelta, 147 TimeStamp aTime) { 148 return PanGesture(aType, aTarget, aPoint, aDelta, aTime, aModifiers); 149 } 150 151 #endif // mozilla_layers_InputUtils_h