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