Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-GestureDetector.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/public-api/dali-core.h>
20 #include <stdlib.h>
21
22 #include <algorithm>
23 #include <iostream>
24
25 using namespace Dali;
26
27 void utc_dali_gesture_detector_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_gesture_detector_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 int UtcDaliGestureDetectorConstructorN(void)
38 {
39   TestApplication application;
40
41   GestureDetector detector;
42
43   Actor actor = Actor::New();
44
45   try
46   {
47     detector.Attach(actor);
48     tet_result(TET_FAIL);
49   }
50   catch(DaliException& e)
51   {
52     DALI_TEST_ASSERT(e, "detector", TEST_LOCATION);
53   }
54   END_TEST;
55 }
56
57 int UtcDaliGestureDetectorConstructorP(void)
58 {
59   TestApplication application;
60
61   // Use pan gesture as GestureDetector cannot be created.
62   GestureDetector detector = PanGestureDetector::New();
63
64   Actor actor = Actor::New();
65
66   try
67   {
68     detector.Attach(actor);
69     tet_result(TET_PASS);
70   }
71   catch(DaliException& e)
72   {
73     DALI_TEST_PRINT_ASSERT(e);
74     tet_result(TET_FAIL);
75   }
76
77   GestureDetector moved = std::move(detector);
78   DALI_TEST_CHECK(moved);
79   DALI_TEST_CHECK(!detector);
80
81   END_TEST;
82 }
83
84 int UtcDaliGestureDetectorAssignP(void)
85 {
86   TestApplication application;
87
88   // Use pan gesture as GestureDetector cannot be created.
89   GestureDetector detector = PanGestureDetector::New();
90   GestureDetector detector2;
91
92   detector2 = detector;
93
94   Actor actor = Actor::New();
95
96   try
97   {
98     detector2.Attach(actor);
99     tet_result(TET_PASS);
100   }
101   catch(DaliException& e)
102   {
103     DALI_TEST_PRINT_ASSERT(e);
104     tet_result(TET_FAIL);
105   }
106
107   GestureDetector moved;
108   moved = std::move(detector2);
109   DALI_TEST_CHECK(moved);
110   DALI_TEST_CHECK(detector);
111   DALI_TEST_CHECK(!detector2);
112   END_TEST;
113 }
114
115 int UtcDaliGestureDetectorDownCastP(void)
116 {
117   TestApplication application;
118   tet_infoline("Testing Dali::GestureDetector::DownCast()");
119
120   GestureDetector detector = PanGestureDetector::New();
121
122   BaseHandle object(detector);
123
124   GestureDetector detector2 = GestureDetector::DownCast(object);
125   DALI_TEST_CHECK(detector2);
126
127   GestureDetector detector3 = DownCast<GestureDetector>(object);
128   DALI_TEST_CHECK(detector3);
129
130   BaseHandle      unInitializedObject;
131   GestureDetector detector4 = GestureDetector::DownCast(unInitializedObject);
132   DALI_TEST_CHECK(!detector4);
133
134   GestureDetector detector5 = DownCast<GestureDetector>(unInitializedObject);
135   DALI_TEST_CHECK(!detector5);
136   END_TEST;
137 }
138
139 int UtcDaliGestureDetectorAttachP(void)
140 {
141   TestApplication application;
142
143   // Use pan gesture as GestureDetector cannot be created.
144   GestureDetector detector = PanGestureDetector::New();
145
146   Actor actor = Actor::New();
147
148   detector.Attach(actor);
149
150   bool found = false;
151   for(size_t i = 0; i < detector.GetAttachedActorCount(); i++)
152   {
153     if(detector.GetAttachedActor(i) == actor)
154     {
155       tet_result(TET_PASS);
156       found = true;
157     }
158   }
159
160   if(!found)
161   {
162     tet_result(TET_FAIL);
163   }
164
165   // Scoped gesture detector. GestureDetectors connect to a destroy signal of the actor. If the
166   // actor is still alive when the gesture detector is destroyed, then it should disconnect from
167   // this signal.  If it does not, then when this function ends, there will be a segmentation fault
168   // thus, a TET case failure.
169   {
170     GestureDetector detector2 = PanGestureDetector::New();
171     detector2.Attach(actor);
172   }
173   END_TEST;
174 }
175
176 int UtcDaliGestureDetectorAttachN(void)
177 {
178   TestApplication application;
179
180   // Use pan gesture as GestureDetector cannot be created.
181   GestureDetector detector = PanGestureDetector::New();
182
183   // Do not initialise actor
184   Actor actor;
185
186   try
187   {
188     detector.Attach(actor);
189     tet_result(TET_FAIL);
190   }
191   catch(DaliException& e)
192   {
193     DALI_TEST_ASSERT(e, "actor", TEST_LOCATION);
194   }
195   END_TEST;
196 }
197
198 int UtcDaliGestureDetectorDetachP(void)
199 {
200   TestApplication application;
201
202   // Use pan gesture as GestureDetector cannot be created.
203   GestureDetector detector = PanGestureDetector::New();
204
205   Actor actor = Actor::New();
206   detector.Attach(actor);
207
208   bool found = false;
209   for(size_t i = 0; i < detector.GetAttachedActorCount(); i++)
210   {
211     if(detector.GetAttachedActor(i) == actor)
212     {
213       tet_result(TET_PASS);
214       found = true;
215     }
216   }
217
218   if(!found)
219   {
220     tet_result(TET_FAIL);
221   }
222
223   // Detach and retrieve attached actors again, the vector should be empty.
224   detector.Detach(actor);
225
226   found = false;
227   for(size_t i = 0; i < detector.GetAttachedActorCount(); i++)
228   {
229     if(detector.GetAttachedActor(i) == actor)
230     {
231       found = true;
232     }
233   }
234
235   if(found)
236   {
237     tet_result(TET_FAIL);
238   }
239   else
240   {
241     tet_result(TET_PASS);
242   }
243
244   END_TEST;
245 }
246
247 int UtcDaliGestureDetectorDetachN01(void)
248 {
249   TestApplication application;
250
251   // Use pan gesture as GestureDetector cannot be created.
252   GestureDetector detector = PanGestureDetector::New();
253
254   // Do not initialise actor
255   Actor actor;
256
257   try
258   {
259     detector.Detach(actor);
260     tet_result(TET_FAIL);
261   }
262   catch(DaliException& e)
263   {
264     DALI_TEST_ASSERT(e, "actor", TEST_LOCATION);
265   }
266   END_TEST;
267 }
268
269 int UtcDaliGestureDetectorDetachN02(void)
270 {
271   TestApplication application;
272
273   // Use pan gesture as GestureDetector cannot be created.
274   GestureDetector detector = PanGestureDetector::New();
275
276   Actor actor = Actor::New();
277   detector.Attach(actor);
278
279   // Detach an actor that hasn't been attached.  This should not cause an exception, it's a no-op.
280   try
281   {
282     Actor actor2 = Actor::New();
283     detector.Detach(actor2);
284     tet_result(TET_PASS);
285   }
286   catch(DaliException& e)
287   {
288     DALI_TEST_PRINT_ASSERT(e);
289     tet_result(TET_FAIL);
290   }
291   END_TEST;
292 }
293
294 int UtcDaliGestureDetectorDetachN03(void)
295 {
296   TestApplication application;
297
298   // Use pan gesture as GestureDetector cannot be created.
299   GestureDetector detector = PanGestureDetector::New();
300
301   Actor actor = Actor::New();
302   detector.Attach(actor);
303
304   DALI_TEST_EQUALS(1, detector.GetAttachedActorCount(), TEST_LOCATION);
305
306   // Detach an actor twice - no exception should happen.
307   try
308   {
309     detector.Detach(actor);
310     detector.Detach(actor);
311   }
312   catch(DaliException& e)
313   {
314     DALI_TEST_PRINT_ASSERT(e);
315     tet_result(TET_FAIL);
316   }
317
318   DALI_TEST_EQUALS(0, detector.GetAttachedActorCount(), TEST_LOCATION);
319
320   END_TEST;
321 }
322
323 int UtcDaliGestureDetectorDetachAllP(void)
324 {
325   TestApplication application;
326
327   // Use pan gesture as GestureDetector cannot be created.
328   GestureDetector detector = PanGestureDetector::New();
329
330   const unsigned int actorsToAdd = 5;
331   std::vector<Actor> myActors;
332
333   for(unsigned int i = 0; i < actorsToAdd; ++i)
334   {
335     Actor actor = Actor::New();
336     myActors.push_back(actor);
337     detector.Attach(actor);
338   }
339
340   DALI_TEST_EQUALS(actorsToAdd, detector.GetAttachedActorCount(), TEST_LOCATION);
341
342   // Detach and retrieve attached actors again, the vector should be empty.
343   detector.DetachAll();
344
345   DALI_TEST_EQUALS(0u, detector.GetAttachedActorCount(), TEST_LOCATION);
346   END_TEST;
347 }
348
349 int UtcDaliGestureDetectorDetachAllN(void)
350 {
351   TestApplication application;
352
353   // Use pan gesture as GestureDetector cannot be created.
354   GestureDetector detector = PanGestureDetector::New();
355
356   const unsigned int actorsToAdd = 5;
357   std::vector<Actor> myActors;
358
359   for(unsigned int i = 0; i < actorsToAdd; ++i)
360   {
361     Actor actor = Actor::New();
362     myActors.push_back(actor);
363     detector.Attach(actor);
364   }
365
366   DALI_TEST_EQUALS(actorsToAdd, detector.GetAttachedActorCount(), TEST_LOCATION);
367
368   // Detach and retrieve attached actors again, the vector should be empty.
369   detector.DetachAll();
370
371   DALI_TEST_EQUALS(0u, detector.GetAttachedActorCount(), TEST_LOCATION);
372
373   // Call DetachAll again, there should not be any exception
374   try
375   {
376     detector.DetachAll();
377   }
378   catch(DaliException& e)
379   {
380     DALI_TEST_PRINT_ASSERT(e);
381     tet_result(TET_FAIL);
382   }
383   END_TEST;
384 }
385
386 int UtcDaliGestureDetectorGetAttachedActors(void)
387 {
388   TestApplication application;
389
390   // Use pan gesture as GestureDetector cannot be created.
391   GestureDetector detector = PanGestureDetector::New();
392
393   // Initially there should not be any actors.
394   DALI_TEST_EQUALS(0u, detector.GetAttachedActorCount(), TEST_LOCATION);
395
396   // Attach one actor
397   Actor actor1 = Actor::New();
398   detector.Attach(actor1);
399   DALI_TEST_EQUALS(1u, detector.GetAttachedActorCount(), TEST_LOCATION);
400
401   // Attach another actor
402   Actor actor2 = Actor::New();
403   detector.Attach(actor2);
404   DALI_TEST_EQUALS(2u, detector.GetAttachedActorCount(), TEST_LOCATION);
405
406   // Attach another five actors
407   std::vector<Actor> myActors;
408   for(unsigned int i = 0; i < 5; ++i)
409   {
410     Actor actor = Actor::New();
411     myActors.push_back(actor);
412     detector.Attach(actor);
413   }
414   DALI_TEST_EQUALS(7u, detector.GetAttachedActorCount(), TEST_LOCATION);
415
416   // Detach actor2
417   detector.Detach(actor2);
418   DALI_TEST_EQUALS(6u, detector.GetAttachedActorCount(), TEST_LOCATION);
419
420   // Attach actor1 again, count should not increase.
421   detector.Attach(actor1);
422   DALI_TEST_EQUALS(6u, detector.GetAttachedActorCount(), TEST_LOCATION);
423
424   // Detach actor2 again, count should not decrease.
425   detector.Detach(actor2);
426   DALI_TEST_EQUALS(6u, detector.GetAttachedActorCount(), TEST_LOCATION);
427
428   // Detach actor1.
429   detector.Detach(actor1);
430   DALI_TEST_EQUALS(5u, detector.GetAttachedActorCount(), TEST_LOCATION);
431
432   // Create scoped actor, actor should be automatically removed from the detector when it goes out
433   // of scope.
434   {
435     Actor scopedActor = Actor::New();
436     detector.Attach(scopedActor);
437     DALI_TEST_EQUALS(6u, detector.GetAttachedActorCount(), TEST_LOCATION);
438   }
439   DALI_TEST_EQUALS(5u, detector.GetAttachedActorCount(), TEST_LOCATION);
440
441   // Detach all so nothing remains.
442   detector.DetachAll();
443   DALI_TEST_EQUALS(0u, detector.GetAttachedActorCount(), TEST_LOCATION);
444   END_TEST;
445 }
446
447 int UtcDaliGestureDetectorProperties(void)
448 {
449   TestApplication application;
450
451   // Use pinch gesture as that doen't currently have any properties. Will need to change it if default properties are added.
452   GestureDetector detector = PinchGestureDetector::New();
453
454   DALI_TEST_EQUALS(detector.GetPropertyCount(), 0u, TEST_LOCATION);
455
456   Property::IndexContainer indices;
457   detector.GetPropertyIndices(indices);
458   DALI_TEST_EQUALS(indices.Size(), 0u, TEST_LOCATION);
459
460   DALI_TEST_EQUALS(detector.IsPropertyWritable(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX), false, TEST_LOCATION);
461   DALI_TEST_EQUALS(detector.IsPropertyAnimatable(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX), false, TEST_LOCATION);
462   DALI_TEST_EQUALS(detector.IsPropertyAConstraintInput(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX), false, TEST_LOCATION);
463   DALI_TEST_EQUALS(detector.GetPropertyType(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX), Property::NONE, TEST_LOCATION);
464   DALI_TEST_EQUALS(detector.GetPropertyIndex("InvalidIndex"), Property::INVALID_INDEX, TEST_LOCATION);
465
466   Property::Value propValue = detector.GetProperty(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX);
467   DALI_TEST_EQUALS(propValue.GetType(), Property::NONE, TEST_LOCATION);
468
469   DALI_TEST_CHECK(detector.GetPropertyName(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX).empty());
470
471   // For coverage only, not testable
472   detector.SetProperty(DEFAULT_GESTURE_DETECTOR_PROPERTY_START_INDEX, true);
473
474   END_TEST;
475 }
476
477 int UtcDaliGestureDetectorRegisterProperty(void)
478 {
479   TestApplication application;
480
481   GestureDetector detector = PinchGestureDetector::New();
482
483   Property::Index index = detector.RegisterProperty("sceneProperty", 0);
484   DALI_TEST_EQUALS(index, (Property::Index)PROPERTY_CUSTOM_START_INDEX, TEST_LOCATION);
485   DALI_TEST_EQUALS(detector.GetProperty<int32_t>(index), 0, TEST_LOCATION);
486
487   detector.SetProperty(index, -123);
488   DALI_TEST_EQUALS(detector.GetProperty<int32_t>(index), -123, TEST_LOCATION);
489
490   using Dali::Animation;
491   Animation animation = Animation::New(1.0f);
492   animation.AnimateTo(Property(detector, index), 99);
493
494   DALI_TEST_EQUALS(detector.GetProperty<int32_t>(index), -123, TEST_LOCATION);
495   // Start the animation
496   animation.Play();
497
498   application.SendNotification();
499   application.Render(1000 /* 100% progress */);
500   DALI_TEST_EQUALS(detector.GetProperty<int32_t>(index), 99, TEST_LOCATION);
501
502   END_TEST;
503 }