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