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