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