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