2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
23 using namespace Toolkit;
25 void dali_tableview_startup(void)
27 test_return_value = TET_UNDEF;
30 void dali_tableview_cleanup(void)
32 test_return_value = TET_PASS;
37 static bool gObjectCreatedCallBackCalled;
39 static void TestCallback(BaseHandle handle)
41 gObjectCreatedCallBackCalled = true;
52 * function operator to apply the parent size
54 Dali::Vector3 operator()(const Dali::Vector3& current)
56 return Dali::Vector3( 100.0f, 100.0f, 100.0f );
60 // Convenience function to quickly set up a 10x10 table with each cell being 10x10 pixels in size by default.
61 static void SetupTableViewAndActors(TableView& tableView, Actor& actor1, Actor& actor2, Actor& actor3)
63 tableView = TableView::New(10,10); // 10 by 10 grid.
64 DALI_TEST_CHECK(tableView);
66 Stage::GetCurrent().Add( tableView );
67 tableView.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, Constraint100() ) );
68 tableView.SetLayoutAnimationDuration(0.0f);
70 actor1 = Actor::New();
71 actor2 = Actor::New();
72 actor3 = Actor::New();
74 actor1.SetSize(10,10);
75 actor2.SetSize(10,10);
76 actor3.SetSize(10,10);
78 tableView.AddChild(actor1, TableView::CellPosition(0,0));
79 tableView.AddChild(actor2, TableView::CellPosition(0,1));
80 tableView.AddChild(actor3, TableView::CellPosition(1,0));
86 int UtcDaliTableViewNew(void)
88 ToolkitTestApplication application;
90 TableView tableView = TableView::New(10,10);
91 DALI_TEST_CHECK(tableView);
93 //Additional check to ensure object is created by checking if it's registered
94 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
95 DALI_TEST_CHECK( registry );
97 gObjectCreatedCallBackCalled = false;
98 registry.ObjectCreatedSignal().Connect(&TestCallback);
100 TableView tableView = TableView::New(10,10);
102 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
106 // Test adjusting the metric values for the cell.
107 int UtcDaliTableViewMetricsPadding(void)
109 ToolkitTestApplication application;
111 tet_infoline("UtcDaliTableViewMetricsPadding");
118 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
120 // 1. check that padding works. no padding:
121 tableView.SetCellPadding(Size(0.0f, 0.0f));
122 application.SendNotification();
123 application.Render();
125 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(0.0f, 0.0f), TEST_LOCATION );
126 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
127 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
128 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
130 // 1. check that padding works. some padding:
131 tableView.SetCellPadding(Size(5.0f, 10.0f));
132 application.SendNotification();
133 application.Render();
135 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(5.0f, 10.0f), TEST_LOCATION );
136 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(5.0f, 10.0f, 0.0f), TEST_LOCATION );
140 // Test adjusting the metric values for the cell.
141 int UtcDaliTableViewMetricsFixed(void)
143 ToolkitTestApplication application;
145 tet_infoline("UtcDaliTableViewMetricsFixed");
152 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
153 application.SendNotification();
154 application.Render();
156 // 1. check that with no fixed width/heights, actors are in default position.
157 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
158 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
159 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
161 // 2. check that with a fixed width & height, actors to the right and below are offsetted.
162 tableView.SetFixedWidth(0, 20.0f);
163 tableView.SetFixedHeight(0, 50.0f);
164 DALI_TEST_EQUALS( tableView.GetFixedWidth(0), 20.0f, TEST_LOCATION );
165 DALI_TEST_EQUALS( tableView.GetFixedHeight(0), 50.0f, TEST_LOCATION );
167 application.SendNotification();
168 application.Render();
170 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
171 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(20.0f, 0.0f, 0.0f), TEST_LOCATION );
172 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
176 // Test adjusting the metric values for the cell.
177 int UtcDaliTableViewMetricsRelative(void)
179 ToolkitTestApplication application;
181 tet_infoline("UtcDaliTableViewMetricsRelative");
188 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
189 application.SendNotification();
190 application.Render();
192 // 1. check that with no relative width/heights, actors are in default position.
193 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
194 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
195 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
197 // 2. check that with a relative width & height, actors to the right and below are offsetted.
198 tableView.SetRelativeWidth(0, 0.3f); // cell 0,0 occupies 30%x50% of the grid (i.e. 30x50 pixels)
199 tableView.SetRelativeHeight(0, 0.5f);
200 DALI_TEST_EQUALS( tableView.GetRelativeWidth(0), 0.3f, TEST_LOCATION );
201 DALI_TEST_EQUALS( tableView.GetRelativeHeight(0), 0.5f, TEST_LOCATION );
203 application.SendNotification();
204 application.Render();
206 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
207 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(30.0f, 0.0f, 0.0f), TEST_LOCATION );
208 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
213 // Test animation duration setting.
214 int UtcDaliTableViewAnimation(void)
216 ToolkitTestApplication application;
218 tet_infoline("UtcDaliTableViewAnimation");
219 TableView tableView = TableView::New(10,10);
220 DALI_TEST_CHECK(tableView);
222 tableView.SetLayoutAnimationDuration(5.0f);
223 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 5.0f, TEST_LOCATION);
225 tableView.SetLayoutAnimationDuration(2.5f);
226 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 2.5f, TEST_LOCATION);
230 // Test Adding/Removing/Finding Children.
231 int UtcDaliTableViewChild(void)
233 ToolkitTestApplication application;
235 tet_infoline("UtcDaliTableViewChild");
237 // Create a 10x10 table-view
238 TableView tableView = TableView::New(10,10);
239 DALI_TEST_CHECK( tableView );
241 // Check if actor doesn't exist.
242 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
244 // Add an actor to it at 0,0
245 Actor actor = Actor::New();
246 tableView.AddChild(actor, TableView::CellPosition());
249 DALI_TEST_CHECK( tableView.GetChildAt(TableView::CellPosition(0,0)) );
252 tableView.RemoveChildAt(TableView::CellPosition());
254 // Check if actor no longer exists.
255 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
257 // Add actor to it again, but at 2,5
258 tableView.AddChild(actor, TableView::CellPosition(2,5));
260 // Add another actor somewhere else 7,8
261 Actor actor2 = Actor::New();
262 tableView.AddChild(actor2, TableView::CellPosition(7,8));
266 // Check that no actor exists in a few random places.
267 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
268 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(2,1)) );
269 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(6,3)) );
270 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(9,5)) );
272 // Check for actors at actual positions.
273 searchActor = tableView.GetChildAt(TableView::CellPosition(2,5));
274 DALI_TEST_CHECK( searchActor == actor);
276 searchActor = tableView.GetChildAt(TableView::CellPosition(7,8));
277 DALI_TEST_CHECK( searchActor == actor2);
279 // Create a second table, and add already added Child to new one.
280 TableView tableView2 = TableView::New(5,5);
281 tableView2.AddChild(actor, TableView::CellPosition(2,2));
282 DALI_TEST_CHECK( tableView2.GetChildAt(TableView::CellPosition(2,2)) );
286 // Test calling Add on it's own (to invoke the OnChildAdd)
287 int UtcDaliTableViewAdd(void)
289 ToolkitTestApplication application;
291 tet_infoline("UtcDaliTableViewAdd");
293 // Create a 4x1 table-view, and just keep adding.
294 TableView tableView = TableView::New(1,4);
295 DALI_TEST_CHECK( tableView );
297 for(unsigned int i = 0;i<16;i++)
299 Actor currentActor = Actor::New();
300 TableView::CellPosition position = TableView::CellPosition();
301 tableView.Add( currentActor );
302 tableView.FindChildPosition(currentActor, position);
303 tet_printf("%dx%d (%d,%d)\n", tableView.GetColumns(), tableView.GetRows(), position.columnIndex, position.rowIndex);
305 DALI_TEST_EQUALS((position.rowIndex * 4 + position.columnIndex), i, TEST_LOCATION);
310 // Test cell modification.
311 int UtcDaliTableViewCells(void)
313 ToolkitTestApplication application;
314 tet_infoline("UtcDaliTableViewCells");
316 // Create a 10x10 table-view
317 TableView tableView = TableView::New(10,10);
318 DALI_TEST_CHECK( tableView );
320 // Add a few actors to the table.
321 Actor actor1 = Actor::New();
322 Actor actor2 = Actor::New();
323 Actor actor3 = Actor::New();
324 actor1.SetName("Actor1");
325 actor2.SetName("Actor2");
326 actor3.SetName("Actor3");
328 // note: positions are specified in reversed cartesian coords - row,col (i.e. y,x)
329 tableView.AddChild(actor1, TableView::CellPosition(0,0));
330 tableView.AddChild(actor2, TableView::CellPosition(5,5));
331 tableView.AddChild(actor3, TableView::CellPosition(7,2));
333 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
335 // Add a row between actor1 and actor2 | insert column on actor1 and see what happens...
336 tableView.InsertRow(3);
337 tableView.InsertColumn(0);
338 DALI_TEST_CHECK( tableView.GetRows() == 11 && tableView.GetColumns() == 11 );
340 TableView::CellPosition cellPosition;
343 result = tableView.FindChildPosition(actor1, cellPosition);
344 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
345 result = tableView.FindChildPosition(actor2, cellPosition);
346 DALI_TEST_CHECK( result && cellPosition.rowIndex == 6 && cellPosition.columnIndex == 6);
347 result = tableView.FindChildPosition(actor3, cellPosition);
348 DALI_TEST_CHECK( result && cellPosition.rowIndex == 8 && cellPosition.columnIndex == 3);
350 // Delete a row between actor2 and actor3 | delete column on actor2 and see what happens...
351 tableView.DeleteRow(7);
352 tableView.DeleteColumn(6);
353 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
355 result = tableView.FindChildPosition(actor1, cellPosition);
356 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
357 result = tableView.FindChildPosition(actor2, cellPosition);
358 DALI_TEST_CHECK( !result );
359 result = tableView.FindChildPosition(actor3, cellPosition);
360 DALI_TEST_CHECK( result && cellPosition.rowIndex == 7 && cellPosition.columnIndex == 3);
362 // Delete the other two remaining actors by a row delete and a column delete.
363 std::vector<Actor> actorsRemoved;
364 tableView.DeleteRow(0, actorsRemoved);
365 tet_printf("Row Delete >> Actors Removed: %d {", actorsRemoved.size());
366 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
368 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
369 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
371 actorsRemoved.clear();
372 tableView.DeleteColumn(3, actorsRemoved);
373 tet_printf("Column Delete >> Actors Removed: %d {", actorsRemoved.size());
374 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
376 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
377 DALI_TEST_CHECK( actorsRemoved[0] == actor3 );
379 DALI_TEST_CHECK( tableView.GetRows() == 9 && tableView.GetColumns() == 9 );
381 tableView.AddChild(actor1, TableView::CellPosition(5,8));
382 tableView.Resize(100,100);
383 DALI_TEST_CHECK( tableView.GetRows() == 100 && tableView.GetColumns() == 100 );
385 tableView.AddChild(actor2, TableView::CellPosition(69,57));
386 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && tableView.FindChildPosition(actor2, cellPosition) );
388 tableView.Resize(20,20);
389 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
391 actorsRemoved.clear();
392 tableView.Resize(1,1, actorsRemoved);
393 DALI_TEST_CHECK( !tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
394 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
395 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
397 // Add child outside table size, forcing a resize.
398 tableView.AddChild(actor1, TableView::CellPosition(100, 100, 1, 1));
399 DALI_TEST_CHECK( tableView.GetRows() == 101 && tableView.GetColumns() == 101 );
401 // Add child outside table size, forcing a resize.
402 tableView.AddChild(actor1, TableView::CellPosition(110, 110, 5, 5));
403 DALI_TEST_CHECK( tableView.GetRows() == 115 && tableView.GetColumns() == 115 );
405 DALI_TEST_CHECK( true );
409 int UtcDaliTableViewChildAssert(void)
411 ToolkitTestApplication application;
412 tet_infoline("UtcDaliTableViewChildAssert");
414 // Create a 10x10 table-view
415 TableView tableView = TableView::New(10,10);
416 DALI_TEST_CHECK( tableView );
421 tableView.AddChild( childActor, TableView::CellPosition(0,0,5,5) );
423 tet_result(TET_FAIL);
425 catch( Dali::DaliException &e )
427 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
428 DALI_TEST_EQUALS(e.mCondition, "child", TEST_LOCATION);
433 int UtcDaliTableViewMetricsAssert(void)
435 ToolkitTestApplication application;
436 tet_infoline("UtcDaliTableViewChildAssert");
438 // Create a 10x10 table-view
439 TableView tableView = TableView::New(10,10);
440 DALI_TEST_CHECK( tableView );
446 tableView.SetFixedHeight( 10, 1.0f );
448 tet_result(TET_FAIL);
450 catch( Dali::DaliException &e)
452 tet_printf("1. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
453 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
458 tableView.GetFixedHeight( 10 );
460 tet_result(TET_FAIL);
462 catch( Dali::DaliException &e)
464 tet_printf("2. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
465 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
470 tableView.SetFixedWidth( 10, 1.0f );
472 tet_result(TET_FAIL);
474 catch( Dali::DaliException &e)
476 tet_printf("3. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
477 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
482 tableView.GetFixedWidth( 10 );
484 tet_result(TET_FAIL);
486 catch( Dali::DaliException &e)
488 tet_printf("4. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
489 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
496 tableView.SetRelativeHeight( 10, 0.1f );
498 tet_result(TET_FAIL);
500 catch( Dali::DaliException &e)
502 tet_printf("5. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
503 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
508 tableView.GetRelativeHeight( 10 );
510 tet_result(TET_FAIL);
512 catch( Dali::DaliException &e)
514 tet_printf("6. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
515 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
520 tableView.SetRelativeWidth( 10, 0.1f );
522 tet_result(TET_FAIL);
524 catch( Dali::DaliException &e)
526 tet_printf("7. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
527 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);
532 tableView.GetRelativeWidth( 10 );
534 tet_result(TET_FAIL);
536 catch( Dali::DaliException &e)
538 tet_printf("8. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
539 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);