[dali_1.0.36] Merge branch 'tizen'
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BaseHandle.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 <dali/public-api/dali-core.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23
24 #include "dali-test-suite-utils/dali-test-suite-utils.h"
25
26 using namespace Dali;
27
28
29 void utc_base_handle_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 // Called after each test
35 void utc_base_handle_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42
43 // Functor to test whether an animation finish signal is emitted
44 struct AnimationFinishCheck
45 {
46   AnimationFinishCheck(bool& signalReceived)
47   : mSignalReceived(signalReceived)
48   {
49   }
50
51   void operator()(Animation& animation)
52   {
53     mSignalReceived = true;
54   }
55
56   void Reset()
57   {
58     mSignalReceived = false;
59   }
60
61   void CheckSignalReceived()
62   {
63     if (!mSignalReceived)
64     {
65       tet_printf("Expected Finish signal was not received\n");
66       tet_result(TET_FAIL);
67     }
68     else
69     {
70       tet_result(TET_PASS);
71     }
72   }
73
74   bool& mSignalReceived; // owned by individual tests
75 };
76
77 BaseHandle ImplicitCopyConstructor(BaseHandle passedByValue)
78 {
79   // object + copy + passedByValue, ref count == 3
80   DALI_TEST_CHECK(passedByValue);
81   if (passedByValue)
82   {
83     DALI_TEST_EQUALS(3, passedByValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
84   }
85
86   return passedByValue;
87 }
88
89 static bool gTouchCallBackCalled;
90
91 struct TestCallback
92 {
93   void operator()()
94   {
95     gTouchCallBackCalled = true;
96   }
97 };
98
99 } // anon namespace
100
101 int UtcDaliBaseHandleConstructorVoid(void)
102 {
103   TestApplication application;
104   tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
105
106   BaseHandle object;
107
108   DALI_TEST_CHECK(!object);
109   END_TEST;
110 }
111
112
113 int UtcDaliBaseHandleCopyConstructor(void)
114 {
115   TestApplication application;
116   tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
117
118   // Initialize an object, ref count == 1
119   BaseHandle object = Actor::New();
120
121   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
122
123   // Copy the object, ref count == 2
124   BaseHandle copy(object);
125   DALI_TEST_CHECK(copy);
126   if (copy)
127   {
128     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
129   }
130
131   {
132     // Pass by value, and return another copy, ref count == 3
133     BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
134
135     DALI_TEST_CHECK(anotherCopy);
136     if (anotherCopy)
137     {
138       DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
139     }
140   }
141
142   // anotherCopy out of scope, ref count == 2
143   DALI_TEST_CHECK(copy);
144   if (copy)
145   {
146     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
147   }
148   END_TEST;
149 }
150
151 int UtcDaliBaseHandleAssignmentOperator(void)
152 {
153   TestApplication application;
154   tet_infoline("Testing Dali::BaseHandle::operator=");
155
156   BaseHandle object = Actor::New();
157
158   DALI_TEST_CHECK(object);
159   if (object)
160   {
161     DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
162   }
163
164   BaseHandle copy = object;
165
166   DALI_TEST_CHECK(copy);
167   if (copy)
168   {
169     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
170   }
171   END_TEST;
172 }
173
174 int UtcDaliBaseHandleGetBaseObject(void)
175 {
176   TestApplication application;
177   tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
178
179   BaseHandle object = Actor::New();
180
181   BaseObject& handle = object.GetBaseObject();
182
183   DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
184   END_TEST;
185 }
186
187 int UtcDaliBaseHandleReset(void)
188 {
189   TestApplication application;
190   tet_infoline("Testing Dali::BaseHandle::Reset()");
191
192   // Initialize an object, ref count == 1
193   BaseHandle object = Actor::New();
194
195   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
196
197   object.Reset();
198
199   DALI_TEST_CHECK(!object);
200   END_TEST;
201 }
202
203 int UtcDaliBaseHandleEqualityOperator01(void)
204 {
205   TestApplication application;
206   tet_infoline("Positive Test Dali::BaseHandle::operator==");
207
208   BaseHandle object = Actor::New();
209
210   DALI_TEST_CHECK(object);
211
212   BaseHandle theSameBaseHandle = object;
213
214   DALI_TEST_CHECK(object == theSameBaseHandle);
215   END_TEST;
216 }
217
218 int UtcDaliBaseHandleEqualityOperator02(void)
219 {
220   TestApplication application;
221   tet_infoline("Negative Test Dali::BaseHandle::operator==");
222
223   BaseHandle object = Actor::New();
224
225   DALI_TEST_CHECK(object);
226
227   BaseHandle aDifferentBaseHandle = Actor::New();
228
229   DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
230   END_TEST;
231 }
232
233 int UtcDaliBaseHandleInequalityOperator01(void)
234 {
235   TestApplication application;
236   tet_infoline("Positive Test Dali::BaseHandle::operator!=");
237
238   BaseHandle object = Actor::New();
239
240   DALI_TEST_CHECK(object);
241
242   BaseHandle aDifferentBaseHandle = Actor::New();
243
244   DALI_TEST_CHECK(object != aDifferentBaseHandle);
245   END_TEST;
246 }
247
248 int UtcDaliBaseHandleInequalityOperator02(void)
249 {
250   TestApplication application;
251   tet_infoline("Negative Test Dali::BaseHandle::operator!=");
252
253   BaseHandle object = Actor::New();
254
255   DALI_TEST_CHECK(object);
256
257   BaseHandle theSameBaseHandle = object;
258
259   DALI_TEST_CHECK(!(object != theSameBaseHandle));
260   END_TEST;
261 }
262
263 int UtcDaliBaseHandleStlVector(void)
264 {
265   TestApplication application;
266   tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
267
268   const int TargetVectorSize(5);
269
270   std::vector<Actor> myVector;
271
272   for (int i=0; i<TargetVectorSize; ++i)
273   {
274     Actor actor = Actor::New();
275
276     std::stringstream stream;
277     stream << "Actor " << i+1;
278     actor.SetName(stream.str());
279
280     myVector.push_back(actor);
281   }
282
283   DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
284
285   DALI_TEST_CHECK(myVector[0].GetName() == "Actor 1");
286   DALI_TEST_CHECK(myVector[1].GetName() == "Actor 2");
287   DALI_TEST_CHECK(myVector[2].GetName() == "Actor 3");
288   DALI_TEST_CHECK(myVector[3].GetName() == "Actor 4");
289   DALI_TEST_CHECK(myVector[4].GetName() == "Actor 5");
290   END_TEST;
291 }
292
293 int UtcDaliBaseHandleDoAction(void)
294 {
295   TestApplication application;
296   tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
297
298   Actor actor = Actor::New();
299   BaseHandle actorObject = actor;
300
301   DALI_TEST_CHECK(actorObject);
302
303   // Check that an invalid command is not performed
304   std::vector<Property::Value> attributes;
305   DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
306
307   // Check that the actor is visible
308   actor.SetVisible(true);
309   DALI_TEST_CHECK(actor.IsVisible() == true);
310
311   // Check the actor performed an action to hide itself
312   DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
313
314   // flush the queue and render once
315   application.SendNotification();
316   application.Render();
317
318   // Check that the actor is now invisible
319   DALI_TEST_CHECK(actor.IsVisible() == false);
320
321   // Check the actor performed an action to show itself
322   DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
323
324   // flush the queue and render once
325   application.SendNotification();
326   application.Render();
327
328   // Check that the actor is now visible
329   DALI_TEST_CHECK(actor.IsVisible() == true);
330
331   Stage::GetCurrent().Add(actor);
332
333   // Build an animation with initial duration of 1 second
334   float durationSeconds(1.0f);
335   Animation animation = Animation::New(durationSeconds);
336   BaseHandle animationObject = animation;
337
338   DALI_TEST_CHECK(animationObject);
339
340   // Check the current animation duration is 1 second
341   DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
342
343   Vector3 targetPosition(100.0f, 100.0f, 100.0f);
344   animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunctions::Linear);
345
346   // Set the new duration to be 2 seconds
347   float newDurationSeconds(2.0f);
348   Property::Value newDurationSecondsValue = Property::Value( newDurationSeconds );
349   attributes.push_back(newDurationSecondsValue);
350
351   // Check the animation performed an action to play itself with the specified duration of 2 seconds
352   animationObject.DoAction("play", attributes);
353
354   bool signalReceived(false);
355   AnimationFinishCheck finishCheck(signalReceived);
356   animation.FinishedSignal().Connect(&application, finishCheck);
357
358   application.SendNotification();
359   application.Render(static_cast<unsigned int>(newDurationSeconds * 1000.0f) + 1u/*just beyond the animation duration*/);
360
361   // We expect the animation to finish
362   application.SendNotification();
363   finishCheck.CheckSignalReceived();
364   DALI_TEST_EQUALS( actor.GetCurrentPosition(), targetPosition, TEST_LOCATION );
365
366   // Check the new animation duration is 2 seconds
367   DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
368   END_TEST;
369 }
370
371
372 int UtcDaliBaseHandleConnectSignal(void)
373 {
374   TestApplication application;
375   tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
376
377   gTouchCallBackCalled = false;
378
379   // get the root layer
380   Actor actor = Actor::New();
381   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
382   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
383   actor.SetPosition( 240, 400 );
384   actor.SetSize( 100, 100 );
385
386   Stage::GetCurrent().Add( actor );
387
388   DALI_TEST_CHECK( gTouchCallBackCalled == false );
389
390   // connect to its touch signal
391   actor.ConnectSignal( &application, "touched", TestCallback() );
392
393   application.SendNotification();
394   application.Render(1000);
395   application.SendNotification();
396   application.Render(1000);
397
398   // simulate a touch event
399   Dali::TouchPoint point( 0, TouchPoint::Down, 240, 400  );
400   Dali::Integration::TouchEvent event;
401   event.AddPoint( point );
402   application.ProcessEvent( event );
403
404   application.SendNotification();
405   application.Render(1000);
406   application.SendNotification();
407   application.Render(1000);
408
409   DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
410   DALI_TEST_CHECK( gTouchCallBackCalled == true );
411
412   gTouchCallBackCalled = false;
413   application.DisconnectAll();
414
415   // simulate another touch event
416   application.ProcessEvent( event );
417
418   DALI_TEST_CHECK( gTouchCallBackCalled == false );
419   END_TEST;
420 }
421
422 int UtcDaliBaseHandleGetTypeName(void)
423 {
424   TestApplication application;
425   tet_infoline("Testing Dali::BaseHandle::GetTypeName");
426
427   // get the root layer
428   Actor actor = Actor::New();
429
430   std::string typeName = actor.GetTypeName();
431
432   DALI_TEST_CHECK( typeName.size() );
433   DALI_TEST_CHECK( typeName == std::string("Actor") );
434   END_TEST;
435 }
436
437 int UtcDaliBaseHandleGetObjectPtr(void)
438 {
439   TestApplication application;
440   tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
441
442   // get the root layer
443   Actor actor = Actor::New();
444
445   Dali::RefObject* p = actor.GetObjectPtr();
446
447   DALI_TEST_CHECK( p != NULL );
448   END_TEST;
449 }
450
451 int UtcDaliBaseHandleBooleanCast(void)
452 {
453   TestApplication application;
454   tet_infoline("Testing Dali::BaseHandle::BooleanType");
455
456   // get the root layer
457   BaseHandle handle = Actor::New();
458
459   DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
460   END_TEST;
461 }