Refactored actor size negotiation
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / test-custom-actor.cpp
1 #include "test-custom-actor.h"
2
3 using namespace Dali;
4
5 std::vector<std::string> MasterCallStack;
6 bool                     gOnRelayout = false;
7
8 namespace Test
9 {
10 namespace Impl
11 {
12 struct TestCustomActor;
13 }
14
15 TestCustomActor TestCustomActor::New()
16 {
17   Impl::TestCustomActor* impl = new Impl::TestCustomActor;
18   TestCustomActor        custom(*impl); // takes ownership
19
20   impl->Initialize();
21
22   return custom;
23 }
24
25 TestCustomActor TestCustomActor::NewNegoSize()
26 {
27   Impl::TestCustomActor* impl = new Impl::TestCustomActor(true);
28   TestCustomActor        custom(*impl); // takes ownership
29   custom.SetProperty(Dali::Actor::Property::NAME, "SizeNegotiationActor");
30
31   impl->Initialize();
32
33   return custom;
34 }
35
36 TestCustomActor TestCustomActor::NewVariant1(Actor childToAdd)
37 {
38   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant1(childToAdd);
39   TestCustomActor        custom(*impl); // takes ownership
40
41   impl->Initialize();
42
43   return custom;
44 }
45
46 TestCustomActor TestCustomActor::NewVariant2()
47 {
48   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant2();
49   TestCustomActor        custom(*impl); // takes ownership
50
51   impl->Initialize();
52
53   return custom;
54 }
55
56 TestCustomActor TestCustomActor::NewVariant3(Actor childToAdd)
57 {
58   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant3(childToAdd);
59   TestCustomActor        custom(*impl); // takes ownership
60
61   impl->Initialize();
62
63   return custom;
64 }
65
66 TestCustomActor TestCustomActor::NewVariant4()
67 {
68   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant4();
69   TestCustomActor        custom(*impl); // takes ownership
70
71   impl->Initialize();
72
73   return custom;
74 }
75
76 TestCustomActor TestCustomActor::NewVariant5(Integration::Scene scene)
77 {
78   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant5(scene);
79   TestCustomActor        custom(*impl); // takes ownership
80
81   impl->Initialize();
82
83   return custom;
84 }
85
86 TestCustomActor TestCustomActor::NewVariant6(Integration::Scene scene)
87 {
88   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant6(scene);
89   TestCustomActor        custom(*impl); // takes ownership
90
91   impl->Initialize();
92
93   return custom;
94 }
95
96 TestCustomActor TestCustomActor::NewVariant7(const char* name)
97 {
98   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant7();
99   TestCustomActor        custom(*impl); // takes ownership
100
101   impl->Initialize(name);
102
103   return custom;
104 }
105
106 TestCustomActor TestCustomActor::NewVariant8(Actor rival)
107 {
108   Impl::TestCustomActor* impl = new Impl::TestCustomActorVariant8(rival);
109   TestCustomActor        custom(*impl); // takes ownership
110
111   impl->Initialize();
112
113   return custom;
114 }
115
116 TestCustomActor::~TestCustomActor()
117 {
118 }
119
120 Impl::TestCustomActor& TestCustomActor::GetImpl()
121 {
122   return static_cast<Impl::TestCustomActor&>(GetImplementation());
123 }
124
125 std::vector<std::string>& TestCustomActor::GetMethodsCalled()
126 {
127   return GetImpl().mMethodsCalled;
128 }
129
130 void TestCustomActor::ResetCallStack()
131 {
132   GetImpl().ResetCallStack();
133 }
134
135 void TestCustomActor::SetDaliProperty(std::string s)
136 {
137   GetImpl().SetDaliProperty(s);
138 }
139
140 Vector3 TestCustomActor::GetSize()
141 {
142   return GetImpl().mSizeSet;
143 }
144
145 Vector3 TestCustomActor::GetTargetSize()
146 {
147   return GetImpl().mTargetSize;
148 }
149
150 Vector3 TestCustomActor::GetNaturalSize()
151 {
152   return GetImpl().GetNaturalSize();
153 }
154
155 float TestCustomActor::GetHeightForWidth(float width)
156 {
157   return GetImpl().GetHeightForWidth(width);
158 }
159
160 float TestCustomActor::GetWidthForHeight(float height)
161 {
162   return GetImpl().GetWidthForHeight(height);
163 }
164
165 void TestCustomActor::OnRelayout(const Vector2& size, RelayoutContainer& container)
166 {
167 }
168
169 void TestCustomActor::OnLayoutNegotiated(float size, Dimension::Type dimension)
170 {
171 }
172
173 void TestCustomActor::OnCalculateRelayoutSize(Dimension::Type dimension)
174 {
175 }
176
177 void TestCustomActor::TestRelayoutRequest()
178 {
179   GetImpl().TestRelayoutRequest();
180 }
181
182 float TestCustomActor::TestGetHeightForWidthBase(float width)
183 {
184   return GetImpl().TestGetHeightForWidthBase(width);
185 }
186
187 float TestCustomActor::TestGetWidthForHeightBase(float height)
188 {
189   return GetImpl().TestGetWidthForHeightBase(height);
190 }
191
192 float TestCustomActor::TestCalculateChildSizeBase(const Dali::Actor& child, Dimension::Type dimension)
193 {
194   return GetImpl().TestCalculateChildSizeBase(child, dimension);
195 }
196
197 bool TestCustomActor::TestRelayoutDependentOnChildrenBase(Dimension::Type dimension)
198 {
199   return GetImpl().TestRelayoutDependentOnChildrenBase(dimension);
200 }
201
202 uint32_t TestCustomActor::GetDepth()
203 {
204   return GetImpl().mDepth;
205 }
206
207 void TestCustomActor::SetTransparent(bool transparent)
208 {
209   return GetImpl().SetTransparent(transparent);
210 }
211
212 bool TestCustomActor::IsTransparent()
213 {
214   return GetImpl().IsTransparent();
215 }
216
217 TestCustomActor::TestCustomActor()
218 {
219 }
220
221 TestCustomActor::TestCustomActor(Impl::TestCustomActor& impl)
222 : CustomActor(impl)
223 {
224 }
225
226 TestCustomActor::TestCustomActor(Dali::Internal::CustomActor* internal)
227 : CustomActor(internal)
228 {
229 }
230
231 TestCustomActor TestCustomActor::DownCast(BaseHandle handle)
232 {
233   TestCustomActor actor;
234   CustomActor     custom = Dali::CustomActor::DownCast(handle);
235   if(custom)
236   {
237     CustomActorImpl& customImpl = custom.GetImplementation();
238
239     Test::Impl::TestCustomActor* impl = dynamic_cast<Test::Impl::TestCustomActor*>(&customImpl);
240     if(impl)
241     {
242       actor = TestCustomActor(customImpl.GetOwner());
243     }
244   }
245   return actor;
246 }
247
248 namespace Impl
249 {
250 TestCustomActor::TestCustomActor()
251 : CustomActorImpl(ActorFlags(DISABLE_SIZE_NEGOTIATION)),
252   mDaliProperty(Property::INVALID_INDEX),
253   mSizeSet(Vector3::ZERO),
254   mTargetSize(Vector3::ZERO),
255   mNego(false),
256   mDepth(0u),
257   develProp6(10.0f)
258 {
259 }
260
261 TestCustomActor::TestCustomActor(bool nego)
262 : CustomActorImpl(ActorFlags()),
263   mDaliProperty(Property::INVALID_INDEX),
264   mSizeSet(Vector3::ZERO),
265   mTargetSize(Vector3::ZERO),
266   mNego(nego),
267   develProp6(10.0f)
268 {
269 }
270 /**
271  * Destructor
272  */
273 TestCustomActor::~TestCustomActor()
274 {
275 }
276
277 void TestCustomActor::Initialize(const char* name)
278 {
279   mDaliProperty = Self().RegisterProperty("Dali", std::string("no"), Property::READ_WRITE);
280
281   OnInitialize(name);
282 }
283
284 void TestCustomActor::OnInitialize(const char* name)
285 {
286 }
287
288 /**
289  * Resets the call stack
290  */
291 void TestCustomActor::ResetCallStack()
292 {
293   mSizeSet    = Vector3();
294   mTargetSize = Vector3();
295   mMethodsCalled.clear();
296 }
297
298 void TestCustomActor::AddToCallStacks(const char* method)
299 {
300   mMethodsCalled.push_back(method);
301
302   // Combine Actor name with method string
303   std::string nameAndMethod(Self().GetProperty<std::string>(Dali::Actor::Property::NAME));
304   if(0 == nameAndMethod.size())
305   {
306     nameAndMethod = "Unknown: ";
307   }
308   else
309   {
310     nameAndMethod += ": ";
311   }
312   nameAndMethod += method;
313
314   MasterCallStack.push_back(nameAndMethod);
315 }
316
317 // From CustomActorImpl
318 void TestCustomActor::OnSceneConnection(int depth)
319 {
320   AddToCallStacks("OnSceneConnection");
321   mDepth = depth;
322 }
323 void TestCustomActor::OnSceneDisconnection()
324 {
325   AddToCallStacks("OnSceneDisconnection");
326 }
327 void TestCustomActor::OnChildAdd(Actor& child)
328 {
329   AddToCallStacks("OnChildAdd");
330 }
331 void TestCustomActor::OnChildRemove(Actor& child)
332 {
333   AddToCallStacks("OnChildRemove");
334 }
335 void TestCustomActor::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
336 {
337   AddToCallStacks("OnPropertySet");
338 }
339 void TestCustomActor::OnSizeSet(const Vector3& targetSize)
340 {
341   mSizeSet = targetSize;
342   AddToCallStacks("OnSizeSet");
343 }
344 void TestCustomActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
345 {
346   mTargetSize = targetSize;
347   AddToCallStacks("OnSizeAnimation");
348 }
349 void TestCustomActor::OnKeyInputFocusGained()
350 {
351   AddToCallStacks("OnKeyInputFocusGained");
352 }
353 void TestCustomActor::OnKeyInputFocusLost()
354 {
355   AddToCallStacks("OnKeyInputFocusLost");
356 }
357 Vector3 TestCustomActor::GetNaturalSize()
358 {
359   return mNaturalSize;
360 }
361 void TestCustomActor::SetNaturalSize(const Vector3& size)
362 {
363   mNaturalSize = size;
364 }
365
366 float TestCustomActor::GetHeightForWidth(float width)
367 {
368   return mH4Wfactor * width;
369 }
370
371 float TestCustomActor::GetWidthForHeight(float height)
372 {
373   return mW4Hfactor * height;
374 }
375
376 void TestCustomActor::OnRelayout(const Vector2& size, RelayoutContainer& container)
377 {
378   gOnRelayout = true;
379 }
380
381 void TestCustomActor::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
382 {
383 }
384
385 void TestCustomActor::OnCalculateRelayoutSize(Dimension::Type dimension)
386 {
387 }
388
389 float TestCustomActor::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
390 {
391   return CustomActorImpl::CalculateChildSizeBase(child, dimension);
392 }
393
394 void TestCustomActor::OnLayoutNegotiated(float size, Dimension::Type dimension)
395 {
396 }
397
398 bool TestCustomActor::RelayoutDependentOnChildren(Dimension::Type dimension)
399 {
400   return CustomActorImpl::RelayoutDependentOnChildrenBase(dimension);
401 }
402
403 void TestCustomActor::SetTransparent(bool transparent)
404 {
405   CustomActorImpl::SetTransparent(transparent);
406 }
407
408 bool TestCustomActor::IsTransparent() const
409 {
410   return CustomActorImpl::IsTransparent();
411 }
412
413 void TestCustomActor::SetDaliProperty(std::string s)
414 {
415   Self().SetProperty(mDaliProperty, s);
416 }
417 void TestCustomActor::TestRelayoutRequest()
418 {
419   RelayoutRequest();
420 }
421
422 float TestCustomActor::TestGetHeightForWidthBase(float width)
423 {
424   return GetHeightForWidthBase(width);
425 }
426
427 float TestCustomActor::TestGetWidthForHeightBase(float height)
428 {
429   return GetWidthForHeightBase(height);
430 }
431
432 float TestCustomActor::TestCalculateChildSizeBase(const Dali::Actor& child, Dimension::Type dimension)
433 {
434   return CalculateChildSizeBase(child, dimension);
435 }
436
437 bool TestCustomActor::TestRelayoutDependentOnChildrenBase(Dimension::Type dimension)
438 {
439   return RelayoutDependentOnChildrenBase(dimension);
440 }
441
442 void TestCustomActor::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
443 {
444   Test::TestCustomActor actor = Test::TestCustomActor::DownCast(Dali::BaseHandle(object));
445
446   if(actor)
447   {
448     TestCustomActor& actorImpl = GetImpl(actor);
449     switch(index)
450     {
451       case Test::TestCustomActor::Property::TEST_PROPERTY1:
452       {
453         actorImpl.prop1 = value.Get<float>();
454         break;
455       }
456       case Test::TestCustomActor::Property::TEST_PROPERTY2:
457       {
458         actorImpl.prop2 = value.Get<Vector4>();
459         break;
460       }
461       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY3:
462       {
463         actorImpl.develProp3 = value.Get<Vector4>();
464         break;
465       }
466       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY4:
467       {
468         actorImpl.develProp4 = value.Get<int>();
469         break;
470       }
471       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY5:
472       {
473         actorImpl.develProp5 = value.Get<float>();
474         break;
475       }
476     }
477   }
478 }
479
480 Property::Value TestCustomActor::GetProperty(BaseObject* object, Property::Index index)
481 {
482   Test::TestCustomActor actor = Test::TestCustomActor::DownCast(Dali::BaseHandle(object));
483
484   if(actor)
485   {
486     TestCustomActor& actorImpl = GetImpl(actor);
487     switch(index)
488     {
489       case Test::TestCustomActor::Property::TEST_PROPERTY1:
490       {
491         return Property::Value(actorImpl.prop1);
492       }
493       case Test::TestCustomActor::Property::TEST_PROPERTY2:
494       {
495         return Property::Value(actorImpl.prop2);
496       }
497       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY3:
498       {
499         return Property::Value(actorImpl.develProp3);
500       }
501       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY4:
502       {
503         return Property::Value(actorImpl.develProp4);
504       }
505       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY5:
506       {
507         return Property::Value(actorImpl.develProp5);
508       }
509       case Test::DevelTestCustomActor::Property::DEVEL_TEST_PROPERTY6:
510       {
511         return Property::Value(actorImpl.develProp6);
512       }
513     }
514   }
515   return Property::Value();
516 }
517
518 BaseHandle CreateActor()
519 {
520   return Test::TestCustomActor::New();
521 }
522
523 DALI_TYPE_REGISTRATION_BEGIN(Test::TestCustomActor, Dali::CustomActor, CreateActor);
524
525 DALI_PROPERTY_REGISTRATION(Test, TestCustomActor, "testProperty1", FLOAT, TEST_PROPERTY1)
526 DALI_PROPERTY_REGISTRATION(Test, TestCustomActor, "testProperty2", VECTOR4, TEST_PROPERTY2)
527 DALI_DEVEL_PROPERTY_REGISTRATION(Test, TestCustomActor, "develTestProperty3", VECTOR4, DEVEL_TEST_PROPERTY3)
528 DALI_DEVEL_PROPERTY_REGISTRATION(Test, TestCustomActor, "develTestProperty4", INTEGER, DEVEL_TEST_PROPERTY4)
529 DALI_DEVEL_PROPERTY_REGISTRATION(Test, TestCustomActor, "develTestProperty5", FLOAT, DEVEL_TEST_PROPERTY5)
530 DALI_DEVEL_PROPERTY_REGISTRATION_READ_ONLY(Test, TestCustomActor, "develTestProperty6", FLOAT, DEVEL_TEST_PROPERTY6)
531
532 DALI_TYPE_REGISTRATION_END()
533
534 } // namespace Impl
535 } // namespace Test