Merge "DALi C# binding - Write pure C# Color & Position classes and use typemaps...
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ControlWrapper.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19 #include <stdlib.h>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit/devel-api/controls/control-wrapper.h>
28 #include <dali-toolkit/devel-api/controls/control-wrapper-impl.h>
29 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
30 #include <dali-toolkit/devel-api/visual-factory/transition-data.h>
31
32 using namespace Dali;
33 using namespace Dali::Toolkit;
34
35 void utc_dali_toolkit_control_wrapper_startup(void)
36 {
37   test_return_value = TET_UNDEF;
38 }
39
40 void utc_dali_toolkit_control_wrapper_cleanup(void)
41 {
42   test_return_value = TET_PASS;
43 }
44
45 ///////////////////////////////////////////////////////////////////////////////////////////////////
46
47 namespace
48 {
49 bool gOnRelayout = false;
50 } // namespace
51
52 ///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 namespace Impl
55 {
56 struct TestCustomControl : public Toolkit::Internal::ControlWrapper
57 {
58   /**
59   * Constructor
60   */
61   TestCustomControl()  : Toolkit::Internal::ControlWrapper( CustomControlBehaviour( Toolkit::Internal::ControlWrapper::DISABLE_STYLE_CHANGE_SIGNALS |
62           Toolkit::Internal::ControlWrapper::REQUIRES_KEYBOARD_NAVIGATION_SUPPORT )) ,
63           mDaliProperty( Property::INVALID_INDEX ),
64           mSizeSet( Vector3::ZERO ),
65           mTargetSize( Vector3::ZERO ),
66           mNego( false ),
67           mDepth( 0u )
68   {
69   }
70
71   TestCustomControl(bool nego)  : Toolkit::Internal::ControlWrapper( CustomControlBehaviour( Toolkit::Internal::ControlWrapper::DISABLE_STYLE_CHANGE_SIGNALS |
72           Toolkit::Internal::ControlWrapper::REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
73           mDaliProperty( Property::INVALID_INDEX ),
74           mSizeSet( Vector3::ZERO ),
75           mTargetSize( Vector3::ZERO ),
76           mNego( nego )
77   {
78   }
79   /**
80    * Destructor
81    */
82   virtual ~TestCustomControl()
83   {
84   }
85
86   void Initialize( const char* name = NULL )
87   {
88     mDaliProperty = Self().RegisterProperty( "Dali", std::string("no"), Property::READ_WRITE );
89
90     OnInitialize( name );
91   }
92
93   virtual void OnInitialize( const char* name ) {}
94
95   // From Toolkit::Internal::ControlWrapper
96   virtual void OnStageConnection( int depth )
97   {
98     mDepth = depth;
99   }
100   virtual void OnStageDisconnection()
101   {
102   }
103   virtual void OnChildAdd( Actor& child )
104   {
105   }
106   virtual void OnChildRemove( Actor& child )
107   {
108   }
109   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue )
110   {
111   }
112   virtual void OnSizeSet( const Vector3& targetSize )
113   {
114     mSizeSet = targetSize;
115   }
116   virtual void OnSizeAnimation( Animation& animation, const Vector3& targetSize )
117   {
118     mTargetSize = targetSize;
119   }
120   virtual bool OnTouchEvent( const TouchEvent& event )
121   {
122     return true;
123   }
124   virtual bool OnHoverEvent( const HoverEvent& event )
125   {
126     return true;
127   }
128   virtual bool OnWheelEvent( const WheelEvent& event )
129   {
130     return true;
131   }
132   virtual bool OnKeyEvent( const KeyEvent& event )
133   {
134     return true;
135   }
136   virtual void OnKeyInputFocusGained()
137   {
138   }
139   virtual void OnKeyInputFocusLost()
140   {
141   }
142   virtual Vector3 GetNaturalSize()
143   {
144     return Vector3( 0.0f, 0.0f, 0.0f );
145   }
146
147   virtual float GetHeightForWidth( float width )
148   {
149     return 0.0f;
150   }
151
152   virtual float GetWidthForHeight( float height )
153   {
154     return 0.0f;
155   }
156
157   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container )
158   {
159     gOnRelayout = true;
160   }
161
162   virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
163   {
164   }
165
166   virtual void OnCalculateRelayoutSize( Dimension::Type dimension )
167   {
168   }
169
170   virtual float CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension )
171   {
172     return 0.0f;
173   }
174
175   virtual void OnLayoutNegotiated( float size, Dimension::Type dimension )
176   {
177   }
178
179   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS )
180   {
181     return false;
182   }
183
184   void SetDaliProperty(std::string s)
185   {
186     Self().SetProperty(mDaliProperty, s);
187   }
188   void TestRelayoutRequest()
189   {
190     RelayoutRequest();
191   }
192
193   float TestGetHeightForWidthBase( float width )
194   {
195     return GetHeightForWidthBase( width );
196   }
197
198   float TestGetWidthForHeightBase( float height )
199   {
200     return GetWidthForHeightBase( height );
201   }
202
203   float TestCalculateChildSizeBase( const Dali::Actor& child, Dimension::Type dimension )
204   {
205     return CalculateChildSizeBase( child, dimension );
206   }
207
208   bool TestRelayoutDependentOnChildrenBase( Dimension::Type dimension )
209   {
210     return RelayoutDependentOnChildrenBase( dimension );
211   }
212
213   Property::Index mDaliProperty;
214   Vector3 mSizeSet;
215   Vector3 mTargetSize;
216   bool mNego;
217   unsigned int mDepth;
218 };
219 }
220
221 int UtcDaliControlWrapperConstructor(void)
222 {
223   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
224
225   Toolkit::Internal::ControlWrapper* controlWrapperImpl = new Toolkit::Internal::ControlWrapper( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
226   ControlWrapper controlWrapper;
227
228   DALI_TEST_CHECK( !ControlWrapper::DownCast( controlWrapper ) );
229
230   controlWrapper = ControlWrapper::New( *controlWrapperImpl );
231
232   DALI_TEST_CHECK( ControlWrapper::DownCast( controlWrapper ) );
233   END_TEST;
234 }
235
236 int UtcDaliControlWrapperDestructor(void)
237 {
238   TestApplication application;
239
240   ControlWrapper control = ControlWrapper::New( *( new Toolkit::Internal::ControlWrapper( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT ) ) );
241
242   ControlWrapper control2( control );
243
244   DALI_TEST_CHECK( control );
245   control.Reset();
246   DALI_TEST_CHECK( !control );
247
248   DALI_TEST_CHECK( control2 );
249   control2.Reset();
250   DALI_TEST_CHECK( !control2 );
251
252   END_TEST;
253 }
254
255 int UtcDaliControlWrapperRelayoutRequest(void)
256 {
257   TestApplication application;
258
259   DALI_TEST_EQUALS( gOnRelayout, false, TEST_LOCATION );
260
261   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
262   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
263
264   Stage::GetCurrent().Add( controlWrapper );
265
266   application.SendNotification();
267   application.Render();
268
269   DALI_TEST_EQUALS( gOnRelayout, true, TEST_LOCATION );
270   gOnRelayout = false;
271
272   controlWrapperImpl->TestRelayoutRequest();
273   application.SendNotification();
274   application.Render();
275
276   DALI_TEST_EQUALS( gOnRelayout, true, TEST_LOCATION );
277
278   END_TEST;
279 }
280
281 int UtcDaliControlWrapperImplGetHeightForWidthBase(void)
282 {
283   TestApplication application;
284
285   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
286   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
287
288   float width = 300.0f;
289   float v = 0.0f;
290
291   application.SendNotification();
292   application.Render();
293
294   v = controlWrapperImpl->TestGetHeightForWidthBase( width );
295
296   DALI_TEST_EQUALS( width, v, TEST_LOCATION );
297
298   END_TEST;
299 }
300
301 int UtcDaliControlWrapperGetWidthForHeightBase(void)
302 {
303   TestApplication application;
304
305   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
306   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
307
308   float height = 300.0f;
309   float v = 0.0f;
310
311   application.SendNotification();
312   application.Render();
313
314   v = controlWrapperImpl->TestGetWidthForHeightBase( height );
315
316   DALI_TEST_EQUALS( height, v, TEST_LOCATION );
317
318   END_TEST;
319 }
320
321 int UtcDaliControlWrapperCalculateChildSizeBase(void)
322 {
323   TestApplication application;
324
325   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
326   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
327
328   Actor child = Actor::New();
329   child.SetResizePolicy( Dali::ResizePolicy::FIXED, Dali::Dimension::ALL_DIMENSIONS );
330   child.SetSize(150, 150);
331
332   application.SendNotification();
333   application.Render();
334
335   float v = 9.99f;
336   v = controlWrapperImpl->TestCalculateChildSizeBase( child, Dali::Dimension::ALL_DIMENSIONS );
337   DALI_TEST_EQUALS( v, 0.0f, TEST_LOCATION );
338
339   END_TEST;
340 }
341
342 int UtcDaliControlWrapperRelayoutDependentOnChildrenBase(void)
343 {
344   TestApplication application;
345
346   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
347   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
348
349   bool v = false;
350
351   v = controlWrapperImpl->TestRelayoutDependentOnChildrenBase( Dali::Dimension::ALL_DIMENSIONS );
352   application.SendNotification();
353   application.Render();
354
355   DALI_TEST_EQUALS( v, true, TEST_LOCATION );
356
357   controlWrapper.SetResizePolicy( Dali::ResizePolicy::FIXED, Dali::Dimension::ALL_DIMENSIONS );
358   v = controlWrapperImpl->TestRelayoutDependentOnChildrenBase( Dali::Dimension::WIDTH );
359   application.SendNotification();
360   application.Render();
361   DALI_TEST_EQUALS( v, false, TEST_LOCATION );
362
363   END_TEST;
364 }
365
366 int UtcDaliControlWrapperRegisterVisualToSelf(void)
367 {
368   ToolkitTestApplication application;
369
370   Test::ObjectDestructionTracker objectDestructionTracker;
371
372   {
373     Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
374     ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
375
376     objectDestructionTracker.Start( controlWrapper );
377
378     Property::Index index = 1;
379
380     Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
381     Toolkit::Visual::Base visual;
382
383     Property::Map map;
384     map[Visual::Property::TYPE] = Visual::COLOR;
385     map[ColorVisual::Property::MIX_COLOR] = Color::RED;
386
387     visual = visualFactory.CreateVisual( map );
388     DALI_TEST_CHECK( visual );
389
390     // Register to self
391     controlWrapperImpl->RegisterVisual( index, visual );
392
393     DALI_TEST_EQUALS( objectDestructionTracker.IsDestroyed(), false, TEST_LOCATION ); // Control not destroyed yet
394     DALI_TEST_EQUALS( controlWrapperImpl->GetVisual( index ), visual, TEST_LOCATION );
395   }
396
397   DALI_TEST_EQUALS( objectDestructionTracker.IsDestroyed(), true, TEST_LOCATION ); // Should be destroyed
398
399   END_TEST;
400 }
401
402 int UtcDaliControlWrapperRegisterDisabledVisual(void)
403 {
404   ToolkitTestApplication application;
405
406   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
407   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
408
409   Property::Index TEST_PROPERTY = 1;
410
411   Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
412   Toolkit::Visual::Base visual;
413
414   Property::Map map;
415   map[Visual::Property::TYPE] = Visual::COLOR;
416   map[ColorVisual::Property::MIX_COLOR] = Color::RED;
417
418   visual = visualFactory.CreateVisual( map );
419   DALI_TEST_CHECK(visual);
420
421   // Register index with a color visual
422   controlWrapperImpl->RegisterVisual( TEST_PROPERTY, visual, false );
423
424   DALI_TEST_EQUALS( controlWrapperImpl->GetVisual( TEST_PROPERTY ), visual, TEST_LOCATION );
425   DALI_TEST_EQUALS( controlWrapperImpl->IsVisualEnabled( TEST_PROPERTY ), false, TEST_LOCATION );
426
427   Stage::GetCurrent().Add( controlWrapper );
428
429   // Render and notify
430   application.SendNotification();
431   application.Render();
432
433   DALI_TEST_EQUALS( controlWrapperImpl->IsVisualEnabled( TEST_PROPERTY ), false, TEST_LOCATION );
434
435   DALI_TEST_EQUALS( controlWrapper.OnStage(), true, TEST_LOCATION );
436
437   controlWrapperImpl->EnableVisual( TEST_PROPERTY, true );
438
439   DALI_TEST_EQUALS( controlWrapperImpl->IsVisualEnabled( TEST_PROPERTY ), true, TEST_LOCATION );
440
441   END_TEST;
442 }
443
444 int UtcDaliControlWrapperRegisterUnregisterVisual(void)
445 {
446   ToolkitTestApplication application;
447
448   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
449   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
450
451   Property::Index index = 1;
452
453   Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get();
454   Toolkit::Visual::Base visual;
455
456   Property::Map map;
457   map[Visual::Property::TYPE] = Visual::COLOR;
458   map[ColorVisual::Property::MIX_COLOR] = Color::RED;
459
460   visual = visualFactory.CreateVisual( map );
461   DALI_TEST_CHECK(visual);
462
463   // Register index with a color visual
464   controlWrapperImpl->RegisterVisual( index, visual );
465
466   DALI_TEST_EQUALS( controlWrapperImpl->GetVisual( index ), visual, TEST_LOCATION );
467
468   // Unregister visual
469   controlWrapperImpl->UnregisterVisual( index );
470
471   DALI_TEST_CHECK( !controlWrapperImpl->GetVisual( index ) );
472
473   END_TEST;
474 }
475
476 int UtcDaliControlWrapperTransitionDataMap1N(void)
477 {
478   TestApplication application;
479
480   Property::Map map;
481   map["target"] = "Actor1";
482   map["property"] = "randomProperty";
483   map["initialValue"] = Color::MAGENTA;
484   map["targetValue"] = Color::RED;
485   map["animator"] = Property::Map()
486     .Add("alphaFunction", "EASE_OUT")
487     .Add("timePeriod", Property::Map()
488          .Add("delay", 0.5f)
489          .Add("duration", 1.0f));
490
491   Dali::Toolkit::TransitionData transition = TransitionData::New( map );
492
493   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
494   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
495
496   //DummyControl actor = DummyControl::New();
497   controlWrapper.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
498   controlWrapper.SetName("Actor1");
499   controlWrapper.SetColor(Color::CYAN);
500   Stage::GetCurrent().Add(controlWrapper);
501
502   Animation anim = controlWrapperImpl->CreateTransition( transition );
503   DALI_TEST_CHECK( ! anim );
504
505   Property::Map returnedMap = transition.GetAnimatorAt(0);
506
507   Property::Value* value = returnedMap.Find("property");
508   DALI_TEST_CHECK( value != NULL);
509   DALI_TEST_EQUALS( "randomProperty", value->Get<std::string>(), TEST_LOCATION );
510
511   value = returnedMap.Find("initialValue");
512   DALI_TEST_CHECK( value != NULL);
513   DALI_TEST_EQUALS( Color::MAGENTA, value->Get<Vector4>(), TEST_LOCATION );
514
515   value = returnedMap.Find("targetValue");
516   DALI_TEST_CHECK( value != NULL);
517   DALI_TEST_EQUALS( Color::RED, value->Get<Vector4>(), TEST_LOCATION );
518
519   value = returnedMap.Find("animator");
520   DALI_TEST_CHECK( value != NULL);
521   Property::Map returnedAnimatorMap = value->Get<Property::Map>();
522
523   value = returnedAnimatorMap.Find("alphaFunction");
524   DALI_TEST_CHECK( value != NULL);
525   DALI_TEST_EQUALS( "EASE_OUT", value->Get<std::string>(), TEST_LOCATION );
526
527   value = returnedAnimatorMap.Find("timePeriod");
528   DALI_TEST_CHECK( value != NULL);
529   Property::Map returnedTimePeriodMap = value->Get<Property::Map>();
530
531   value = returnedTimePeriodMap.Find("delay");
532   DALI_TEST_CHECK( value != NULL);
533   DALI_TEST_EQUALS( 0.5f, value->Get<float>(), TEST_LOCATION );
534
535   value = returnedTimePeriodMap.Find("duration");
536   DALI_TEST_CHECK( value != NULL);
537   DALI_TEST_EQUALS( 1.0f, value->Get<float>(), TEST_LOCATION );
538
539   END_TEST;
540 }
541
542 int UtcDaliControlWrapperApplyThemeStyle(void)
543 {
544   ToolkitTestApplication application;
545
546   Impl::TestCustomControl* controlWrapperImpl = new ::Impl::TestCustomControl( Toolkit::Internal::ControlWrapper::CONTROL_BEHAVIOUR_DEFAULT );
547   ControlWrapper controlWrapper = ControlWrapper::New( *controlWrapperImpl );
548
549   controlWrapperImpl->ApplyThemeStyle();
550
551   DALI_TEST_CHECK( true );
552   END_TEST;
553 }