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