2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <toolkit-event-thread-callback.h>
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>
32 #include <layout-utils.h>
35 using namespace Toolkit;
40 // Turns the given control into a Root layout control and adds it to the stage.
41 void SetupRootLayoutControl( Control& rootControl )
43 rootControl = Control::New();
44 auto absoluteLayout = AbsoluteLayout::New();
45 DevelControl::SetLayout( rootControl, absoluteLayout );
46 rootControl.SetName( "RootAbsoluteLayout" );
47 Stage stage = Stage::GetCurrent();
48 stage.Add( rootControl );
51 } // unnamed namespace
53 void utc_dali_toolkit_layouting_setting_and_removal_startup(void)
55 test_return_value = TET_UNDEF;
58 void utc_dali_toolkit_layouting_setting_and_removal_cleanup(void)
60 test_return_value = TET_PASS;
63 int UtcDaliLayoutingSettingAndRemoval_RemoveLayout(void)
65 ToolkitTestApplication application;
66 tet_infoline("UtcDaliLayoutingSettingAndRemoval_RemoveLayout - Remove a layout from a control");
68 Stage stage = Stage::GetCurrent();
70 auto rootControl = Control::New();
71 SetupRootLayoutControl( rootControl );
73 auto hbox = Control::New();
74 auto hboxLayout = LinearLayout::New();
75 DevelControl::SetLayout( hbox, hboxLayout );
76 hbox.SetName( "HBox");
77 rootControl.Add( hbox );
80 std::vector< Control > controls;
81 controls.push_back( CreateLeafControl( 100, 100 ) ); // 0
82 controls.push_back( CreateLeafControl( 100, 100 ) ); // 1
83 controls.push_back( CreateLeafControl( 100, 100 ) ); // 2
85 for( auto&& iter : controls )
90 // Ensure layouting happens
91 application.SendNotification();
94 tet_infoline("Get number of child in the rootControl layout");
95 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( rootControl ) ) ).GetChildCount(), 1 , TEST_LOCATION );
97 tet_infoline("SetLayout with empty Layout handle");
99 DevelControl::SetLayout( hbox, LayoutItem{} );
101 // Ensure layouting happens
102 application.SendNotification();
103 application.Render();
105 tet_infoline("Get number of children in the rootControl layout");
106 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( rootControl ) ) ).GetChildCount(), 1 , TEST_LOCATION );
111 int UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromChild(void)
113 ToolkitTestApplication application;
114 tet_infoline("UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromChild - Remove a layout from a child of another layout");
120 control0 control1 control2
123 Test removes layout from control0 but does not remove the control.
127 Stage stage = Stage::GetCurrent();
129 auto rootControl = Control::New();
130 SetupRootLayoutControl( rootControl );
132 auto hbox = Control::New();
133 auto hboxLayout = LinearLayout::New();
134 DevelControl::SetLayout( hbox, hboxLayout );
135 hbox.SetName( "HBox");
136 rootControl.Add( hbox );
138 // Add child controls
139 std::vector< Control > controls;
140 controls.push_back( CreateLeafControl( 100, 100 ) ); // 0
141 controls.push_back( CreateLeafControl( 100, 100 ) ); // 1
142 controls.push_back( CreateLeafControl( 100, 100 ) ); // 2
144 for( auto&& iter : controls )
149 // Ensure layouting happens
150 application.SendNotification();
151 application.Render();
153 tet_infoline("Set LinearLayout to child control 0");
155 auto vboxLayout = LinearLayout::New();
156 DevelControl::SetLayout( controls[0], vboxLayout );
158 // Ensure layouting happens
159 application.SendNotification();
160 application.Render();
162 tet_infoline("Get number of children in the hbox layout");
163 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( hbox ) ) ).GetChildCount(), 3 , TEST_LOCATION );
165 tet_infoline("SetLayout with empty Layout handle");
167 DevelControl::SetLayout( controls[0], LayoutItem{} );
169 // If vbox control has no children then should get a LayoutItem.
170 // but if still has children then should be a LayoutGroup/BinContainer.
172 // Ensure layouting happens
173 application.SendNotification();
174 application.Render();
176 tet_infoline("Get number of child in the hbox layout");
177 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( hbox ) ) ).GetChildCount(), 3 , TEST_LOCATION );
178 // Test should fail as the setting of an empty layout reduces the child count by 1
183 int UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromHbox(void)
185 ToolkitTestApplication application;
186 tet_infoline(" UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromHbox");
188 Stage stage = Stage::GetCurrent();
190 auto rootControl = Control::New();
191 auto absoluteLayout = AbsoluteLayout::New();
192 DevelControl::SetLayout( rootControl, absoluteLayout );
193 rootControl.SetName( "AbsoluteLayout" );
194 stage.Add( rootControl );
196 auto hbox = Control::New();
197 auto hboxLayout = LinearLayout::New();
198 hboxLayout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
199 DevelControl::SetLayout( hbox, hboxLayout );
200 hbox.SetName( "Container" );
202 std::vector< Control > controls;
203 controls.push_back( CreateLeafControl( 40, 40 ) );
204 controls.push_back( CreateLeafControl( 60, 40 ) );
206 for( auto&& iter : controls )
210 hbox.SetParentOrigin( ParentOrigin::CENTER );
211 hbox.SetAnchorPoint( AnchorPoint::CENTER );
212 rootControl.Add( hbox );
214 tet_infoline("Layout as normal");
215 application.SendNotification();
216 application.Render();
218 tet_infoline("Set an empty layout on hbox container");
219 LinearLayout emptyLayout;
220 DevelControl::SetLayout( hbox, emptyLayout );
222 tet_infoline("Run another layout");
223 application.SendNotification();
224 application.Render();
226 tet_infoline("Check leaf controls size and position");
228 DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
229 tet_infoline("Child keeps position from last layout");
230 DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 40.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
232 DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 40.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
233 DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 60.0f, 40.0f, 0.0f ), 0.0001f, TEST_LOCATION );
238 int UtcDaliLayouting_SetLayoutAlreadyInUse(void)
241 1 ControlA (LinearLayoutA)
244 3 ControlB (LinearLayoutA)
247 Test number of children in each control/layout
249 LinearLayoutA should now have 4 children
251 ControlB should have 4 children too
252 ControlA should have 3 children.
256 ToolkitTestApplication application;
257 tet_infoline(" UtcDaliLayouting_SetLayoutAlreadyInUse - Set layout belonging to controlA to controlB");
259 Stage stage = Stage::GetCurrent();
261 auto rootControl = Control::New();
262 auto absoluteLayout = AbsoluteLayout::New();
263 DevelControl::SetLayout( rootControl, absoluteLayout );
264 rootControl.SetName( "AbsoluteLayout" );
265 stage.Add( rootControl );
267 auto controlA = Control::New();
268 auto hboxLayout = LinearLayout::New();
269 controlA.SetName( "controlA");
271 rootControl.Add( controlA );
273 DevelControl::SetLayout( controlA, hboxLayout );
275 // Add child controls
276 std::vector< Control > controls;
277 controls.push_back( CreateLeafControl( 100, 100 ) ); // 0
278 controls.push_back( CreateLeafControl( 100, 100 ) ); // 1
279 controls.push_back( CreateLeafControl( 100, 100 ) ); // 2
281 for( auto&& iter : controls )
283 controlA.Add( iter );
286 // Ensure layouting happens
287 application.SendNotification();
288 application.Render();
290 tet_infoline("SetLayout to another control");
292 auto controlB = Control::New();
293 controlB.SetName( "controlB");
295 std::vector< Control > moreControls;
296 moreControls.push_back( CreateLeafControl( 100, 100 ) ); // 0
297 moreControls.push_back( CreateLeafControl( 100, 100 ) ); // 1
298 moreControls.push_back( CreateLeafControl( 100, 100 ) ); // 3
299 moreControls.push_back( CreateLeafControl( 100, 100 ) ); // 4
301 for( auto&& iter : moreControls )
303 controlB.Add( iter );
306 DevelControl::SetLayout( controlB, hboxLayout ); // Set hboxLayout used by ControlA to ControlB
308 // Ensure layouting happens
309 application.SendNotification();
310 application.Render();
312 tet_infoline("Get number of children in each control's layout");
313 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( controlA ) ) ).GetChildCount(), 3 , TEST_LOCATION );
314 DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( controlB ) ) ).GetChildCount(), 4 , TEST_LOCATION );