2 * Copyright (c) 2014 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 <dali-toolkit/dali-toolkit.h>
24 using namespace Toolkit;
26 void dali_tableview_startup(void)
28 test_return_value = TET_UNDEF;
31 void dali_tableview_cleanup(void)
33 test_return_value = TET_PASS;
38 static bool gObjectCreatedCallBackCalled;
40 static void TestCallback(BaseHandle handle)
42 gObjectCreatedCallBackCalled = true;
53 * function operator to apply the parent size
55 Dali::Vector3 operator()(const Dali::Vector3& current)
57 return Dali::Vector3( 100.0f, 100.0f, 100.0f );
61 // Convenience function to quickly set up a 10x10 table with each cell being 10x10 pixels in size by default.
62 static void SetupTableViewAndActors(TableView& tableView, Actor& actor1, Actor& actor2, Actor& actor3)
64 tableView = TableView::New(10,10); // 10 by 10 grid.
65 DALI_TEST_CHECK(tableView);
67 Stage::GetCurrent().Add( tableView );
68 tableView.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, Constraint100() ) );
69 tableView.SetLayoutAnimationDuration(0.0f);
71 actor1 = Actor::New();
72 actor2 = Actor::New();
73 actor3 = Actor::New();
75 actor1.SetSize(10,10);
76 actor2.SetSize(10,10);
77 actor3.SetSize(10,10);
79 tableView.AddChild(actor1, TableView::CellPosition(0,0));
80 tableView.AddChild(actor2, TableView::CellPosition(0,1));
81 tableView.AddChild(actor3, TableView::CellPosition(1,0));
87 int UtcDaliTableViewNew(void)
89 ToolkitTestApplication application;
91 TableView tableView = TableView::New(10,10);
92 DALI_TEST_CHECK(tableView);
94 //Additional check to ensure object is created by checking if it's registered
95 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
96 DALI_TEST_CHECK( registry );
98 gObjectCreatedCallBackCalled = false;
99 registry.ObjectCreatedSignal().Connect(&TestCallback);
101 TableView tableView = TableView::New(10,10);
103 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
107 // Test adjusting the metric values for the cell.
108 int UtcDaliTableViewMetricsPadding(void)
110 ToolkitTestApplication application;
112 tet_infoline("UtcDaliTableViewMetricsPadding");
119 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
121 // 1. check that padding works. no padding:
122 tableView.SetCellPadding(Size(0.0f, 0.0f));
123 application.SendNotification();
124 application.Render();
126 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(0.0f, 0.0f), TEST_LOCATION );
127 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
128 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
129 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
131 // 1. check that padding works. some padding:
132 tableView.SetCellPadding(Size(5.0f, 10.0f));
133 application.SendNotification();
134 application.Render();
136 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(5.0f, 10.0f), TEST_LOCATION );
137 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(5.0f, 10.0f, 0.0f), TEST_LOCATION );
141 // Test adjusting the metric values for the cell.
142 int UtcDaliTableViewMetricsFixed(void)
144 ToolkitTestApplication application;
146 tet_infoline("UtcDaliTableViewMetricsFixed");
153 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
154 application.SendNotification();
155 application.Render();
157 // 1. check that with no fixed width/heights, actors are in default position.
158 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
159 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
160 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
162 // 2. check that with a fixed width & height, actors to the right and below are offsetted.
163 tableView.SetFixedWidth(0, 20.0f);
164 tableView.SetFixedHeight(0, 50.0f);
165 DALI_TEST_EQUALS( tableView.GetFixedWidth(0), 20.0f, TEST_LOCATION );
166 DALI_TEST_EQUALS( tableView.GetFixedHeight(0), 50.0f, TEST_LOCATION );
168 application.SendNotification();
169 application.Render();
171 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
172 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(20.0f, 0.0f, 0.0f), TEST_LOCATION );
173 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
177 // Test adjusting the metric values for the cell.
178 int UtcDaliTableViewMetricsRelative(void)
180 ToolkitTestApplication application;
182 tet_infoline("UtcDaliTableViewMetricsRelative");
189 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
190 application.SendNotification();
191 application.Render();
193 // 1. check that with no relative width/heights, actors are in default position.
194 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
195 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
196 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
198 // 2. check that with a relative width & height, actors to the right and below are offsetted.
199 tableView.SetRelativeWidth(0, 0.3f); // cell 0,0 occupies 30%x50% of the grid (i.e. 30x50 pixels)
200 tableView.SetRelativeHeight(0, 0.5f);
201 DALI_TEST_EQUALS( tableView.GetRelativeWidth(0), 0.3f, TEST_LOCATION );
202 DALI_TEST_EQUALS( tableView.GetRelativeHeight(0), 0.5f, TEST_LOCATION );
204 application.SendNotification();
205 application.Render();
207 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
208 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(30.0f, 0.0f, 0.0f), TEST_LOCATION );
209 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
214 // Test animation duration setting.
215 int UtcDaliTableViewAnimation(void)
217 ToolkitTestApplication application;
219 tet_infoline("UtcDaliTableViewAnimation");
220 TableView tableView = TableView::New(10,10);
221 DALI_TEST_CHECK(tableView);
223 tableView.SetLayoutAnimationDuration(5.0f);
224 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 5.0f, TEST_LOCATION);
226 tableView.SetLayoutAnimationDuration(2.5f);
227 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 2.5f, TEST_LOCATION);
231 // Test Adding/Removing/Finding Children.
232 int UtcDaliTableViewChild(void)
234 ToolkitTestApplication application;
236 tet_infoline("UtcDaliTableViewChild");
238 // Create a 10x10 table-view
239 TableView tableView = TableView::New(10,10);
240 DALI_TEST_CHECK( tableView );
242 // Check if actor doesn't exist.
243 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
245 // Add an actor to it at 0,0
246 Actor actor = Actor::New();
247 tableView.AddChild(actor, TableView::CellPosition());
250 DALI_TEST_CHECK( tableView.GetChildAt(TableView::CellPosition(0,0)) );
253 tableView.RemoveChildAt(TableView::CellPosition());
255 // Check if actor no longer exists.
256 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
258 // Add actor to it again, but at 2,5
259 tableView.AddChild(actor, TableView::CellPosition(2,5));
261 // Add another actor somewhere else 7,8
262 Actor actor2 = Actor::New();
263 tableView.AddChild(actor2, TableView::CellPosition(7,8));
267 // Check that no actor exists in a few random places.
268 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
269 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(2,1)) );
270 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(6,3)) );
271 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(9,5)) );
273 // Check for actors at actual positions.
274 searchActor = tableView.GetChildAt(TableView::CellPosition(2,5));
275 DALI_TEST_CHECK( searchActor == actor);
277 searchActor = tableView.GetChildAt(TableView::CellPosition(7,8));
278 DALI_TEST_CHECK( searchActor == actor2);
280 // Create a second table, and add already added Child to new one.
281 TableView tableView2 = TableView::New(5,5);
282 tableView2.AddChild(actor, TableView::CellPosition(2,2));
283 DALI_TEST_CHECK( tableView2.GetChildAt(TableView::CellPosition(2,2)) );
287 // Test calling Add on it's own (to invoke the OnChildAdd)
288 int UtcDaliTableViewAdd(void)
290 ToolkitTestApplication application;
292 tet_infoline("UtcDaliTableViewAdd");
294 // Create a 4x1 table-view, and just keep adding.
295 TableView tableView = TableView::New(1,4);
296 DALI_TEST_CHECK( tableView );
298 for(unsigned int i = 0;i<16;i++)
300 Actor currentActor = Actor::New();
301 TableView::CellPosition position = TableView::CellPosition();
302 tableView.Add( currentActor );
303 tableView.FindChildPosition(currentActor, position);
304 tet_printf("%dx%d (%d,%d)\n", tableView.GetColumns(), tableView.GetRows(), position.columnIndex, position.rowIndex);
306 DALI_TEST_EQUALS((position.rowIndex * 4 + position.columnIndex), i, TEST_LOCATION);
311 // Test cell modification.
312 int UtcDaliTableViewCells(void)
314 ToolkitTestApplication application;
315 tet_infoline("UtcDaliTableViewCells");
317 // Create a 10x10 table-view
318 TableView tableView = TableView::New(10,10);
319 DALI_TEST_CHECK( tableView );
321 // Add a few actors to the table.
322 Actor actor1 = Actor::New();
323 Actor actor2 = Actor::New();
324 Actor actor3 = Actor::New();
325 actor1.SetName("Actor1");
326 actor2.SetName("Actor2");
327 actor3.SetName("Actor3");
329 // note: positions are specified in reversed cartesian coords - row,col (i.e. y,x)
330 tableView.AddChild(actor1, TableView::CellPosition(0,0));
331 tableView.AddChild(actor2, TableView::CellPosition(5,5));
332 tableView.AddChild(actor3, TableView::CellPosition(7,2));
334 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
336 // Add a row between actor1 and actor2 | insert column on actor1 and see what happens...
337 tableView.InsertRow(3);
338 tableView.InsertColumn(0);
339 DALI_TEST_CHECK( tableView.GetRows() == 11 && tableView.GetColumns() == 11 );
341 TableView::CellPosition cellPosition;
344 result = tableView.FindChildPosition(actor1, cellPosition);
345 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
346 result = tableView.FindChildPosition(actor2, cellPosition);
347 DALI_TEST_CHECK( result && cellPosition.rowIndex == 6 && cellPosition.columnIndex == 6);
348 result = tableView.FindChildPosition(actor3, cellPosition);
349 DALI_TEST_CHECK( result && cellPosition.rowIndex == 8 && cellPosition.columnIndex == 3);
351 // Delete a row between actor2 and actor3 | delete column on actor2 and see what happens...
352 tableView.DeleteRow(7);
353 tableView.DeleteColumn(6);
354 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
356 result = tableView.FindChildPosition(actor1, cellPosition);
357 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
358 result = tableView.FindChildPosition(actor2, cellPosition);
359 DALI_TEST_CHECK( !result );
360 result = tableView.FindChildPosition(actor3, cellPosition);
361 DALI_TEST_CHECK( result && cellPosition.rowIndex == 7 && cellPosition.columnIndex == 3);
363 // Delete the other two remaining actors by a row delete and a column delete.
364 std::vector<Actor> actorsRemoved;
365 tableView.DeleteRow(0, actorsRemoved);
366 tet_printf("Row Delete >> Actors Removed: %d {", actorsRemoved.size());
367 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
369 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
370 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
372 actorsRemoved.clear();
373 tableView.DeleteColumn(3, actorsRemoved);
374 tet_printf("Column Delete >> Actors Removed: %d {", actorsRemoved.size());
375 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
377 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
378 DALI_TEST_CHECK( actorsRemoved[0] == actor3 );
380 DALI_TEST_CHECK( tableView.GetRows() == 9 && tableView.GetColumns() == 9 );
382 tableView.AddChild(actor1, TableView::CellPosition(5,8));
383 tableView.Resize(100,100);
384 DALI_TEST_CHECK( tableView.GetRows() == 100 && tableView.GetColumns() == 100 );
386 tableView.AddChild(actor2, TableView::CellPosition(69,57));
387 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && tableView.FindChildPosition(actor2, cellPosition) );
389 tableView.Resize(20,20);
390 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
392 actorsRemoved.clear();
393 tableView.Resize(1,1, actorsRemoved);
394 DALI_TEST_CHECK( !tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
395 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
396 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
398 // Add child outside table size, forcing a resize.
399 tableView.AddChild(actor1, TableView::CellPosition(100, 100, 1, 1));
400 DALI_TEST_CHECK( tableView.GetRows() == 101 && tableView.GetColumns() == 101 );
402 // Add child outside table size, forcing a resize.
403 tableView.AddChild(actor1, TableView::CellPosition(110, 110, 5, 5));
404 DALI_TEST_CHECK( tableView.GetRows() == 115 && tableView.GetColumns() == 115 );
406 DALI_TEST_CHECK( true );
410 int UtcDaliTableViewChildAssert(void)
412 ToolkitTestApplication application;
413 tet_infoline("UtcDaliTableViewChildAssert");
415 // Create a 10x10 table-view
416 TableView tableView = TableView::New(10,10);
417 DALI_TEST_CHECK( tableView );
422 tableView.AddChild( childActor, TableView::CellPosition(0,0,5,5) );
424 tet_result(TET_FAIL);
426 catch( Dali::DaliException &e )
428 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
429 DALI_TEST_EQUALS(e.mCondition, "child", TEST_LOCATION);
434 int UtcDaliTableViewMetricsAssert(void)
436 ToolkitTestApplication application;
437 tet_infoline("UtcDaliTableViewChildAssert");
439 // Create a 10x10 table-view
440 TableView tableView = TableView::New(10,10);
441 DALI_TEST_CHECK( tableView );
447 tableView.SetFixedHeight( 10, 1.0f );
449 tet_result(TET_FAIL);
451 catch( Dali::DaliException &e)
453 tet_printf("1. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
454 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
459 tableView.GetFixedHeight( 10 );
461 tet_result(TET_FAIL);
463 catch( Dali::DaliException &e)
465 tet_printf("2. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
466 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
471 tableView.SetFixedWidth( 10, 1.0f );
473 tet_result(TET_FAIL);
475 catch( Dali::DaliException &e)
477 tet_printf("3. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
478 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
483 tableView.GetFixedWidth( 10 );
485 tet_result(TET_FAIL);
487 catch( Dali::DaliException &e)
489 tet_printf("4. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
490 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
497 tableView.SetRelativeHeight( 10, 0.1f );
499 tet_result(TET_FAIL);
501 catch( Dali::DaliException &e)
503 tet_printf("5. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
504 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
509 tableView.GetRelativeHeight( 10 );
511 tet_result(TET_FAIL);
513 catch( Dali::DaliException &e)
515 tet_printf("6. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
516 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
521 tableView.SetRelativeWidth( 10, 0.1f );
523 tet_result(TET_FAIL);
525 catch( Dali::DaliException &e)
527 tet_printf("7. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
528 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);
533 tableView.GetRelativeWidth( 10 );
535 tet_result(TET_FAIL);
537 catch( Dali::DaliException &e)
539 tet_printf("8. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
540 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);