2 * Copyright (c) 2014 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 UtcDaliBaseHandleGetBaseObject(void)
189 TestApplication application;
190 tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
192 BaseHandle object = Actor::New();
194 BaseObject& handle = object.GetBaseObject();
196 DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
200 int UtcDaliBaseHandleReset(void)
202 TestApplication application;
203 tet_infoline("Testing Dali::BaseHandle::Reset()");
205 // Initialize an object, ref count == 1
206 BaseHandle object = Actor::New();
208 DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
212 DALI_TEST_CHECK(!object);
216 int UtcDaliBaseHandleEqualityOperator01(void)
218 TestApplication application;
219 tet_infoline("Positive Test Dali::BaseHandle::operator==");
221 BaseHandle object = Actor::New();
223 DALI_TEST_CHECK(object);
225 BaseHandle theSameBaseHandle = object;
227 DALI_TEST_CHECK(object == theSameBaseHandle);
231 int UtcDaliBaseHandleEqualityOperator02(void)
233 TestApplication application;
234 tet_infoline("Negative Test Dali::BaseHandle::operator==");
236 BaseHandle object = Actor::New();
238 DALI_TEST_CHECK(object);
240 BaseHandle aDifferentBaseHandle = Actor::New();
242 DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
246 int UtcDaliBaseHandleInequalityOperator01(void)
248 TestApplication application;
249 tet_infoline("Positive Test Dali::BaseHandle::operator!=");
251 BaseHandle object = Actor::New();
253 DALI_TEST_CHECK(object);
255 BaseHandle aDifferentBaseHandle = Actor::New();
257 DALI_TEST_CHECK(object != aDifferentBaseHandle);
261 int UtcDaliBaseHandleInequalityOperator02(void)
263 TestApplication application;
264 tet_infoline("Negative Test Dali::BaseHandle::operator!=");
266 BaseHandle object = Actor::New();
268 DALI_TEST_CHECK(object);
270 BaseHandle theSameBaseHandle = object;
272 DALI_TEST_CHECK(!(object != theSameBaseHandle));
276 int UtcDaliBaseHandleStlVector(void)
278 TestApplication application;
279 tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
281 const int TargetVectorSize(5);
283 std::vector<Actor> myVector;
285 for (int i=0; i<TargetVectorSize; ++i)
287 Actor actor = Actor::New();
289 std::stringstream stream;
290 stream << "Actor " << i+1;
291 actor.SetName(stream.str());
293 myVector.push_back(actor);
296 DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
298 DALI_TEST_CHECK(myVector[0].GetName() == "Actor 1");
299 DALI_TEST_CHECK(myVector[1].GetName() == "Actor 2");
300 DALI_TEST_CHECK(myVector[2].GetName() == "Actor 3");
301 DALI_TEST_CHECK(myVector[3].GetName() == "Actor 4");
302 DALI_TEST_CHECK(myVector[4].GetName() == "Actor 5");
306 int UtcDaliBaseHandleDoAction(void)
308 TestApplication application;
309 tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
311 Actor actor = Actor::New();
312 BaseHandle actorObject = actor;
314 DALI_TEST_CHECK(actorObject);
316 // Check that an invalid command is not performed
317 Property::Map attributes;
318 DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
320 // Check that the actor is visible
321 actor.SetVisible(true);
322 DALI_TEST_CHECK(actor.IsVisible() == true);
324 // Check the actor performed an action to hide itself
325 DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
327 // flush the queue and render once
328 application.SendNotification();
329 application.Render();
331 // Check that the actor is now invisible
332 DALI_TEST_CHECK(actor.IsVisible() == false);
334 // Check the actor performed an action to show itself
335 DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
337 // flush the queue and render once
338 application.SendNotification();
339 application.Render();
341 // Check that the actor is now visible
342 DALI_TEST_CHECK(actor.IsVisible() == true);
344 Stage::GetCurrent().Add(actor);
346 // Build an animation with initial duration of 1 second
347 float durationSeconds(1.0f);
348 Animation animation = Animation::New(durationSeconds);
349 BaseHandle animationObject = animation;
351 DALI_TEST_CHECK(animationObject);
353 // Check the current animation duration is 1 second
354 DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
356 Vector3 targetPosition(100.0f, 100.0f, 100.0f);
357 animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
359 // Set the new duration to be 2 seconds
360 float newDurationSeconds(2.0f);
361 Property::Value newDurationSecondsValue = Property::Value( newDurationSeconds );
362 attributes["duration"] = newDurationSecondsValue;
364 // Check the animation performed an action to play itself with the specified duration of 2 seconds
365 animationObject.DoAction("play", attributes);
367 bool signalReceived(false);
368 AnimationFinishCheck finishCheck(signalReceived);
369 // use the handle API to connect the signal
370 animation.ConnectSignal( &application, "finished", finishCheck );
371 // just for coverage connect to non-existant signal as well
372 animation.ConnectSignal( &application, "foo", finishCheck );
373 DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
375 application.SendNotification();
376 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
377 DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
380 animationObject.DoAction("pause", attributes);
381 application.SendNotification();
382 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
383 DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
386 animationObject.DoAction("play", attributes);
387 application.SendNotification();
388 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
390 // We expect the animation to finish
391 application.SendNotification();
392 finishCheck.CheckSignalReceived();
393 DALI_TEST_EQUALS( actor.GetCurrentPosition(), targetPosition, TEST_LOCATION );
396 signalReceived = false;
397 animationObject.DoAction("play", attributes);
398 DALI_TEST_EQUALS(animation.GetCurrentProgress(), 0.f, TEST_LOCATION);
399 application.SendNotification();
400 application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
401 animationObject.DoAction("stop", attributes);
402 application.SendNotification();
403 application.Render(static_cast<uint32_t>(newDurationSeconds * 1000.0f) /* full time */);
404 DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
406 // Check the new animation duration is 2 seconds
407 DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
412 int UtcDaliBaseHandleConnectSignal(void)
414 TestApplication application;
415 tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
417 gTouchCallBackCalled = false;
419 // get the root layer
420 Actor actor = Actor::New();
421 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
422 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
423 actor.SetPosition( 240, 400 );
424 actor.SetSize( 100, 100 );
426 Stage::GetCurrent().Add( actor );
428 DALI_TEST_CHECK( gTouchCallBackCalled == false );
430 // connect to its touch signal
431 actor.ConnectSignal( &application, "touched", TestCallback() );
433 application.SendNotification();
434 application.Render(1000);
435 application.SendNotification();
436 application.Render(1000);
438 // simulate a touch event
439 Dali::Integration::Point point;
440 point.SetState( PointState::DOWN );
441 point.SetScreenPosition( Vector2( 240, 400 ) );
442 Dali::Integration::TouchEvent event;
443 event.AddPoint( point );
444 application.ProcessEvent( event );
446 application.SendNotification();
447 application.Render(1000);
448 application.SendNotification();
449 application.Render(1000);
451 DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
452 DALI_TEST_CHECK( gTouchCallBackCalled == true );
454 gTouchCallBackCalled = false;
455 application.DisconnectAll();
457 // simulate another touch event
458 application.ProcessEvent( event );
460 DALI_TEST_CHECK( gTouchCallBackCalled == false );
464 int UtcDaliBaseHandleGetTypeNameP(void)
466 TestApplication application;
467 tet_infoline("Testing Dali::BaseHandle::GetTypeName");
469 // get the root layer
470 Actor actor = Actor::New();
472 std::string typeName = actor.GetTypeName();
474 DALI_TEST_CHECK( typeName.size() );
475 DALI_TEST_CHECK( typeName == std::string("Actor") );
479 int UtcDaliBaseHandleGetTypeNameN(void)
482 TestApplication application;
483 tet_infoline("Testing Dali::BaseObject::GetTypeName");
485 std::string typeName = object.GetTypeName();
487 DALI_TEST_CHECK( typeName.empty() );
491 int UtcDaliBaseHandleGetTypeInfoP(void)
493 TestApplication application;
494 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
497 Actor actor = Actor::New();
499 bool ok = actor.GetTypeInfo( info );
500 DALI_TEST_CHECK( ok );
504 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
506 TestApplication application;
507 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
510 tet_result(TET_PASS);
515 int UtcDaliBaseHandleGetTypeInfoN(void)
517 TestApplication application;
518 tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
523 bool ok = object.GetTypeInfo( info );
524 DALI_TEST_CHECK( !ok );
528 int UtcDaliBaseHandleGetObjectPtr(void)
530 TestApplication application;
531 tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
533 // get the root layer
534 Actor actor = Actor::New();
536 Dali::RefObject* p = actor.GetObjectPtr();
538 DALI_TEST_CHECK( p != NULL );
542 int UtcDaliBaseHandleBooleanCast(void)
544 TestApplication application;
545 tet_infoline("Testing Dali::BaseHandle::BooleanType");
547 // get the root layer
548 BaseHandle handle = Actor::New();
550 DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
554 int UtcDaliBaseHandleCompareOperatorN(void)
556 TestApplication application;
557 BaseHandle handle1 = Actor::New();
558 BaseHandle handle2 = handle1;
560 DALI_TEST_CHECK( (handle1 < handle2) == false );