[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-TapGestureRecognizer.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
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23 #include <dali/integration-api/render-task-list-integ.h>
24 #include <dali/internal/event/events/tap-gesture-event.h>
25 #include <dali-test-suite-utils.h>
26 #include <test-touch-utils.h>
27
28 using namespace Dali;
29
30 ///////////////////////////////////////////////////////////////////////////////
31 namespace
32 {
33
34 struct SignalData
35 {
36   SignalData()
37   : functorCalled(false),
38     voidFunctorCalled(false),
39     receivedGesture()
40   {}
41
42   void Reset()
43   {
44     functorCalled = false;
45     voidFunctorCalled = false;
46
47     receivedGesture.state = Gesture::Started;
48
49     tappedActor.Reset();
50   }
51
52   bool functorCalled;
53   bool voidFunctorCalled;
54   TapGesture receivedGesture;
55   Actor tappedActor;
56 };
57
58 // Functor that sets the data when called
59 struct GestureReceivedFunctor
60 {
61   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
62
63   void operator()(Actor actor, const TapGesture& tap)
64   {
65     signalData.functorCalled = true;
66     signalData.receivedGesture = tap;
67     signalData.tappedActor = actor;
68   }
69
70   void operator()()
71   {
72     signalData.voidFunctorCalled = true;
73   }
74
75   SignalData& signalData;
76 };
77
78
79 Integration::TouchEvent GenerateSingleTouch( PointState::Type state, const Vector2& screenPosition, uint32_t time )
80 {
81   Integration::TouchEvent touchEvent;
82   Integration::Point point;
83   point.SetState( state );
84   point.SetScreenPosition( screenPosition );
85   point.SetDeviceClass( Device::Class::TOUCH );
86   point.SetDeviceSubclass( Device::Subclass::NONE );
87   touchEvent.points.push_back( point );
88   touchEvent.time = time;
89   return touchEvent;
90 }
91
92 Integration::TouchEvent GenerateDoubleTouch( PointState::Type state, const Vector2& screenPositionA, const Vector2& screenPositionB, uint32_t time )
93 {
94   Integration::TouchEvent touchEvent;
95   Integration::Point point;
96   point.SetState( state );
97   point.SetScreenPosition( screenPositionA );
98   point.SetDeviceClass( Device::Class::TOUCH );
99   point.SetDeviceSubclass( Device::Subclass::NONE );
100   touchEvent.points.push_back( point );
101   point.SetScreenPosition( screenPositionB );
102   touchEvent.points.push_back( point );
103   touchEvent.time = time;
104   return touchEvent;
105 }
106
107
108 } // anon namespace
109
110 ///////////////////////////////////////////////////////////////////////////////
111
112
113 int UtcDaliTapGestureRecognizerBasic(void)
114 {
115   TestApplication application;
116
117   TapGestureDetector detector = TapGestureDetector::New();
118
119   Actor actor = Actor::New();
120   actor.SetSize(100.0f, 100.0f);
121   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
122   Stage::GetCurrent().Add(actor);
123
124   // Render and notify
125   application.SendNotification();
126   application.Render();
127
128   detector.Attach(actor);
129
130   SignalData data;
131   GestureReceivedFunctor functor(data);
132   detector.DetectedSignal().Connect(&application, functor);
133
134   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
135
136   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
137
138   application.SendNotification();
139
140   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
141
142   END_TEST;
143 }
144
145
146 int UtcDaliTapGestureRecognizerGapTooLong(void)
147 {
148   TestApplication application;
149
150   TapGestureDetector detector = TapGestureDetector::New();
151
152   Actor actor = Actor::New();
153   actor.SetSize(100.0f, 100.0f);
154   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
155   Stage::GetCurrent().Add(actor);
156
157   // Render and notify
158   application.SendNotification();
159   application.Render();
160
161   detector.Attach(actor);
162
163   SignalData data;
164   GestureReceivedFunctor functor(data);
165   detector.DetectedSignal().Connect(&application, functor);
166
167   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
168
169   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 651 ) );
170
171   application.SendNotification();
172
173   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
174
175   END_TEST;
176 }
177
178
179 int UtcDaliTapGestureRecognizerInterrupted(void)
180 {
181   TestApplication application;
182
183   TapGestureDetector detector = TapGestureDetector::New();
184
185   Actor actor = Actor::New();
186   actor.SetSize(100.0f, 100.0f);
187   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
188   Stage::GetCurrent().Add(actor);
189
190   // Render and notify
191   application.SendNotification();
192   application.Render();
193
194   detector.Attach(actor);
195
196   SignalData data;
197   GestureReceivedFunctor functor(data);
198   detector.DetectedSignal().Connect(&application, functor);
199
200   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
201
202   application.ProcessEvent( GenerateSingleTouch( PointState::INTERRUPTED, Vector2( 20.0f, 20.0f ), 175 ) );
203
204   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
205
206   application.SendNotification();
207
208   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
209
210   END_TEST;
211 }
212
213
214 int UtcDaliTapGestureRecognizerMoveTooFar(void)
215 {
216   TestApplication application;
217
218   TapGestureDetector detector = TapGestureDetector::New();
219
220   Actor actor = Actor::New();
221   actor.SetSize(100.0f, 100.0f);
222   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
223   Stage::GetCurrent().Add(actor);
224
225   // Render and notify
226   application.SendNotification();
227   application.Render();
228
229   detector.Attach(actor);
230
231   SignalData data;
232   GestureReceivedFunctor functor(data);
233   detector.DetectedSignal().Connect(&application, functor);
234
235   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
236
237   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 50.0f, 20.0f ), 200 ) );
238
239   application.SendNotification();
240
241   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
242
243   END_TEST;
244 }
245
246
247 int UtcDaliTapGestureRecognizerStartDouble(void)
248 {
249   TestApplication application;
250
251   TapGestureDetector detector = TapGestureDetector::New();
252
253   Actor actor = Actor::New();
254   actor.SetSize(100.0f, 100.0f);
255   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
256   Stage::GetCurrent().Add(actor);
257
258   // Render and notify
259   application.SendNotification();
260   application.Render();
261
262   detector.Attach(actor);
263
264   SignalData data;
265   GestureReceivedFunctor functor(data);
266   detector.DetectedSignal().Connect(&application, functor);
267
268   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), Vector2( 25.0f, 25.0f ), 150 ) );
269
270   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
271
272   application.SendNotification();
273
274   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
275
276   END_TEST;
277 }
278
279
280 int UtcDaliTapGestureRecognizerEndDouble(void)
281 {
282   TestApplication application;
283
284   TapGestureDetector detector = TapGestureDetector::New();
285
286   Actor actor = Actor::New();
287   actor.SetSize(100.0f, 100.0f);
288   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
289   Stage::GetCurrent().Add(actor);
290
291   // Render and notify
292   application.SendNotification();
293   application.Render();
294
295   detector.Attach(actor);
296
297   SignalData data;
298   GestureReceivedFunctor functor(data);
299   detector.DetectedSignal().Connect(&application, functor);
300
301   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
302
303   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), Vector2( 25.0f, 25.0f ), 200 ) );
304
305   application.ProcessEvent( GenerateDoubleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), Vector2( 25.0f, 25.0f ), 200 ) );
306
307   application.SendNotification();
308
309   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
310
311   END_TEST;
312 }
313
314
315 int UtcDaliTapGestureRecognizerDoubleTap(void)
316 {
317   TestApplication application;
318
319   TapGestureDetector detector = TapGestureDetector::New(2);
320
321   Actor actor = Actor::New();
322   actor.SetSize(100.0f, 100.0f);
323   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
324   Stage::GetCurrent().Add(actor);
325
326   // Render and notify
327   application.SendNotification();
328   application.Render();
329
330   detector.Attach(actor);
331
332   SignalData data;
333   GestureReceivedFunctor functor(data);
334   detector.DetectedSignal().Connect(&application, functor);
335
336   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
337
338   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
339
340   application.SendNotification();
341
342   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
343
344   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 250 ) );
345
346   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 300 ) );
347
348   application.SendNotification();
349
350   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
351
352   END_TEST;
353 }
354
355
356 int UtcDaliTapGestureRecognizerDoubleTapMoveTooFar(void)
357 {
358   TestApplication application;
359
360   TapGestureDetector detector = TapGestureDetector::New(2);
361
362   Actor actor = Actor::New();
363   actor.SetSize(100.0f, 100.0f);
364   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
365   Stage::GetCurrent().Add(actor);
366
367   // Render and notify
368   application.SendNotification();
369   application.Render();
370
371   detector.Attach(actor);
372
373   SignalData data;
374   GestureReceivedFunctor functor(data);
375   detector.DetectedSignal().Connect(&application, functor);
376
377   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
378
379   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
380
381   application.SendNotification();
382
383   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
384
385   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 50.0f ), 250 ) );
386
387   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 50.0f ), 300 ) );
388
389   application.SendNotification();
390
391   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
392
393   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 50.0f, 50.0f ), 450 ) );
394
395   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 50.0f, 50.0f ), 500 ) );
396
397   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 50.0f, 50.0f ), 550 ) );
398
399   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 50.0f, 50.0f ), 600 ) );
400
401   application.SendNotification();
402
403   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
404
405   END_TEST;
406 }
407
408
409 int UtcDaliTapGestureRecognizerDoubleTapWaitTooLong(void)
410 {
411   TestApplication application;
412
413   TapGestureDetector detector = TapGestureDetector::New(2);
414
415   Actor actor = Actor::New();
416   actor.SetSize(100.0f, 100.0f);
417   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
418   Stage::GetCurrent().Add(actor);
419
420   // Render and notify
421   application.SendNotification();
422   application.Render();
423
424   detector.Attach(actor);
425
426   SignalData data;
427   GestureReceivedFunctor functor(data);
428   detector.DetectedSignal().Connect(&application, functor);
429
430   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
431
432   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
433
434   application.SendNotification();
435
436   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
437
438   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 650 ) );
439
440   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 750 ) );
441
442   application.SendNotification();
443
444   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
445
446   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 50.0f, 50.0f ), 950 ) );
447
448   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 50.0f, 50.0f ), 1000 ) );
449
450   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 50.0f, 50.0f ), 1050 ) );
451
452   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 50.0f, 50.0f ), 1000 ) );
453
454   application.SendNotification();
455
456   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
457
458   END_TEST;
459 }
460
461
462 int UtcDaliTapGestureRecognizerMultipleDetectors(void)
463 {
464   TestApplication application;
465
466   Actor actor = Actor::New();
467   actor.SetSize(100.0f, 100.0f);
468   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
469   Stage::GetCurrent().Add(actor);
470
471   Actor actor2 = Actor::New();
472   actor2.SetSize(100.0f, 100.0f);
473   actor2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
474   actor2.SetX(100.0f);
475   Stage::GetCurrent().Add(actor2);
476
477   // Render and notify
478   application.SendNotification();
479   application.Render();
480
481   TapGestureDetector detector = TapGestureDetector::New();
482   detector.Attach(actor);
483
484   TapGestureDetector detector2 = TapGestureDetector::New(2);
485   detector2.Attach(actor2);
486
487   SignalData data;
488   GestureReceivedFunctor functor(data);
489   detector.DetectedSignal().Connect(&application, functor);
490
491   SignalData data2;
492   GestureReceivedFunctor functor2(data2);
493   detector2.DetectedSignal().Connect(&application, functor2);
494
495   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
496
497   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
498
499   application.SendNotification();
500
501   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
502   DALI_TEST_EQUALS(true, actor == data.tappedActor, TEST_LOCATION);
503   data.Reset();
504   DALI_TEST_EQUALS(false, data2.functorCalled, TEST_LOCATION);
505
506   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 120.0f, 20.0f ), 250 ) );
507
508   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 120.0f, 20.0f ), 300 ) );
509
510   application.SendNotification();
511
512   DALI_TEST_EQUALS(false, data2.functorCalled, TEST_LOCATION);
513
514   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 120.0f, 20.0f ), 350 ) );
515
516   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 120.0f, 20.0f ), 400 ) );
517
518   application.SendNotification();
519
520   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
521   DALI_TEST_EQUALS(true, data2.functorCalled, TEST_LOCATION);
522
523   END_TEST;
524 }