Saving a bit of memory by using pretty function instead of full file name
[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 UtcDaliGestureDetectorConstructorNegative(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 UtcDaliGestureDetectorConstructorPositive(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 UtcDaliGestureDetectorDownCast(void)
81 {
82   TestApplication application;
83   tet_infoline("Testing Dali::GestureDetector::DownCast()");
84
85   GestureDetector detector = PanGestureDetector::New();
86
87   BaseHandle object(detector);
88
89   GestureDetector detector2 = GestureDetector::DownCast(object);
90   DALI_TEST_CHECK(detector2);
91
92   GestureDetector detector3 = DownCast< GestureDetector >(object);
93   DALI_TEST_CHECK(detector3);
94
95   BaseHandle unInitializedObject;
96   GestureDetector detector4 = GestureDetector::DownCast(unInitializedObject);
97   DALI_TEST_CHECK(!detector4);
98
99   GestureDetector detector5 = DownCast< GestureDetector >(unInitializedObject);
100   DALI_TEST_CHECK(!detector5);
101   END_TEST;
102 }
103
104 int UtcDaliGestureDetectorAttachPositive(void)
105 {
106   TestApplication application;
107
108   // Use pan gesture as GestureDetector cannot be created.
109   GestureDetector detector = PanGestureDetector::New();
110
111   Actor actor = Actor::New();
112
113   detector.Attach(actor);
114
115   std::vector<Actor> actors = detector.GetAttachedActors();
116
117   if (find(actors.begin(), actors.end(), actor) != actors.end())
118   {
119     tet_result(TET_PASS);
120   }
121   else
122   {
123     tet_result(TET_FAIL);
124   }
125
126   // Scoped gesture detector. GestureDetectors connect to a destroy signal of the actor. If the
127   // actor is still alive when the gesture detector is destroyed, then it should disconnect from
128   // this signal.  If it does not, then when this function ends, there will be a segmentation fault
129   // thus, a TET case failure.
130   {
131     GestureDetector detector2 = PanGestureDetector::New();
132     detector2.Attach(actor);
133   }
134   END_TEST;
135 }
136
137 int UtcDaliGestureDetectorAttachNegative(void)
138 {
139   TestApplication application;
140
141   // Use pan gesture as GestureDetector cannot be created.
142   GestureDetector detector = PanGestureDetector::New();
143
144   // Do not initialise actor
145   Actor actor;
146
147   try
148   {
149     detector.Attach(actor);
150     tet_result(TET_FAIL);
151   }
152   catch (DaliException& e)
153   {
154     DALI_TEST_ASSERT( e, "actor", TEST_LOCATION );
155   }
156   END_TEST;
157 }
158
159 int UtcDaliGestureDetectorDetachPositive(void)
160 {
161   TestApplication application;
162
163   // Use pan gesture as GestureDetector cannot be created.
164   GestureDetector detector = PanGestureDetector::New();
165
166   Actor actor = Actor::New();
167   detector.Attach(actor);
168   std::vector<Actor> actors = detector.GetAttachedActors();
169
170   if (find(actors.begin(), actors.end(), actor) != actors.end())
171   {
172     tet_result(TET_PASS);
173   }
174   else
175   {
176     tet_result(TET_FAIL);
177   }
178
179   // Detach and retrieve attached actors again, the vector should be empty.
180   detector.Detach(actor);
181
182   actors = detector.GetAttachedActors();
183   if (actors.empty())
184   {
185     tet_result(TET_PASS);
186   }
187   else
188   {
189     tet_result(TET_FAIL);
190   }
191   END_TEST;
192 }
193
194 int UtcDaliGestureDetectorDetachNegative01(void)
195 {
196   TestApplication application;
197
198   // Use pan gesture as GestureDetector cannot be created.
199   GestureDetector detector = PanGestureDetector::New();
200
201   // Do not initialise actor
202   Actor actor;
203
204   try
205   {
206     detector.Detach(actor);
207     tet_result(TET_FAIL);
208   }
209   catch (DaliException& e)
210   {
211     DALI_TEST_ASSERT( e, "actor", TEST_LOCATION );
212   }
213   END_TEST;
214 }
215
216 int UtcDaliGestureDetectorDetachNegative02(void)
217 {
218   TestApplication application;
219
220   // Use pan gesture as GestureDetector cannot be created.
221   GestureDetector detector = PanGestureDetector::New();
222
223   Actor actor = Actor::New();
224   detector.Attach(actor);
225
226   // Detach an actor that hasn't been attached.  This should not cause an exception, it's a no-op.
227   try
228   {
229     Actor actor2 = Actor::New();
230     detector.Detach(actor2);
231     tet_result(TET_PASS);
232   }
233   catch (DaliException& e)
234   {
235     DALI_TEST_PRINT_ASSERT( e );
236     tet_result(TET_FAIL);
237   }
238   END_TEST;
239 }
240
241 int UtcDaliGestureDetectorDetachNegative03(void)
242 {
243   TestApplication application;
244   TestGestureManager& gestureManager = application.GetGestureManager();
245
246   // Use pan gesture as GestureDetector cannot be created.
247   GestureDetector detector = PanGestureDetector::New();
248
249   Actor actor = Actor::New();
250   detector.Attach(actor);
251
252   // Detach an actor twice - no exception should happen.
253   try
254   {
255     detector.Detach(actor);
256     DALI_TEST_EQUALS(true, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
257
258     gestureManager.Initialize(); // Reset values
259     detector.Detach(actor);
260     DALI_TEST_EQUALS(false, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
261   }
262   catch (DaliException& e)
263   {
264     DALI_TEST_PRINT_ASSERT( e );
265     tet_result(TET_FAIL);
266   }
267   END_TEST;
268 }
269
270 int UtcDaliGestureDetectorDetachAll(void)
271 {
272   TestApplication application;
273
274   // Use pan gesture as GestureDetector cannot be created.
275   GestureDetector detector = PanGestureDetector::New();
276
277   const unsigned int actorsToAdd = 5;
278   std::vector<Actor> myActors;
279
280   for (unsigned int i = 0; i < actorsToAdd; ++i)
281   {
282     Actor actor = Actor::New();
283     myActors.push_back(actor);
284     detector.Attach(actor);
285   }
286
287   std::vector<Actor> attachedActors = detector.GetAttachedActors();
288   DALI_TEST_EQUALS(actorsToAdd, attachedActors.size(), TEST_LOCATION);
289
290   // Detach and retrieve attached actors again, the vector should be empty.
291   detector.DetachAll();
292
293   attachedActors = detector.GetAttachedActors();
294   DALI_TEST_EQUALS(true, attachedActors.empty(), TEST_LOCATION);
295   END_TEST;
296 }
297
298 int UtcDaliGestureDetectorDetachAllNegative(void)
299 {
300   TestApplication application;
301   TestGestureManager& gestureManager = application.GetGestureManager();
302
303   // Use pan gesture as GestureDetector cannot be created.
304   GestureDetector detector = PanGestureDetector::New();
305
306   const unsigned int actorsToAdd = 5;
307   std::vector<Actor> myActors;
308
309   for (unsigned int i = 0; i < actorsToAdd; ++i)
310   {
311     Actor actor = Actor::New();
312     myActors.push_back(actor);
313     detector.Attach(actor);
314   }
315
316   std::vector<Actor> attachedActors = detector.GetAttachedActors();
317   DALI_TEST_EQUALS(actorsToAdd, attachedActors.size(), TEST_LOCATION);
318
319   // Detach and retrieve attached actors again, the vector should be empty.
320   detector.DetachAll();
321
322   attachedActors = detector.GetAttachedActors();
323   DALI_TEST_EQUALS(true, attachedActors.empty(), TEST_LOCATION);
324   DALI_TEST_EQUALS(true, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
325
326   // Call DetachAll again, there should not be any exception
327   try
328   {
329     gestureManager.Initialize(); // Reset values
330     detector.DetachAll();
331     DALI_TEST_EQUALS(false, gestureManager.WasCalled(TestGestureManager::UnregisterType), TEST_LOCATION);
332   }
333   catch (DaliException& e)
334   {
335     DALI_TEST_PRINT_ASSERT( e );
336     tet_result(TET_FAIL);
337   }
338   END_TEST;
339 }
340
341 int UtcDaliGestureDetectorGetAttachedActors(void)
342 {
343   TestApplication application;
344
345   // Use pan gesture as GestureDetector cannot be created.
346   GestureDetector detector = PanGestureDetector::New();
347
348   // Initially there should not be any actors.
349   DALI_TEST_EQUALS(0u, detector.GetAttachedActors().size(), TEST_LOCATION);
350
351   // Attach one actor
352   Actor actor1 = Actor::New();
353   detector.Attach(actor1);
354   DALI_TEST_EQUALS(1u, detector.GetAttachedActors().size(), TEST_LOCATION);
355
356   // Attach another actor
357   Actor actor2 = Actor::New();
358   detector.Attach(actor2);
359   DALI_TEST_EQUALS(2u, detector.GetAttachedActors().size(), TEST_LOCATION);
360
361   // Attach another five actors
362   std::vector<Actor> myActors;
363   for (unsigned int i = 0; i < 5; ++i)
364   {
365     Actor actor = Actor::New();
366     myActors.push_back(actor);
367     detector.Attach(actor);
368   }
369   DALI_TEST_EQUALS(7u, detector.GetAttachedActors().size(), TEST_LOCATION);
370
371   // Detach actor2
372   detector.Detach(actor2);
373   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
374
375   // Attach actor1 again, count should not increase.
376   detector.Attach(actor1);
377   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
378
379   // Detach actor2 again, count should not decrease.
380   detector.Detach(actor2);
381   DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
382
383   // Detach actor1.
384   detector.Detach(actor1);
385   DALI_TEST_EQUALS(5u, detector.GetAttachedActors().size(), TEST_LOCATION);
386
387   // Create scoped actor, actor should be automatically removed from the detector when it goes out
388   // of scope.
389   {
390     Actor scopedActor = Actor::New();
391     detector.Attach(scopedActor);
392     DALI_TEST_EQUALS(6u, detector.GetAttachedActors().size(), TEST_LOCATION);
393   }
394   DALI_TEST_EQUALS(5u, detector.GetAttachedActors().size(), TEST_LOCATION);
395
396   // Detach all so nothing remains.
397   detector.DetachAll();
398   DALI_TEST_EQUALS(0u, detector.GetAttachedActors().size(), TEST_LOCATION);
399   END_TEST;
400 }