2 * Copyright (c) 2020 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.
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
24 #include "dali-test-suite-utils/dali-test-suite-utils.h"
29 void utc_base_handle_startup(void)
31 test_return_value = TET_UNDEF;
34 // Called after each test
35 void utc_base_handle_cleanup(void)
37 test_return_value = TET_PASS;
43 // Functor to test whether an animation finish signal is emitted
44 struct AnimationFinishCheck
46 AnimationFinishCheck(bool& signalReceived)
47 : mSignalReceived(signalReceived)
53 mSignalReceived = true;
58 mSignalReceived = false;
61 void CheckSignalReceived()
65 tet_printf("Expected Finish signal was not received\n");
74 bool& mSignalReceived; // owned by individual tests
77 BaseHandle ImplicitCopyConstructor(BaseHandle passedByValue)
79 // object + copy + passedByValue, ref count == 3
80 DALI_TEST_CHECK(passedByValue);
83 DALI_TEST_EQUALS(3, passedByValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
89 static bool gTouchCallBackCalled;
95 gTouchCallBackCalled = true;
99 // Used for testing BaseObject::GetTypeName with an object that is not registered
100 class FakeObject : public BaseObject
103 // used for testing ThisIsSaferThanReturningVoidStar
104 class FakeHandle : public BaseHandle
110 return ThisIsSaferThanReturningVoidStar();
115 int UtcDaliBaseHandleConstructorVoid(void)
117 TestApplication application;
118 tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
122 DALI_TEST_CHECK(!object);
126 int UtcDaliBaseHandleCopyConstructor(void)
128 TestApplication application;
129 tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
131 // Initialize an object, ref count == 1
132 BaseHandle object = Actor::New();
134 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
136 // Copy the object, ref count == 2
137 BaseHandle copy(object);
138 DALI_TEST_CHECK(copy);
141 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
145 // Pass by value, and return another copy, ref count == 3
146 BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
148 DALI_TEST_CHECK(anotherCopy);
151 DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
155 // anotherCopy out of scope, ref count == 2
156 DALI_TEST_CHECK(copy);
159 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
164 int UtcDaliBaseHandleAssignmentOperator(void)
166 TestApplication application;
167 tet_infoline("Testing Dali::BaseHandle::operator=");
169 BaseHandle object = Actor::New();
171 DALI_TEST_CHECK(object);
174 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
177 BaseHandle copy = object;
179 DALI_TEST_CHECK(copy);
182 DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
187 int UtcDaliBaseHandleMoveConstructor(void)
189 TestApplication application;
191 // Initialize an object, ref count == 1
192 BaseHandle object = Actor::New();
194 DALI_TEST_EQUALS( 1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION );
196 // Move the object, ref count == 1
197 BaseHandle move = std::move( object );
198 DALI_TEST_CHECK( move );
200 // Check that object is moved (not copied, so ref count keeps the same)
203 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
205 DALI_TEST_CHECK( !object );
210 int UtcDaliBaseHandleMoveAssignment(void)
212 TestApplication application;
214 // Initialize an object, ref count == 1
215 BaseHandle object = Actor::New();
217 DALI_TEST_EQUALS( 1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION );
219 // Move the object, ref count == 1
221 move = std::move( object );
222 DALI_TEST_CHECK( move );
224 // Check that object is moved (not copied, so ref count keeps the same)
227 DALI_TEST_EQUALS( 1, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
229 DALI_TEST_CHECK( !object );
234 int UtcDaliBaseHandleGetBaseObject(void)
236 TestApplication application;
237 tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
239 BaseHandle object = Actor::New();
241 BaseObject& handle = object.GetBaseObject();
243 DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
247 int UtcDaliBaseHandleReset(void)
249 TestApplication application;
250 tet_infoline("Testing Dali::BaseHandle::Reset()");
252 // Initialize an object, ref count == 1
253 BaseHandle object = Actor::New();
255 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
259 DALI_TEST_CHECK(!object);
263 int UtcDaliBaseHandleEqualityOperator01(void)
265 TestApplication application;
266 tet_infoline("Positive Test Dali::BaseHandle::operator==");
268 BaseHandle object = Actor::New();
270 DALI_TEST_CHECK(object);
272 BaseHandle theSameBaseHandle = object;
274 DALI_TEST_CHECK(object == theSameBaseHandle);
278 int UtcDaliBaseHandleEqualityOperator02(void)
280 TestApplication application;
281 tet_infoline("Negative Test Dali::BaseHandle::operator==");
283 BaseHandle object = Actor::New();
285 DALI_TEST_CHECK(object);
287 BaseHandle aDifferentBaseHandle = Actor::New();
289 DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
293 int UtcDaliBaseHandleInequalityOperator01(void)
295 TestApplication application;
296 tet_infoline("Positive Test Dali::BaseHandle::operator!=");
298 BaseHandle object = Actor::New();
300 DALI_TEST_CHECK(object);
302 BaseHandle aDifferentBaseHandle = Actor::New();
304 DALI_TEST_CHECK(object != aDifferentBaseHandle);
308 int UtcDaliBaseHandleInequalityOperator02(void)
310 TestApplication application;
311 tet_infoline("Negative Test Dali::BaseHandle::operator!=");
313 BaseHandle object = Actor::New();
315 DALI_TEST_CHECK(object);
317 BaseHandle theSameBaseHandle = object;
319 DALI_TEST_CHECK(!(object != theSameBaseHandle));
323 int UtcDaliBaseHandleStlVector(void)
325 TestApplication application;
326 tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
328 const int TargetVectorSize(5);
330 std::vector<Actor> myVector;
332 for (int i=0; i<TargetVectorSize; ++i)
334 Actor actor = Actor::New();
336 std::stringstream stream;
337 stream << "Actor " << i+1;
338 actor.SetProperty( Actor::Property::NAME,stream.str());
340 myVector.push_back(actor);
343 DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
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");
353 int UtcDaliBaseHandleDoAction(void)
355 TestApplication application;
356 tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
358 Actor actor = Actor::New();
359 BaseHandle actorObject = actor;
361 DALI_TEST_CHECK(actorObject);
363 // Check that an invalid command is not performed
364 Property::Map attributes;
365 DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
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);
371 // Check the actor performed an action to hide itself
372 DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
374 // flush the queue and render once
375 application.SendNotification();
376 application.Render();
378 // Check that the actor is now invisible
379 DALI_TEST_CHECK(actor.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) == false);
381 // Check the actor performed an action to show itself
382 DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
384 // flush the queue and render once
385 application.SendNotification();
386 application.Render();
388 // Check that the actor is now visible
389 DALI_TEST_CHECK(actor.GetCurrentProperty< bool >( Actor::Property::VISIBLE ) == true);
391 application.GetScene().Add(actor);
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;
398 DALI_TEST_CHECK(animationObject);
400 // Check the current animation duration is 1 second
401 DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
403 Vector3 targetPosition(100.0f, 100.0f, 100.0f);
404 animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
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;
411 // Check the animation performed an action to play itself with the specified duration of 2 seconds
412 animationObject.DoAction("play", attributes);
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 );
422 application.SendNotification();
423 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
424 DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
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 );
433 animationObject.DoAction("play", attributes);
434 application.SendNotification();
435 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
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 );
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 );
453 // Check the new animation duration is 2 seconds
454 DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
459 int UtcDaliBaseHandleConnectSignal(void)
461 TestApplication application;
462 tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
464 gTouchCallBackCalled = false;
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 ) );
473 application.GetScene().Add( actor );
475 DALI_TEST_CHECK( gTouchCallBackCalled == false );
477 // connect to its touch signal
478 actor.ConnectSignal( &application, "touched", TestCallback() );
480 application.SendNotification();
481 application.Render(1000);
482 application.SendNotification();
483 application.Render(1000);
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 );
493 application.SendNotification();
494 application.Render(1000);
495 application.SendNotification();
496 application.Render(1000);
498 DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
499 DALI_TEST_CHECK( gTouchCallBackCalled == true );
501 gTouchCallBackCalled = false;
502 application.DisconnectAll();
504 // simulate another touch event
505 application.ProcessEvent( event );
507 DALI_TEST_CHECK( gTouchCallBackCalled == false );
511 int UtcDaliBaseHandleGetTypeNameP(void)
513 TestApplication application;
514 tet_infoline("Testing Dali::BaseHandle::GetTypeName");
516 // get the root layer
517 Actor actor = Actor::New();
519 std::string typeName = actor.GetTypeName();
521 DALI_TEST_CHECK( typeName.size() );
522 DALI_TEST_CHECK( typeName == std::string("Actor") );
526 int UtcDaliBaseHandleGetTypeNameN(void)
529 TestApplication application;
530 tet_infoline("Testing Dali::BaseObject::GetTypeName");
532 std::string typeName = object.GetTypeName();
534 DALI_TEST_CHECK( typeName.empty() );
538 int UtcDaliBaseHandleGetTypeInfoP(void)
540 TestApplication application;
541 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
544 Actor actor = Actor::New();
546 bool ok = actor.GetTypeInfo( info );
547 DALI_TEST_CHECK( ok );
551 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
553 TestApplication application;
554 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
557 tet_result(TET_PASS);
562 int UtcDaliBaseHandleGetTypeInfoN(void)
564 TestApplication application;
565 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
570 bool ok = object.GetTypeInfo( info );
571 DALI_TEST_CHECK( !ok );
575 int UtcDaliBaseHandleGetObjectPtr(void)
577 TestApplication application;
578 tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
580 // get the root layer
581 Actor actor = Actor::New();
583 Dali::RefObject* p = actor.GetObjectPtr();
585 DALI_TEST_CHECK( p != NULL );
589 int UtcDaliBaseHandleBooleanCast(void)
591 TestApplication application;
592 tet_infoline("Testing Dali::BaseHandle::BooleanType");
594 // get the root layer
595 BaseHandle handle = Actor::New();
597 DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
601 int UtcDaliBaseHandleCompareOperatorN(void)
603 TestApplication application;
604 BaseHandle handle1 = Actor::New();
605 BaseHandle handle2 = handle1;
607 DALI_TEST_CHECK( (handle1 < handle2) == false );