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