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