Added Trace Functionality using Performance Server.
[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 // Used for testing BaseObject::GetTypeName with an object that is not registered
100 class FakeObject : public BaseObject
101 {
102 };
103 // used for testing ThisIsSaferThanReturningVoidStar
104 class FakeHandle :  public BaseHandle
105 {
106 public:
107
108   void RunTest()
109   {
110     return ThisIsSaferThanReturningVoidStar();
111   }
112 };
113 } // anon namespace
114
115 int UtcDaliBaseHandleConstructorVoid(void)
116 {
117   TestApplication application;
118   tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
119
120   BaseHandle object;
121
122   DALI_TEST_CHECK(!object);
123   END_TEST;
124 }
125
126 int UtcDaliBaseHandleCopyConstructor(void)
127 {
128   TestApplication application;
129   tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
130
131   // Initialize an object, ref count == 1
132   BaseHandle object = Actor::New();
133
134   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
135
136   // Copy the object, ref count == 2
137   BaseHandle copy(object);
138   DALI_TEST_CHECK(copy);
139   if (copy)
140   {
141     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
142   }
143
144   {
145     // Pass by value, and return another copy, ref count == 3
146     BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
147
148     DALI_TEST_CHECK(anotherCopy);
149     if (anotherCopy)
150     {
151       DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
152     }
153   }
154
155   // anotherCopy out of scope, ref count == 2
156   DALI_TEST_CHECK(copy);
157   if (copy)
158   {
159     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
160   }
161   END_TEST;
162 }
163
164 int UtcDaliBaseHandleAssignmentOperator(void)
165 {
166   TestApplication application;
167   tet_infoline("Testing Dali::BaseHandle::operator=");
168
169   BaseHandle object = Actor::New();
170
171   DALI_TEST_CHECK(object);
172   if (object)
173   {
174     DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
175   }
176
177   BaseHandle copy = object;
178
179   DALI_TEST_CHECK(copy);
180   if (copy)
181   {
182     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
183   }
184   END_TEST;
185 }
186
187 int UtcDaliBaseHandleGetBaseObject(void)
188 {
189   TestApplication application;
190   tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
191
192   BaseHandle object = Actor::New();
193
194   BaseObject& handle = object.GetBaseObject();
195
196   DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
197   END_TEST;
198 }
199
200 int UtcDaliBaseHandleReset(void)
201 {
202   TestApplication application;
203   tet_infoline("Testing Dali::BaseHandle::Reset()");
204
205   // Initialize an object, ref count == 1
206   BaseHandle object = Actor::New();
207
208   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
209
210   object.Reset();
211
212   DALI_TEST_CHECK(!object);
213   END_TEST;
214 }
215
216 int UtcDaliBaseHandleEqualityOperator01(void)
217 {
218   TestApplication application;
219   tet_infoline("Positive Test Dali::BaseHandle::operator==");
220
221   BaseHandle object = Actor::New();
222
223   DALI_TEST_CHECK(object);
224
225   BaseHandle theSameBaseHandle = object;
226
227   DALI_TEST_CHECK(object == theSameBaseHandle);
228   END_TEST;
229 }
230
231 int UtcDaliBaseHandleEqualityOperator02(void)
232 {
233   TestApplication application;
234   tet_infoline("Negative Test Dali::BaseHandle::operator==");
235
236   BaseHandle object = Actor::New();
237
238   DALI_TEST_CHECK(object);
239
240   BaseHandle aDifferentBaseHandle = Actor::New();
241
242   DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
243   END_TEST;
244 }
245
246 int UtcDaliBaseHandleInequalityOperator01(void)
247 {
248   TestApplication application;
249   tet_infoline("Positive Test Dali::BaseHandle::operator!=");
250
251   BaseHandle object = Actor::New();
252
253   DALI_TEST_CHECK(object);
254
255   BaseHandle aDifferentBaseHandle = Actor::New();
256
257   DALI_TEST_CHECK(object != aDifferentBaseHandle);
258   END_TEST;
259 }
260
261 int UtcDaliBaseHandleInequalityOperator02(void)
262 {
263   TestApplication application;
264   tet_infoline("Negative Test Dali::BaseHandle::operator!=");
265
266   BaseHandle object = Actor::New();
267
268   DALI_TEST_CHECK(object);
269
270   BaseHandle theSameBaseHandle = object;
271
272   DALI_TEST_CHECK(!(object != theSameBaseHandle));
273   END_TEST;
274 }
275
276 int UtcDaliBaseHandleStlVector(void)
277 {
278   TestApplication application;
279   tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
280
281   const int TargetVectorSize(5);
282
283   std::vector<Actor> myVector;
284
285   for (int i=0; i<TargetVectorSize; ++i)
286   {
287     Actor actor = Actor::New();
288
289     std::stringstream stream;
290     stream << "Actor " << i+1;
291     actor.SetName(stream.str());
292
293     myVector.push_back(actor);
294   }
295
296   DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
297
298   DALI_TEST_CHECK(myVector[0].GetName() == "Actor 1");
299   DALI_TEST_CHECK(myVector[1].GetName() == "Actor 2");
300   DALI_TEST_CHECK(myVector[2].GetName() == "Actor 3");
301   DALI_TEST_CHECK(myVector[3].GetName() == "Actor 4");
302   DALI_TEST_CHECK(myVector[4].GetName() == "Actor 5");
303   END_TEST;
304 }
305
306 int UtcDaliBaseHandleDoAction(void)
307 {
308   TestApplication application;
309   tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
310
311   Actor actor = Actor::New();
312   BaseHandle actorObject = actor;
313
314   DALI_TEST_CHECK(actorObject);
315
316   // Check that an invalid command is not performed
317   Property::Map attributes;
318   DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
319
320   // Check that the actor is visible
321   actor.SetVisible(true);
322   DALI_TEST_CHECK(actor.IsVisible() == true);
323
324   // Check the actor performed an action to hide itself
325   DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
326
327   // flush the queue and render once
328   application.SendNotification();
329   application.Render();
330
331   // Check that the actor is now invisible
332   DALI_TEST_CHECK(actor.IsVisible() == false);
333
334   // Check the actor performed an action to show itself
335   DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
336
337   // flush the queue and render once
338   application.SendNotification();
339   application.Render();
340
341   // Check that the actor is now visible
342   DALI_TEST_CHECK(actor.IsVisible() == true);
343
344   Stage::GetCurrent().Add(actor);
345
346   // Build an animation with initial duration of 1 second
347   float durationSeconds(1.0f);
348   Animation animation = Animation::New(durationSeconds);
349   BaseHandle animationObject = animation;
350
351   DALI_TEST_CHECK(animationObject);
352
353   // Check the current animation duration is 1 second
354   DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
355
356   Vector3 targetPosition(100.0f, 100.0f, 100.0f);
357   animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
358
359   // Set the new duration to be 2 seconds
360   float newDurationSeconds(2.0f);
361   Property::Value newDurationSecondsValue = Property::Value( newDurationSeconds );
362   attributes["duration"] = newDurationSecondsValue;
363
364   // Check the animation performed an action to play itself with the specified duration of 2 seconds
365   animationObject.DoAction("play", attributes);
366
367   bool signalReceived(false);
368   AnimationFinishCheck finishCheck(signalReceived);
369   animation.FinishedSignal().Connect(&application, finishCheck);
370
371   application.SendNotification();
372   application.Render(static_cast<unsigned int>(newDurationSeconds * 1000.0f) + 1u/*just beyond the animation duration*/);
373
374   // We expect the animation to finish
375   application.SendNotification();
376   finishCheck.CheckSignalReceived();
377   DALI_TEST_EQUALS( actor.GetCurrentPosition(), targetPosition, TEST_LOCATION );
378
379   // Check the new animation duration is 2 seconds
380   DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
381   END_TEST;
382 }
383
384
385 int UtcDaliBaseHandleConnectSignal(void)
386 {
387   TestApplication application;
388   tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
389
390   gTouchCallBackCalled = false;
391
392   // get the root layer
393   Actor actor = Actor::New();
394   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
395   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
396   actor.SetPosition( 240, 400 );
397   actor.SetSize( 100, 100 );
398
399   Stage::GetCurrent().Add( actor );
400
401   DALI_TEST_CHECK( gTouchCallBackCalled == false );
402
403   // connect to its touch signal
404   actor.ConnectSignal( &application, "touched", TestCallback() );
405
406   application.SendNotification();
407   application.Render(1000);
408   application.SendNotification();
409   application.Render(1000);
410
411   // simulate a touch event
412   Dali::Integration::Point point;
413   point.SetState( PointState::DOWN );
414   point.SetScreenPosition( Vector2( 240, 400 ) );
415   Dali::Integration::TouchEvent event;
416   event.AddPoint( point );
417   application.ProcessEvent( event );
418
419   application.SendNotification();
420   application.Render(1000);
421   application.SendNotification();
422   application.Render(1000);
423
424   DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
425   DALI_TEST_CHECK( gTouchCallBackCalled == true );
426
427   gTouchCallBackCalled = false;
428   application.DisconnectAll();
429
430   // simulate another touch event
431   application.ProcessEvent( event );
432
433   DALI_TEST_CHECK( gTouchCallBackCalled == false );
434   END_TEST;
435 }
436
437 int UtcDaliBaseHandleGetTypeNameP(void)
438 {
439   TestApplication application;
440   tet_infoline("Testing Dali::BaseHandle::GetTypeName");
441
442   // get the root layer
443   Actor actor = Actor::New();
444
445   std::string typeName = actor.GetTypeName();
446
447   DALI_TEST_CHECK( typeName.size() );
448   DALI_TEST_CHECK( typeName == std::string("Actor") );
449   END_TEST;
450 }
451
452 int UtcDaliBaseHandleGetTypeNameN(void)
453 {
454
455   TestApplication application;
456   tet_infoline("Testing Dali::BaseObject::GetTypeName");
457   FakeObject object;
458   std::string typeName = object.GetTypeName();
459
460   DALI_TEST_CHECK( typeName.empty() );
461   END_TEST;
462 }
463
464 int UtcDaliBaseHandleGetTypeInfoP(void)
465 {
466   TestApplication application;
467   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
468
469   Dali::TypeInfo info;
470   Actor actor = Actor::New();
471
472   bool ok = actor.GetTypeInfo( info );
473   DALI_TEST_CHECK( ok );
474   END_TEST;
475 }
476
477 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
478 {
479   TestApplication application;
480   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
481   FakeHandle handle;
482   handle.RunTest();
483   tet_result(TET_PASS);
484   END_TEST;
485
486 }
487
488 int UtcDaliBaseHandleGetTypeInfoN(void)
489 {
490   TestApplication application;
491   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
492
493   Dali::TypeInfo info;
494   FakeObject object;
495
496   bool ok = object.GetTypeInfo( info );
497   DALI_TEST_CHECK( !ok );
498   END_TEST;
499 }
500
501 int UtcDaliBaseHandleGetObjectPtr(void)
502 {
503   TestApplication application;
504   tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
505
506   // get the root layer
507   Actor actor = Actor::New();
508
509   Dali::RefObject* p = actor.GetObjectPtr();
510
511   DALI_TEST_CHECK( p != NULL );
512   END_TEST;
513 }
514
515 int UtcDaliBaseHandleBooleanCast(void)
516 {
517   TestApplication application;
518   tet_infoline("Testing Dali::BaseHandle::BooleanType");
519
520   // get the root layer
521   BaseHandle handle = Actor::New();
522
523   DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
524   END_TEST;
525 }
526
527 int UtcDaliBaseHandleCompareOperatorN(void)
528 {
529   TestApplication application;
530   BaseHandle handle1 = Actor::New();
531   BaseHandle handle2 = handle1;
532
533   DALI_TEST_CHECK( (handle1 < handle2) == false );
534
535   END_TEST;
536 }