2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali/integration-api/events/touch-event-integ.h>
19 #include <dali/public-api/dali-core.h>
24 #include "dali-test-suite-utils/dali-test-suite-utils.h"
28 void utc_base_handle_startup(void)
30 test_return_value = TET_UNDEF;
33 // Called after each test
34 void utc_base_handle_cleanup(void)
36 test_return_value = TET_PASS;
41 // Functor to test whether an animation finish signal is emitted
42 struct AnimationFinishCheck
44 AnimationFinishCheck(bool& signalReceived)
45 : mSignalReceived(signalReceived)
51 mSignalReceived = true;
56 mSignalReceived = false;
59 void CheckSignalReceived()
63 tet_printf("Expected Finish signal was not received\n");
72 bool& mSignalReceived; // owned by individual tests
75 BaseHandle ImplicitCopyConstructor(BaseHandle passedByValue)
77 // object + copy + passedByValue, ref count == 3
78 DALI_TEST_CHECK(passedByValue);
81 DALI_TEST_EQUALS(3, passedByValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
87 static bool gTouchCallBackCalled;
93 gTouchCallBackCalled = true;
97 // Used for testing BaseObject::GetTypeName with an object that is not registered
98 class FakeObject : public BaseObject
103 int UtcDaliBaseHandleConstructorVoid(void)
105 TestApplication application;
106 tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
110 DALI_TEST_CHECK(!object);
114 int UtcDaliBaseHandleCopyConstructor(void)
116 TestApplication application;
117 tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
119 // Initialize an object, ref count == 1
120 BaseHandle object = Actor::New();
122 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
124 // Copy the object, ref count == 2
125 BaseHandle copy(object);
126 DALI_TEST_CHECK(copy);
129 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
133 // Pass by value, and return another copy, ref count == 3
134 BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
136 DALI_TEST_CHECK(anotherCopy);
139 DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
143 // anotherCopy out of scope, ref count == 2
144 DALI_TEST_CHECK(copy);
147 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
152 int UtcDaliBaseHandleAssignmentOperator(void)
154 TestApplication application;
155 tet_infoline("Testing Dali::BaseHandle::operator=");
157 BaseHandle object = Actor::New();
159 DALI_TEST_CHECK(object);
162 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
165 BaseHandle copy = object;
167 DALI_TEST_CHECK(copy);
170 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
175 int UtcDaliBaseHandleMoveConstructor(void)
177 TestApplication application;
179 // Initialize an object, ref count == 1
180 BaseHandle object = Actor::New();
182 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
184 // Move the object, ref count == 1
185 BaseHandle move = std::move(object);
186 DALI_TEST_CHECK(move);
188 // Check that object is moved (not copied, so ref count keeps the same)
191 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
193 DALI_TEST_CHECK(!object);
198 int UtcDaliBaseHandleMoveAssignment(void)
200 TestApplication application;
202 // Initialize an object, ref count == 1
203 BaseHandle object = Actor::New();
205 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
207 // Move the object, ref count == 1
209 move = std::move(object);
210 DALI_TEST_CHECK(move);
212 // Check that object is moved (not copied, so ref count keeps the same)
215 DALI_TEST_EQUALS(1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION);
217 DALI_TEST_CHECK(!object);
222 int UtcDaliBaseHandleGetBaseObject(void)
224 TestApplication application;
225 tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
227 BaseHandle object = Actor::New();
229 BaseObject& handle = object.GetBaseObject();
231 DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
235 int UtcDaliBaseHandleReset(void)
237 TestApplication application;
238 tet_infoline("Testing Dali::BaseHandle::Reset()");
240 // Initialize an object, ref count == 1
241 BaseHandle object = Actor::New();
243 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
247 DALI_TEST_CHECK(!object);
251 int UtcDaliBaseHandleEqualityOperator01(void)
253 TestApplication application;
254 tet_infoline("Positive Test Dali::BaseHandle::operator==");
256 BaseHandle object = Actor::New();
258 DALI_TEST_CHECK(object);
260 BaseHandle theSameBaseHandle = object;
262 DALI_TEST_CHECK(object == theSameBaseHandle);
266 int UtcDaliBaseHandleEqualityOperator02(void)
268 TestApplication application;
269 tet_infoline("Negative Test Dali::BaseHandle::operator==");
271 BaseHandle object = Actor::New();
273 DALI_TEST_CHECK(object);
275 BaseHandle aDifferentBaseHandle = Actor::New();
277 DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
281 int UtcDaliBaseHandleInequalityOperator01(void)
283 TestApplication application;
284 tet_infoline("Positive Test Dali::BaseHandle::operator!=");
286 BaseHandle object = Actor::New();
288 DALI_TEST_CHECK(object);
290 BaseHandle aDifferentBaseHandle = Actor::New();
292 DALI_TEST_CHECK(object != aDifferentBaseHandle);
296 int UtcDaliBaseHandleInequalityOperator02(void)
298 TestApplication application;
299 tet_infoline("Negative Test Dali::BaseHandle::operator!=");
301 BaseHandle object = Actor::New();
303 DALI_TEST_CHECK(object);
305 BaseHandle theSameBaseHandle = object;
307 DALI_TEST_CHECK(!(object != theSameBaseHandle));
311 int UtcDaliBaseHandleInequalityWithNullptr(void)
313 TestApplication application;
314 tet_infoline("Test for Dali::BaseHandle::operator == nullptr");
318 // object is nullptr.
319 DALI_TEST_CHECK(object == nullptr);
320 DALI_TEST_CHECK(nullptr == object);
321 DALI_TEST_CHECK(!(object != nullptr));
322 DALI_TEST_CHECK(!(nullptr != object));
324 object = Actor::New();
326 // object is not nullptr.
327 DALI_TEST_CHECK(!(object == nullptr));
328 DALI_TEST_CHECK(!(nullptr == object));
329 DALI_TEST_CHECK(object != nullptr);
330 DALI_TEST_CHECK(nullptr != object);
335 int UtcDaliBaseHandleStlVector(void)
337 TestApplication application;
338 tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
340 const int TargetVectorSize(5);
342 std::vector<Actor> myVector;
344 for(int i = 0; i < TargetVectorSize; ++i)
346 Actor actor = Actor::New();
348 std::stringstream stream;
349 stream << "Actor " << i + 1;
350 actor.SetProperty(Actor::Property::NAME, stream.str());
352 myVector.push_back(actor);
355 DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
357 DALI_TEST_CHECK(myVector[0].GetProperty<std::string>(Actor::Property::NAME) == "Actor 1");
358 DALI_TEST_CHECK(myVector[1].GetProperty<std::string>(Actor::Property::NAME) == "Actor 2");
359 DALI_TEST_CHECK(myVector[2].GetProperty<std::string>(Actor::Property::NAME) == "Actor 3");
360 DALI_TEST_CHECK(myVector[3].GetProperty<std::string>(Actor::Property::NAME) == "Actor 4");
361 DALI_TEST_CHECK(myVector[4].GetProperty<std::string>(Actor::Property::NAME) == "Actor 5");
365 int UtcDaliBaseHandleDoAction(void)
367 TestApplication application;
368 tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
370 Actor actor = Actor::New();
371 BaseHandle actorObject = actor;
373 DALI_TEST_CHECK(actorObject);
375 // Check that an invalid command is not performed
376 Property::Map attributes;
377 DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
379 // Check that the actor is visible
380 actor.SetProperty(Actor::Property::VISIBLE, true);
381 DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
383 // Check the actor performed an action to hide itself
384 DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
386 // flush the queue and render once
387 application.SendNotification();
388 application.Render();
390 // Check that the actor is now invisible
391 DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == false);
393 // Check the actor performed an action to show itself
394 DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
396 // flush the queue and render once
397 application.SendNotification();
398 application.Render();
400 // Check that the actor is now visible
401 DALI_TEST_CHECK(actor.GetCurrentProperty<bool>(Actor::Property::VISIBLE) == true);
403 application.GetScene().Add(actor);
405 // Build an animation with initial duration of 1 second
406 float durationSeconds(1.0f);
407 Animation animation = Animation::New(durationSeconds);
408 BaseHandle animationObject = animation;
410 DALI_TEST_CHECK(animationObject);
412 // Check the current animation duration is 1 second
413 DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
415 Vector3 targetPosition(100.0f, 100.0f, 100.0f);
416 animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
418 // Set the new duration to be 2 seconds
419 float newDurationSeconds(2.0f);
420 Property::Value newDurationSecondsValue = Property::Value(newDurationSeconds);
421 attributes["duration"] = newDurationSecondsValue;
423 // Check the animation performed an action to play itself with the specified duration of 2 seconds
424 animationObject.DoAction("play", attributes);
426 bool signalReceived(false);
427 AnimationFinishCheck finishCheck(signalReceived);
428 // use the handle API to connect the signal
429 animation.ConnectSignal(&application, "finished", finishCheck);
430 // just for coverage connect to non-existant signal as well
431 animation.ConnectSignal(&application, "foo", finishCheck);
432 DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
434 application.SendNotification();
435 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
436 DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
439 animationObject.DoAction("pause", attributes);
440 application.SendNotification();
441 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u /*just beyond the animation duration*/);
442 DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
445 animationObject.DoAction("play", attributes);
446 application.SendNotification();
447 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u /*just beyond the animation duration*/);
449 // We expect the animation to finish
450 application.SendNotification();
451 finishCheck.CheckSignalReceived();
452 DALI_TEST_EQUALS(actor.GetCurrentProperty<Vector3>(Actor::Property::POSITION), targetPosition, TEST_LOCATION);
455 signalReceived = false;
456 animationObject.DoAction("play", attributes);
457 DALI_TEST_EQUALS(animation.GetCurrentProgress(), 0.f, TEST_LOCATION);
458 application.SendNotification();
459 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
460 animationObject.DoAction("stop", attributes);
461 application.SendNotification();
462 application.Render(static_cast<uint32_t>(newDurationSeconds * 1000.0f) /* full time */);
463 DALI_TEST_EQUALS(signalReceived, false, TEST_LOCATION);
465 // Check the new animation duration is 2 seconds
466 DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
470 int UtcDaliBaseHandleConnectSignal(void)
472 TestApplication application;
473 tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
475 gTouchCallBackCalled = false;
477 // get the root layer
478 Actor actor = Actor::New();
479 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
480 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
481 actor.SetProperty(Actor::Property::POSITION, Vector2(240, 400));
482 actor.SetProperty(Actor::Property::SIZE, Vector2(100, 100));
484 application.GetScene().Add(actor);
486 DALI_TEST_CHECK(gTouchCallBackCalled == false);
488 // connect to its touch signal
489 actor.ConnectSignal(&application, "touched", TestCallback());
491 application.SendNotification();
492 application.Render(1000);
493 application.SendNotification();
494 application.Render(1000);
496 // simulate a touch event
497 Dali::Integration::Point point;
498 point.SetState(PointState::DOWN);
499 point.SetScreenPosition(Vector2(240, 400));
500 Dali::Integration::TouchEvent event;
501 event.AddPoint(point);
502 application.ProcessEvent(event);
504 application.SendNotification();
505 application.Render(1000);
506 application.SendNotification();
507 application.Render(1000);
509 DALI_TEST_CHECK(application.GetConnectionCount() > 0);
510 DALI_TEST_CHECK(gTouchCallBackCalled == true);
512 gTouchCallBackCalled = false;
513 application.DisconnectAll();
515 // simulate another touch event
516 application.ProcessEvent(event);
518 DALI_TEST_CHECK(gTouchCallBackCalled == false);
522 int UtcDaliBaseHandleGetTypeNameP(void)
524 TestApplication application;
525 tet_infoline("Testing Dali::BaseHandle::GetTypeName");
527 // get the root layer
528 Actor actor = Actor::New();
530 std::string typeName = actor.GetTypeName();
532 DALI_TEST_CHECK(typeName.size());
533 DALI_TEST_CHECK(typeName == std::string("Actor"));
537 int UtcDaliBaseHandleGetTypeNameN(void)
539 TestApplication application;
540 tet_infoline("Testing Dali::BaseObject::GetTypeName");
542 std::string typeName = object.GetTypeName();
544 DALI_TEST_CHECK(typeName.empty());
548 int UtcDaliBaseHandleGetTypeInfoP(void)
550 TestApplication application;
551 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
554 Actor actor = Actor::New();
556 bool ok = actor.GetTypeInfo(info);
561 int UtcDaliBaseHandleGetTypeInfoN(void)
563 TestApplication application;
564 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
569 bool ok = object.GetTypeInfo(info);
570 DALI_TEST_CHECK(!ok);
574 int UtcDaliBaseHandleGetObjectPtr(void)
576 TestApplication application;
577 tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
579 // get the root layer
580 Actor actor = Actor::New();
582 Dali::RefObject* p = actor.GetObjectPtr();
584 DALI_TEST_CHECK(p != NULL);
588 int UtcDaliBaseHandleBooleanCast(void)
590 TestApplication application;
591 tet_infoline("Testing Dali::BaseHandle::operator bool");
593 // get the root layer
594 BaseHandle handle = Actor::New();
596 DALI_TEST_CHECK(static_cast<bool>(handle));
600 int UtcDaliBaseHandleCompareOperatorN(void)
602 TestApplication application;
603 BaseHandle handle1 = Actor::New();
604 BaseHandle handle2 = handle1;
606 DALI_TEST_CHECK((handle1 < handle2) == false);
611 int UtcDaliBaseHandleDoActionNegative(void)
613 TestApplication application;
614 Dali::BaseHandle instance;
618 Dali::Property::Map arg2;
619 instance.DoAction(arg1, arg2);
620 DALI_TEST_CHECK(false); // Should not get here
624 DALI_TEST_CHECK(true); // We expect an assert
629 int UtcDaliBaseHandleGetTypeInfoNegative(void)
631 TestApplication application;
632 Dali::BaseHandle instance;
636 instance.GetTypeInfo(arg1);
637 DALI_TEST_CHECK(false); // Should not get here
641 DALI_TEST_CHECK(true); // We expect an assert
646 int UtcDaliBaseHandleGetTypeNameNegative(void)
648 TestApplication application;
649 Dali::BaseHandle instance;
652 instance.GetTypeName();
653 DALI_TEST_CHECK(false); // Should not get here
657 DALI_TEST_CHECK(true); // We expect an assert