change Object::OnPropertySet() interface signature.
[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   void OnSceneConnection( int depth ) override;
135   void OnSceneDisconnection() override;
136   void OnChildAdd(Dali::Actor& child) override;
137   void OnChildRemove(Dali::Actor& child) override;
138   void OnPropertySet( Dali::Property::Index index, const Dali::Property::Value& propertyValue ) override;
139   void OnSizeSet(const Dali::Vector3& targetSize) override;
140   void OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize) override;
141   bool OnHoverEvent(const Dali::HoverEvent& event) override;
142   bool OnWheelEvent(const Dali::WheelEvent& event) override;
143   bool OnKeyEvent(const Dali::KeyEvent& event) override;
144   virtual void OnKeyInputFocusGained();
145   virtual void OnKeyInputFocusLost();
146   Dali::Vector3 GetNaturalSize() override;
147   float GetHeightForWidth( float width ) override;
148   float GetWidthForHeight( float height ) override;
149   void OnRelayout( const Dali::Vector2& size, Dali::RelayoutContainer& container ) override;
150   void OnSetResizePolicy( Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension ) override;
151   void OnCalculateRelayoutSize( Dali::Dimension::Type dimension ) override;
152   float CalculateChildSize( const Dali::Actor& child, Dali::Dimension::Type dimension ) override;
153   void OnLayoutNegotiated( float size, Dali::Dimension::Type dimension ) override;
154   bool RelayoutDependentOnChildren( Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS ) override;
155   static void SetProperty( Dali::BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value );
156   static Dali::Property::Value GetProperty( Dali::BaseObject* object, Dali::Property::Index index );
157
158   void SetDaliProperty(std::string s);
159   void TestRelayoutRequest();
160   float TestGetHeightForWidthBase( float width );
161   float TestGetWidthForHeightBase( float height );
162   float TestCalculateChildSizeBase( const Dali::Actor& child, Dali::Dimension::Type dimension );
163   bool TestRelayoutDependentOnChildrenBase( Dali::Dimension::Type dimension );
164
165 public:
166   Dali::Property::Index mDaliProperty;
167   std::vector< std::string > mMethodsCalled;
168   Dali::Vector3         mSizeSet;
169   Dali::Vector3         mTargetSize;
170   bool                  mNego;
171   uint32_t          mDepth;
172
173   float         prop1;
174   Dali::Vector4 prop2;
175   Dali::Vector4 develProp3;
176   int           develProp4;
177   float         develProp5;
178   float         develProp6;
179 };
180
181 inline TestCustomActor& GetImpl( Test::TestCustomActor& handle )
182 {
183   DALI_ASSERT_ALWAYS( handle );
184   Dali::RefObject& object = handle.GetImplementation();
185   return static_cast<TestCustomActor&>( object );
186 }
187
188 inline const TestCustomActor& GetImpl( const Test::TestCustomActor& handle )
189 {
190   DALI_ASSERT_ALWAYS( handle );
191   const Dali::RefObject& object = handle.GetImplementation();
192   return static_cast<const TestCustomActor&>( object );
193 }
194
195
196 /**
197  * Variant which adds a new child during OnSceneConnection
198  */
199 struct TestCustomActorVariant1 : public TestCustomActor
200 {
201   /**
202    * Constructor
203    */
204   TestCustomActorVariant1( Dali::Actor childToAdd )
205   : mChildToAdd( childToAdd )
206   {
207   }
208
209   // From CustomActorImpl
210   void OnSceneConnection( int depth ) override
211   {
212     // Chain up first
213     TestCustomActor::OnSceneConnection( depth );
214
215     // Add the child
216     Self().Add( mChildToAdd );
217   }
218
219   Dali::Actor mChildToAdd;
220 };
221
222 /**
223  * Variant which removes children during OnSceneConnection
224  */
225 struct TestCustomActorVariant2 : public TestCustomActor
226 {
227   /**
228    * Constructor
229    */
230   TestCustomActorVariant2()
231   {
232   }
233
234   // From CustomActorImpl
235   virtual void OnSceneConnection( int depth )
236   {
237     // Chain up first
238     TestCustomActor::OnSceneConnection( depth );
239
240     // Remove all the children
241     for( uint32_t i=0, num=Self().GetChildCount(); i<num; ++i )
242     {
243       Self().Remove( Self().GetChildAt(0) );
244     }
245   }
246 };
247
248 /**
249  * Variant which adds a new child during OnSceneDisconnection
250  */
251 struct TestCustomActorVariant3 : public TestCustomActor
252 {
253   /**
254    * Constructor
255    */
256   TestCustomActorVariant3( Dali::Actor childToAdd )
257   : mChildToAdd( childToAdd )
258   {
259   }
260
261   // From CustomActorImpl
262   virtual void OnSceneDisconnection()
263   {
264     // Chain up first
265     TestCustomActor::OnSceneDisconnection();
266
267     // Add the child
268     Self().Add( mChildToAdd );
269   }
270
271   Dali::Actor mChildToAdd;
272 };
273
274 /**
275  * Variant which removes children during OnSceneDisconnection
276  */
277 struct TestCustomActorVariant4 : public TestCustomActor
278 {
279   /**
280    * Constructor
281    */
282   TestCustomActorVariant4()
283   {
284   }
285
286   // From CustomActorImpl
287   virtual void OnSceneDisconnection()
288   {
289     // Chain up first
290     TestCustomActor::OnSceneDisconnection();
291
292     // Remove all the children
293     for( uint32_t i=0, num=Self().GetChildCount(); i<num; ++i )
294     {
295       Self().Remove( Self().GetChildAt(0) );
296     }
297   }
298 };
299
300 /**
301  * Variant which removes its parent from Stage during OnSceneConnection
302  */
303 struct TestCustomActorVariant5 : public TestCustomActor
304 {
305   /**
306    * Constructor
307    */
308   TestCustomActorVariant5(Dali::Integration::Scene scene)
309   : mScene(scene)
310   {
311   }
312
313   // From CustomActorImpl
314   virtual void OnSceneConnection( int depth )
315   {
316     // Chain up first
317     TestCustomActor::OnSceneConnection( depth );
318
319     // Take parent off-stage
320     Dali::Actor parent = Self().GetParent();
321     if ( parent )
322     {
323       mScene.Remove( parent );
324     }
325   }
326
327 private:
328   Dali::Integration::Scene mScene;
329 };
330
331 /**
332  * Variant which adds its parent to Stage during OnSceneDisconnection
333  */
334 struct TestCustomActorVariant6 : public TestCustomActor
335 {
336   /**
337    * Constructor
338    */
339   TestCustomActorVariant6(Dali::Integration::Scene scene)
340   : mScene(scene)
341   {
342   }
343
344   // From CustomActorImpl
345   virtual void OnSceneDisconnection()
346   {
347     // Chain up first
348     TestCustomActor::OnSceneDisconnection();
349
350     // Put parent on-stage
351     Dali::Actor parent = Self().GetParent();
352     if ( parent )
353     {
354       mScene.Add( parent );
355     }
356   }
357
358 private:
359   Dali::Integration::Scene mScene;
360 };
361
362 /**
363  * Variant which reparents its children into a separate container
364  */
365 struct TestCustomActorVariant7 : public TestCustomActor
366 {
367   /**
368    * Constructor
369    */
370   TestCustomActorVariant7()
371   {
372   }
373
374   virtual void OnInitialize( const char* name )
375   {
376     // We need to do this early, before the OnChildAdd is recorded
377     Self().SetProperty( Dali::Actor::Property::NAME, name );
378
379     mContainer = Dali::Actor::New();
380     mContainer.SetProperty( Dali::Actor::Property::NAME, "Container" );
381     Self().Add( mContainer );
382   }
383
384   // From CustomActorImpl
385   virtual void OnChildAdd(Dali::Actor& child)
386   {
387     // Chain up first
388     TestCustomActor::OnChildAdd(child);
389
390     // Reparent child
391     if ( child != mContainer )
392     {
393       mContainer.Add( child );
394     }
395   }
396
397   Dali::Actor mContainer;
398 };
399
400 /**
401  * Variant which attempts to interfere with the reparenting of a child to another container
402  */
403 struct TestCustomActorVariant8 : public TestCustomActor
404 {
405   /**
406    * Constructor
407    */
408   TestCustomActorVariant8( Dali::Actor rival )
409   : mRivalContainer( rival )
410   {
411   }
412
413   // From CustomActorImpl
414   virtual void OnChildRemove(Dali::Actor& child)
415   {
416     // Chain up first
417     TestCustomActor::OnChildRemove(child);
418
419     mRivalContainer.Remove( child ); // attempt to block reparenting to rival (should be a NOOP)
420   }
421
422   Dali::Actor mRivalContainer;
423 };
424
425 // Need a class that doesn't override virtual methods
426 class SimpleTestCustomActor : public Dali::CustomActorImpl
427 {
428 public:
429
430   /**
431    * Constructor
432    */
433   SimpleTestCustomActor()
434   : CustomActorImpl( ActorFlags( REQUIRES_TOUCH_EVENTS | DISABLE_SIZE_NEGOTIATION ) )
435   {
436   }
437
438   /**
439    * Destructor
440    */
441   virtual ~SimpleTestCustomActor()
442   {
443   }
444
445   // From CustomActorImpl
446   void OnSceneConnection( int depth ) override
447   {
448   }
449   void OnSceneDisconnection() override
450   {
451   }
452   void OnChildAdd(Dali::Actor& child) override
453   {
454   }
455   void OnChildRemove(Dali::Actor& child) override
456   {
457   }
458   void OnSizeSet(const Dali::Vector3& targetSize) override
459   {
460   }
461   void OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize) override
462   {
463   }
464   bool OnHoverEvent(const Dali::HoverEvent& event) override
465   {
466     return true;
467   }
468   bool OnWheelEvent(const Dali::WheelEvent& event) override
469   {
470     return true;
471   }
472   bool OnKeyEvent(const Dali::KeyEvent& event) override
473   {
474     return true;
475   }
476   virtual void OnKeyInputFocusGained()
477   {
478   }
479   virtual void OnKeyInputFocusLost()
480   {
481   }
482
483   Dali::Vector3 GetNaturalSize() override
484   {
485     return Dali::Vector3( 0.0f, 0.0f, 0.0f );
486   }
487
488   float GetHeightForWidth( float width ) override
489   {
490     return 0.0f;
491   }
492
493   float GetWidthForHeight( float height ) override
494   {
495     return 0.0f;
496   }
497
498   void OnRelayout( const Dali::Vector2& size, Dali::RelayoutContainer& container ) override
499   {
500   }
501
502   void OnSetResizePolicy( Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension ) override
503   {
504   }
505
506   void OnCalculateRelayoutSize( Dali::Dimension::Type dimension ) override
507   {
508   }
509
510   float CalculateChildSize( const Dali::Actor& child, Dali::Dimension::Type dimension ) override
511   {
512     return 0.0f;
513   }
514
515   void OnLayoutNegotiated( float size, Dali::Dimension::Type dimension ) override
516   {
517   }
518
519   bool RelayoutDependentOnChildren( Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS ) override
520   {
521     return false;
522   }
523 };
524
525 } //namespace Impl
526 } //namespace Test
527
528 #endif // TEST_CUSTOM_ACTOR_H