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