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