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