UTC test coverage
[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   std::vector<Actor> actors = detector.GetAttachedActors();
141
142   if (find(actors.begin(), actors.end(), actor) != actors.end())
143   {
144     tet_result(TET_PASS);
145   }
146   else
147   {
148     tet_result(TET_FAIL);
149   }
150
151   // Scoped gesture detector. GestureDetectors connect to a destroy signal of the actor. If the
152   // actor is still alive when the gesture detector is destroyed, then it should disconnect from
153   // this signal.  If it does not, then when this function ends, there will be a segmentation fault
154   // thus, a TET case failure.
155   {
156     GestureDetector detector2 = PanGestureDetector::New();
157     detector2.Attach(actor);
158   }
159   END_TEST;
160 }
161
162 int UtcDaliGestureDetectorAttachN(void)
163 {
164   TestApplication application;
165
166   // Use pan gesture as GestureDetector cannot be created.
167   GestureDetector detector = PanGestureDetector::New();
168
169   // Do not initialise actor
170   Actor actor;
171
172   try
173   {
174     detector.Attach(actor);
175     tet_result(TET_FAIL);
176   }
177   catch (DaliException& e)
178   {
179     DALI_TEST_ASSERT( e, "actor", TEST_LOCATION );
180   }
181   END_TEST;
182 }
183
184 int UtcDaliGestureDetectorDetachP(void)
185 {
186   TestApplication application;
187
188   // Use pan gesture as GestureDetector cannot be created.
189   GestureDetector detector = PanGestureDetector::New();
190
191   Actor actor = Actor::New();
192   detector.Attach(actor);
193   std::vector<Actor> actors = detector.GetAttachedActors();
194
195   if (find(actors.begin(), actors.end(), actor) != actors.end())
196   {
197     tet_result(TET_PASS);
198   }
199   else
200   {
201     tet_result(TET_FAIL);
202   }
203
204   // Detach and retrieve attached actors again, the vector should be empty.
205   detector.Detach(actor);
206
207   actors = detector.GetAttachedActors();
208   if (actors.empty())
209   {
210     tet_result(TET_PASS);
211   }
212   else
213   {
214     tet_result(TET_FAIL);
215   }
216   END_TEST;
217 }
218
219 int UtcDaliGestureDetectorDetachN01(void)
220 {
221   TestApplication application;
222
223   // Use pan gesture as GestureDetector cannot be created.
224   GestureDetector detector = PanGestureDetector::New();
225
226   // Do not initialise actor
227   Actor actor;
228
229   try
230   {
231     detector.Detach(actor);
232     tet_result(TET_FAIL);
233   }
234   catch (DaliException& e)
235   {
236     DALI_TEST_ASSERT( e, "actor", TEST_LOCATION );
237   }
238   END_TEST;
239 }
240
241 int UtcDaliGestureDetectorDetachN02(void)
242 {
243   TestApplication application;
244
245   // Use pan gesture as GestureDetector cannot be created.
246   GestureDetector detector = PanGestureDetector::New();
247
248   Actor actor = Actor::New();
249   detector.Attach(actor);
250
251   // Detach an actor that hasn't been attached.  This should not cause an exception, it's a no-op.
252   try
253   {
254     Actor actor2 = Actor::New();
255     detector.Detach(actor2);
256     tet_result(TET_PASS);
257   }
258   catch (DaliException& e)
259   {
260     DALI_TEST_PRINT_ASSERT( e );
261     tet_result(TET_FAIL);
262   }
263   END_TEST;
264 }
265
266 int UtcDaliGestureDetectorDetachN03(void)
267 {
268   TestApplication application;
269   TestGestureManager& gestureManager = application.GetGestureManager();
270
271   // Use pan gesture as GestureDetector cannot be created.
272   GestureDetector detector = PanGestureDetector::New();
273
274   Actor actor = Actor::New();
275   detector.Attach(actor);
276
277   // Detach an actor twice - no exception should happen.
278   try
279   {
280     detector.Detach(actor);
281     DALI_TEST_EQUALS(true, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
282
283     gestureManager.Initialize(); // Reset values
284     detector.Detach(actor);
285     DALI_TEST_EQUALS(false, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
286   }
287   catch (DaliException& e)
288   {
289     DALI_TEST_PRINT_ASSERT( e );
290     tet_result(TET_FAIL);
291   }
292   END_TEST;
293 }
294
295 int UtcDaliGestureDetectorDetachAllP(void)
296 {
297   TestApplication application;
298
299   // Use pan gesture as GestureDetector cannot be created.
300   GestureDetector detector = PanGestureDetector::New();
301
302   const unsigned int actorsToAdd = 5;
303   std::vector<Actor> myActors;
304
305   for (unsigned int i = 0; i < actorsToAdd; ++i)
306   {
307     Actor actor = Actor::New();
308     myActors.push_back(actor);
309     detector.Attach(actor);
310   }
311
312   std::vector<Actor> attachedActors = detector.GetAttachedActors();
313   DALI_TEST_EQUALS(actorsToAdd, attachedActors.size(), TEST_LOCATION);
314
315   // Detach and retrieve attached actors again, the vector should be empty.
316   detector.DetachAll();
317
318   attachedActors = detector.GetAttachedActors();
319   DALI_TEST_EQUALS(true, attachedActors.empty(), TEST_LOCATION);
320   END_TEST;
321 }
322
323 int UtcDaliGestureDetectorDetachAllN(void)
324 {
325   TestApplication application;
326   TestGestureManager& gestureManager = application.GetGestureManager();
327
328   // Use pan gesture as GestureDetector cannot be created.
329   GestureDetector detector = PanGestureDetector::New();
330
331   const unsigned int actorsToAdd = 5;
332   std::vector<Actor> myActors;
333
334   for (unsigned int i = 0; i < actorsToAdd; ++i)
335   {
336     Actor actor = Actor::New();
337     myActors.push_back(actor);
338     detector.Attach(actor);
339   }
340
341   std::vector<Actor> attachedActors = detector.GetAttachedActors();
342   DALI_TEST_EQUALS(actorsToAdd, attachedActors.size(), TEST_LOCATION);
343
344   // Detach and retrieve attached actors again, the vector should be empty.
345   detector.DetachAll();
346
347   attachedActors = detector.GetAttachedActors();
348   DALI_TEST_EQUALS(true, attachedActors.empty(), TEST_LOCATION);
349   DALI_TEST_EQUALS(true, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
350
351   // Call DetachAll again, there should not be any exception
352   try
353   {
354     gestureManager.Initialize(); // Reset values
355     detector.DetachAll();
356     DALI_TEST_EQUALS(false, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
357   }
358   catch (DaliException& e)
359   {
360     DALI_TEST_PRINT_ASSERT( e );
361     tet_result(TET_FAIL);
362   }
363   END_TEST;
364 }
365
366 int UtcDaliGestureDetectorGetAttachedActors(void)
367 {
368   TestApplication application;
369
370   // Use pan gesture as GestureDetector cannot be created.
371   GestureDetector detector = PanGestureDetector::New();
372
373   // Initially there should not be any actors.
374   DALI_TEST_EQUALS(0u, detector.GetAttachedActors().size(), TEST_LOCATION);
375
376   // Attach one actor
377   Actor actor1 = Actor::New();
378   detector.Attach(actor1);
379   DALI_TEST_EQUALS(1u, detector.GetAttachedActors().size(), TEST_LOCATION);
380
381   // Attach another actor
382   Actor actor2 = Actor::New();
383   detector.Attach(actor2);
384   DALI_TEST_EQUALS(2u, detector.GetAttachedActors().size(), TEST_LOCATION);
385
386   // Attach another five actors
387   std::vector<Actor> myActors;
388   for (unsigned int i = 0; i < 5; ++i)
389   {
390     Actor actor = Actor::New();
391     myActors.push_back(actor);
392     detector.Attach(actor);
393   }
394   DALI_TEST_EQUALS(7u, detector.GetAttachedActors().size(), TEST_LOCATION);
395
396   // Detach actor2
397   detector.Detach(actor2);
398   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
399
400   // Attach actor1 again, count should not increase.
401   detector.Attach(actor1);
402   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
403
404   // Detach actor2 again, count should not decrease.
405   detector.Detach(actor2);
406   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
407
408   // Detach actor1.
409   detector.Detach(actor1);
410   DALI_TEST_EQUALS(5u, detector.GetAttachedActors().size(), TEST_LOCATION);
411
412   // Create scoped actor, actor should be automatically removed from the detector when it goes out
413   // of scope.
414   {
415     Actor scopedActor = Actor::New();
416     detector.Attach(scopedActor);
417     DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
418   }
419   DALI_TEST_EQUALS(5u, detector.GetAttachedActors().size(), TEST_LOCATION);
420
421   // Detach all so nothing remains.
422   detector.DetachAll();
423   DALI_TEST_EQUALS(0u, detector.GetAttachedActors().size(), TEST_LOCATION);
424   END_TEST;
425 }