Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-GridLayout.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 <float.h>       // for FLT_MAX
21
22 // Need to override adaptor classes for toolkit test harness, so include
23 // test harness headers before dali headers.
24 #include <dali-toolkit-test-suite-utils.h>
25
26 #include <dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 void utc_dali_toolkit_grid_layout_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_grid_layout_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42
43 namespace
44 {
45
46 const unsigned int TOTAL_ITEM_NUMBER = 200;
47
48 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
49 {
50   float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
51
52   return Vector3(width, width, width);
53 }
54
55
56 // Implementation of ItemFactory for providing actors to ItemView
57 class TestItemFactory : public ItemFactory
58 {
59 public:
60
61   /**
62    * Constructor
63    * @param application class, stored as reference
64    */
65   TestItemFactory()
66   {
67   }
68
69 public: // From ItemFactory
70
71   /**
72    * Query the number of items available from the factory.
73    * The maximum available item has an ID of GetNumberOfItems() - 1.
74    */
75   virtual unsigned int GetNumberOfItems()
76   {
77     return TOTAL_ITEM_NUMBER;
78   }
79
80   /**
81    * Create an Actor to represent a visible item.
82    * @param itemId
83    * @return the created actor.
84    */
85   virtual Actor NewItem(unsigned int itemId)
86   {
87     // Create an test actor for this item
88     ImageActor actor = CreateSolidColorActor(Color::RED);
89     actor.SetSize(64.0f, 64.0f);
90     return actor;
91   }
92 };
93
94 } // namespace
95
96
97
98 int UtcDaliGridLayoutNew(void)
99 {
100   ToolkitTestApplication application;
101
102   // Create a grid layout
103   GridLayoutPtr gridLayout = GridLayout::New();
104
105   DALI_TEST_CHECK(gridLayout);
106   END_TEST;
107 }
108
109 int UtcDaliGridLayoutSetAndGetNumberOfColumns(void)
110 {
111   ToolkitTestApplication application;
112
113   // Create a grid layout
114   GridLayoutPtr gridLayout = GridLayout::New();
115
116   // Set the number of columns
117   gridLayout->SetNumberOfColumns(6);
118
119   // Check whether we get the correct number of columns
120   DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
121   END_TEST;
122 }
123
124 int UtcDaliGridLayoutSetAndGetRowSpacing(void)
125 {
126   ToolkitTestApplication application;
127
128   // Create a grid layout
129   GridLayoutPtr gridLayout = GridLayout::New();
130
131   // Set the row spacing
132   gridLayout->SetRowSpacing(10.0f);
133
134   // Check whether we get the correct row spacing
135   DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
136   END_TEST;
137 }
138
139 int UtcDaliGridLayoutSetAndGetColumnSpacing(void)
140 {
141   ToolkitTestApplication application;
142
143   // Create a grid layout
144   GridLayoutPtr gridLayout = GridLayout::New();
145
146   // Set the column spacing
147   gridLayout->SetColumnSpacing(10.0f);
148
149   // Check whether we get the correct column spacing
150   DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
151   END_TEST;
152 }
153
154 int UtcDaliGridLayoutSetAndGetTopMargin(void)
155 {
156   ToolkitTestApplication application;
157
158   // Create a grid layout
159   GridLayoutPtr gridLayout = GridLayout::New();
160
161   // Set the top margin
162   gridLayout->SetTopMargin(30.0f);
163
164   // Check whether we get the correct top margin
165   DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
166   END_TEST;
167 }
168
169 int UtcDaliGridLayoutSetAndGetBottomMargin(void)
170 {
171   ToolkitTestApplication application;
172
173   // Create a grid layout
174   GridLayoutPtr gridLayout = GridLayout::New();
175
176   // Set the bottom margin
177   gridLayout->SetBottomMargin(30.0f);
178
179   // Check whether we get the correct bottom margin
180   DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
181   END_TEST;
182 }
183
184 int UtcDaliGridLayoutSetAndGetSideMargin(void)
185 {
186   ToolkitTestApplication application;
187
188   // Create a grid layout
189   GridLayoutPtr gridLayout = GridLayout::New();
190
191   // Set the side margin
192   gridLayout->SetSideMargin(10.0f);
193
194   // Check whether we get the correct side margin
195   DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
196   END_TEST;
197 }
198
199 int UtcDaliGridLayoutSetAndGetZGap(void)
200 {
201   ToolkitTestApplication application;
202
203   // Create a grid layout
204   GridLayoutPtr gridLayout = GridLayout::New();
205
206   // Set the gap of items in the Z axis in different columns
207   gridLayout->SetZGap(5.0f);
208
209   // Check whether we get the correct Z gap
210   DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
211   END_TEST;
212 }
213
214 int UtcDaliGridLayoutSetAndGetItemSizeFunction(void)
215 {
216   ToolkitTestApplication application;
217
218   // Create a grid layout
219   GridLayoutPtr gridLayout = GridLayout::New();
220
221   // Set the item size function
222   gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
223
224   // Check whether we get the correct item size function
225   DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
226   END_TEST;
227 }
228
229 int UtcDaliGridLayoutSetAndGetScrollSpeedFactor(void)
230 {
231   ToolkitTestApplication application;
232
233   // Create a grid layout
234   GridLayoutPtr gridLayout = GridLayout::New();
235
236   // Set the scroll speed factor
237   gridLayout->SetScrollSpeedFactor(0.05f);
238
239   // Check whether we get the correct scroll speed factor
240   DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
241   END_TEST;
242 }
243
244 int UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed(void)
245 {
246   ToolkitTestApplication application;
247
248   // Create a grid layout
249   GridLayoutPtr gridLayout = GridLayout::New();
250
251   // Set the maximum swipe speed
252   gridLayout->SetMaximumSwipeSpeed(50.0f);
253
254   // Check whether we get the correct maximum swipe speed
255   DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
256   END_TEST;
257 }
258
259 int UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration(void)
260 {
261   ToolkitTestApplication application;
262
263   // Create a grid layout
264   GridLayoutPtr gridLayout = GridLayout::New();
265
266   // Set the flick animaiton duration
267   gridLayout->SetItemFlickAnimationDuration(0.35f);
268
269   // Check whether we get the correct flick animaiton duration
270   DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
271   END_TEST;
272 }
273
274 int UtcDaliGridLayoutConstraintLeft(void)
275 {
276   ToolkitTestApplication application;
277
278   // Create the ItemView actor
279   TestItemFactory factory;
280   ItemView view = ItemView::New(factory);
281   Vector3 vec(480.0f, 800.0f, 0.0f);
282   GridLayoutPtr gridLayout = GridLayout::New();
283   gridLayout->SetNumberOfColumns(6);
284
285   view.SetName("view actor");
286   view.AddLayout(*gridLayout);
287   view.SetSize(vec);
288
289   Stage::GetCurrent().Add(view);
290   gridLayout->SetOrientation(ControlOrientation::Left);
291   view.ActivateLayout(0, vec, 0.0f);
292
293   application.SendNotification();
294   application.Render(0);
295
296   // render 10 frames
297   for(int i = 0; i < 10; ++i)
298   {
299     application.Render(16); // 60hz frames
300   }
301
302   // Confirm: we have actors in the view and they are positioned some distance from the origin.
303   int nonZeroCount = 0;
304   int elementsFound = 0;
305   for(unsigned int i = 0; i < 10; i++)
306   {
307     Actor testActor = view.GetItem(i);
308     if (testActor)
309     {
310       elementsFound++;
311       Vector3 pos = testActor.GetCurrentPosition();
312
313       if (pos.LengthSquared() > 0.0f)
314       {
315         nonZeroCount++;
316       }
317     }
318   }
319
320   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
321   Stage::GetCurrent().Remove(view);
322   END_TEST;
323 }
324
325 int UtcDaliGridLayoutConstraintRight(void)
326 {
327   ToolkitTestApplication application;
328
329   // Create the ItemView actor
330   TestItemFactory factory;
331   ItemView view = ItemView::New(factory);
332   Vector3 vec(480.0f, 800.0f, 0.0f);
333   GridLayoutPtr gridLayout = GridLayout::New();
334   gridLayout->SetNumberOfColumns(6);
335
336   view.SetName("view actor");
337   view.AddLayout(*gridLayout);
338   view.SetSize(vec);
339
340   Stage::GetCurrent().Add(view);
341   gridLayout->SetOrientation(ControlOrientation::Right);
342   view.ActivateLayout(0, vec, 0.0f);
343
344   application.SendNotification();
345   application.Render(0);
346
347   // render 10 frames
348   for(int i = 0; i < 10; ++i)
349   {
350     application.Render(16); // 60hz frames
351   }
352
353   // Confirm: we have actors in the view and they are positioned some distance from the origin.
354   int nonZeroCount = 0;
355   int elementsFound = 0;
356   for(unsigned int i = 0; i < 10; i++)
357   {
358     Actor testActor = view.GetItem(i);
359     if (testActor)
360     {
361       elementsFound++;
362       Vector3 pos = testActor.GetCurrentPosition();
363
364       if (pos.LengthSquared() > 0.0f)
365       {
366         nonZeroCount++;
367       }
368     }
369   }
370
371   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
372   Stage::GetCurrent().Remove(view);
373   END_TEST;
374 }
375
376 int UtcDaliGridLayoutConstraintUp(void)
377 {
378   ToolkitTestApplication application;
379
380   // Create the ItemView actor
381   TestItemFactory factory;
382   ItemView view = ItemView::New(factory);
383   Vector3 vec(480.0f, 800.0f, 0.0f);
384   GridLayoutPtr gridLayout = GridLayout::New();
385   gridLayout->SetNumberOfColumns(6);
386
387   view.SetName("view actor");
388   view.AddLayout(*gridLayout);
389   view.SetSize(vec);
390
391   Stage::GetCurrent().Add(view);
392   gridLayout->SetOrientation(ControlOrientation::Up);
393   view.ActivateLayout(0, vec, 0.0f);
394
395   application.SendNotification();
396   application.Render(0);
397
398   // render 10 frames
399   for(int i = 0; i < 10; ++i)
400   {
401     application.Render(16); // 60hz frames
402   }
403
404   // Confirm: we have actors in the view and they are positioned some distance from the origin.
405   int nonZeroCount = 0;
406   int elementsFound = 0;
407   for(unsigned int i = 0; i < 10; i++)
408   {
409     Actor testActor = view.GetItem(i);
410     if (testActor)
411     {
412       elementsFound++;
413       Vector3 pos = testActor.GetCurrentPosition();
414
415       if (pos.LengthSquared() > 0.0f)
416       {
417         nonZeroCount++;
418       }
419     }
420   }
421
422   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
423
424   ItemLayoutPtr layout = gridLayout;
425   layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
426   int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
427   DALI_TEST_CHECK(nextItem == 1);
428
429   Stage::GetCurrent().Remove(view);
430   END_TEST;
431 }
432
433 int UtcDaliGridLayoutConstraintDown(void)
434 {
435   ToolkitTestApplication application;
436
437   // Create the ItemView actor
438   TestItemFactory factory;
439   ItemView view = ItemView::New(factory);
440   Vector3 vec(480.0f, 800.0f, 0.0f);
441   GridLayoutPtr gridLayout = GridLayout::New();
442   gridLayout->SetNumberOfColumns(6);
443
444   view.SetName("view actor");
445   view.AddLayout(*gridLayout);
446   view.SetSize(vec);
447
448   Stage::GetCurrent().Add(view);
449   gridLayout->SetOrientation(ControlOrientation::Down);
450   view.ActivateLayout(0, vec, 0.0f);
451
452   application.SendNotification();
453   application.Render(0);
454
455   // render 10 frames
456   for(int i = 0; i < 10; ++i)
457   {
458     application.Render(16); // 60hz frames
459   }
460
461   // Confirm: we have actors in the view and they are positioned some distance from the origin.
462   int nonZeroCount = 0;
463   int elementsFound = 0;
464   for(unsigned int i = 0; i < 10; i++)
465   {
466     Actor testActor = view.GetItem(i);
467     if (testActor)
468     {
469       elementsFound++;
470       Vector3 pos = testActor.GetCurrentPosition();
471
472       if (pos.LengthSquared() > 0.0f)
473       {
474         nonZeroCount++;
475       }
476     }
477   }
478
479   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
480   Stage::GetCurrent().Remove(view);
481   END_TEST;
482 }
483
484 int UtcDaliGridLayoutScrollDirection(void)
485 {
486   ToolkitTestApplication application;
487
488   // Create the ItemView actor
489   TestItemFactory factory;
490   ItemView view = ItemView::New(factory);
491   Vector3 vec(480.0f, 800.0f, 0.0f);
492   GridLayoutPtr gridLayout = GridLayout::New();
493   gridLayout->SetNumberOfColumns(6);
494
495   view.SetName("view actor");
496   view.AddLayout(*gridLayout);
497   view.SetSize(vec);
498
499   Stage::GetCurrent().Add(view);
500   gridLayout->SetOrientation(ControlOrientation::Left);
501   view.ActivateLayout(0, vec, 0.0f);
502
503   application.SendNotification();
504   application.Render(0);
505
506   ItemLayoutPtr layout = gridLayout;
507
508   // render 10 frames
509   for(int i = 0; i < 10; ++i)
510   {
511     application.Render(16); // 60hz frames
512   }
513
514   gridLayout->SetOrientation(ControlOrientation::Up);
515   view.ActivateLayout(0, vec, 0.0f);
516   application.SendNotification();
517   application.Render();
518
519   Degree deg = layout->GetScrollDirection();
520   DALI_TEST_CHECK(deg == 0.0f);
521
522   gridLayout->SetOrientation(ControlOrientation::Down);
523   view.ActivateLayout(0, vec, 0.0f);
524   application.SendNotification();
525   application.Render();
526
527   deg = layout->GetScrollDirection();
528   DALI_TEST_CHECK((deg == 180.0f));
529
530   layout->SetOrientation(ControlOrientation::Left);
531   view.ActivateLayout(0, vec, 0.0f);
532   application.SendNotification();
533   application.Render();
534
535   deg = layout->GetScrollDirection();
536   DALI_TEST_CHECK(deg == 90.f);
537
538   gridLayout->SetOrientation(ControlOrientation::Right);
539   view.ActivateLayout(0, vec, 0.0f);
540   application.SendNotification();
541   application.Render();
542
543   deg = layout->GetScrollDirection();
544   DALI_TEST_CHECK(deg == 270.0f);
545
546   Stage::GetCurrent().Remove(view);
547   END_TEST;
548 }