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