Revert "Revert "Removed TouchEvent from actor & stage""
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-WeakHandle.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/public-api/dali-core.h>
19 #include <dali-test-suite-utils.h>
20
21 using namespace Dali;
22
23 namespace
24 {
25
26 /*******************************************************************************
27  *
28  * Custom Actor
29  *
30  ******************************************************************************/
31 namespace Impl
32 {
33 struct MyTestCustomActor : public CustomActorImpl
34 {
35   typedef Signal< void ()> SignalType;
36   typedef Signal< void (float)> SignalTypeFloat;
37
38   MyTestCustomActor() : CustomActorImpl( ActorFlags( REQUIRES_TOUCH_EVENTS ) )
39   { }
40
41   virtual ~MyTestCustomActor()
42   { }
43
44   void ResetCallStack()
45   {
46   }
47
48   // From CustomActorImpl
49   virtual void OnSceneConnection( int depth )
50   {
51   }
52   virtual void OnSceneDisconnection()
53   {
54   }
55   virtual void OnChildAdd(Actor& child)
56   {
57   }
58   virtual void OnChildRemove(Actor& child)
59   {
60   }
61   virtual void OnSizeSet(const Vector3& targetSize)
62   {
63   }
64   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize)
65   {
66   }
67   virtual bool OnHoverEvent(const HoverEvent& event)
68   {
69     return true;
70   }
71   virtual bool OnWheelEvent(const WheelEvent& event)
72   {
73     return true;
74   }
75   virtual bool OnKeyEvent(const KeyEvent& event)
76   {
77     return true;
78   }
79   virtual void OnKeyInputFocusGained()
80   {
81   }
82   virtual void OnKeyInputFocusLost()
83   {
84   }
85   virtual Vector3 GetNaturalSize()
86   {
87     return Vector3( 0.0f, 0.0f, 0.0f );
88   }
89
90   virtual float GetHeightForWidth( float width )
91   {
92     return 0.0f;
93   }
94
95   virtual float GetWidthForHeight( float height )
96   {
97     return 0.0f;
98   }
99
100   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container )
101   {
102   }
103
104   virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
105   {
106   }
107
108   virtual void OnCalculateRelayoutSize( Dimension::Type dimension )
109   {
110   }
111
112   virtual float CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
113   {
114     return 0.0f;
115   }
116
117   virtual void OnLayoutNegotiated( float size, Dimension::Type dimension )
118   {
119   }
120
121   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS )
122   {
123     return false;
124   }
125
126 public:
127
128   SignalType mSignal;
129 };
130
131 }; // namespace Impl
132
133 class MyTestCustomActor : public CustomActor
134 {
135 public:
136
137   typedef Signal< void ()> SignalType;
138   typedef Signal< void (float)> SignalTypeFloat;
139
140   MyTestCustomActor()
141   {
142   }
143
144   static MyTestCustomActor New()
145   {
146     Impl::MyTestCustomActor* p = new Impl::MyTestCustomActor;
147     return MyTestCustomActor( *p ); // takes ownership
148   }
149
150   virtual ~MyTestCustomActor()
151   {
152   }
153
154   static MyTestCustomActor DownCast( BaseHandle handle )
155   {
156     MyTestCustomActor result;
157
158     CustomActor custom = Dali::CustomActor::DownCast( handle );
159     if ( custom )
160     {
161       CustomActorImpl& customImpl = custom.GetImplementation();
162
163       Impl::MyTestCustomActor* impl = dynamic_cast<Impl::MyTestCustomActor*>(&customImpl);
164
165       if (impl)
166       {
167         result = MyTestCustomActor(customImpl.GetOwner());
168       }
169     }
170
171     return result;
172   }
173
174   SignalType& GetCustomSignal()
175   {
176     Dali::RefObject& obj = GetImplementation();
177     return static_cast<Impl::MyTestCustomActor&>( obj ).mSignal;
178   }
179
180   MyTestCustomActor(Internal::CustomActor* internal)
181   : CustomActor(internal)
182   {
183   }
184
185   MyTestCustomActor( Impl::MyTestCustomActor& impl )
186   : CustomActor( impl )
187   {
188   }
189 };
190
191 }
192
193 int UtcDaliWeakHandleBaseConstructorVoid(void)
194 {
195   TestApplication application;
196   tet_infoline("Testing Dali::WeakHandleBase::WeakHandleBase()");
197
198   WeakHandleBase object;
199
200   DALI_TEST_CHECK(!object.GetBaseHandle());
201
202   END_TEST;
203 }
204
205 int UtcDaliWeakHandleBaseConstructorWithBaseHandle(void)
206 {
207   TestApplication application;
208   tet_infoline("Testing Dali::WeakHandleBase::WeakHandleBase(BaseHandle)");
209
210   BaseHandle emptyHandle;
211   WeakHandleBase emptyObject(emptyHandle);
212   DALI_TEST_CHECK(!emptyObject.GetBaseHandle());
213
214   Actor actor = Actor::New();
215   WeakHandleBase object(actor);
216   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
217
218   Animation animation = Animation::New( 1.0f );
219   WeakHandleBase animationObject( animation );
220   DALI_TEST_CHECK( animationObject.GetBaseHandle() == animation );
221
222   END_TEST;
223 }
224
225 int UtcDaliWeakHandleBaseCopyConstructor(void)
226 {
227   TestApplication application;
228   tet_infoline("Testing Dali::WeakHandleBase::WeakHandleBase(const WeakHandleBase&)");
229
230   Actor actor = Actor::New();
231   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
232
233   WeakHandleBase object(actor);
234   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
235   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
236
237   WeakHandleBase copy(object);
238   DALI_TEST_CHECK(copy.GetBaseHandle() == actor);
239   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
240
241   END_TEST;
242 }
243
244 int UtcDaliWeakHandleBaseAssignmentOperator(void)
245 {
246   TestApplication application;
247   tet_infoline("Testing Dali::WeakHandleBase::operator=");
248
249   Actor actor = Actor::New();
250   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
251
252   WeakHandleBase object(actor);
253   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
254   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
255
256   WeakHandleBase copy = object;
257   DALI_TEST_CHECK(copy.GetBaseHandle() == actor);
258   DALI_TEST_EQUALS(1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION); // reference count of the actor is not increased
259
260   END_TEST;
261 }
262
263 int UtcDaliWeakHandleBaseMoveConstructor(void)
264 {
265   TestApplication application;
266
267   Actor actor = Actor::New();
268   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
269
270   WeakHandleBase object( actor );
271   DALI_TEST_CHECK( object.GetBaseHandle() == actor);
272   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
273
274   WeakHandleBase move = std::move( object );
275   DALI_TEST_CHECK(move.GetBaseHandle() == actor );
276   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
277   DALI_TEST_CHECK( !object.GetBaseHandle() ); // object moved
278
279   END_TEST;
280 }
281
282 int UtcDaliWeakHandleBaseMoveAssignment(void)
283 {
284   TestApplication application;
285
286   Actor actor = Actor::New();
287   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
288
289   WeakHandleBase object( actor );
290   DALI_TEST_CHECK( object.GetBaseHandle() == actor);
291   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
292
293   WeakHandleBase move;
294   move = std::move( object );
295   DALI_TEST_CHECK(move.GetBaseHandle() == actor );
296   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
297   DALI_TEST_CHECK( !object.GetBaseHandle() ); // object moved
298
299   END_TEST;
300 }
301
302 int UtcDaliWeakHandleBaseEqualityOperatorP(void)
303 {
304   TestApplication application;
305   tet_infoline("Positive Test Dali::WeakHandleBase::operator==");
306
307   WeakHandleBase object;
308   WeakHandleBase theSameObject;
309   DALI_TEST_CHECK(object == theSameObject);
310
311   Actor actor = Actor::New();
312
313   object = WeakHandleBase(actor);
314   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
315
316   theSameObject = object;
317   DALI_TEST_CHECK(theSameObject.GetBaseHandle() == actor);
318   DALI_TEST_CHECK(object == theSameObject);
319
320   END_TEST;
321 }
322
323 int UtcDaliWeakHandleBaseEqualityOperatorN(void)
324 {
325   TestApplication application;
326   tet_infoline("Negative Test Dali::WeakHandleBase::operator==");
327
328   Actor actor = Actor::New();
329
330   WeakHandleBase object(actor);
331   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
332
333   Actor differentActor = Actor::New();
334   WeakHandleBase aDifferentWeakHandleBase(differentActor);
335
336   DALI_TEST_CHECK(!(object == aDifferentWeakHandleBase));
337
338   END_TEST;
339 }
340
341 int UtcDaliWeakHandleBaseInequalityOperatorP(void)
342 {
343   TestApplication application;
344   tet_infoline("Positive Test Dali::WeakHandleBase::operator!=");
345
346   Actor actor = Actor::New();
347
348   WeakHandleBase object(actor);
349   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
350
351   Actor differentActor = Actor::New();
352   WeakHandleBase aDifferentWeakHandleBase(differentActor);
353
354   DALI_TEST_CHECK(object != aDifferentWeakHandleBase);
355   END_TEST;
356 }
357
358 int UtcDaliWeakHandleBaseInequalityOperatorN(void)
359 {
360   TestApplication application;
361   tet_infoline("Negative Test Dali::WeakHandleBase::operator!=");
362
363   Actor actor = Actor::New();
364
365   WeakHandleBase object(actor);
366   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
367
368   WeakHandleBase theSameWeakHandleBase = object;
369
370   DALI_TEST_CHECK(!(object != theSameWeakHandleBase));
371   END_TEST;
372 }
373
374 int UtcDaliWeakHandleBaseGetBaseHandle(void)
375 {
376   TestApplication application;
377   tet_infoline("Testing Dali::WeakHandleBase::GetBaseHandle()");
378
379   Handle emptyHandle;
380   WeakHandleBase emptyObject(emptyHandle);
381   DALI_TEST_CHECK(!emptyObject.GetBaseHandle());
382
383   Actor actor = Actor::New();
384   WeakHandleBase object(actor);
385   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
386
387   WeakHandleBase theSameObject = WeakHandleBase(actor);
388   DALI_TEST_CHECK(object.GetBaseHandle() == theSameObject.GetBaseHandle());
389
390   Actor differentActor = Actor::New();
391   WeakHandleBase aDifferentWeakHandleBase(differentActor);
392   DALI_TEST_CHECK(object.GetBaseHandle() != aDifferentWeakHandleBase.GetBaseHandle());
393
394   Animation animation = Animation::New( 1.0f );
395   WeakHandleBase animationObject( animation );
396   DALI_TEST_CHECK( animationObject.GetBaseHandle() == animation );
397
398   WeakHandleBase theSameAnimationObject = WeakHandleBase( animation );
399   DALI_TEST_CHECK( animationObject.GetBaseHandle() == theSameAnimationObject.GetBaseHandle() );
400
401   Animation differentAnimation = Animation::New( 1.0f );
402   WeakHandleBase aDifferentAnimationObject( differentAnimation );
403   DALI_TEST_CHECK( animationObject.GetBaseHandle() != aDifferentAnimationObject.GetBaseHandle() );
404
405   END_TEST;
406 }
407
408 int UtcDaliWeakHandleBaseReset(void)
409 {
410   TestApplication application;
411   tet_infoline( "Testing Daku::WeakHandleBase::Reset()" );
412
413   Actor actor = Actor::New();
414   WeakHandleBase object(actor);
415   DALI_TEST_CHECK(object.GetBaseHandle() == actor);
416
417   object.Reset();
418
419   DALI_TEST_CHECK(object == WeakHandleBase());
420   DALI_TEST_CHECK(object.GetBaseHandle() == Handle());
421
422   END_TEST;
423 }
424
425 int UtcDaliWeakHandleGetHandle(void)
426 {
427   TestApplication application;
428   tet_infoline("Testing Dali::WeakHandle::GetHandle()");
429
430   Actor actor = Actor::New();
431   WeakHandle<Actor> object(actor);
432   DALI_TEST_CHECK(object.GetHandle() == actor);
433
434   MyTestCustomActor customActor = MyTestCustomActor::New();
435   WeakHandle<MyTestCustomActor> customObject(customActor);
436   DALI_TEST_CHECK(customObject.GetHandle() == customActor);
437
438   DALI_TEST_CHECK(object.GetHandle() != customObject.GetHandle());
439
440   Animation animation = Animation::New( 1.0f );
441   WeakHandle<Animation>  animationObject( animation );
442   DALI_TEST_CHECK( animationObject.GetHandle() == animation );
443
444   animation.Reset();
445   DALI_TEST_CHECK( animationObject.GetHandle() == Animation() );
446
447   END_TEST;
448 }
449
450 int UtcDaliWeakHandleMoveConstructor(void)
451 {
452   TestApplication application;
453
454   Actor actor = Actor::New();
455   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
456
457   WeakHandle<Actor> object( actor );
458   DALI_TEST_CHECK( object.GetHandle() == actor );
459   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
460
461   WeakHandle<Actor> move = std::move( object );
462   DALI_TEST_CHECK( move.GetHandle() == actor );
463   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
464   DALI_TEST_CHECK( !object.GetHandle() ); // object moved
465
466   END_TEST;
467 }
468
469 int UtcDaliWeakHandleMoveAssignment(void)
470 {
471   TestApplication application;
472
473   Actor actor = Actor::New();
474   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
475
476   WeakHandle<Actor> object( actor );
477   DALI_TEST_CHECK( object.GetHandle() == actor );
478   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
479
480   WeakHandle<Actor> move;
481   move = std::move( object );
482   DALI_TEST_CHECK( move.GetHandle() == actor );
483   DALI_TEST_EQUALS( 1, actor.GetBaseObject().ReferenceCount(), TEST_LOCATION ); // reference count of the actor is not increased
484   DALI_TEST_CHECK( !object.GetHandle() ); // object moved
485
486   END_TEST;
487 }