2a4d4834142cb1e809c1ec89657fbe5aca1708ee
[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) 2021 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/devel-api/object/property-helper-devel.h>
22 #include <dali/integration-api/scene.h>
23 #include <dali/public-api/dali-core.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   void                      SetTransparent(bool transparent);
93   bool                      IsTransparent();
94
95 private:
96   TestCustomActor(Impl::TestCustomActor& impl);
97   explicit TestCustomActor(Dali::Internal::CustomActor* internal);
98 };
99
100 namespace DevelTestCustomActor
101 {
102 namespace Property
103 {
104 enum Type
105 {
106   TEST_PROPERTY1       = Test::TestCustomActor::Property::TEST_PROPERTY1,
107   TEST_PROPERTY2       = Test::TestCustomActor::Property::TEST_PROPERTY2,
108   DEVEL_TEST_PROPERTY3 = TEST_PROPERTY2 + 1,
109   DEVEL_TEST_PROPERTY4 = TEST_PROPERTY2 + 2,
110   DEVEL_TEST_PROPERTY5 = TEST_PROPERTY2 + 3,
111   DEVEL_TEST_PROPERTY6 = TEST_PROPERTY2 + 4
112 };
113
114 } // Namespace Property
115 } // Namespace DevelTestCustomActor
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 struct TestCustomActor : public Dali::CustomActorImpl
121 {
122 public:
123   /**
124    * Constructor
125    */
126   TestCustomActor();
127   TestCustomActor(bool nego);
128   virtual ~TestCustomActor();
129   void                         Initialize(const char* name = NULL);
130   virtual void                 OnInitialize(const char* name);
131   void                         ResetCallStack();
132   void                         AddToCallStacks(const char* method);
133   void                         OnSceneConnection(int depth) override;
134   void                         OnSceneDisconnection() override;
135   void                         OnChildAdd(Dali::Actor& child) override;
136   void                         OnChildRemove(Dali::Actor& child) override;
137   void                         OnPropertySet(Dali::Property::Index index, const Dali::Property::Value& propertyValue) override;
138   void                         OnSizeSet(const Dali::Vector3& targetSize) override;
139   void                         OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize) override;
140   virtual void                 OnKeyInputFocusGained();
141   virtual void                 OnKeyInputFocusLost();
142   Dali::Vector3                GetNaturalSize() override;
143   float                        GetHeightForWidth(float width) override;
144   float                        GetWidthForHeight(float height) override;
145   void                         OnRelayout(const Dali::Vector2& size, Dali::RelayoutContainer& container) override;
146   void                         OnSetResizePolicy(Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension) override;
147   void                         OnCalculateRelayoutSize(Dali::Dimension::Type dimension) override;
148   float                        CalculateChildSize(const Dali::Actor& child, Dali::Dimension::Type dimension) override;
149   void                         OnLayoutNegotiated(float size, Dali::Dimension::Type dimension) override;
150   bool                         RelayoutDependentOnChildren(Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS) override;
151   void                         SetTransparent(bool transparent);
152   bool                         IsTransparent() const;
153   static void                  SetProperty(Dali::BaseObject* object, Dali::Property::Index index, const Dali::Property::Value& value);
154   static Dali::Property::Value GetProperty(Dali::BaseObject* object, Dali::Property::Index index);
155
156   void  SetDaliProperty(std::string s);
157   void  TestRelayoutRequest();
158   float TestGetHeightForWidthBase(float width);
159   float TestGetWidthForHeightBase(float height);
160   float TestCalculateChildSizeBase(const Dali::Actor& child, Dali::Dimension::Type dimension);
161   bool  TestRelayoutDependentOnChildrenBase(Dali::Dimension::Type dimension);
162
163 public:
164   Dali::Property::Index    mDaliProperty;
165   std::vector<std::string> mMethodsCalled;
166   Dali::Vector3            mSizeSet;
167   Dali::Vector3            mTargetSize;
168   bool                     mNego;
169   uint32_t                 mDepth;
170
171   float         prop1;
172   Dali::Vector4 prop2;
173   Dali::Vector4 develProp3;
174   int           develProp4;
175   float         develProp5;
176   float         develProp6;
177 };
178
179 inline TestCustomActor& GetImpl(Test::TestCustomActor& handle)
180 {
181   DALI_ASSERT_ALWAYS(handle);
182   Dali::RefObject& object = handle.GetImplementation();
183   return static_cast<TestCustomActor&>(object);
184 }
185
186 inline const TestCustomActor& GetImpl(const Test::TestCustomActor& handle)
187 {
188   DALI_ASSERT_ALWAYS(handle);
189   const Dali::RefObject& object = handle.GetImplementation();
190   return static_cast<const TestCustomActor&>(object);
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    * Constructor
428    */
429   SimpleTestCustomActor()
430   : CustomActorImpl(ActorFlags(DISABLE_SIZE_NEGOTIATION))
431   {
432   }
433
434   /**
435    * Destructor
436    */
437   virtual ~SimpleTestCustomActor()
438   {
439   }
440
441   // From CustomActorImpl
442   void OnSceneConnection(int depth) override
443   {
444   }
445   void OnSceneDisconnection() override
446   {
447   }
448   void OnChildAdd(Dali::Actor& child) override
449   {
450   }
451   void OnChildRemove(Dali::Actor& child) override
452   {
453   }
454   void OnSizeSet(const Dali::Vector3& targetSize) override
455   {
456   }
457   void OnSizeAnimation(Dali::Animation& animation, const Dali::Vector3& targetSize) override
458   {
459   }
460   virtual void OnKeyInputFocusGained()
461   {
462   }
463   virtual void OnKeyInputFocusLost()
464   {
465   }
466   Dali::Vector3 GetNaturalSize() override
467   {
468     return Dali::Vector3(0.0f, 0.0f, 0.0f);
469   }
470
471   float GetHeightForWidth(float width) override
472   {
473     return 0.0f;
474   }
475
476   float GetWidthForHeight(float height) override
477   {
478     return 0.0f;
479   }
480
481   void OnRelayout(const Dali::Vector2& size, Dali::RelayoutContainer& container) override
482   {
483   }
484
485   void OnSetResizePolicy(Dali::ResizePolicy::Type policy, Dali::Dimension::Type dimension) override
486   {
487   }
488
489   void OnCalculateRelayoutSize(Dali::Dimension::Type dimension) override
490   {
491   }
492
493   float CalculateChildSize(const Dali::Actor& child, Dali::Dimension::Type dimension) override
494   {
495     return 0.0f;
496   }
497
498   void OnLayoutNegotiated(float size, Dali::Dimension::Type dimension) override
499   {
500   }
501
502   bool RelayoutDependentOnChildren(Dali::Dimension::Type dimension = Dali::Dimension::ALL_DIMENSIONS) override
503   {
504     return false;
505   }
506 };
507
508 } //namespace Impl
509 } //namespace Test
510
511 #endif // TEST_CUSTOM_ACTOR_H