8d0cad67616e858a7b4ba62fbd4fdee4fbbc4d4f
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-TableView.cpp
1 /*
2  * Copyright (c) 2014 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 <dali-toolkit/dali-toolkit.h>
22
23 using namespace Dali;
24 using namespace Toolkit;
25
26 void dali_tableview_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void dali_tableview_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 namespace
37 {
38 static bool gObjectCreatedCallBackCalled;
39
40 static void TestCallback(BaseHandle handle)
41 {
42   gObjectCreatedCallBackCalled = true;
43 }
44
45
46 struct Constraint100
47 {
48   Constraint100( )
49   {
50   }
51
52   /**
53    * function operator to apply the parent size
54    */
55   Dali::Vector3 operator()(const Dali::Vector3& current)
56   {
57     return Dali::Vector3( 100.0f, 100.0f, 100.0f );
58   }
59 };
60
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)
63 {
64   tableView = TableView::New(10,10); // 10 by 10 grid.
65   DALI_TEST_CHECK(tableView);
66
67   Stage::GetCurrent().Add( tableView );
68   tableView.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, Constraint100() ) );
69   tableView.SetLayoutAnimationDuration(0.0f);
70
71   actor1 = Actor::New();
72   actor2 = Actor::New();
73   actor3 = Actor::New();
74
75   actor1.SetSize(10,10);
76   actor2.SetSize(10,10);
77   actor3.SetSize(10,10);
78
79   tableView.AddChild(actor1, TableView::CellPosition(0,0));
80   tableView.AddChild(actor2, TableView::CellPosition(0,1));
81   tableView.AddChild(actor3, TableView::CellPosition(1,0));
82 }
83
84 } // namespace
85
86
87 int UtcDaliTableViewNew(void)
88 {
89   ToolkitTestApplication application;
90
91   TableView tableView = TableView::New(10,10);
92   DALI_TEST_CHECK(tableView);
93
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 );
97
98   gObjectCreatedCallBackCalled = false;
99   registry.ObjectCreatedSignal().Connect(&TestCallback);
100   {
101     TableView tableView = TableView::New(10,10);
102   }
103   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
104   END_TEST;
105 }
106
107 // Test adjusting the metric values for the cell.
108 int UtcDaliTableViewMetricsPadding(void)
109 {
110   ToolkitTestApplication application;
111
112   tet_infoline("UtcDaliTableViewMetricsPadding");
113
114   TableView tableView;
115   Actor actor1;
116   Actor actor2;
117   Actor actor3;
118
119   SetupTableViewAndActors(tableView, actor1, actor2, actor3);
120
121   // 1. check that padding works. no padding:
122   tableView.SetCellPadding(Size(0.0f, 0.0f));
123   application.SendNotification();
124   application.Render();
125
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 );
130
131   // 1. check that padding works. some padding:
132   tableView.SetCellPadding(Size(5.0f, 10.0f));
133   application.SendNotification();
134   application.Render();
135
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 );
138   END_TEST;
139 }
140
141 // Test adjusting the metric values for the cell.
142 int UtcDaliTableViewMetricsFixed(void)
143 {
144   ToolkitTestApplication application;
145
146   tet_infoline("UtcDaliTableViewMetricsFixed");
147
148   TableView tableView;
149   Actor actor1;
150   Actor actor2;
151   Actor actor3;
152
153   SetupTableViewAndActors(tableView, actor1, actor2, actor3);
154   application.SendNotification();
155   application.Render();
156
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 );
161
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 );
167
168   application.SendNotification();
169   application.Render();
170
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 );
174   END_TEST;
175 }
176
177 // Test adjusting the metric values for the cell.
178 int UtcDaliTableViewMetricsRelative(void)
179 {
180   ToolkitTestApplication application;
181
182   tet_infoline("UtcDaliTableViewMetricsRelative");
183
184   TableView tableView;
185   Actor actor1;
186   Actor actor2;
187   Actor actor3;
188
189   SetupTableViewAndActors(tableView, actor1, actor2, actor3);
190   application.SendNotification();
191   application.Render();
192
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 );
197
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 );
203
204   application.SendNotification();
205   application.Render();
206
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 );
210   END_TEST;
211 }
212
213
214 // Test animation duration setting.
215 int UtcDaliTableViewAnimation(void)
216 {
217   ToolkitTestApplication application;
218
219   tet_infoline("UtcDaliTableViewAnimation");
220   TableView tableView = TableView::New(10,10);
221   DALI_TEST_CHECK(tableView);
222
223   tableView.SetLayoutAnimationDuration(5.0f);
224   DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 5.0f, TEST_LOCATION);
225
226   tableView.SetLayoutAnimationDuration(2.5f);
227   DALI_TEST_EQUALS(tableView.GetLayoutAnimationDuration(), 2.5f, TEST_LOCATION);
228   END_TEST;
229 }
230
231 // Test Adding/Removing/Finding Children.
232 int UtcDaliTableViewChild(void)
233 {
234   ToolkitTestApplication application;
235
236   tet_infoline("UtcDaliTableViewChild");
237
238   // Create a 10x10 table-view
239   TableView tableView = TableView::New(10,10);
240   DALI_TEST_CHECK( tableView );
241
242   // Check if actor doesn't exist.
243   DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
244
245   // Add an actor to it at 0,0
246   Actor actor = Actor::New();
247   tableView.AddChild(actor, TableView::CellPosition());
248
249   // Check if exists.
250   DALI_TEST_CHECK( tableView.GetChildAt(TableView::CellPosition(0,0)) );
251
252   // Remove this actor
253   tableView.RemoveChildAt(TableView::CellPosition());
254
255   // Check if actor no longer exists.
256   DALI_TEST_CHECK( !tableView.GetChildAt(TableView::CellPosition(0,0)) );
257
258   // Add actor to it again, but at 2,5
259   tableView.AddChild(actor, TableView::CellPosition(2,5));
260
261   // Add another actor somewhere else 7,8
262   Actor actor2 = Actor::New();
263   tableView.AddChild(actor2, TableView::CellPosition(7,8));
264
265   Actor searchActor;
266
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)) );
272
273   // Check for actors at actual positions.
274   searchActor = tableView.GetChildAt(TableView::CellPosition(2,5));
275   DALI_TEST_CHECK( searchActor == actor);
276
277   searchActor = tableView.GetChildAt(TableView::CellPosition(7,8));
278   DALI_TEST_CHECK( searchActor == actor2);
279
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)) );
284   END_TEST;
285 }
286
287 // Test calling Add on it's own (to invoke the OnChildAdd)
288 int UtcDaliTableViewAdd(void)
289 {
290   ToolkitTestApplication application;
291
292   tet_infoline("UtcDaliTableViewAdd");
293
294   // Create a 4x1 table-view, and just keep adding.
295   TableView tableView = TableView::New(1,4);
296   DALI_TEST_CHECK( tableView );
297
298   for(unsigned int i = 0;i<16;i++)
299   {
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);
305
306     DALI_TEST_EQUALS((position.rowIndex * 4 + position.columnIndex), i, TEST_LOCATION);
307   }
308   END_TEST;
309 }
310
311 // Test cell modification.
312 int UtcDaliTableViewCells(void)
313 {
314   ToolkitTestApplication application;
315   tet_infoline("UtcDaliTableViewCells");
316
317   // Create a 10x10 table-view
318   TableView tableView = TableView::New(10,10);
319   DALI_TEST_CHECK( tableView );
320
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");
328
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));
333
334   DALI_TEST_CHECK( tableView.GetRows() == 10 && tableView.GetColumns() == 10 );
335
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 );
340
341   TableView::CellPosition cellPosition;
342   bool result;
343
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);
350
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 );
355
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);
362
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());
368   tet_printf("}\n");
369   DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
370   DALI_TEST_CHECK( actorsRemoved[0] == actor1 );
371
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());
376   tet_printf("}\n");
377   DALI_TEST_EQUALS( static_cast<int>(actorsRemoved.size()), 1, TEST_LOCATION );
378   DALI_TEST_CHECK( actorsRemoved[0] == actor3 );
379
380   DALI_TEST_CHECK( tableView.GetRows() == 9 && tableView.GetColumns() == 9 );
381
382   tableView.AddChild(actor1, TableView::CellPosition(5,8));
383   tableView.Resize(100,100);
384   DALI_TEST_CHECK( tableView.GetRows() == 100 && tableView.GetColumns() == 100 );
385
386   tableView.AddChild(actor2, TableView::CellPosition(69,57));
387   DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && tableView.FindChildPosition(actor2, cellPosition) );
388
389   tableView.Resize(20,20);
390   DALI_TEST_CHECK( tableView.FindChildPosition(actor1, cellPosition) && !tableView.FindChildPosition(actor2, cellPosition) );
391
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 );
397
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 );
401
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 );
405
406   DALI_TEST_CHECK( true );
407   END_TEST;
408 }
409
410 int UtcDaliTableViewChildAssert(void)
411 {
412   ToolkitTestApplication application;
413   tet_infoline("UtcDaliTableViewChildAssert");
414
415   // Create a 10x10 table-view
416   TableView tableView = TableView::New(10,10);
417   DALI_TEST_CHECK( tableView );
418   Actor childActor;
419
420   try
421   {
422     tableView.AddChild( childActor, TableView::CellPosition(0,0,5,5) );
423     // should assert
424     tet_result(TET_FAIL);
425   }
426   catch( Dali::DaliException &e )
427   {
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);
430   }
431   END_TEST;
432 }
433
434 int UtcDaliTableViewMetricsAssert(void)
435 {
436   ToolkitTestApplication application;
437   tet_infoline("UtcDaliTableViewChildAssert");
438
439   // Create a 10x10 table-view
440   TableView tableView = TableView::New(10,10);
441   DALI_TEST_CHECK( tableView );
442
443   // fixeds...
444
445   try
446   {
447     tableView.SetFixedHeight( 10, 1.0f );
448
449     tet_result(TET_FAIL);
450   }
451   catch( Dali::DaliException &e)
452   {
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);
455   }
456
457   try
458   {
459     tableView.GetFixedHeight( 10 );
460
461     tet_result(TET_FAIL);
462   }
463   catch( Dali::DaliException &e)
464   {
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);
467   }
468
469   try
470   {
471     tableView.SetFixedWidth( 10, 1.0f );
472
473     tet_result(TET_FAIL);
474   }
475   catch( Dali::DaliException &e)
476   {
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);
479   }
480
481   try
482   {
483     tableView.GetFixedWidth( 10 );
484
485     tet_result(TET_FAIL);
486   }
487   catch( Dali::DaliException &e)
488   {
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);
491   }
492
493   // relatives...
494
495   try
496   {
497     tableView.SetRelativeHeight( 10, 0.1f );
498
499     tet_result(TET_FAIL);
500   }
501   catch( Dali::DaliException &e)
502   {
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);
505   }
506
507   try
508   {
509     tableView.GetRelativeHeight( 10 );
510
511     tet_result(TET_FAIL);
512   }
513   catch( Dali::DaliException &e)
514   {
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);
517   }
518
519   try
520   {
521     tableView.SetRelativeWidth( 10, 0.1f );
522
523     tet_result(TET_FAIL);
524   }
525   catch( Dali::DaliException &e)
526   {
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);
529   }
530
531   try
532   {
533     tableView.GetRelativeWidth( 10 );
534
535     tet_result(TET_FAIL);
536   }
537   catch( Dali::DaliException &e)
538   {
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);
541   }
542   END_TEST;
543 }