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