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