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.
22 #include <dali/public-api/dali-core.h>
23 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
25 #include <dali-toolkit-test-suite-utils.h>
28 using namespace Toolkit;
30 static void Startup();
31 static void Cleanup();
35 static bool gObjectCreatedCallBackCalled;
37 static void TestCallback(BaseHandle handle)
39 gObjectCreatedCallBackCalled = true;
44 void (*tet_startup)() = Startup;
45 void (*tet_cleanup)() = Cleanup;
48 static void UtcDaliTableViewNew();
49 static void UtcDaliTableViewMetricsPadding();
50 static void UtcDaliTableViewMetricsFixed();
51 static void UtcDaliTableViewMetricsRelative();
52 static void UtcDaliTableViewAnimation();
53 static void UtcDaliTableViewChild();
54 static void UtcDaliTableViewAdd();
55 static void UtcDaliTableViewCells();
56 static void UtcDaliTableViewChildAssert();
57 static void UtcDaliTableViewMetricsAssert();
60 POSITIVE_TC_IDX = 0x01,
64 // Add test functionality for all APIs in the class (Positive and Negative)
66 struct tet_testlist tet_testlist[] = {
67 { UtcDaliTableViewNew, POSITIVE_TC_IDX },
68 { UtcDaliTableViewMetricsPadding, POSITIVE_TC_IDX },
69 { UtcDaliTableViewMetricsFixed, POSITIVE_TC_IDX },
70 { UtcDaliTableViewMetricsRelative, POSITIVE_TC_IDX },
71 { UtcDaliTableViewAnimation, POSITIVE_TC_IDX },
72 { UtcDaliTableViewChild, POSITIVE_TC_IDX },
73 { UtcDaliTableViewAdd, POSITIVE_TC_IDX },
74 { UtcDaliTableViewCells, POSITIVE_TC_IDX },
75 { UtcDaliTableViewChildAssert, POSITIVE_TC_IDX },
76 { UtcDaliTableViewMetricsAssert, POSITIVE_TC_IDX },
81 // Called only once before first test is run.
86 // Called only once after last test is run
98 * function operator to apply the parent size
100 Dali::Vector3 operator()(const Dali::Vector3& current)
102 return Dali::Vector3( 100.0f, 100.0f, 100.0f );
106 // Convenience function to quickly set up a 10x10 table with each cell being 10x10 pixels in size by default.
107 static void SetupTableViewAndActors(TableView& tableView, Actor& actor1, Actor& actor2, Actor& actor3)
109 tableView = TableView::New(10,10); // 10 by 10 grid.
110 DALI_TEST_CHECK(tableView);
112 Stage::GetCurrent().Add( tableView );
113 tableView.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, Constraint100() ) );
114 tableView.SetLayoutAnimationDuration(0.0f);
116 actor1 = Actor::New();
117 actor2 = Actor::New();
118 actor3 = Actor::New();
120 actor1.SetSize(10,10);
121 actor2.SetSize(10,10);
122 actor3.SetSize(10,10);
124 tableView.AddChild(actor1, TableView::CellPosition(0,0));
125 tableView.AddChild(actor2, TableView::CellPosition(0,1));
126 tableView.AddChild(actor3, TableView::CellPosition(1,0));
129 static void UtcDaliTableViewNew()
131 ToolkitTestApplication application;
133 TableView tableView = TableView::New(10,10);
134 DALI_TEST_CHECK(tableView);
136 //Additional check to ensure object is created by checking if it's registered
137 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
138 DALI_TEST_CHECK( registry );
140 gObjectCreatedCallBackCalled = false;
141 registry.ObjectCreatedSignal().Connect(&TestCallback);
143 TableView tableView = TableView::New(10,10);
145 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
148 // Test adjusting the metric values for the cell.
149 static void UtcDaliTableViewMetricsPadding()
151 ToolkitTestApplication application;
153 tet_infoline("UtcDaliTableViewMetricsPadding");
160 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
162 // 1. check that padding works. no padding:
163 tableView.SetCellPadding(Size(0.0f, 0.0f));
164 application.SendNotification();
165 application.Render();
167 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(0.0f, 0.0f), TEST_LOCATION );
168 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
169 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
170 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
172 // 1. check that padding works. some padding:
173 tableView.SetCellPadding(Size(5.0f, 10.0f));
174 application.SendNotification();
175 application.Render();
177 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(5.0f, 10.0f), TEST_LOCATION );
178 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(5.0f, 10.0f, 0.0f), TEST_LOCATION );
181 // Test adjusting the metric values for the cell.
182 static void UtcDaliTableViewMetricsFixed()
184 ToolkitTestApplication application;
186 tet_infoline("UtcDaliTableViewMetricsFixed");
193 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
194 application.SendNotification();
195 application.Render();
197 // 1. check that with no fixed width/heights, actors are in default position.
198 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
199 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
200 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
202 // 2. check that with a fixed width & height, actors to the right and below are offsetted.
203 tableView.SetFixedWidth(0, 20.0f);
204 tableView.SetFixedHeight(0, 50.0f);
205 DALI_TEST_EQUALS( tableView.GetFixedWidth(0), 20.0f, TEST_LOCATION );
206 DALI_TEST_EQUALS( tableView.GetFixedHeight(0), 50.0f, TEST_LOCATION );
208 application.SendNotification();
209 application.Render();
211 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
212 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(20.0f, 0.0f, 0.0f), TEST_LOCATION );
213 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
216 // Test adjusting the metric values for the cell.
217 static void UtcDaliTableViewMetricsRelative()
219 ToolkitTestApplication application;
221 tet_infoline("UtcDaliTableViewMetricsRelative");
228 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
229 application.SendNotification();
230 application.Render();
232 // 1. check that with no relative width/heights, actors are in default position.
233 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
234 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
235 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
237 // 2. check that with a relative width & height, actors to the right and below are offsetted.
238 tableView.SetRelativeWidth(0, 0.3f); // cell 0,0 occupies 30%x50% of the grid (i.e. 30x50 pixels)
239 tableView.SetRelativeHeight(0, 0.5f);
240 DALI_TEST_EQUALS( tableView.GetRelativeWidth(0), 0.3f, TEST_LOCATION );
241 DALI_TEST_EQUALS( tableView.GetRelativeHeight(0), 0.5f, TEST_LOCATION );
243 application.SendNotification();
244 application.Render();
246 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
247 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(30.0f, 0.0f, 0.0f), TEST_LOCATION );
248 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
252 // Test animation duration setting.
253 static void UtcDaliTableViewAnimation()
255 ToolkitTestApplication application;
257 tet_infoline("UtcDaliTableViewAnimation");
258 TableView tableView = TableView::New(10,10);
259 DALI_TEST_CHECK(tableView);
261 tableView.SetLayoutAnimationDuration(5.0f);
262 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 5.0f, TEST_LOCATION);
264 tableView.SetLayoutAnimationDuration(2.5f);
265 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 2.5f, TEST_LOCATION);
268 // Test Adding/Removing/Finding Children.
269 static void UtcDaliTableViewChild()
271 ToolkitTestApplication application;
273 tet_infoline("UtcDaliTableViewChild");
275 // Create a 10x10 table-view
276 TableView tableView = TableView::New(10,10);
277 DALI_TEST_CHECK( tableView );
279 // Check if actor doesn't exist.
280 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
282 // Add an actor to it at 0,0
283 Actor actor = Actor::New();
284 tableView.AddChild(actor, TableView::CellPosition());
287 DALI_TEST_CHECK( tableView.GetChildAt(TableView::CellPosition(0,0)) );
290 tableView.RemoveChildAt(TableView::CellPosition());
292 // Check if actor no longer exists.
293 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
295 // Add actor to it again, but at 2,5
296 tableView.AddChild(actor, TableView::CellPosition(2,5));
298 // Add another actor somewhere else 7,8
299 Actor actor2 = Actor::New();
300 tableView.AddChild(actor2, TableView::CellPosition(7,8));
304 // Check that no actor exists in a few random places.
305 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
306 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(2,1)) );
307 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(6,3)) );
308 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(9,5)) );
310 // Check for actors at actual positions.
311 searchActor = tableView.GetChildAt(TableView::CellPosition(2,5));
312 DALI_TEST_CHECK( searchActor == actor);
314 searchActor = tableView.GetChildAt(TableView::CellPosition(7,8));
315 DALI_TEST_CHECK( searchActor == actor2);
317 // Create a second table, and add already added Child to new one.
318 TableView tableView2 = TableView::New(5,5);
319 tableView2.AddChild(actor, TableView::CellPosition(2,2));
320 DALI_TEST_CHECK( tableView2.GetChildAt(TableView::CellPosition(2,2)) );
323 // Test calling Add on it's own (to invoke the OnChildAdd)
324 static void UtcDaliTableViewAdd()
326 ToolkitTestApplication application;
328 tet_infoline("UtcDaliTableViewAdd");
330 // Create a 4x1 table-view, and just keep adding.
331 TableView tableView = TableView::New(1,4);
332 DALI_TEST_CHECK( tableView );
334 for(unsigned int i = 0;i<16;i++)
336 Actor currentActor = Actor::New();
337 TableView::CellPosition position = TableView::CellPosition();
338 tableView.Add( currentActor );
339 tableView.FindChildPosition(currentActor, position);
340 tet_printf("%dx%d (%d,%d)\n", tableView.GetColumns(), tableView.GetRows(), position.columnIndex, position.rowIndex);
342 DALI_TEST_EQUALS((position.rowIndex * 4 + position.columnIndex), i, TEST_LOCATION);
346 // Test cell modification.
347 static void UtcDaliTableViewCells()
349 ToolkitTestApplication application;
350 tet_infoline("UtcDaliTableViewCells");
352 // Create a 10x10 table-view
353 TableView tableView = TableView::New(10,10);
354 DALI_TEST_CHECK( tableView );
356 // Add a few actors to the table.
357 Actor actor1 = Actor::New();
358 Actor actor2 = Actor::New();
359 Actor actor3 = Actor::New();
360 actor1.SetName("Actor1");
361 actor2.SetName("Actor2");
362 actor3.SetName("Actor3");
364 // note: positions are specified in reversed cartesian coords - row,col (i.e. y,x)
365 tableView.AddChild(actor1, TableView::CellPosition(0,0));
366 tableView.AddChild(actor2, TableView::CellPosition(5,5));
367 tableView.AddChild(actor3, TableView::CellPosition(7,2));
369 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
371 // Add a row between actor1 and actor2 | insert column on actor1 and see what happens...
372 tableView.InsertRow(3);
373 tableView.InsertColumn(0);
374 DALI_TEST_CHECK( tableView.GetRows() == 11 && tableView.GetColumns() == 11 );
376 TableView::CellPosition cellPosition;
379 result = tableView.FindChildPosition(actor1, cellPosition);
380 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
381 result = tableView.FindChildPosition(actor2, cellPosition);
382 DALI_TEST_CHECK( result && cellPosition.rowIndex == 6 && cellPosition.columnIndex == 6);
383 result = tableView.FindChildPosition(actor3, cellPosition);
384 DALI_TEST_CHECK( result && cellPosition.rowIndex == 8 && cellPosition.columnIndex == 3);
386 // Delete a row between actor2 and actor3 | delete column on actor2 and see what happens...
387 tableView.DeleteRow(7);
388 tableView.DeleteColumn(6);
389 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
391 result = tableView.FindChildPosition(actor1, cellPosition);
392 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
393 result = tableView.FindChildPosition(actor2, cellPosition);
394 DALI_TEST_CHECK( !result );
395 result = tableView.FindChildPosition(actor3, cellPosition);
396 DALI_TEST_CHECK( result && cellPosition.rowIndex == 7 && cellPosition.columnIndex == 3);
398 // Delete the other two remaining actors by a row delete and a column delete.
399 std::vector<Actor> actorsRemoved;
400 tableView.DeleteRow(0, actorsRemoved);
401 tet_printf("Row Delete >> Actors Removed: %d {", actorsRemoved.size());
402 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
404 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
405 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
407 actorsRemoved.clear();
408 tableView.DeleteColumn(3, actorsRemoved);
409 tet_printf("Column Delete >> Actors Removed: %d {", actorsRemoved.size());
410 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
412 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
413 DALI_TEST_CHECK( actorsRemoved[0] == actor3 );
415 DALI_TEST_CHECK( tableView.GetRows() == 9 && tableView.GetColumns() == 9 );
417 tableView.AddChild(actor1, TableView::CellPosition(5,8));
418 tableView.Resize(100,100);
419 DALI_TEST_CHECK( tableView.GetRows() == 100 && tableView.GetColumns() == 100 );
421 tableView.AddChild(actor2, TableView::CellPosition(69,57));
422 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && tableView.FindChildPosition(actor2, cellPosition) );
424 tableView.Resize(20,20);
425 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
427 actorsRemoved.clear();
428 tableView.Resize(1,1, actorsRemoved);
429 DALI_TEST_CHECK( !tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
430 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
431 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
433 // Add child outside table size, forcing a resize.
434 tableView.AddChild(actor1, TableView::CellPosition(100, 100, 1, 1));
435 DALI_TEST_CHECK( tableView.GetRows() == 101 && tableView.GetColumns() == 101 );
437 // Add child outside table size, forcing a resize.
438 tableView.AddChild(actor1, TableView::CellPosition(110, 110, 5, 5));
439 DALI_TEST_CHECK( tableView.GetRows() == 115 && tableView.GetColumns() == 115 );
441 DALI_TEST_CHECK( true );
444 static void UtcDaliTableViewChildAssert()
446 ToolkitTestApplication application;
447 tet_infoline("UtcDaliTableViewChildAssert");
449 // Create a 10x10 table-view
450 TableView tableView = TableView::New(10,10);
451 DALI_TEST_CHECK( tableView );
456 tableView.AddChild( childActor, TableView::CellPosition(0,0,5,5) );
458 tet_result(TET_FAIL);
460 catch( Dali::DaliException &e )
462 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
463 DALI_TEST_EQUALS(e.mCondition, "child", TEST_LOCATION);
467 static void UtcDaliTableViewMetricsAssert()
469 ToolkitTestApplication application;
470 tet_infoline("UtcDaliTableViewChildAssert");
472 // Create a 10x10 table-view
473 TableView tableView = TableView::New(10,10);
474 DALI_TEST_CHECK( tableView );
480 tableView.SetFixedHeight( 10, 1.0f );
482 tet_result(TET_FAIL);
484 catch( Dali::DaliException &e)
486 tet_printf("1. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
487 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
492 tableView.GetFixedHeight( 10 );
494 tet_result(TET_FAIL);
496 catch( Dali::DaliException &e)
498 tet_printf("2. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
499 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
504 tableView.SetFixedWidth( 10, 1.0f );
506 tet_result(TET_FAIL);
508 catch( Dali::DaliException &e)
510 tet_printf("3. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
511 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
516 tableView.GetFixedWidth( 10 );
518 tet_result(TET_FAIL);
520 catch( Dali::DaliException &e)
522 tet_printf("4. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
523 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
530 tableView.SetRelativeHeight( 10, 0.1f );
532 tet_result(TET_FAIL);
534 catch( Dali::DaliException &e)
536 tet_printf("5. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
537 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
542 tableView.GetRelativeHeight( 10 );
544 tet_result(TET_FAIL);
546 catch( Dali::DaliException &e)
548 tet_printf("6. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
549 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
554 tableView.SetRelativeWidth( 10, 0.1f );
556 tet_result(TET_FAIL);
558 catch( Dali::DaliException &e)
560 tet_printf("7. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
561 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);
566 tableView.GetRelativeWidth( 10 );
568 tet_result(TET_FAIL);
570 catch( Dali::DaliException &e)
572 tet_printf("8. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
573 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);