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