Removed old context test cases
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-RotationGestureRecognizer.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali-test-suite-utils.h>
19 #include <dali/integration-api/events/touch-event-integ.h>
20 #include <dali/integration-api/input-options.h>
21 #include <dali/public-api/dali-core.h>
22 #include <stdlib.h>
23
24 #include <iostream>
25
26 using namespace Dali;
27
28 void utc_dali_rotation_gesture_recognizer_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_rotation_gesture_recognizer_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 ///////////////////////////////////////////////////////////////////////////////
39 namespace
40 {
41 struct SignalData
42 {
43   SignalData()
44   : functorCalled(false),
45     voidFunctorCalled(false),
46     receivedGesture()
47   {
48   }
49
50   void Reset()
51   {
52     functorCalled     = false;
53     voidFunctorCalled = false;
54
55     receivedGesture.Reset();
56
57     rotatedActor.Reset();
58   }
59
60   bool            functorCalled;
61   bool            voidFunctorCalled;
62   RotationGesture receivedGesture;
63   Actor           rotatedActor;
64 };
65
66 // Functor that sets the data when called
67 struct GestureReceivedFunctor
68 {
69   GestureReceivedFunctor(SignalData& data)
70   : signalData(data)
71   {
72   }
73
74   void operator()(Actor actor, const RotationGesture& rotation)
75   {
76     signalData.functorCalled   = true;
77     signalData.receivedGesture = rotation;
78     signalData.rotatedActor    = actor;
79   }
80
81   void operator()()
82   {
83     signalData.voidFunctorCalled = true;
84   }
85
86   SignalData& signalData;
87 };
88
89 Integration::TouchEvent GenerateSingleTouch(PointState::Type state, const Vector2& screenPosition, uint32_t time)
90 {
91   Integration::TouchEvent touchEvent;
92   Integration::Point      point;
93   point.SetState(state);
94   point.SetScreenPosition(screenPosition);
95   point.SetDeviceClass(Device::Class::TOUCH);
96   point.SetDeviceSubclass(Device::Subclass::NONE);
97   touchEvent.points.push_back(point);
98   touchEvent.time = time;
99   return touchEvent;
100 }
101
102 Integration::TouchEvent GenerateDoubleTouch(PointState::Type stateA, const Vector2& screenPositionA, PointState::Type stateB, const Vector2& screenPositionB, uint32_t time)
103 {
104   Integration::TouchEvent touchEvent;
105   Integration::Point      point;
106   point.SetState(stateA);
107   point.SetScreenPosition(screenPositionA);
108   point.SetDeviceClass(Device::Class::TOUCH);
109   point.SetDeviceSubclass(Device::Subclass::NONE);
110   touchEvent.points.push_back(point);
111   point.SetScreenPosition(screenPositionB);
112   point.SetState(stateB);
113   touchEvent.points.push_back(point);
114   touchEvent.time = time;
115   return touchEvent;
116 }
117
118 } // namespace
119
120 ///////////////////////////////////////////////////////////////////////////////
121 int UtcDaliRotationGestureRecognizerRealistic(void)
122 {
123   TestApplication application;
124
125   RotationGestureDetector detector = RotationGestureDetector::New();
126
127   Actor actor = Actor::New();
128   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
129   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
130   application.GetScene().Add(actor);
131
132   // Render and notify
133   application.SendNotification();
134   application.Render();
135
136   detector.Attach(actor);
137
138   SignalData             data;
139   GestureReceivedFunctor functor(data);
140   detector.DetectedSignal().Connect(&application, functor);
141
142   application.ProcessEvent(GenerateSingleTouch(PointState::DOWN, Vector2(20.0f, 20.0f), 100));
143   application.ProcessEvent(GenerateSingleTouch(PointState::UP, Vector2(20.0f, 20.0f), 105));
144   application.ProcessEvent(GenerateSingleTouch(PointState::DOWN, Vector2(20.0f, 20.0f), 110));
145   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 25.0f), PointState::DOWN, Vector2(20.0f, 90.0f), 115));
146   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 30.0f), PointState::MOTION, Vector2(20.0f, 85.0f), 120));
147   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 35.0f), PointState::MOTION, Vector2(20.0f, 80.0f), 125));
148   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 40.0f), PointState::MOTION, Vector2(20.0f, 75.0f), 130));
149   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 45.0f), PointState::MOTION, Vector2(20.0f, 70.0f), 135));
150   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 50.0f), PointState::MOTION, Vector2(20.0f, 65.0f), 140));
151   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 55.0f), PointState::MOTION, Vector2(20.0f, 60.0f), 145));
152   application.ProcessEvent(GenerateSingleTouch(PointState::UP, Vector2(20.0f, 56.0f), 155));
153
154   application.SendNotification();
155
156   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
157
158   END_TEST;
159 }
160
161 int UtcDaliRotationGestureRecognizerBasicInterrupted(void)
162 {
163   TestApplication application;
164
165   RotationGestureDetector detector = RotationGestureDetector::New();
166
167   Actor actor = Actor::New();
168   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
169   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
170   application.GetScene().Add(actor);
171
172   // Render and notify
173   application.SendNotification();
174   application.Render();
175
176   detector.Attach(actor);
177
178   SignalData             data;
179   GestureReceivedFunctor functor(data);
180   detector.DetectedSignal().Connect(&application, functor);
181
182   application.ProcessEvent(GenerateSingleTouch(PointState::INTERRUPTED, Vector2(20.0f, 30.0f), 152));
183
184   application.SendNotification();
185
186   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
187
188   END_TEST;
189 }
190
191 int UtcDaliRotationGestureRecognizerMinimumTouchEvents(void)
192 {
193   TestApplication application;
194
195   Actor actor = Actor::New();
196   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
197   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
198   application.GetScene().Add(actor);
199
200   application.SendNotification();
201   application.Render();
202
203   SignalData             data;
204   GestureReceivedFunctor functor(data);
205
206   RotationGestureDetector detector = RotationGestureDetector::New();
207   detector.Attach(actor);
208   detector.DetectedSignal().Connect(&application, functor);
209
210   // Case 1
211   // 2 touch events make a gesture begin
212   Integration::SetRotationGestureMinimumTouchEvents(2);
213   application.ProcessEvent(GenerateDoubleTouch(PointState::DOWN, Vector2(20.0f, 20.0f), PointState::DOWN, Vector2(20.0f, 90.0f), 150));
214   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(90.0f, 90.0f), 160));
215
216   DALI_TEST_EQUALS(GestureState::STARTED, data.receivedGesture.GetState(), TEST_LOCATION);
217   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
218   data.Reset();
219
220   // Case 2
221   // 4 touch events make a gesture begin
222   Integration::SetRotationGestureMinimumTouchEvents(4);
223   application.ProcessEvent(GenerateDoubleTouch(PointState::DOWN, Vector2(20.0f, 20.0f), PointState::DOWN, Vector2(20.0f, 90.0f), 150));
224   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(90.0f, 90.0f), 160));
225
226   // Check the gesture is not detected unlike previous case
227   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
228
229   END_TEST;
230 }
231
232 int UtcDaliRotationGestureRecognizerMinimumTouchEventsAfterStart(void)
233 {
234   TestApplication application;
235
236   Actor actor = Actor::New();
237   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
238   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
239   application.GetScene().Add(actor);
240
241   application.SendNotification();
242   application.Render();
243
244   SignalData             data;
245   GestureReceivedFunctor functor(data);
246
247   RotationGestureDetector detector = RotationGestureDetector::New();
248   detector.Attach(actor);
249   detector.DetectedSignal().Connect(&application, functor);
250
251   // Case 1
252   // > 2 touch events make a gesture begin
253   // > 4 touch events generate gestures after begin
254   Integration::SetRotationGestureMinimumTouchEvents(2);
255   Integration::SetRotationGestureMinimumTouchEventsAfterStart(6);
256
257   application.ProcessEvent(GenerateDoubleTouch(PointState::DOWN, Vector2(20.0f, 20.0f), PointState::DOWN, Vector2(20.0f, 90.0f), 150));
258   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(90.0f, 90.0f), 160));
259
260   DALI_TEST_EQUALS(GestureState::STARTED, data.receivedGesture.GetState(), TEST_LOCATION);
261   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
262
263   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 170));
264   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 180));
265   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 190));
266   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 200));
267   // > Test : not enough touch events to make the gesture state "CONTINUING"
268   DALI_TEST_EQUALS(GestureState::STARTED, data.receivedGesture.GetState(), TEST_LOCATION);
269
270   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 210));
271   application.ProcessEvent(GenerateDoubleTouch(PointState::MOTION, Vector2(20.0f, 20.0f), PointState::MOTION, Vector2(20.0f, 90.0f), 220));
272   // > Test : 6 touch events after start make the gesture state "CONTINUING"
273   DALI_TEST_EQUALS(GestureState::CONTINUING, data.receivedGesture.GetState(), TEST_LOCATION);
274
275   END_TEST;
276 }