[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-BaseHandle.cpp
1 /*
2  * Copyright (c) 2014 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 <iostream>
19
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23
24 #include "dali-test-suite-utils/dali-test-suite-utils.h"
25
26 using namespace Dali;
27
28
29 void utc_base_handle_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 // Called after each test
35 void utc_base_handle_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42
43 // Functor to test whether an animation finish signal is emitted
44 struct AnimationFinishCheck
45 {
46   AnimationFinishCheck(bool& signalReceived)
47   : mSignalReceived(signalReceived)
48   {
49   }
50
51   void operator()()
52   {
53     mSignalReceived = true;
54   }
55
56   void Reset()
57   {
58     mSignalReceived = false;
59   }
60
61   void CheckSignalReceived()
62   {
63     if (!mSignalReceived)
64     {
65       tet_printf("Expected Finish signal was not received\n");
66       tet_result(TET_FAIL);
67     }
68     else
69     {
70       tet_result(TET_PASS);
71     }
72   }
73
74   bool& mSignalReceived; // owned by individual tests
75 };
76
77 BaseHandle ImplicitCopyConstructor(BaseHandle passedByValue)
78 {
79   // object + copy + passedByValue, ref count == 3
80   DALI_TEST_CHECK(passedByValue);
81   if (passedByValue)
82   {
83     DALI_TEST_EQUALS(3, passedByValue.GetBaseObject().ReferenceCount(), TEST_LOCATION);
84   }
85
86   return passedByValue;
87 }
88
89 static bool gTouchCallBackCalled;
90
91 struct TestCallback
92 {
93   void operator()()
94   {
95     gTouchCallBackCalled = true;
96   }
97 };
98
99 // Used for testing BaseObject::GetTypeName with an object that is not registered
100 class FakeObject : public BaseObject
101 {
102 };
103 // used for testing ThisIsSaferThanReturningVoidStar
104 class FakeHandle :  public BaseHandle
105 {
106 public:
107
108   void RunTest()
109   {
110     return ThisIsSaferThanReturningVoidStar();
111   }
112 };
113 } // anon namespace
114
115 int UtcDaliBaseHandleConstructorVoid(void)
116 {
117   TestApplication application;
118   tet_infoline("Testing Dali::BaseHandle::BaseHandle()");
119
120   BaseHandle object;
121
122   DALI_TEST_CHECK(!object);
123   END_TEST;
124 }
125
126 int UtcDaliBaseHandleCopyConstructor(void)
127 {
128   TestApplication application;
129   tet_infoline("Testing Dali::BaseHandle::BaseHandle(const BaseHandle&)");
130
131   // Initialize an object, ref count == 1
132   BaseHandle object = Actor::New();
133
134   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
135
136   // Copy the object, ref count == 2
137   BaseHandle copy(object);
138   DALI_TEST_CHECK(copy);
139   if (copy)
140   {
141     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
142   }
143
144   {
145     // Pass by value, and return another copy, ref count == 3
146     BaseHandle anotherCopy = ImplicitCopyConstructor(copy);
147
148     DALI_TEST_CHECK(anotherCopy);
149     if (anotherCopy)
150     {
151       DALI_TEST_EQUALS(3, anotherCopy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
152     }
153   }
154
155   // anotherCopy out of scope, ref count == 2
156   DALI_TEST_CHECK(copy);
157   if (copy)
158   {
159     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
160   }
161   END_TEST;
162 }
163
164 int UtcDaliBaseHandleAssignmentOperator(void)
165 {
166   TestApplication application;
167   tet_infoline("Testing Dali::BaseHandle::operator=");
168
169   BaseHandle object = Actor::New();
170
171   DALI_TEST_CHECK(object);
172   if (object)
173   {
174     DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
175   }
176
177   BaseHandle copy = object;
178
179   DALI_TEST_CHECK(copy);
180   if (copy)
181   {
182     DALI_TEST_EQUALS(2, copy.GetBaseObject().ReferenceCount(), TEST_LOCATION);
183   }
184   END_TEST;
185 }
186
187 int UtcDaliBaseHandleGetBaseObject(void)
188 {
189   TestApplication application;
190   tet_infoline("Testing Dali::BaseHandle::GetBaseObject()");
191
192   BaseHandle object = Actor::New();
193
194   BaseObject& handle = object.GetBaseObject();
195
196   DALI_TEST_EQUALS(1, handle.ReferenceCount(), TEST_LOCATION);
197   END_TEST;
198 }
199
200 int UtcDaliBaseHandleReset(void)
201 {
202   TestApplication application;
203   tet_infoline("Testing Dali::BaseHandle::Reset()");
204
205   // Initialize an object, ref count == 1
206   BaseHandle object = Actor::New();
207
208   DALI_TEST_EQUALS(1, object.GetBaseObject().ReferenceCount(), TEST_LOCATION);
209
210   object.Reset();
211
212   DALI_TEST_CHECK(!object);
213   END_TEST;
214 }
215
216 int UtcDaliBaseHandleEqualityOperator01(void)
217 {
218   TestApplication application;
219   tet_infoline("Positive Test Dali::BaseHandle::operator==");
220
221   BaseHandle object = Actor::New();
222
223   DALI_TEST_CHECK(object);
224
225   BaseHandle theSameBaseHandle = object;
226
227   DALI_TEST_CHECK(object == theSameBaseHandle);
228   END_TEST;
229 }
230
231 int UtcDaliBaseHandleEqualityOperator02(void)
232 {
233   TestApplication application;
234   tet_infoline("Negative Test Dali::BaseHandle::operator==");
235
236   BaseHandle object = Actor::New();
237
238   DALI_TEST_CHECK(object);
239
240   BaseHandle aDifferentBaseHandle = Actor::New();
241
242   DALI_TEST_CHECK(!(object == aDifferentBaseHandle));
243   END_TEST;
244 }
245
246 int UtcDaliBaseHandleInequalityOperator01(void)
247 {
248   TestApplication application;
249   tet_infoline("Positive Test Dali::BaseHandle::operator!=");
250
251   BaseHandle object = Actor::New();
252
253   DALI_TEST_CHECK(object);
254
255   BaseHandle aDifferentBaseHandle = Actor::New();
256
257   DALI_TEST_CHECK(object != aDifferentBaseHandle);
258   END_TEST;
259 }
260
261 int UtcDaliBaseHandleInequalityOperator02(void)
262 {
263   TestApplication application;
264   tet_infoline("Negative Test Dali::BaseHandle::operator!=");
265
266   BaseHandle object = Actor::New();
267
268   DALI_TEST_CHECK(object);
269
270   BaseHandle theSameBaseHandle = object;
271
272   DALI_TEST_CHECK(!(object != theSameBaseHandle));
273   END_TEST;
274 }
275
276 int UtcDaliBaseHandleStlVector(void)
277 {
278   TestApplication application;
279   tet_infoline("Testing Dali::BaseHandle compatibility with std::vector");
280
281   const int TargetVectorSize(5);
282
283   std::vector<Actor> myVector;
284
285   for (int i=0; i<TargetVectorSize; ++i)
286   {
287     Actor actor = Actor::New();
288
289     std::stringstream stream;
290     stream << "Actor " << i+1;
291     actor.SetName(stream.str());
292
293     myVector.push_back(actor);
294   }
295
296   DALI_TEST_EQUALS(TargetVectorSize, static_cast<int>(myVector.size()), TEST_LOCATION);
297
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");
303   END_TEST;
304 }
305
306 int UtcDaliBaseHandleDoAction(void)
307 {
308   TestApplication application;
309   tet_infoline("Positive Test Dali::BaseHandle::UtcDaliBaseHandleDoAction");
310
311   Actor actor = Actor::New();
312   BaseHandle actorObject = actor;
313
314   DALI_TEST_CHECK(actorObject);
315
316   // Check that an invalid command is not performed
317   Property::Map attributes;
318   DALI_TEST_CHECK(actorObject.DoAction("invalidCommand", attributes) == false);
319
320   // Check that the actor is visible
321   actor.SetVisible(true);
322   DALI_TEST_CHECK(actor.IsVisible() == true);
323
324   // Check the actor performed an action to hide itself
325   DALI_TEST_CHECK(actorObject.DoAction("hide", attributes) == true);
326
327   // flush the queue and render once
328   application.SendNotification();
329   application.Render();
330
331   // Check that the actor is now invisible
332   DALI_TEST_CHECK(actor.IsVisible() == false);
333
334   // Check the actor performed an action to show itself
335   DALI_TEST_CHECK(actorObject.DoAction("show", attributes) == true);
336
337   // flush the queue and render once
338   application.SendNotification();
339   application.Render();
340
341   // Check that the actor is now visible
342   DALI_TEST_CHECK(actor.IsVisible() == true);
343
344   Stage::GetCurrent().Add(actor);
345
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;
350
351   DALI_TEST_CHECK(animationObject);
352
353   // Check the current animation duration is 1 second
354   DALI_TEST_EQUALS(animation.GetDuration(), durationSeconds, TEST_LOCATION);
355
356   Vector3 targetPosition(100.0f, 100.0f, 100.0f);
357   animation.AnimateTo(Property(actor, Actor::Property::POSITION), targetPosition, AlphaFunction::LINEAR);
358
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;
363
364   // Check the animation performed an action to play itself with the specified duration of 2 seconds
365   animationObject.DoAction("play", attributes);
366
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 );
374
375   application.SendNotification();
376   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) /* half of time */);
377   DALI_TEST_EQUALS( signalReceived, false, TEST_LOCATION );
378
379   // pause
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 );
384
385   // continue
386   animationObject.DoAction("play", attributes);
387   application.SendNotification();
388   application.Render(static_cast<uint32_t>(newDurationSeconds * 500.0f) + 1u/*just beyond the animation duration*/);
389
390   // We expect the animation to finish
391   application.SendNotification();
392   finishCheck.CheckSignalReceived();
393   DALI_TEST_EQUALS( actor.GetCurrentPosition(), targetPosition, TEST_LOCATION );
394
395   // play again
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 );
405
406   // Check the new animation duration is 2 seconds
407   DALI_TEST_EQUALS(animation.GetDuration(), newDurationSeconds, TEST_LOCATION);
408   END_TEST;
409 }
410
411
412 int UtcDaliBaseHandleConnectSignal(void)
413 {
414   TestApplication application;
415   tet_infoline("Testing Dali::BaseHandle::ConnectSignal");
416
417   gTouchCallBackCalled = false;
418
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 );
425
426   Stage::GetCurrent().Add( actor );
427
428   DALI_TEST_CHECK( gTouchCallBackCalled == false );
429
430   // connect to its touch signal
431   actor.ConnectSignal( &application, "touched", TestCallback() );
432
433   application.SendNotification();
434   application.Render(1000);
435   application.SendNotification();
436   application.Render(1000);
437
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 );
445
446   application.SendNotification();
447   application.Render(1000);
448   application.SendNotification();
449   application.Render(1000);
450
451   DALI_TEST_CHECK( application.GetConnectionCount() > 0 );
452   DALI_TEST_CHECK( gTouchCallBackCalled == true );
453
454   gTouchCallBackCalled = false;
455   application.DisconnectAll();
456
457   // simulate another touch event
458   application.ProcessEvent( event );
459
460   DALI_TEST_CHECK( gTouchCallBackCalled == false );
461   END_TEST;
462 }
463
464 int UtcDaliBaseHandleGetTypeNameP(void)
465 {
466   TestApplication application;
467   tet_infoline("Testing Dali::BaseHandle::GetTypeName");
468
469   // get the root layer
470   Actor actor = Actor::New();
471
472   std::string typeName = actor.GetTypeName();
473
474   DALI_TEST_CHECK( typeName.size() );
475   DALI_TEST_CHECK( typeName == std::string("Actor") );
476   END_TEST;
477 }
478
479 int UtcDaliBaseHandleGetTypeNameN(void)
480 {
481
482   TestApplication application;
483   tet_infoline("Testing Dali::BaseObject::GetTypeName");
484   FakeObject object;
485   std::string typeName = object.GetTypeName();
486
487   DALI_TEST_CHECK( typeName.empty() );
488   END_TEST;
489 }
490
491 int UtcDaliBaseHandleGetTypeInfoP(void)
492 {
493   TestApplication application;
494   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
495
496   Dali::TypeInfo info;
497   Actor actor = Actor::New();
498
499   bool ok = actor.GetTypeInfo( info );
500   DALI_TEST_CHECK( ok );
501   END_TEST;
502 }
503
504 int UtcDaliBaseHandleThisIsSaferThanReturningVoidStar(void)
505 {
506   TestApplication application;
507   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
508   FakeHandle handle;
509   handle.RunTest();
510   tet_result(TET_PASS);
511   END_TEST;
512
513 }
514
515 int UtcDaliBaseHandleGetTypeInfoN(void)
516 {
517   TestApplication application;
518   tet_infoline("Testing Dali::BaseHandle::GetTypeInfo");
519
520   Dali::TypeInfo info;
521   FakeObject object;
522
523   bool ok = object.GetTypeInfo( info );
524   DALI_TEST_CHECK( !ok );
525   END_TEST;
526 }
527
528 int UtcDaliBaseHandleGetObjectPtr(void)
529 {
530   TestApplication application;
531   tet_infoline("Testing Dali::BaseHandle::GetObjectPtr");
532
533   // get the root layer
534   Actor actor = Actor::New();
535
536   Dali::RefObject* p = actor.GetObjectPtr();
537
538   DALI_TEST_CHECK( p != NULL );
539   END_TEST;
540 }
541
542 int UtcDaliBaseHandleBooleanCast(void)
543 {
544   TestApplication application;
545   tet_infoline("Testing Dali::BaseHandle::BooleanType");
546
547   // get the root layer
548   BaseHandle handle = Actor::New();
549
550   DALI_TEST_CHECK( static_cast<BaseHandle::BooleanType>( handle ) );
551   END_TEST;
552 }
553
554 int UtcDaliBaseHandleCompareOperatorN(void)
555 {
556   TestApplication application;
557   BaseHandle handle1 = Actor::New();
558   BaseHandle handle2 = handle1;
559
560   DALI_TEST_CHECK( (handle1 < handle2) == false );
561
562   END_TEST;
563 }