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