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