121c7bb18a2d5bdd5a4cee621e126b8c8fa9faab
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BaseHandle.cpp
1 /*
2  * Copyright (c) 2020 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()()
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 UtcDaliBaseHandleMoveConstructor(void)
188 {
189   TestApplication application;
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   // Move the object, ref count == 1
197   BaseHandle move = std::move( object );
198   DALI_TEST_CHECK( move );
199
200   // Check that object is moved (not copied, so ref count keeps the same)
201   if ( move )
202   {
203     DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
204   }
205   DALI_TEST_CHECK( !object );
206
207   END_TEST;
208 }
209
210 int UtcDaliBaseHandleMoveAssignment(void)
211 {
212   TestApplication application;
213
214   // Initialize an object, ref count == 1
215   BaseHandle object = Actor::New();
216
217   DALI_TEST_EQUALS( 1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION );
218
219   // Move the object, ref count == 1
220   BaseHandle move;
221   move = std::move( object );
222   DALI_TEST_CHECK( move );
223
224   // Check that object is moved (not copied, so ref count keeps the same)
225   if ( move )
226   {
227     DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
228   }
229   DALI_TEST_CHECK( !object );
230
231   END_TEST;
232 }
233
234 int UtcDaliBaseHandleGetBaseObject(void)
235 {
236   TestApplication application;
237   tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
238
239   BaseHandle object = Actor::New();
240
241   BaseObject& handle = object.GetBaseObject();
242
243   DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
244   END_TEST;
245 }
246
247 int UtcDaliBaseHandleReset(void)
248 {
249   TestApplication application;
250   tet_infoline("Testing Dali::BaseHandle::Reset()");
251
252   // Initialize an object, ref count == 1
253   BaseHandle object = Actor::New();
254
255   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
256
257   object.Reset();
258
259   DALI_TEST_CHECK(!object);
260   END_TEST;
261 }
262
263 int UtcDaliBaseHandleEqualityOperator01(void)
264 {
265   TestApplication application;
266   tet_infoline("Positive Test Dali::BaseHandle::operator==");
267
268   BaseHandle object = Actor::New();
269
270   DALI_TEST_CHECK(object);
271
272   BaseHandle theSameBaseHandle = object;
273
274   DALI_TEST_CHECK(object == theSameBaseHandle);
275   END_TEST;
276 }
277
278 int UtcDaliBaseHandleEqualityOperator02(void)
279 {
280   TestApplication application;
281   tet_infoline("Negative Test Dali::BaseHandle::operator==");
282
283   BaseHandle object = Actor::New();
284
285   DALI_TEST_CHECK(object);
286
287   BaseHandle aDifferentBaseHandle = Actor::New();
288
289   DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
290   END_TEST;
291 }
292
293 int UtcDaliBaseHandleInequalityOperator01(void)
294 {
295   TestApplication application;
296   tet_infoline("Positive Test Dali::BaseHandle::operator!=");
297
298   BaseHandle object = Actor::New();
299
300   DALI_TEST_CHECK(object);
301
302   BaseHandle aDifferentBaseHandle = Actor::New();
303
304   DALI_TEST_CHECK(object != aDifferentBaseHandle);
305   END_TEST;
306 }
307
308 int UtcDaliBaseHandleInequalityOperator02(void)
309 {
310   TestApplication application;
311   tet_infoline("Negative Test Dali::BaseHandle::operator!=");
312
313   BaseHandle object = Actor::New();
314
315   DALI_TEST_CHECK(object);
316
317   BaseHandle theSameBaseHandle = object;
318
319   DALI_TEST_CHECK(!(object != theSameBaseHandle));
320   END_TEST;
321 }
322
323 int UtcDaliBaseHandleStlVector(void)
324 {
325   TestApplication application;
326   tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
327
328   const int TargetVectorSize(5);
329
330   std::vector<Actor> myVector;
331
332   for (int i=0; i<TargetVectorSize; ++i)
333   {
334     Actor actor = Actor::New();
335
336     std::stringstream stream;
337     stream << "Actor " << i+1;
338     actor.SetProperty( Actor::Property::NAME,stream.str());
339
340     myVector.push_back(actor);
341   }
342
343   DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
344
345   DALI_TEST_CHECK(myVector[0].GetProperty< std::string >( Actor::Property::NAME ) == "Actor 1");
346   DALI_TEST_CHECK(myVector[1].GetProperty< std::string >( Actor::Property::NAME ) == "Actor 2");
347   DALI_TEST_CHECK(myVector[2].GetProperty< std::string >( Actor::Property::NAME ) == "Actor 3");
348   DALI_TEST_CHECK(myVector[3].GetProperty< std::string >( Actor::Property::NAME ) == "Actor 4");
349   DALI_TEST_CHECK(myVector[4].GetProperty< std::string >( Actor::Property::NAME ) == "Actor 5");
350   END_TEST;
351 }
352
353 int UtcDaliBaseHandleDoAction(void)
354 {
355   TestApplication application;
356   tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
357
358   Actor actor = Actor::New();
359   BaseHandle actorObject = actor;
360
361   DALI_TEST_CHECK(actorObject);
362
363   // Check that an invalid command is not performed
364   Property::Map attributes;
365   DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
366
367   // Check that the actor is visible
368   actor.SetProperty( Actor::Property::VISIBLE,true);
369   DALI_TEST_CHECK(actor.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) == true);
370
371   // Check the actor performed an action to hide itself
372   DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
373
374   // flush the queue and render once
375   application.SendNotification();
376   application.Render();
377
378   // Check that the actor is now invisible
379   DALI_TEST_CHECK(actor.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) == false);
380
381   // Check the actor performed an action to show itself
382   DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
383
384   // flush the queue and render once
385   application.SendNotification();
386   application.Render();
387
388   // Check that the actor is now visible
389   DALI_TEST_CHECK(actor.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) == true);
390
391   application.GetScene().Add(actor);
392
393   // Build an animation with initial duration of 1 second
394   float durationSeconds(1.0f);
395   Animation animation = Animation::New(durationSeconds);
396   BaseHandle animationObject = animation;
397
398   DALI_TEST_CHECK(animationObject);
399
400   // Check the current animation duration is 1 second
401   DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
402
403   Vector3 targetPosition(100.0f, 100.0f, 100.0f);
404   animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
405
406   // Set the new duration to be 2 seconds
407   float newDurationSeconds(2.0f);
408   Property::Value newDurationSecondsValue = Property::Value( newDurationSeconds );
409   attributes["duration"] = newDurationSecondsValue;
410
411   // Check the animation performed an action to play itself with the specified duration of 2 seconds
412   animationObject.DoAction("play", attributes);
413
414   bool signalReceived(false);
415   AnimationFinishCheck finishCheck(signalReceived);
416   // use the handle API to connect the signal
417   animation.ConnectSignal( &application, "finished", finishCheck );
418   // just for coverage connect to non-existant signal as well
419   animation.ConnectSignal( &application, "foo", finishCheck );
420   DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
421
422   application.SendNotification();
423   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
424   DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
425
426   // pause
427   animationObject.DoAction("pause", attributes);
428   application.SendNotification();
429   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
430   DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
431
432   // continue
433   animationObject.DoAction("play", attributes);
434   application.SendNotification();
435   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
436
437   // We expect the animation to finish
438   application.SendNotification();
439   finishCheck.CheckSignalReceived();
440   DALI_TEST_EQUALS( actor.GetCurrentProperty< Vector3 >( Actor::Property::POSITION ), targetPosition, TEST_LOCATION );
441
442   // play again
443   signalReceived = false;
444   animationObject.DoAction("play", attributes);
445   DALI_TEST_EQUALS(animation.GetCurrentProgress(), 0.f, TEST_LOCATION);
446   application.SendNotification();
447   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
448   animationObject.DoAction("stop", attributes);
449   application.SendNotification();
450   application.Render(static_cast<uint32_t>(newDurationSeconds * 1000.0f) /* full time */);
451   DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
452
453   // Check the new animation duration is 2 seconds
454   DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
455   END_TEST;
456 }
457
458
459 int UtcDaliBaseHandleConnectSignal(void)
460 {
461   TestApplication application;
462   tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
463
464   gTouchCallBackCalled = false;
465
466   // get the root layer
467   Actor actor = Actor::New();
468   actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
469   actor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT );
470   actor.SetProperty( Actor::Property::POSITION, Vector2( 240, 400 ));
471   actor.SetProperty( Actor::Property::SIZE, Vector2( 100, 100 ) );
472
473   application.GetScene().Add( actor );
474
475   DALI_TEST_CHECK( gTouchCallBackCalled == false );
476
477   // connect to its touch signal
478   actor.ConnectSignal( &application, "touch", TestCallback() );
479
480   application.SendNotification();
481   application.Render(1000);
482   application.SendNotification();
483   application.Render(1000);
484
485   // simulate a touch event
486   Dali::Integration::Point point;
487   point.SetState( PointState::DOWN );
488   point.SetScreenPosition( Vector2( 240, 400 ) );
489   Dali::Integration::TouchEvent event;
490   event.AddPoint( point );
491   application.ProcessEvent( event );
492
493   application.SendNotification();
494   application.Render(1000);
495   application.SendNotification();
496   application.Render(1000);
497
498   DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
499   DALI_TEST_CHECK( gTouchCallBackCalled == true );
500
501   gTouchCallBackCalled = false;
502   application.DisconnectAll();
503
504   // simulate another touch event
505   application.ProcessEvent( event );
506
507   DALI_TEST_CHECK( gTouchCallBackCalled == false );
508   END_TEST;
509 }
510
511 int UtcDaliBaseHandleGetTypeNameP(void)
512 {
513   TestApplication application;
514   tet_infoline("Testing Dali::BaseHandle::GetTypeName");
515
516   // get the root layer
517   Actor actor = Actor::New();
518
519   std::string typeName = actor.GetTypeName();
520
521   DALI_TEST_CHECK( typeName.size() );
522   DALI_TEST_CHECK( typeName == std::string("Actor") );
523   END_TEST;
524 }
525
526 int UtcDaliBaseHandleGetTypeNameN(void)
527 {
528
529   TestApplication application;
530   tet_infoline("Testing Dali::BaseObject::GetTypeName");
531   FakeObject object;
532   std::string typeName = object.GetTypeName();
533
534   DALI_TEST_CHECK( typeName.empty() );
535   END_TEST;
536 }
537
538 int UtcDaliBaseHandleGetTypeInfoP(void)
539 {
540   TestApplication application;
541   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
542
543   Dali::TypeInfo info;
544   Actor actor = Actor::New();
545
546   bool ok = actor.GetTypeInfo( info );
547   DALI_TEST_CHECK( ok );
548   END_TEST;
549 }
550
551 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
552 {
553   TestApplication application;
554   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
555   FakeHandle handle;
556   handle.RunTest();
557   tet_result(TET_PASS);
558   END_TEST;
559
560 }
561
562 int UtcDaliBaseHandleGetTypeInfoN(void)
563 {
564   TestApplication application;
565   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
566
567   Dali::TypeInfo info;
568   FakeObject object;
569
570   bool ok = object.GetTypeInfo( info );
571   DALI_TEST_CHECK( !ok );
572   END_TEST;
573 }
574
575 int UtcDaliBaseHandleGetObjectPtr(void)
576 {
577   TestApplication application;
578   tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
579
580   // get the root layer
581   Actor actor = Actor::New();
582
583   Dali::RefObject* p = actor.GetObjectPtr();
584
585   DALI_TEST_CHECK( p != NULL );
586   END_TEST;
587 }
588
589 int UtcDaliBaseHandleBooleanCast(void)
590 {
591   TestApplication application;
592   tet_infoline("Testing Dali::BaseHandle::BooleanType");
593
594   // get the root layer
595   BaseHandle handle = Actor::New();
596
597   DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
598   END_TEST;
599 }
600
601 int UtcDaliBaseHandleCompareOperatorN(void)
602 {
603   TestApplication application;
604   BaseHandle handle1 = Actor::New();
605   BaseHandle handle2 = handle1;
606
607   DALI_TEST_CHECK( (handle1 < handle2) == false );
608
609   END_TEST;
610 }
611
612 int UtcDaliBaseHandleDoActionNegative(void)
613 {
614   TestApplication application;
615   Dali::BaseHandle instance;
616   try
617   {
618     std::string arg1;
619     Dali::Property::Map arg2;
620     instance.DoAction(arg1,arg2);
621     DALI_TEST_CHECK(false); // Should not get here
622   }
623   catch(...)
624   {
625     DALI_TEST_CHECK(true); // We expect an assert
626   }
627   END_TEST;
628 }
629
630 int UtcDaliBaseHandleGetTypeInfoNegative(void)
631 {
632   TestApplication application;
633   Dali::BaseHandle instance;
634   try
635   {
636     Dali::TypeInfo arg1;
637     instance.GetTypeInfo(arg1);
638     DALI_TEST_CHECK(false); // Should not get here
639   }
640   catch(...)
641   {
642     DALI_TEST_CHECK(true); // We expect an assert
643   }
644   END_TEST;
645 }
646
647 int UtcDaliBaseHandleGetTypeNameNegative(void)
648 {
649   TestApplication application;
650   Dali::BaseHandle instance;
651   try
652   {
653     instance.GetTypeName();
654     DALI_TEST_CHECK(false); // Should not get here
655   }
656   catch(...)
657   {
658     DALI_TEST_CHECK(true); // We expect an assert
659   }
660   END_TEST;
661 }