Rename OnStage signals and related internal changes
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / test-custom-actor.h
1 #ifndef TEST_CUSTOM_ACTOR_H
2 #define TEST_CUSTOM_ACTOR_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/public-api/dali-core.h>
22 #include <dali/devel-api/object/property-helper-devel.h>
23 #include <dali/integration-api/scene.h>
24
25 extern std::vector< std::string > MasterCallStack;
26 extern bool gOnRelayout;
27
28 namespace Test
29 {
30 namespace Impl
31 {
32 struct TestCustomActor;
33 }
34
35 /**
36  * Test custom actor handle
37  */
38 class TestCustomActor : public Dali::CustomActor
39 {
40 public:
41   enum PropertyRange
42   {
43     PROPERTY_START_INDEX = Dali::PROPERTY_REGISTRATION_START_INDEX,
44     PROPERTY_END_INDEX =   PROPERTY_START_INDEX + 1000
45   };
46
47   struct Property
48   {
49     enum Type
50     {
51       TEST_PROPERTY1 = PROPERTY_START_INDEX,
52       TEST_PROPERTY2
53     };
54   };
55
56   TestCustomActor();
57   static TestCustomActor New();
58   static TestCustomActor NewNegoSize();
59   static TestCustomActor NewVariant1( Actor childToAdd );
60   static TestCustomActor NewVariant2();
61   static TestCustomActor NewVariant3( Actor childToAdd );
62   static TestCustomActor NewVariant4();
63   static TestCustomActor NewVariant5( Dali::Integration::Scene scene );
64   static TestCustomActor NewVariant6( Dali::Integration::Scene scene );
65   static TestCustomActor NewVariant7( const char* name );
66   static TestCustomActor NewVariant8( Actor rival );
67   static TestCustomActor DownCast( Dali::BaseHandle handle );
68
69   virtual ~TestCustomActor();
70   Impl::TestCustomActor& GetImpl();
71
72   TestCustomActor( TestCustomActor&& rhs ) = default;
73   TestCustomActor& operator=( TestCustomActor&& rhs ) = default;
74
75   std::vector< std::string >& GetMethodsCalled();
76   void ResetCallStack();
77   void SetDaliProperty(std::string s);
78   Dali::Vector3 GetSize();
79   Dali::Vector3 GetTargetSize();
80   virtual Dali::Vector3 GetNaturalSize();
81   virtual float GetHeightForWidth( float width );
82   virtual float GetWidthForHeight( float height );
83   virtual void OnRelayout( const Dali::Vector2& size, Dali::RelayoutContainer& container );
84   virtual void OnLayoutNegotiated( float size, Dali::Dimension::Type dimension );
85   virtual void OnCalculateRelayoutSize( Dali::Dimension::Type dimension );
86   void TestRelayoutRequest();
87   float TestGetHeightForWidthBase( float width );
88   float TestGetWidthForHeightBase( float height );
89   float TestCalculateChildSizeBase( const Dali::Actor& child, Dali::Dimension::Type dimension );
90   bool TestRelayoutDependentOnChildrenBase( Dali::Dimension::Type dimension );
91   uint32_t GetDepth();
92
93 private:
94   TestCustomActor( Impl::TestCustomActor& impl );
95   explicit TestCustomActor(Dali::Internal::CustomActor* internal);
96 };
97
98
99 namespace DevelTestCustomActor
100 {
101 namespace Property
102 {
103 enum Type
104 {
105   TEST_PROPERTY1 = Test::TestCustomActor::Property::TEST_PROPERTY1,
106   TEST_PROPERTY2 = Test::TestCustomActor::Property::TEST_PROPERTY2,
107   DEVEL_TEST_PROPERTY3 = TEST_PROPERTY2+1,
108   DEVEL_TEST_PROPERTY4 = TEST_PROPERTY2+2,
109   DEVEL_TEST_PROPERTY5 = TEST_PROPERTY2+3,
110   DEVEL_TEST_PROPERTY6 = TEST_PROPERTY2+4
111 };
112
113 } // Namespace Property
114 } // Namespace DevelTestCustomActor
115
116
117 // TypeRegistry needs custom actor Implementations to have the same name (namespaces are ignored so we use one here)
118 namespace Impl
119 {
120
121 struct TestCustomActor : public Dali::CustomActorImpl
122 {
123 public:
124   /**
125    * Constructor
126    */
127   TestCustomActor();
128   TestCustomActor(bool nego);
129   virtual ~TestCustomActor();
130   void Initialize( const char* name = NULL );
131   virtual void OnInitialize( const char* name );
132   void ResetCallStack();
133   void AddToCallStacks( const char* method );
134   virtual void OnSceneConnection( int depth );
135   virtual void OnSceneDisconnection();
136   virtual void OnChildAdd(Dali::Actor& child);
137   virtual void OnChildRemove(Dali::Actor& child);
138   virtual void OnPropertySet( Dali::Property::Index index, Dali::Property::Value propertyValue );
139   virtual void OnSizeSet(const Dali::Vector3& targetSize);
140   virtual void OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize);
141   virtual bool OnTouchEvent(const Dali::TouchEvent& event);
142   virtual bool OnHoverEvent(const Dali::HoverEvent& event);
143   virtual bool OnWheelEvent(const Dali::WheelEvent& event);
144   virtual bool OnKeyEvent(const Dali::KeyEvent& event);
145   virtual void OnKeyInputFocusGained();
146   virtual void OnKeyInputFocusLost();
147   virtual Dali::Vector3 GetNaturalSize();
148   virtual float GetHeightForWidth( float width );
149   virtual float GetWidthForHeight( float height );
150   virtual void OnRelayout( const Dali::Vector2& size, Dali::RelayoutContainer& container );
151   virtual void OnSetResizePolicy( Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension );
152   virtual void OnCalculateRelayoutSize( Dali::Dimension::Type dimension );
153   virtual float CalculateChildSize( const Dali::Actor& child, Dali::Dimension::Type dimension );
154   virtual void OnLayoutNegotiated( float size, Dali::Dimension::Type dimension );
155   virtual bool RelayoutDependentOnChildren( Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS );
156   static void SetProperty( Dali::BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value );
157   static Dali::Property::Value GetProperty( Dali::BaseObject* object, Dali::Property::Index index );
158
159   void SetDaliProperty(std::string s);
160   void TestRelayoutRequest();
161   float TestGetHeightForWidthBase( float width );
162   float TestGetWidthForHeightBase( float height );
163   float TestCalculateChildSizeBase( const Dali::Actor& child, Dali::Dimension::Type dimension );
164   bool TestRelayoutDependentOnChildrenBase( Dali::Dimension::Type dimension );
165
166 public:
167   Dali::Property::Index mDaliProperty;
168   std::vector< std::string > mMethodsCalled;
169   Dali::Vector3         mSizeSet;
170   Dali::Vector3         mTargetSize;
171   bool                  mNego;
172   uint32_t          mDepth;
173
174   float         prop1;
175   Dali::Vector4 prop2;
176   Dali::Vector4 develProp3;
177   int           develProp4;
178   float         develProp5;
179   float         develProp6;
180 };
181
182 inline TestCustomActor& GetImpl( Test::TestCustomActor& handle )
183 {
184   DALI_ASSERT_ALWAYS( handle );
185   Dali::RefObject& object = handle.GetImplementation();
186   return static_cast<TestCustomActor&>( object );
187 }
188
189 inline const TestCustomActor& GetImpl( const Test::TestCustomActor& handle )
190 {
191   DALI_ASSERT_ALWAYS( handle );
192   const Dali::RefObject& object = handle.GetImplementation();
193   return static_cast<const TestCustomActor&>( object );
194 }
195
196
197 /**
198  * Variant which adds a new child during OnSceneConnection
199  */
200 struct TestCustomActorVariant1 : public TestCustomActor
201 {
202   /**
203    * Constructor
204    */
205   TestCustomActorVariant1( Dali::Actor childToAdd )
206   : mChildToAdd( childToAdd )
207   {
208   }
209
210   // From CustomActorImpl
211   virtual void OnSceneConnection( int depth )
212   {
213     // Chain up first
214     TestCustomActor::OnSceneConnection( depth );
215
216     // Add the child
217     Self().Add( mChildToAdd );
218   }
219
220   Dali::Actor mChildToAdd;
221 };
222
223 /**
224  * Variant which removes children during OnSceneConnection
225  */
226 struct TestCustomActorVariant2 : public TestCustomActor
227 {
228   /**
229    * Constructor
230    */
231   TestCustomActorVariant2()
232   {
233   }
234
235   // From CustomActorImpl
236   virtual void OnSceneConnection( int depth )
237   {
238     // Chain up first
239     TestCustomActor::OnSceneConnection( depth );
240
241     // Remove all the children
242     for( uint32_t i=0, num=Self().GetChildCount(); i<num; ++i )
243     {
244       Self().Remove( Self().GetChildAt(0) );
245     }
246   }
247 };
248
249 /**
250  * Variant which adds a new child during OnSceneDisconnection
251  */
252 struct TestCustomActorVariant3 : public TestCustomActor
253 {
254   /**
255    * Constructor
256    */
257   TestCustomActorVariant3( Dali::Actor childToAdd )
258   : mChildToAdd( childToAdd )
259   {
260   }
261
262   // From CustomActorImpl
263   virtual void OnSceneDisconnection()
264   {
265     // Chain up first
266     TestCustomActor::OnSceneDisconnection();
267
268     // Add the child
269     Self().Add( mChildToAdd );
270   }
271
272   Dali::Actor mChildToAdd;
273 };
274
275 /**
276  * Variant which removes children during OnSceneDisconnection
277  */
278 struct TestCustomActorVariant4 : public TestCustomActor
279 {
280   /**
281    * Constructor
282    */
283   TestCustomActorVariant4()
284   {
285   }
286
287   // From CustomActorImpl
288   virtual void OnSceneDisconnection()
289   {
290     // Chain up first
291     TestCustomActor::OnSceneDisconnection();
292
293     // Remove all the children
294     for( uint32_t i=0, num=Self().GetChildCount(); i<num; ++i )
295     {
296       Self().Remove( Self().GetChildAt(0) );
297     }
298   }
299 };
300
301 /**
302  * Variant which removes its parent from Stage during OnSceneConnection
303  */
304 struct TestCustomActorVariant5 : public TestCustomActor
305 {
306   /**
307    * Constructor
308    */
309   TestCustomActorVariant5(Dali::Integration::Scene scene)
310   : mScene(scene)
311   {
312   }
313
314   // From CustomActorImpl
315   virtual void OnSceneConnection( int depth )
316   {
317     // Chain up first
318     TestCustomActor::OnSceneConnection( depth );
319
320     // Take parent off-stage
321     Dali::Actor parent = Self().GetParent();
322     if ( parent )
323     {
324       mScene.Remove( parent );
325     }
326   }
327
328 private:
329   Dali::Integration::Scene mScene;
330 };
331
332 /**
333  * Variant which adds its parent to Stage during OnSceneDisconnection
334  */
335 struct TestCustomActorVariant6 : public TestCustomActor
336 {
337   /**
338    * Constructor
339    */
340   TestCustomActorVariant6(Dali::Integration::Scene scene)
341   : mScene(scene)
342   {
343   }
344
345   // From CustomActorImpl
346   virtual void OnSceneDisconnection()
347   {
348     // Chain up first
349     TestCustomActor::OnSceneDisconnection();
350
351     // Put parent on-stage
352     Dali::Actor parent = Self().GetParent();
353     if ( parent )
354     {
355       mScene.Add( parent );
356     }
357   }
358
359 private:
360   Dali::Integration::Scene mScene;
361 };
362
363 /**
364  * Variant which reparents its children into a separate container
365  */
366 struct TestCustomActorVariant7 : public TestCustomActor
367 {
368   /**
369    * Constructor
370    */
371   TestCustomActorVariant7()
372   {
373   }
374
375   virtual void OnInitialize( const char* name )
376   {
377     // We need to do this early, before the OnChildAdd is recorded
378     Self().SetProperty( Dali::Actor::Property::NAME, name );
379
380     mContainer = Dali::Actor::New();
381     mContainer.SetProperty( Dali::Actor::Property::NAME, "Container" );
382     Self().Add( mContainer );
383   }
384
385   // From CustomActorImpl
386   virtual void OnChildAdd(Dali::Actor& child)
387   {
388     // Chain up first
389     TestCustomActor::OnChildAdd(child);
390
391     // Reparent child
392     if ( child != mContainer )
393     {
394       mContainer.Add( child );
395     }
396   }
397
398   Dali::Actor mContainer;
399 };
400
401 /**
402  * Variant which attempts to interfere with the reparenting of a child to another container
403  */
404 struct TestCustomActorVariant8 : public TestCustomActor
405 {
406   /**
407    * Constructor
408    */
409   TestCustomActorVariant8( Dali::Actor rival )
410   : mRivalContainer( rival )
411   {
412   }
413
414   // From CustomActorImpl
415   virtual void OnChildRemove(Dali::Actor& child)
416   {
417     // Chain up first
418     TestCustomActor::OnChildRemove(child);
419
420     mRivalContainer.Remove( child ); // attempt to block reparenting to rival (should be a NOOP)
421   }
422
423   Dali::Actor mRivalContainer;
424 };
425
426 // Need a class that doesn't override virtual methods
427 class SimpleTestCustomActor : public Dali::CustomActorImpl
428 {
429 public:
430
431   /**
432    * Constructor
433    */
434   SimpleTestCustomActor()
435   : CustomActorImpl( ActorFlags( REQUIRES_TOUCH_EVENTS | DISABLE_SIZE_NEGOTIATION ) )
436   {
437   }
438
439   /**
440    * Destructor
441    */
442   virtual ~SimpleTestCustomActor()
443   {
444   }
445
446   // From CustomActorImpl
447   virtual void OnSceneConnection( int depth )
448   {
449   }
450   virtual void OnSceneDisconnection()
451   {
452   }
453   virtual void OnChildAdd(Dali::Actor& child)
454   {
455   }
456   virtual void OnChildRemove(Dali::Actor& child)
457   {
458   }
459   virtual void OnSizeSet(const Dali::Vector3& targetSize)
460   {
461   }
462   virtual void OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize)
463   {
464   }
465   virtual bool OnTouchEvent(const Dali::TouchEvent& event)
466   {
467     return true;
468   }
469   virtual bool OnHoverEvent(const Dali::HoverEvent& event)
470   {
471     return true;
472   }
473   virtual bool OnWheelEvent(const Dali::WheelEvent& event)
474   {
475     return true;
476   }
477   virtual bool OnKeyEvent(const Dali::KeyEvent& event)
478   {
479     return true;
480   }
481   virtual void OnKeyInputFocusGained()
482   {
483   }
484   virtual void OnKeyInputFocusLost()
485   {
486   }
487
488   virtual Dali::Vector3 GetNaturalSize()
489   {
490     return Dali::Vector3( 0.0f, 0.0f, 0.0f );
491   }
492
493   virtual float GetHeightForWidth( float width )
494   {
495     return 0.0f;
496   }
497
498   virtual float GetWidthForHeight( float height )
499   {
500     return 0.0f;
501   }
502
503   virtual void OnRelayout( const Dali::Vector2& size, Dali::RelayoutContainer& container )
504   {
505   }
506
507   virtual void OnSetResizePolicy( Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension )
508   {
509   }
510
511   virtual void OnCalculateRelayoutSize( Dali::Dimension::Type dimension )
512   {
513   }
514
515   virtual float CalculateChildSize( const Dali::Actor& child, Dali::Dimension::Type dimension )
516   {
517     return 0.0f;
518   }
519
520   virtual void OnLayoutNegotiated( float size, Dali::Dimension::Type dimension )
521   {
522   }
523
524   virtual bool RelayoutDependentOnChildren( Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS )
525   {
526     return false;
527   }
528 };
529
530 } //namespace Impl
531 } //namespace Test
532
533 #endif // TEST_CUSTOM_ACTOR_H