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