Layout removal support
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-LayoutingSettingAndRemoval.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
32 #include <layout-utils.h>
33
34 using namespace Dali;
35 using namespace Toolkit;
36
37 namespace
38 {
39
40 // Turns the given control into a Root layout control and adds it to the stage.
41 void SetupRootLayoutControl( Control& rootControl )
42 {
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 );
49 }
50
51 } // unnamed namespace
52
53 void utc_dali_toolkit_layouting_setting_and_removal_startup(void)
54 {
55   test_return_value = TET_UNDEF;
56 }
57
58 void utc_dali_toolkit_layouting_setting_and_removal_cleanup(void)
59 {
60   test_return_value = TET_PASS;
61 }
62
63 int UtcDaliLayoutingSettingAndRemoval_RemoveLayout(void)
64 {
65   ToolkitTestApplication application;
66   tet_infoline("UtcDaliLayoutingSettingAndRemoval_RemoveLayout - Remove a layout from a control");
67
68   Stage stage = Stage::GetCurrent();
69
70   auto rootControl = Control::New();
71   SetupRootLayoutControl( rootControl );
72
73   auto hbox = Control::New();
74   auto hboxLayout = LinearLayout::New();
75   DevelControl::SetLayout( hbox, hboxLayout );
76   hbox.SetName( "HBox");
77   rootControl.Add( hbox );
78
79   // Add child controls
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
84
85   for( auto&& iter : controls )
86   {
87     hbox.Add( iter );
88   }
89
90   // Ensure layouting happens
91   application.SendNotification();
92   application.Render();
93
94   tet_infoline("Get number of child in the rootControl layout");
95   DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( rootControl ) ) ).GetChildCount(), 1 , TEST_LOCATION );
96
97   tet_infoline("SetLayout with empty Layout handle");
98
99   DevelControl::SetLayout( hbox, LayoutItem{} );
100
101   // Ensure layouting happens
102   application.SendNotification();
103   application.Render();
104
105   tet_infoline("Get number of children in the rootControl layout");
106   DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( rootControl ) ) ).GetChildCount(), 1 , TEST_LOCATION );
107
108   END_TEST;
109 }
110
111 int UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromChild(void)
112 {
113   ToolkitTestApplication application;
114   tet_infoline("UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromChild - Remove a layout from a child of another layout");
115
116   /*
117
118               Hbox
119      |          |           |
120   control0  control1  control2
121   (vbox)     (leaf)     (leaf)
122
123   Test removes layout from control0 but does not remove the control.
124
125   */
126
127   Stage stage = Stage::GetCurrent();
128
129   auto rootControl = Control::New();
130   SetupRootLayoutControl( rootControl );
131
132   auto hbox = Control::New();
133   auto hboxLayout = LinearLayout::New();
134   DevelControl::SetLayout( hbox, hboxLayout );
135   hbox.SetName( "HBox");
136   rootControl.Add( hbox );
137
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
143
144   for( auto&& iter : controls )
145   {
146     hbox.Add( iter );
147   }
148
149   // Ensure layouting happens
150   application.SendNotification();
151   application.Render();
152
153   tet_infoline("Set LinearLayout to child control 0");
154
155   auto vboxLayout = LinearLayout::New();
156   DevelControl::SetLayout( controls[0], vboxLayout );
157
158   // Ensure layouting happens
159   application.SendNotification();
160   application.Render();
161
162   tet_infoline("Get number of children in the hbox layout");
163   DALI_TEST_EQUALS( ( LayoutGroup::DownCast( DevelControl::GetLayout( hbox ) ) ).GetChildCount(), 3 , TEST_LOCATION );
164
165   tet_infoline("SetLayout with empty Layout handle");
166
167   DevelControl::SetLayout( controls[0], LayoutItem{} );
168
169   // If vbox control has no children then should get a LayoutItem.
170   // but if still has children then should be a LayoutGroup/BinContainer.
171
172   // Ensure layouting happens
173   application.SendNotification();
174   application.Render();
175
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
179
180   END_TEST;
181 }
182
183 int UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromHbox(void)
184 {
185   ToolkitTestApplication application;
186   tet_infoline(" UtcDaliLayoutingSettingAndRemoval_RemoveLayoutFromHbox");
187
188   Stage stage = Stage::GetCurrent();
189
190   auto rootControl = Control::New();
191   auto absoluteLayout = AbsoluteLayout::New();
192   DevelControl::SetLayout( rootControl, absoluteLayout );
193   rootControl.SetName( "AbsoluteLayout" );
194   stage.Add( rootControl );
195
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" );
201
202   std::vector< Control > controls;
203   controls.push_back( CreateLeafControl( 40, 40 ) );
204   controls.push_back( CreateLeafControl( 60, 40 ) );
205
206   for( auto&& iter : controls )
207   {
208     hbox.Add( iter );
209   }
210   hbox.SetParentOrigin( ParentOrigin::CENTER );
211   hbox.SetAnchorPoint( AnchorPoint::CENTER );
212   rootControl.Add( hbox );
213
214   tet_infoline("Layout as normal");
215   application.SendNotification();
216   application.Render();
217
218   tet_infoline("Set an empty layout on hbox container");
219   LinearLayout emptyLayout;
220   DevelControl::SetLayout( hbox, emptyLayout );
221
222   tet_infoline("Run another layout");
223   application.SendNotification();
224   application.Render();
225
226   tet_infoline("Check leaf controls size and position");
227
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 );
231
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 );
234
235   END_TEST;
236 }