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