(Partial Update) Mark as not rendered if the node is transparent or culled
[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 <dali/integration-api/events/touch-event-integ.h>
19 #include <dali/public-api/dali-core.h>
20 #include <stdlib.h>
21
22 #include <iostream>
23
24 #include "dali-test-suite-utils/dali-test-suite-utils.h"
25
26 using namespace Dali;
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 // Functor to test whether an animation finish signal is emitted
42 struct AnimationFinishCheck
43 {
44   AnimationFinishCheck(bool& signalReceived)
45   : mSignalReceived(signalReceived)
46   {
47   }
48
49   void operator()()
50   {
51     mSignalReceived = true;
52   }
53
54   void Reset()
55   {
56     mSignalReceived = false;
57   }
58
59   void CheckSignalReceived()
60   {
61     if(!mSignalReceived)
62     {
63       tet_printf("Expected Finish signal was not received\n");
64       tet_result(TET_FAIL);
65     }
66     else
67     {
68       tet_result(TET_PASS);
69     }
70   }
71
72   bool& mSignalReceived; // owned by individual tests
73 };
74
75 BaseHandle ImplicitCopyConstructor(BaseHandle passedByValue)
76 {
77   // object + copy + passedByValue, ref count == 3
78   DALI_TEST_CHECK(passedByValue);
79   if(passedByValue)
80   {
81     DALI_TEST_EQUALS(3, passedByValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
82   }
83
84   return passedByValue;
85 }
86
87 static bool gTouchCallBackCalled;
88
89 struct TestCallback
90 {
91   void operator()()
92   {
93     gTouchCallBackCalled = true;
94   }
95 };
96
97 // Used for testing BaseObject::GetTypeName with an object that is not registered
98 class FakeObject : public BaseObject
99 {
100 };
101 // used for testing ThisIsSaferThanReturningVoidStar
102 class FakeHandle : public BaseHandle
103 {
104 public:
105   void RunTest()
106   {
107     return ThisIsSaferThanReturningVoidStar();
108   }
109 };
110 } // namespace
111
112 int UtcDaliBaseHandleConstructorVoid(void)
113 {
114   TestApplication application;
115   tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
116
117   BaseHandle object;
118
119   DALI_TEST_CHECK(!object);
120   END_TEST;
121 }
122
123 int UtcDaliBaseHandleCopyConstructor(void)
124 {
125   TestApplication application;
126   tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
127
128   // Initialize an object, ref count == 1
129   BaseHandle object = Actor::New();
130
131   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
132
133   // Copy the object, ref count == 2
134   BaseHandle copy(object);
135   DALI_TEST_CHECK(copy);
136   if(copy)
137   {
138     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
139   }
140
141   {
142     // Pass by value, and return another copy, ref count == 3
143     BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
144
145     DALI_TEST_CHECK(anotherCopy);
146     if(anotherCopy)
147     {
148       DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
149     }
150   }
151
152   // anotherCopy out of scope, ref count == 2
153   DALI_TEST_CHECK(copy);
154   if(copy)
155   {
156     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
157   }
158   END_TEST;
159 }
160
161 int UtcDaliBaseHandleAssignmentOperator(void)
162 {
163   TestApplication application;
164   tet_infoline("Testing Dali::BaseHandle::operator=");
165
166   BaseHandle object = Actor::New();
167
168   DALI_TEST_CHECK(object);
169   if(object)
170   {
171     DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
172   }
173
174   BaseHandle copy = object;
175
176   DALI_TEST_CHECK(copy);
177   if(copy)
178   {
179     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
180   }
181   END_TEST;
182 }
183
184 int UtcDaliBaseHandleMoveConstructor(void)
185 {
186   TestApplication application;
187
188   // Initialize an object, ref count == 1
189   BaseHandle object = Actor::New();
190
191   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
192
193   // Move the object, ref count == 1
194   BaseHandle move = std::move(object);
195   DALI_TEST_CHECK(move);
196
197   // Check that object is moved (not copied, so ref count keeps the same)
198   if(move)
199   {
200     DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
201   }
202   DALI_TEST_CHECK(!object);
203
204   END_TEST;
205 }
206
207 int UtcDaliBaseHandleMoveAssignment(void)
208 {
209   TestApplication application;
210
211   // Initialize an object, ref count == 1
212   BaseHandle object = Actor::New();
213
214   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
215
216   // Move the object, ref count == 1
217   BaseHandle move;
218   move = std::move(object);
219   DALI_TEST_CHECK(move);
220
221   // Check that object is moved (not copied, so ref count keeps the same)
222   if(move)
223   {
224     DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
225   }
226   DALI_TEST_CHECK(!object);
227
228   END_TEST;
229 }
230
231 int UtcDaliBaseHandleGetBaseObject(void)
232 {
233   TestApplication application;
234   tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
235
236   BaseHandle object = Actor::New();
237
238   BaseObject& handle = object.GetBaseObject();
239
240   DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
241   END_TEST;
242 }
243
244 int UtcDaliBaseHandleReset(void)
245 {
246   TestApplication application;
247   tet_infoline("Testing Dali::BaseHandle::Reset()");
248
249   // Initialize an object, ref count == 1
250   BaseHandle object = Actor::New();
251
252   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
253
254   object.Reset();
255
256   DALI_TEST_CHECK(!object);
257   END_TEST;
258 }
259
260 int UtcDaliBaseHandleEqualityOperator01(void)
261 {
262   TestApplication application;
263   tet_infoline("Positive Test Dali::BaseHandle::operator==");
264
265   BaseHandle object = Actor::New();
266
267   DALI_TEST_CHECK(object);
268
269   BaseHandle theSameBaseHandle = object;
270
271   DALI_TEST_CHECK(object == theSameBaseHandle);
272   END_TEST;
273 }
274
275 int UtcDaliBaseHandleEqualityOperator02(void)
276 {
277   TestApplication application;
278   tet_infoline("Negative Test Dali::BaseHandle::operator==");
279
280   BaseHandle object = Actor::New();
281
282   DALI_TEST_CHECK(object);
283
284   BaseHandle aDifferentBaseHandle = Actor::New();
285
286   DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
287   END_TEST;
288 }
289
290 int UtcDaliBaseHandleInequalityOperator01(void)
291 {
292   TestApplication application;
293   tet_infoline("Positive Test Dali::BaseHandle::operator!=");
294
295   BaseHandle object = Actor::New();
296
297   DALI_TEST_CHECK(object);
298
299   BaseHandle aDifferentBaseHandle = Actor::New();
300
301   DALI_TEST_CHECK(object != aDifferentBaseHandle);
302   END_TEST;
303 }
304
305 int UtcDaliBaseHandleInequalityOperator02(void)
306 {
307   TestApplication application;
308   tet_infoline("Negative Test Dali::BaseHandle::operator!=");
309
310   BaseHandle object = Actor::New();
311
312   DALI_TEST_CHECK(object);
313
314   BaseHandle theSameBaseHandle = object;
315
316   DALI_TEST_CHECK(!(object != theSameBaseHandle));
317   END_TEST;
318 }
319
320 int UtcDaliBaseHandleStlVector(void)
321 {
322   TestApplication application;
323   tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
324
325   const int TargetVectorSize(5);
326
327   std::vector<Actor> myVector;
328
329   for(int i = 0; i < TargetVectorSize; ++i)
330   {
331     Actor actor = Actor::New();
332
333     std::stringstream stream;
334     stream << "Actor " << i + 1;
335     actor.SetProperty(Actor::Property::NAME, stream.str());
336
337     myVector.push_back(actor);
338   }
339
340   DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
341
342   DALI_TEST_CHECK(myVector[0].GetProperty<std::string>(Actor::Property::NAME) == "Actor 1");
343   DALI_TEST_CHECK(myVector[1].GetProperty<std::string>(Actor::Property::NAME) == "Actor 2");
344   DALI_TEST_CHECK(myVector[2].GetProperty<std::string>(Actor::Property::NAME) == "Actor 3");
345   DALI_TEST_CHECK(myVector[3].GetProperty<std::string>(Actor::Property::NAME) == "Actor 4");
346   DALI_TEST_CHECK(myVector[4].GetProperty<std::string>(Actor::Property::NAME) == "Actor 5");
347   END_TEST;
348 }
349
350 int UtcDaliBaseHandleDoAction(void)
351 {
352   TestApplication application;
353   tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
354
355   Actor      actor       = Actor::New();
356   BaseHandle actorObject = actor;
357
358   DALI_TEST_CHECK(actorObject);
359
360   // Check that an invalid command is not performed
361   Property::Map attributes;
362   DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
363
364   // Check that the actor is visible
365   actor.SetProperty(Actor::Property::VISIBLE, true);
366   DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
367
368   // Check the actor performed an action to hide itself
369   DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
370
371   // flush the queue and render once
372   application.SendNotification();
373   application.Render();
374
375   // Check that the actor is now invisible
376   DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == false);
377
378   // Check the actor performed an action to show itself
379   DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
380
381   // flush the queue and render once
382   application.SendNotification();
383   application.Render();
384
385   // Check that the actor is now visible
386   DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
387
388   application.GetScene().Add(actor);
389
390   // Build an animation with initial duration of 1 second
391   float      durationSeconds(1.0f);
392   Animation  animation       = Animation::New(durationSeconds);
393   BaseHandle animationObject = animation;
394
395   DALI_TEST_CHECK(animationObject);
396
397   // Check the current animation duration is 1 second
398   DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
399
400   Vector3 targetPosition(100.0f, 100.0f, 100.0f);
401   animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
402
403   // Set the new duration to be 2 seconds
404   float           newDurationSeconds(2.0f);
405   Property::Value newDurationSecondsValue = Property::Value(newDurationSeconds);
406   attributes["duration"]                  = newDurationSecondsValue;
407
408   // Check the animation performed an action to play itself with the specified duration of 2 seconds
409   animationObject.DoAction("play", attributes);
410
411   bool                 signalReceived(false);
412   AnimationFinishCheck finishCheck(signalReceived);
413   // use the handle API to connect the signal
414   animation.ConnectSignal(&application, "finished", finishCheck);
415   // just for coverage connect to non-existant signal as well
416   animation.ConnectSignal(&application, "foo", finishCheck);
417   DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
418
419   application.SendNotification();
420   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
421   DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
422
423   // pause
424   animationObject.DoAction("pause", attributes);
425   application.SendNotification();
426   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u /*just beyond the animation duration*/);
427   DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
428
429   // continue
430   animationObject.DoAction("play", attributes);
431   application.SendNotification();
432   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u /*just beyond the animation duration*/);
433
434   // We expect the animation to finish
435   application.SendNotification();
436   finishCheck.CheckSignalReceived();
437   DALI_TEST_EQUALS(actor.GetCurrentProperty<Vector3>(Actor::Property::POSITION), targetPosition, TEST_LOCATION);
438
439   // play again
440   signalReceived = false;
441   animationObject.DoAction("play", attributes);
442   DALI_TEST_EQUALS(animation.GetCurrentProgress(), 0.f, TEST_LOCATION);
443   application.SendNotification();
444   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
445   animationObject.DoAction("stop", attributes);
446   application.SendNotification();
447   application.Render(static_cast<uint32_t>(newDurationSeconds * 1000.0f) /* full time */);
448   DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
449
450   // Check the new animation duration is 2 seconds
451   DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
452   END_TEST;
453 }
454
455 int UtcDaliBaseHandleConnectSignal(void)
456 {
457   TestApplication application;
458   tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
459
460   gTouchCallBackCalled = false;
461
462   // get the root layer
463   Actor actor = Actor::New();
464   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
465   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
466   actor.SetProperty(Actor::Property::POSITION, Vector2(240, 400));
467   actor.SetProperty(Actor::Property::SIZE, Vector2(100, 100));
468
469   application.GetScene().Add(actor);
470
471   DALI_TEST_CHECK(gTouchCallBackCalled == false);
472
473   // connect to its touch signal
474   actor.ConnectSignal(&application, "touched", TestCallback());
475
476   application.SendNotification();
477   application.Render(1000);
478   application.SendNotification();
479   application.Render(1000);
480
481   // simulate a touch event
482   Dali::Integration::Point point;
483   point.SetState(PointState::DOWN);
484   point.SetScreenPosition(Vector2(240, 400));
485   Dali::Integration::TouchEvent event;
486   event.AddPoint(point);
487   application.ProcessEvent(event);
488
489   application.SendNotification();
490   application.Render(1000);
491   application.SendNotification();
492   application.Render(1000);
493
494   DALI_TEST_CHECK(application.GetConnectionCount() > 0);
495   DALI_TEST_CHECK(gTouchCallBackCalled == true);
496
497   gTouchCallBackCalled = false;
498   application.DisconnectAll();
499
500   // simulate another touch event
501   application.ProcessEvent(event);
502
503   DALI_TEST_CHECK(gTouchCallBackCalled == false);
504   END_TEST;
505 }
506
507 int UtcDaliBaseHandleGetTypeNameP(void)
508 {
509   TestApplication application;
510   tet_infoline("Testing Dali::BaseHandle::GetTypeName");
511
512   // get the root layer
513   Actor actor = Actor::New();
514
515   std::string typeName = actor.GetTypeName();
516
517   DALI_TEST_CHECK(typeName.size());
518   DALI_TEST_CHECK(typeName == std::string("Actor"));
519   END_TEST;
520 }
521
522 int UtcDaliBaseHandleGetTypeNameN(void)
523 {
524   TestApplication application;
525   tet_infoline("Testing Dali::BaseObject::GetTypeName");
526   FakeObject  object;
527   std::string typeName = object.GetTypeName();
528
529   DALI_TEST_CHECK(typeName.empty());
530   END_TEST;
531 }
532
533 int UtcDaliBaseHandleGetTypeInfoP(void)
534 {
535   TestApplication application;
536   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
537
538   Dali::TypeInfo info;
539   Actor          actor = Actor::New();
540
541   bool ok = actor.GetTypeInfo(info);
542   DALI_TEST_CHECK(ok);
543   END_TEST;
544 }
545
546 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
547 {
548   TestApplication application;
549   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
550   FakeHandle handle;
551   handle.RunTest();
552   tet_result(TET_PASS);
553   END_TEST;
554 }
555
556 int UtcDaliBaseHandleGetTypeInfoN(void)
557 {
558   TestApplication application;
559   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
560
561   Dali::TypeInfo info;
562   FakeObject     object;
563
564   bool ok = object.GetTypeInfo(info);
565   DALI_TEST_CHECK(!ok);
566   END_TEST;
567 }
568
569 int UtcDaliBaseHandleGetObjectPtr(void)
570 {
571   TestApplication application;
572   tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
573
574   // get the root layer
575   Actor actor = Actor::New();
576
577   Dali::RefObject* p = actor.GetObjectPtr();
578
579   DALI_TEST_CHECK(p != NULL);
580   END_TEST;
581 }
582
583 int UtcDaliBaseHandleBooleanCast(void)
584 {
585   TestApplication application;
586   tet_infoline("Testing Dali::BaseHandle::BooleanType");
587
588   // get the root layer
589   BaseHandle handle = Actor::New();
590
591   DALI_TEST_CHECK(static_cast<BaseHandle::BooleanType>(handle));
592   END_TEST;
593 }
594
595 int UtcDaliBaseHandleCompareOperatorN(void)
596 {
597   TestApplication application;
598   BaseHandle      handle1 = Actor::New();
599   BaseHandle      handle2 = handle1;
600
601   DALI_TEST_CHECK((handle1 < handle2) == false);
602
603   END_TEST;
604 }
605
606 int UtcDaliBaseHandleDoActionNegative(void)
607 {
608   TestApplication  application;
609   Dali::BaseHandle instance;
610   try
611   {
612     std::string         arg1;
613     Dali::Property::Map arg2;
614     instance.DoAction(arg1, arg2);
615     DALI_TEST_CHECK(false); // Should not get here
616   }
617   catch(...)
618   {
619     DALI_TEST_CHECK(true); // We expect an assert
620   }
621   END_TEST;
622 }
623
624 int UtcDaliBaseHandleGetTypeInfoNegative(void)
625 {
626   TestApplication  application;
627   Dali::BaseHandle instance;
628   try
629   {
630     Dali::TypeInfo arg1;
631     instance.GetTypeInfo(arg1);
632     DALI_TEST_CHECK(false); // Should not get here
633   }
634   catch(...)
635   {
636     DALI_TEST_CHECK(true); // We expect an assert
637   }
638   END_TEST;
639 }
640
641 int UtcDaliBaseHandleGetTypeNameNegative(void)
642 {
643   TestApplication  application;
644   Dali::BaseHandle instance;
645   try
646   {
647     instance.GetTypeName();
648     DALI_TEST_CHECK(false); // Should not get here
649   }
650   catch(...)
651   {
652     DALI_TEST_CHECK(true); // We expect an assert
653   }
654   END_TEST;
655 }