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