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.
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/public-api/controls/table-view/table-view.h>
26 #include <dali-toolkit-test-suite-utils.h>
29 using namespace Toolkit;
31 static void Startup();
32 static void Cleanup();
36 static bool gObjectCreatedCallBackCalled;
38 static void TestCallback(BaseHandle handle)
40 gObjectCreatedCallBackCalled = true;
45 void (*tet_startup)() = Startup;
46 void (*tet_cleanup)() = Cleanup;
49 static void UtcDaliTableViewNew();
50 static void UtcDaliTableViewMetricsPadding();
51 static void UtcDaliTableViewMetricsFixed();
52 static void UtcDaliTableViewMetricsRelative();
53 static void UtcDaliTableViewAnimation();
54 static void UtcDaliTableViewChild();
55 static void UtcDaliTableViewAdd();
56 static void UtcDaliTableViewCells();
57 static void UtcDaliTableViewChildAssert();
58 static void UtcDaliTableViewMetricsAssert();
61 POSITIVE_TC_IDX = 0x01,
65 // Add test functionality for all APIs in the class (Positive and Negative)
67 struct tet_testlist tet_testlist[] = {
68 { UtcDaliTableViewNew, POSITIVE_TC_IDX },
69 { UtcDaliTableViewMetricsPadding, POSITIVE_TC_IDX },
70 { UtcDaliTableViewMetricsFixed, POSITIVE_TC_IDX },
71 { UtcDaliTableViewMetricsRelative, POSITIVE_TC_IDX },
72 { UtcDaliTableViewAnimation, POSITIVE_TC_IDX },
73 { UtcDaliTableViewChild, POSITIVE_TC_IDX },
74 { UtcDaliTableViewAdd, POSITIVE_TC_IDX },
75 { UtcDaliTableViewCells, POSITIVE_TC_IDX },
76 { UtcDaliTableViewChildAssert, POSITIVE_TC_IDX },
77 { UtcDaliTableViewMetricsAssert, POSITIVE_TC_IDX },
82 // Called only once before first test is run.
87 // Called only once after last test is run
99 * function operator to apply the parent size
101 Dali::Vector3 operator()(const Dali::Vector3& current)
103 return Dali::Vector3( 100.0f, 100.0f, 100.0f );
107 // Convenience function to quickly set up a 10x10 table with each cell being 10x10 pixels in size by default.
108 static void SetupTableViewAndActors(TableView& tableView, Actor& actor1, Actor& actor2, Actor& actor3)
110 tableView = TableView::New(10,10); // 10 by 10 grid.
111 DALI_TEST_CHECK(tableView);
113 Stage::GetCurrent().Add( tableView );
114 tableView.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, Constraint100() ) );
115 tableView.SetLayoutAnimationDuration(0.0f);
117 actor1 = Actor::New();
118 actor2 = Actor::New();
119 actor3 = Actor::New();
121 actor1.SetSize(10,10);
122 actor2.SetSize(10,10);
123 actor3.SetSize(10,10);
125 tableView.AddChild(actor1, TableView::CellPosition(0,0));
126 tableView.AddChild(actor2, TableView::CellPosition(0,1));
127 tableView.AddChild(actor3, TableView::CellPosition(1,0));
130 static void UtcDaliTableViewNew()
132 ToolkitTestApplication application;
134 TableView tableView = TableView::New(10,10);
135 DALI_TEST_CHECK(tableView);
137 //Additional check to ensure object is created by checking if it's registered
138 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
139 DALI_TEST_CHECK( registry );
141 gObjectCreatedCallBackCalled = false;
142 registry.ObjectCreatedSignal().Connect(&TestCallback);
144 TableView tableView = TableView::New(10,10);
146 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
149 // Test adjusting the metric values for the cell.
150 static void UtcDaliTableViewMetricsPadding()
152 ToolkitTestApplication application;
154 tet_infoline("UtcDaliTableViewMetricsPadding");
161 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
163 // 1. check that padding works. no padding:
164 tableView.SetCellPadding(Size(0.0f, 0.0f));
165 application.SendNotification();
166 application.Render();
168 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(0.0f, 0.0f), TEST_LOCATION );
169 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
170 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
171 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
173 // 1. check that padding works. some padding:
174 tableView.SetCellPadding(Size(5.0f, 10.0f));
175 application.SendNotification();
176 application.Render();
178 DALI_TEST_EQUALS( tableView.GetCellPadding(), Size(5.0f, 10.0f), TEST_LOCATION );
179 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(5.0f, 10.0f, 0.0f), TEST_LOCATION );
182 // Test adjusting the metric values for the cell.
183 static void UtcDaliTableViewMetricsFixed()
185 ToolkitTestApplication application;
187 tet_infoline("UtcDaliTableViewMetricsFixed");
194 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
195 application.SendNotification();
196 application.Render();
198 // 1. check that with no fixed width/heights, actors are in default position.
199 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
200 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
201 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
203 // 2. check that with a fixed width & height, actors to the right and below are offsetted.
204 tableView.SetFixedWidth(0, 20.0f);
205 tableView.SetFixedHeight(0, 50.0f);
206 DALI_TEST_EQUALS( tableView.GetFixedWidth(0), 20.0f, TEST_LOCATION );
207 DALI_TEST_EQUALS( tableView.GetFixedHeight(0), 50.0f, TEST_LOCATION );
209 application.SendNotification();
210 application.Render();
212 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
213 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(20.0f, 0.0f, 0.0f), TEST_LOCATION );
214 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
217 // Test adjusting the metric values for the cell.
218 static void UtcDaliTableViewMetricsRelative()
220 ToolkitTestApplication application;
222 tet_infoline("UtcDaliTableViewMetricsRelative");
229 SetupTableViewAndActors(tableView, actor1, actor2, actor3);
230 application.SendNotification();
231 application.Render();
233 // 1. check that with no relative width/heights, actors are in default position.
234 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
235 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(10.0f, 0.0f, 0.0f), TEST_LOCATION );
236 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 10.0f, 0.0f), TEST_LOCATION );
238 // 2. check that with a relative width & height, actors to the right and below are offsetted.
239 tableView.SetRelativeWidth(0, 0.3f); // cell 0,0 occupies 30%x50% of the grid (i.e. 30x50 pixels)
240 tableView.SetRelativeHeight(0, 0.5f);
241 DALI_TEST_EQUALS( tableView.GetRelativeWidth(0), 0.3f, TEST_LOCATION );
242 DALI_TEST_EQUALS( tableView.GetRelativeHeight(0), 0.5f, TEST_LOCATION );
244 application.SendNotification();
245 application.Render();
247 DALI_TEST_EQUALS( actor1.GetCurrentPosition(), Vector3(0.0f, 0.0f, 0.0f), TEST_LOCATION );
248 DALI_TEST_EQUALS( actor2.GetCurrentPosition(), Vector3(30.0f, 0.0f, 0.0f), TEST_LOCATION );
249 DALI_TEST_EQUALS( actor3.GetCurrentPosition(), Vector3(0.0f, 50.0f, 0.0f), TEST_LOCATION );
253 // Test animation duration setting.
254 static void UtcDaliTableViewAnimation()
256 ToolkitTestApplication application;
258 tet_infoline("UtcDaliTableViewAnimation");
259 TableView tableView = TableView::New(10,10);
260 DALI_TEST_CHECK(tableView);
262 tableView.SetLayoutAnimationDuration(5.0f);
263 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 5.0f, TEST_LOCATION);
265 tableView.SetLayoutAnimationDuration(2.5f);
266 DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 2.5f, TEST_LOCATION);
269 // Test Adding/Removing/Finding Children.
270 static void UtcDaliTableViewChild()
272 ToolkitTestApplication application;
274 tet_infoline("UtcDaliTableViewChild");
276 // Create a 10x10 table-view
277 TableView tableView = TableView::New(10,10);
278 DALI_TEST_CHECK( tableView );
280 // Check if actor doesn't exist.
281 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
283 // Add an actor to it at 0,0
284 Actor actor = Actor::New();
285 tableView.AddChild(actor, TableView::CellPosition());
288 DALI_TEST_CHECK( tableView.GetChildAt(TableView::CellPosition(0,0)) );
291 tableView.RemoveChildAt(TableView::CellPosition());
293 // Check if actor no longer exists.
294 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
296 // Add actor to it again, but at 2,5
297 tableView.AddChild(actor, TableView::CellPosition(2,5));
299 // Add another actor somewhere else 7,8
300 Actor actor2 = Actor::New();
301 tableView.AddChild(actor2, TableView::CellPosition(7,8));
305 // Check that no actor exists in a few random places.
306 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
307 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(2,1)) );
308 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(6,3)) );
309 DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(9,5)) );
311 // Check for actors at actual positions.
312 searchActor = tableView.GetChildAt(TableView::CellPosition(2,5));
313 DALI_TEST_CHECK( searchActor == actor);
315 searchActor = tableView.GetChildAt(TableView::CellPosition(7,8));
316 DALI_TEST_CHECK( searchActor == actor2);
318 // Create a second table, and add already added Child to new one.
319 TableView tableView2 = TableView::New(5,5);
320 tableView2.AddChild(actor, TableView::CellPosition(2,2));
321 DALI_TEST_CHECK( tableView2.GetChildAt(TableView::CellPosition(2,2)) );
324 // Test calling Add on it's own (to invoke the OnChildAdd)
325 static void UtcDaliTableViewAdd()
327 ToolkitTestApplication application;
329 tet_infoline("UtcDaliTableViewAdd");
331 // Create a 4x1 table-view, and just keep adding.
332 TableView tableView = TableView::New(1,4);
333 DALI_TEST_CHECK( tableView );
335 for(unsigned int i = 0;i<16;i++)
337 Actor currentActor = Actor::New();
338 TableView::CellPosition position = TableView::CellPosition();
339 tableView.Add( currentActor );
340 tableView.FindChildPosition(currentActor, position);
341 tet_printf("%dx%d (%d,%d)\n", tableView.GetColumns(), tableView.GetRows(), position.columnIndex, position.rowIndex);
343 DALI_TEST_EQUALS((position.rowIndex * 4 + position.columnIndex), i, TEST_LOCATION);
347 // Test cell modification.
348 static void UtcDaliTableViewCells()
350 ToolkitTestApplication application;
351 tet_infoline("UtcDaliTableViewCells");
353 // Create a 10x10 table-view
354 TableView tableView = TableView::New(10,10);
355 DALI_TEST_CHECK( tableView );
357 // Add a few actors to the table.
358 Actor actor1 = Actor::New();
359 Actor actor2 = Actor::New();
360 Actor actor3 = Actor::New();
361 actor1.SetName("Actor1");
362 actor2.SetName("Actor2");
363 actor3.SetName("Actor3");
365 // note: positions are specified in reversed cartesian coords - row,col (i.e. y,x)
366 tableView.AddChild(actor1, TableView::CellPosition(0,0));
367 tableView.AddChild(actor2, TableView::CellPosition(5,5));
368 tableView.AddChild(actor3, TableView::CellPosition(7,2));
370 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
372 // Add a row between actor1 and actor2 | insert column on actor1 and see what happens...
373 tableView.InsertRow(3);
374 tableView.InsertColumn(0);
375 DALI_TEST_CHECK( tableView.GetRows() == 11 && tableView.GetColumns() == 11 );
377 TableView::CellPosition cellPosition;
380 result = tableView.FindChildPosition(actor1, cellPosition);
381 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
382 result = tableView.FindChildPosition(actor2, cellPosition);
383 DALI_TEST_CHECK( result && cellPosition.rowIndex == 6 && cellPosition.columnIndex == 6);
384 result = tableView.FindChildPosition(actor3, cellPosition);
385 DALI_TEST_CHECK( result && cellPosition.rowIndex == 8 && cellPosition.columnIndex == 3);
387 // Delete a row between actor2 and actor3 | delete column on actor2 and see what happens...
388 tableView.DeleteRow(7);
389 tableView.DeleteColumn(6);
390 DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
392 result = tableView.FindChildPosition(actor1, cellPosition);
393 DALI_TEST_CHECK( result && cellPosition.rowIndex == 0 && cellPosition.columnIndex == 1);
394 result = tableView.FindChildPosition(actor2, cellPosition);
395 DALI_TEST_CHECK( !result );
396 result = tableView.FindChildPosition(actor3, cellPosition);
397 DALI_TEST_CHECK( result && cellPosition.rowIndex == 7 && cellPosition.columnIndex == 3);
399 // Delete the other two remaining actors by a row delete and a column delete.
400 std::vector<Actor> actorsRemoved;
401 tableView.DeleteRow(0, actorsRemoved);
402 tet_printf("Row Delete >> Actors Removed: %d {", actorsRemoved.size());
403 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
405 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
406 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
408 actorsRemoved.clear();
409 tableView.DeleteColumn(3, actorsRemoved);
410 tet_printf("Column Delete >> Actors Removed: %d {", actorsRemoved.size());
411 for(size_t i = 0;i<actorsRemoved.size();i++) tet_printf("%d => %s, ", i, actorsRemoved[i].GetName().c_str());
413 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
414 DALI_TEST_CHECK( actorsRemoved[0] == actor3 );
416 DALI_TEST_CHECK( tableView.GetRows() == 9 && tableView.GetColumns() == 9 );
418 tableView.AddChild(actor1, TableView::CellPosition(5,8));
419 tableView.Resize(100,100);
420 DALI_TEST_CHECK( tableView.GetRows() == 100 && tableView.GetColumns() == 100 );
422 tableView.AddChild(actor2, TableView::CellPosition(69,57));
423 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && tableView.FindChildPosition(actor2, cellPosition) );
425 tableView.Resize(20,20);
426 DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
428 actorsRemoved.clear();
429 tableView.Resize(1,1, actorsRemoved);
430 DALI_TEST_CHECK( !tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
431 DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
432 DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
434 // Add child outside table size, forcing a resize.
435 tableView.AddChild(actor1, TableView::CellPosition(100, 100, 1, 1));
436 DALI_TEST_CHECK( tableView.GetRows() == 101 && tableView.GetColumns() == 101 );
438 // Add child outside table size, forcing a resize.
439 tableView.AddChild(actor1, TableView::CellPosition(110, 110, 5, 5));
440 DALI_TEST_CHECK( tableView.GetRows() == 115 && tableView.GetColumns() == 115 );
442 DALI_TEST_CHECK( true );
445 static void UtcDaliTableViewChildAssert()
447 ToolkitTestApplication application;
448 tet_infoline("UtcDaliTableViewChildAssert");
450 // Create a 10x10 table-view
451 TableView tableView = TableView::New(10,10);
452 DALI_TEST_CHECK( tableView );
457 tableView.AddChild( childActor, TableView::CellPosition(0,0,5,5) );
459 tet_result(TET_FAIL);
461 catch( Dali::DaliException &e )
463 tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
464 DALI_TEST_EQUALS(e.mCondition, "child", TEST_LOCATION);
468 static void UtcDaliTableViewMetricsAssert()
470 ToolkitTestApplication application;
471 tet_infoline("UtcDaliTableViewChildAssert");
473 // Create a 10x10 table-view
474 TableView tableView = TableView::New(10,10);
475 DALI_TEST_CHECK( tableView );
481 tableView.SetFixedHeight( 10, 1.0f );
483 tet_result(TET_FAIL);
485 catch( Dali::DaliException &e)
487 tet_printf("1. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
488 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
493 tableView.GetFixedHeight( 10 );
495 tet_result(TET_FAIL);
497 catch( Dali::DaliException &e)
499 tet_printf("2. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
500 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mFixedHeights.size()", TEST_LOCATION);
505 tableView.SetFixedWidth( 10, 1.0f );
507 tet_result(TET_FAIL);
509 catch( Dali::DaliException &e)
511 tet_printf("3. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
512 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
517 tableView.GetFixedWidth( 10 );
519 tet_result(TET_FAIL);
521 catch( Dali::DaliException &e)
523 tet_printf("4. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
524 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mFixedWidths.size()", TEST_LOCATION);
531 tableView.SetRelativeHeight( 10, 0.1f );
533 tet_result(TET_FAIL);
535 catch( Dali::DaliException &e)
537 tet_printf("5. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
538 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
543 tableView.GetRelativeHeight( 10 );
545 tet_result(TET_FAIL);
547 catch( Dali::DaliException &e)
549 tet_printf("6. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
550 DALI_TEST_EQUALS(e.mCondition, "rowIndex < mRelativeHeights.size()", TEST_LOCATION);
555 tableView.SetRelativeWidth( 10, 0.1f );
557 tet_result(TET_FAIL);
559 catch( Dali::DaliException &e)
561 tet_printf("7. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
562 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);
567 tableView.GetRelativeWidth( 10 );
569 tet_result(TET_FAIL);
571 catch( Dali::DaliException &e)
573 tet_printf("8. Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
574 DALI_TEST_EQUALS(e.mCondition, "columnIndex < mRelativeWidths.size()", TEST_LOCATION);