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