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