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