Merge "Controls are LayoutGroups instead of LayoutItems" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-GridLayout.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/grid.h>
27 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
28 #include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
29
30 #include <../custom-layout.h>
31
32 #include <layout-utils.h>
33
34 using namespace Dali;
35 using namespace Toolkit;
36
37 void utc_dali_toolkit_grid_layouting_startup(void)
38 {
39   test_return_value = TET_UNDEF;
40 }
41
42 void utc_dali_toolkit_grid_layouting_cleanup(void)
43 {
44   test_return_value = TET_PASS;
45 }
46
47 int UtcDaliLayouting_GridLayout01(void)
48 {
49   ToolkitTestApplication application;
50   tet_infoline(" UtcDaliLayouting_GridLayout01 2 Column, 4 Items");
51
52   const auto NUMBER_OF_COLUMNS = 2;
53   const auto NUMBER_OF_ITEMS = 4;
54
55   tet_printf( "Testing %d columns with %d items\n", NUMBER_OF_COLUMNS, NUMBER_OF_ITEMS );
56
57   Stage stage = Stage::GetCurrent();
58
59   auto rootControl = Control::New();
60   auto absoluteLayout = AbsoluteLayout::New();
61   DevelControl::SetLayout( rootControl, absoluteLayout );
62   rootControl.SetName( "AbsoluteLayout" );
63   stage.Add( rootControl );
64
65   auto gridContainer = Control::New();
66   auto gridLayout = Grid::New();
67   gridLayout.SetNumberOfColumns( NUMBER_OF_COLUMNS );
68   gridContainer.SetName( "GridLayout");
69   DevelControl::SetLayout( gridContainer, gridLayout );
70   gridContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
71   gridContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
72
73   std::vector< Control > controls;
74   for( auto i=0; i < NUMBER_OF_ITEMS; i++ )
75   {
76     controls.push_back( CreateLeafControl( 100, 100 ) );
77   }
78
79   for( auto&& iter : controls )
80   {
81     gridContainer.Add( iter );
82   }
83
84   rootControl.Add( gridContainer );
85
86   // Ensure layouting happens
87   application.SendNotification();
88   application.Render();
89
90  // Grid will layout first 2 items on first row then last 2 on second row.
91   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
92   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
93   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
94   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
95
96   // Item sizes will not be changed
97   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
98   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
99   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
100   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
101
102   tet_printf( "Confirm number of columns is as set\n");
103   DALI_TEST_EQUALS( gridLayout.GetNumberOfColumns(), NUMBER_OF_COLUMNS, TEST_LOCATION );
104
105   END_TEST;
106 }
107
108 int UtcDaliLayouting_GridLayout02(void)
109 {
110   ToolkitTestApplication application;
111   tet_infoline("UtcDaliLayouting_GridLayout02");
112
113   const auto NUMBER_OF_COLUMNS = 3;
114   const auto NUMBER_OF_ITEMS = 7;
115
116   tet_printf( "Testing %d columns with %d items\n", NUMBER_OF_COLUMNS, NUMBER_OF_ITEMS );
117
118   Stage stage = Stage::GetCurrent();
119
120   auto rootControl = Control::New();
121   auto absoluteLayout = AbsoluteLayout::New();
122   DevelControl::SetLayout( rootControl, absoluteLayout );
123   rootControl.SetName( "AbsoluteLayout" );
124   stage.Add( rootControl );
125
126   auto gridContainer = Control::New();
127   auto gridLayout = Grid::New();
128   gridLayout.SetNumberOfColumns( NUMBER_OF_COLUMNS );
129   gridContainer.SetName( "GridLayout");
130   DevelControl::SetLayout( gridContainer, gridLayout );
131   gridContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
132   gridContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
133
134   std::vector< Control > controls;
135   for( auto i=0; i < NUMBER_OF_ITEMS; i++ )
136   {
137     controls.push_back( CreateLeafControl( 100, 100 ) );
138   }
139
140   for( auto&& iter : controls )
141   {
142     gridContainer.Add( iter );
143   }
144
145   rootControl.Add( gridContainer );
146
147   // Ensure layouting happens
148   application.SendNotification();
149   application.Render();
150
151   // grid  layouts out 3 items per row, which is 480x800.
152   // Row 1
153   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
154   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
155   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 200.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
156   // Row 2
157   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
158   DALI_TEST_EQUALS( controls[4].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
159   DALI_TEST_EQUALS( controls[5].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 200.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
160   // Row 3
161   DALI_TEST_EQUALS( controls[6].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 200.0f, 0.0f ), 0.0001f, TEST_LOCATION );
162
163   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
164   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
165   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
166   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
167   DALI_TEST_EQUALS( controls[4].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
168   DALI_TEST_EQUALS( controls[5].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
169   DALI_TEST_EQUALS( controls[6].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
170
171   END_TEST;
172 }
173
174 int UtcDaliLayouting_GridLayout03(void)
175 {
176   ToolkitTestApplication application;
177   tet_infoline(" UtcDaliLayouting_GridLayout03 Grid Padding");
178
179   const auto NUMBER_OF_COLUMNS = 2;
180   const auto NUMBER_OF_ITEMS = 4;
181
182   tet_printf( "Testing %d columns with %d items\n", NUMBER_OF_COLUMNS, NUMBER_OF_ITEMS );
183
184   Extents GRID_PADDING( Extents( 10, 10, 20, 20 ) ); // start,end,top,bottom
185
186   tet_printf( "Testing with Padding 10,10,20,20\n");
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 gridContainer = Control::New();
197   auto gridLayout = Grid::New();
198   gridLayout.SetNumberOfColumns( NUMBER_OF_COLUMNS );
199   gridContainer.SetName( "GridLayout");
200   DevelControl::SetLayout( gridContainer, gridLayout );
201   gridContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
202   gridContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
203   gridContainer.SetProperty( Control::Property::PADDING, GRID_PADDING );
204
205   std::vector< Control > controls;
206   for( auto i=0; i < NUMBER_OF_ITEMS; i++ )
207   {
208     controls.push_back( CreateLeafControl( 100, 100 ) );
209   }
210
211   for( auto&& iter : controls )
212   {
213     gridContainer.Add( iter );
214   }
215
216   rootControl.Add( gridContainer );
217
218   // Ensure layouting happens
219   application.SendNotification();
220   application.Render();
221
222   tet_infoline(" UtcDaliLayouting_GridLayout03 Grid Padding 2 Column, 4 Items");
223   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f + GRID_PADDING.start , 0.0f + GRID_PADDING.top, 0.0f ), 0.0001f, TEST_LOCATION );
224   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f + GRID_PADDING.start, 0.0f + GRID_PADDING.top, 0.0f ), 0.0001f, TEST_LOCATION );
225   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f + GRID_PADDING.start, 100.0f + GRID_PADDING.top , 0.0f ), 0.0001f, TEST_LOCATION );
226   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f + GRID_PADDING.start, 100.0f + GRID_PADDING.top, 0.0f ), 0.0001f, TEST_LOCATION );
227
228   tet_infoline(" UtcDaliLayouting_GridLayout03 Size of Grid should include padding");
229   DALI_TEST_EQUALS( gridContainer.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f * NUMBER_OF_COLUMNS + GRID_PADDING.start + + GRID_PADDING.end,
230                                                                                  100.0f * ( NUMBER_OF_ITEMS / NUMBER_OF_COLUMNS ) +
231                                                                                  GRID_PADDING.top + GRID_PADDING.bottom,
232                                                                                  0.0f ), 0.0001f, TEST_LOCATION );
233
234   tet_infoline(" UtcDaliLayouting_GridLayout03 Item sizes unchanged");
235   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
236   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
237   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
238   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
239
240   END_TEST;
241 }
242
243 int UtcDaliLayouting_GridLayout04(void)
244 {
245   ToolkitTestApplication application;
246   tet_infoline(" UtcDaliLayouting_GridLayout04 Child Margin");
247
248   const auto NUMBER_OF_COLUMNS = 2;
249   const auto NUMBER_OF_ITEMS = 4;
250
251   tet_printf( "Testing %d columns with %d items\n", NUMBER_OF_COLUMNS, NUMBER_OF_ITEMS );
252
253   Extents GRID_PADDING( Extents( 10, 10, 20, 20 ) ); // start,end,top,bottom
254   Extents ITEM_MARGIN( Extents( 10, 10, 5, 5 ) ); // start,end,top,bottom
255
256   tet_printf( "Testing with Margin 10,10,5,5\n");
257
258   Stage stage = Stage::GetCurrent();
259
260   auto rootControl = Control::New();
261   auto absoluteLayout = AbsoluteLayout::New();
262   DevelControl::SetLayout( rootControl, absoluteLayout );
263   rootControl.SetName( "AbsoluteLayout" );
264   stage.Add( rootControl );
265
266   auto gridContainer = Control::New();
267   auto gridLayout = Grid::New();
268   gridLayout.SetNumberOfColumns( NUMBER_OF_COLUMNS );
269   gridContainer.SetName( "GridLayout");
270   DevelControl::SetLayout( gridContainer, gridLayout );
271   gridContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
272   gridContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
273   gridContainer.SetProperty( Control::Property::PADDING, GRID_PADDING );
274
275   std::vector< Control > controls;
276   for( auto i=0; i < NUMBER_OF_ITEMS; i++ )
277   {
278     auto control = CreateLeafControl( 100, 100 );
279     control.SetProperty(Toolkit::Control::Property::MARGIN, ITEM_MARGIN );
280     controls.push_back( control );
281   }
282
283   for( auto&& iter : controls )
284   {
285     gridContainer.Add( iter );
286   }
287
288   rootControl.Add( gridContainer );
289
290   // Ensure layouting happens
291   application.SendNotification();
292   application.Render();
293
294   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ),
295                     Vector3( 0.0f + GRID_PADDING.start + ITEM_MARGIN.start,
296                     0.0f + GRID_PADDING.top + ITEM_MARGIN.top,
297                     0.0f ),
298                     0.0001f, TEST_LOCATION );
299
300   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ),
301                     Vector3( 100.0f + GRID_PADDING.start + ITEM_MARGIN.start *2 + ITEM_MARGIN.end,
302                     0.0f + GRID_PADDING.top + ITEM_MARGIN.top,
303                     0.0f ), 0.0001f, TEST_LOCATION );
304
305   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::POSITION ),
306                     Vector3( 0.0f + GRID_PADDING.start + ITEM_MARGIN.start,
307                     100.0f + GRID_PADDING.top + ITEM_MARGIN.top*2 + ITEM_MARGIN.bottom,
308                     0.0f ), 0.0001f, TEST_LOCATION );
309
310   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::POSITION ),
311                     Vector3( 100.0f + GRID_PADDING.start + ITEM_MARGIN.start*2 + ITEM_MARGIN.end,
312                     100.0f + GRID_PADDING.top + ITEM_MARGIN.top*2 + ITEM_MARGIN.bottom,
313                     0.0f ), 0.0001f, TEST_LOCATION );
314
315   tet_infoline(" UtcDaliLayouting_GridLayout03 Size of Grid should include padding and margins");
316
317   const auto NUMBER_OF_ROWS = ( NUMBER_OF_ITEMS / NUMBER_OF_COLUMNS );
318
319   DALI_TEST_EQUALS( gridContainer.GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f * NUMBER_OF_COLUMNS + GRID_PADDING.start + GRID_PADDING.end +
320                                                                                           ITEM_MARGIN.start *NUMBER_OF_COLUMNS + ITEM_MARGIN.end *NUMBER_OF_COLUMNS,
321                                                                                           100.0f * NUMBER_OF_ROWS +
322                                                                                           GRID_PADDING.top + GRID_PADDING.bottom +
323                                                                                           ITEM_MARGIN.bottom *NUMBER_OF_ROWS + ITEM_MARGIN.bottom *NUMBER_OF_ROWS,
324                                                                                           0.0f ), 0.0001f, TEST_LOCATION );
325
326   tet_infoline(" UtcDaliLayouting_GridLayout03 Item sizes unchanged");
327   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
328   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
329   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
330   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
331
332   END_TEST;
333 }
334
335
336 int UtcDaliLayouting_GridLayoutDownCast(void)
337 {
338   TestApplication application;
339   tet_infoline(" UtcDaliLayouting_GridLayoutDownCast - Testing Downcast");
340
341   Grid gridLayout = Grid::New();
342
343   LayoutGroup layoutGroup( gridLayout );
344
345   Grid gridLayoutCandidate = Grid::DownCast( layoutGroup );
346   DALI_TEST_CHECK( gridLayoutCandidate );
347
348   END_TEST;
349 }