155b54a01c25d77644be2035c4f0df5ee0afb43f
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-LayoutingNesting.cpp
1 /*
2  * Copyright (c) 2018 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 #include <dali-toolkit-test-suite-utils.h>
21 #include <toolkit-event-thread-callback.h>
22
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/layouting/absolute-layout.h>
26 #include <dali-toolkit/devel-api/layouting/linear-layout.h>
27 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
28 #include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
29 #include <dali/devel-api/actors/actor-devel.h>
30
31 #include <../custom-layout.h>
32
33 #include <layout-utils.h>
34
35 using namespace Dali;
36 using namespace Toolkit;
37
38 namespace
39 {
40
41 // Turns the given control into a Root layout control and adds it to the stage.
42 void SetupRootLayoutControl( Control& rootControl )
43 {
44   rootControl = Control::New();
45   auto absoluteLayout = AbsoluteLayout::New();
46   DevelControl::SetLayout( rootControl, absoluteLayout );
47   rootControl.SetName( "RootAbsoluteLayout" );
48   Stage stage = Stage::GetCurrent();
49   stage.Add( rootControl );
50 }
51
52 } // unnamed namespace
53
54 void utc_dali_toolkit_layouting_nesting_startup(void)
55 {
56   test_return_value = TET_UNDEF;
57 }
58
59 void utc_dali_toolkit_layouting_nesting_cleanup(void)
60 {
61   test_return_value = TET_PASS;
62 }
63
64 // Test nesting of Layouts and Controls
65
66 int UtcDaliLayoutingNesting_01(void)
67 {
68   /*
69
70   Root
71     |
72   Control (LinearLayout Horizontal)
73     |
74   Control (LayoutingRequired)
75     |
76   Control (LinearLayout Horizontal)
77     |
78   LeafControl
79
80   */
81
82   ToolkitTestApplication application;
83   tet_infoline("UtcDaliLayoutingNesting_01 - Nesting a LinearLayout (MATCH_PARENT) containing a leaf control within a LinearLayout");
84
85   Stage stage = Stage::GetCurrent();
86
87   auto rootControl = Control::New();
88   SetupRootLayoutControl( rootControl );
89
90   auto hbox = Control::New();
91   auto hboxLayout = LinearLayout::New();
92   hboxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
93   DevelControl::SetLayout( hbox, hboxLayout );
94   hbox.SetName( "hBox" );
95   hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
96   hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, 800 );
97
98
99   auto vbox = Control::New();
100   auto vboxLayout = LinearLayout::New();
101   vboxLayout.SetOrientation( LinearLayout::Orientation::VERTICAL );
102   DevelControl::SetLayout( vbox, vboxLayout );
103   vbox.SetName( "vBox" );
104   vbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
105   vbox.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
106   hbox.Add( vbox );
107
108   std::vector< Control > controls;
109   controls.push_back( CreateLeafControl( 40, 40 ) );
110
111   for( auto&& iter : controls )
112   {
113     hbox.Add( iter );
114   }
115
116   rootControl.Add( hbox );
117
118   // Ensure layouting happens
119   application.SendNotification();
120   application.Render();
121
122   DALI_TEST_EQUALS( hbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
123   DALI_TEST_EQUALS( vbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
124
125   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 40.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
126
127   END_TEST;
128 }
129
130 int UtcDaliLayoutingNesting_02(void)
131 {
132   /*
133
134   Root
135     |
136   Control (LinearLayout Horizontal)
137     |
138   Control (LayoutingRequired)
139     |
140   Control (LinearLayout Horizontal)
141     |    |
142   LeafControl
143
144   */
145
146   ToolkitTestApplication application;
147   tet_infoline("UtcDaliLayoutingNesting_02 - Nesting a LinearLayout (MATCH_PARENT) containing a leaf control within a LinearLayout");
148   tet_infoline("Then change the parent's size and test child responded correctly");
149
150   Stage stage = Stage::GetCurrent();
151
152   auto rootControl = Control::New();
153   SetupRootLayoutControl( rootControl );
154
155   auto hbox = Control::New();
156   auto hboxLayout = LinearLayout::New();
157   hboxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
158   DevelControl::SetLayout( hbox, hboxLayout );
159   hbox.SetName( "hBox" );
160   hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
161   hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, 800 );
162
163
164   auto vbox = Control::New();
165   auto vboxLayout = LinearLayout::New();
166   vboxLayout.SetOrientation( LinearLayout::Orientation::VERTICAL );
167   DevelControl::SetLayout( vbox, vboxLayout );
168   vbox.SetName( "vBox" );
169   vbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
170   vbox.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
171   hbox.Add( vbox );
172
173   std::vector< Control > controls;
174   controls.push_back( CreateLeafControl( 40, 40 ) );
175
176   for( auto&& iter : controls )
177   {
178     hbox.Add( iter );
179   }
180
181   rootControl.Add( hbox );
182
183   // Ensure layouting happens
184   application.SendNotification();
185   application.Render();
186
187   DALI_TEST_EQUALS( hbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
188   DALI_TEST_EQUALS( vbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 480.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
189   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 40.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
190
191   hbox.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 400 );
192
193   // Ensure layouting happens
194   application.SendNotification();
195   application.Render();
196
197
198   DALI_TEST_EQUALS( hbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 400.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
199   DALI_TEST_EQUALS( vbox.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 400.0f, 800.0f, 0.0f ), 0.0001f, TEST_LOCATION );
200   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 40.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
201
202   END_TEST;
203 }