[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-LongPressGestureRecognizer.cpp
1 /*
2  * Copyright (c) 2019 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 <iostream>
19 #include <thread>
20 #include <chrono>
21
22 #include <stdlib.h>
23 #include <dali/public-api/dali-core.h>
24 #include <dali/integration-api/events/touch-event-integ.h>
25 #include <dali/integration-api/render-task-list-integ.h>
26 #include <dali/internal/event/events/long-press-gesture-event.h>
27 #include <dali-test-suite-utils.h>
28 #include <test-touch-utils.h>
29
30 using namespace Dali;
31
32 ///////////////////////////////////////////////////////////////////////////////
33 namespace
34 {
35
36 struct SignalData
37 {
38   SignalData()
39   : functorCalled(false),
40     voidFunctorCalled(false),
41     receivedGesture(Gesture::Started)
42   {}
43
44   void Reset()
45   {
46     functorCalled = false;
47     voidFunctorCalled = false;
48
49     receivedGesture.state = Gesture::Started;
50
51     pressedActor.Reset();
52   }
53
54   bool functorCalled;
55   bool voidFunctorCalled;
56   LongPressGesture receivedGesture;
57   Actor pressedActor;
58 };
59
60 // Functor that sets the data when called
61 struct GestureReceivedFunctor
62 {
63   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
64
65   void operator()(Actor actor, const LongPressGesture& pan)
66   {
67     signalData.functorCalled = true;
68     signalData.receivedGesture = pan;
69     signalData.pressedActor = actor;
70   }
71
72   void operator()()
73   {
74     signalData.voidFunctorCalled = true;
75   }
76
77   SignalData& signalData;
78 };
79
80
81 Integration::TouchEvent GenerateSingleTouch( PointState::Type state, const Vector2& screenPosition, uint32_t time )
82 {
83   Integration::TouchEvent touchEvent;
84   Integration::Point point;
85   point.SetState( state );
86   point.SetDeviceId(4);
87   point.SetScreenPosition( screenPosition );
88   point.SetDeviceClass( Device::Class::TOUCH );
89   point.SetDeviceSubclass( Device::Subclass::NONE );
90   touchEvent.points.push_back( point );
91   touchEvent.time = time;
92   return touchEvent;
93 }
94
95 Integration::TouchEvent GenerateDoubleTouch( PointState::Type stateA, const Vector2& screenPositionA, PointState::Type stateB, const Vector2& screenPositionB, uint32_t time )
96 {
97   Integration::TouchEvent touchEvent;
98   Integration::Point point;
99   point.SetState( stateA );
100   point.SetDeviceId(4);
101   point.SetScreenPosition( screenPositionA );
102   point.SetDeviceClass( Device::Class::TOUCH );
103   point.SetDeviceSubclass( Device::Subclass::NONE );
104   touchEvent.points.push_back( point );
105   point.SetScreenPosition( screenPositionB );
106   point.SetState( stateB);
107   point.SetDeviceId(7);
108   touchEvent.points.push_back( point );
109   touchEvent.time = time;
110   return touchEvent;
111 }
112
113
114 } // anon namespace
115
116 ///////////////////////////////////////////////////////////////////////////////
117
118
119 int UtcDaliLongPressGestureRecognizerBasicNoAction(void)
120 {
121   TestApplication application;
122
123   LongPressGestureDetector detector = LongPressGestureDetector::New();
124
125   Actor actor = Actor::New();
126   actor.SetSize(100.0f, 100.0f);
127   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
128   Stage::GetCurrent().Add(actor);
129
130   // Render and notify
131   application.SendNotification();
132   application.Render();
133
134   detector.Attach(actor);
135
136   SignalData data;
137   GestureReceivedFunctor functor(data);
138   detector.DetectedSignal().Connect(&application, functor);
139
140   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
141
142   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
143
144   application.SendNotification();
145
146   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
147
148   END_TEST;
149 }
150
151 int UtcDaliLongPressGestureRecognizerBasic(void)
152 {
153   TestApplication application;
154
155   LongPressGestureDetector detector = LongPressGestureDetector::New();
156
157   Actor actor = Actor::New();
158   actor.SetSize(100.0f, 100.0f);
159   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
160   Stage::GetCurrent().Add(actor);
161
162   // Render and notify
163   application.SendNotification();
164   application.Render();
165
166   detector.Attach(actor);
167
168   SignalData data;
169   GestureReceivedFunctor functor(data);
170   detector.DetectedSignal().Connect(&application, functor);
171
172   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
173
174   application.GetPlatform().TriggerTimer();
175
176   application.SendNotification();
177
178   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
179
180   END_TEST;
181 }
182
183 int UtcDaliLongPressGestureRecognizerTooShortWait(void)
184 {
185   TestApplication application;
186
187   LongPressGestureDetector detector = LongPressGestureDetector::New();
188
189   Actor actor = Actor::New();
190   actor.SetSize(100.0f, 100.0f);
191   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
192   Stage::GetCurrent().Add(actor);
193
194   // Render and notify
195   application.SendNotification();
196   application.Render();
197
198   detector.Attach(actor);
199
200   SignalData data;
201   GestureReceivedFunctor functor(data);
202   detector.DetectedSignal().Connect(&application, functor);
203
204   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
205
206   application.SendNotification();
207
208   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
209
210   END_TEST;
211 }
212
213 int UtcDaliLongPressGestureRecognizerTooFewPoints(void)
214 {
215   TestApplication application;
216
217   LongPressGestureDetector detector = LongPressGestureDetector::New();
218
219   detector.SetTouchesRequired(2,2);
220
221   Actor actor = Actor::New();
222   actor.SetSize(100.0f, 100.0f);
223   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
224   Stage::GetCurrent().Add(actor);
225
226   // Render and notify
227   application.SendNotification();
228   application.Render();
229
230   detector.Attach(actor);
231
232   SignalData data;
233   GestureReceivedFunctor functor(data);
234   detector.DetectedSignal().Connect(&application, functor);
235
236   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
237
238   // There should be no function to call
239   application.GetPlatform().TriggerTimer();
240
241   application.SendNotification();
242
243   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
244
245   END_TEST;
246 }
247
248 int UtcDaliLongPressGestureRecognizerTooManyPoints(void)
249 {
250   TestApplication application;
251
252   LongPressGestureDetector detector = LongPressGestureDetector::New();
253
254   Actor actor = Actor::New();
255   actor.SetSize(100.0f, 100.0f);
256   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
257   Stage::GetCurrent().Add(actor);
258
259   // Render and notify
260   application.SendNotification();
261   application.Render();
262
263   detector.Attach(actor);
264
265   SignalData data;
266   GestureReceivedFunctor functor(data);
267   detector.DetectedSignal().Connect(&application, functor);
268
269   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
270   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 151 ) );
271
272   // There should be no function to call as the double touch should have cancelled it
273   application.GetPlatform().TriggerTimer();
274
275   application.SendNotification();
276
277   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
278
279   END_TEST;
280 }
281
282 int UtcDaliLongPressGestureRecognizerMultiplePointsMoving(void)
283 {
284   TestApplication application;
285
286   LongPressGestureDetector detector = LongPressGestureDetector::New();
287
288   detector.SetTouchesRequired(2,2);
289
290   Actor actor = Actor::New();
291   actor.SetSize(100.0f, 100.0f);
292   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
293   Stage::GetCurrent().Add(actor);
294
295   // Render and notify
296   application.SendNotification();
297   application.Render();
298
299   detector.Attach(actor);
300
301   SignalData data;
302   GestureReceivedFunctor functor(data);
303   detector.DetectedSignal().Connect(&application, functor);
304
305   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 0.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 151 ) );
306   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 10.0f ), PointState::MOTION, Vector2( 20.0f, 80.0f ), 153 ) );
307   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 70.0f ), 155 ) );
308   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 30.0f ), PointState::MOTION, Vector2( 20.0f, 60.0f ), 157 ) );
309   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 40.0f ), PointState::MOTION, Vector2( 20.0f, 50.0f ), 159 ) );
310   application.ProcessEvent( GenerateDoubleTouch( PointState::STATIONARY, Vector2( 20.0f, 40.0f ), PointState::UP, Vector2( 20.0f, 50.0f ), 160 ) );
311   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 40.0f ), 161 ) );
312
313   application.GetPlatform().TriggerTimer();
314
315   application.SendNotification();
316
317   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
318
319   END_TEST;
320 }
321
322 int UtcDaliLongPressGestureRecognizerMultiplePointsLongPress(void)
323 {
324   TestApplication application;
325
326   LongPressGestureDetector detector = LongPressGestureDetector::New();
327
328   detector.SetTouchesRequired(2,2);
329
330   Actor actor = Actor::New();
331   actor.SetSize(100.0f, 100.0f);
332   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
333   Stage::GetCurrent().Add(actor);
334
335   // Render and notify
336   application.SendNotification();
337   application.Render();
338
339   detector.Attach(actor);
340
341   SignalData data;
342   GestureReceivedFunctor functor(data);
343   detector.DetectedSignal().Connect(&application, functor);
344
345   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 40.0f ), 140 ) );
346   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 40.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 150 ) );
347
348   application.GetPlatform().TriggerTimer();
349
350   application.ProcessEvent( GenerateDoubleTouch( PointState::STATIONARY, Vector2( 20.0f, 20.0f ), PointState::UP, Vector2( 20.0f, 90.0f ), 760 ) );
351   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 761 ) );
352
353   application.SendNotification();
354
355   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
356
357   END_TEST;
358 }
359
360 int UtcDaliLongPressGestureRecognizerMultipleDetectors(void)
361 {
362   TestApplication application;
363
364     Actor actor = Actor::New();
365     actor.SetSize(100.0f, 100.0f);
366     actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
367     Stage::GetCurrent().Add(actor);
368
369     Actor actor2 = Actor::New();
370     actor2.SetSize(100.0f, 100.0f);
371     actor2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
372     actor2.SetX(100.0f);
373     Stage::GetCurrent().Add(actor2);
374
375     // Render and notify
376     application.SendNotification();
377     application.Render();
378
379     LongPressGestureDetector detector = LongPressGestureDetector::New();
380     detector.Attach(actor);
381
382     LongPressGestureDetector detector2 = LongPressGestureDetector::New(2);
383     detector2.Attach(actor2);
384
385     SignalData data;
386     GestureReceivedFunctor functor(data);
387     detector.DetectedSignal().Connect(&application, functor);
388
389     SignalData data2;
390     GestureReceivedFunctor functor2(data2);
391     detector2.DetectedSignal().Connect(&application, functor2);
392
393     application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
394
395     application.GetPlatform().TriggerTimer();
396
397     application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 700 ) );
398
399     application.SendNotification();
400
401     DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
402     DALI_TEST_EQUALS(true, actor == data.pressedActor, TEST_LOCATION);
403     data.Reset();
404     DALI_TEST_EQUALS(false, data2.functorCalled, TEST_LOCATION);
405
406     application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 120.0f, 40.0f ), 800 ) );
407     application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 120.0f, 40.0f ), PointState::DOWN, Vector2( 120.0f, 90.0f ), 805 ) );
408
409     application.GetPlatform().TriggerTimer();
410
411     application.SendNotification();
412
413     DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
414     DALI_TEST_EQUALS(true, data2.functorCalled, TEST_LOCATION);
415
416     END_TEST;
417 }