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