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