Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / item-view / utc-Dali-DepthLayout.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 <float.h>       // for FLT_MAX
19 #include <stdlib.h>
20 #include <tet_api.h>
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit-test-suite-utils.h>
24
25 using namespace Dali;
26 using namespace Toolkit;
27
28 namespace
29 {
30 const unsigned int TOTAL_ITEM_NUMBER = 200;
31
32 Vector3 DepthLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth)
33 {
34   float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * 0.8f;
35   return Vector3(width, width, width);
36 }
37
38 float DepthLayoutBottomMarginFunction(float layoutHeight)
39 {
40   return layoutHeight * 0.25f;
41 }
42
43 float DepthLayoutColumnPositionFunction(unsigned int numberOfColumns, unsigned int columnNumber, const Vector3& itemSize, float layoutWidth)
44 {
45   float availableSpace = layoutWidth - itemSize.width * numberOfColumns;
46   float leftMargin = availableSpace / numberOfColumns * 0.5f;
47   float columnPosition = leftMargin + itemSize.width * 0.5f + columnNumber * (itemSize.width + availableSpace / numberOfColumns);
48   return columnPosition - layoutWidth * 0.5f;
49 }
50 } // namespace
51
52 static void Startup();
53 static void Cleanup();
54
55 // Implementation of ItemFactory for providing actors to ItemView
56 class TestItemFactory : public ItemFactory
57 {
58 public:
59
60   /**
61    * Constructor
62    * @param application class, stored as reference
63    */
64   TestItemFactory()
65   {
66   }
67
68 public: // From ItemFactory
69
70   /**
71    * Query the number of items available from the factory.
72    * The maximum available item has an ID of GetNumberOfItems() - 1.
73    */
74   virtual unsigned int GetNumberOfItems()
75   {
76     return TOTAL_ITEM_NUMBER;
77   }
78
79   /**
80    * Create an Actor to represent a visible item.
81    * @param itemId
82    * @return the created actor.
83    */
84   virtual Actor NewItem(unsigned int itemId)
85   {
86     // Create an test actor for this item
87     ImageActor actor = CreateSolidColorActor(Color::RED);
88     actor.SetSize(64.0f, 64.0f);
89
90     return actor;
91   }
92 };
93
94 extern "C" {
95   void (*tet_startup)() = Startup;
96   void (*tet_cleanup)() = Cleanup;
97 }
98
99 static void UtcDaliDepthLayoutNew();
100 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns();
101 static void UtcDaliDepthLayoutSetAndGetNumberOfRows();
102 static void UtcDaliDepthLayoutSetAndGetRowSpacing();
103 static void UtcDaliDepthLayoutSetAndGetTiltAngle();
104 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction();
105 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction();
106 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle();
107 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction();
108 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor();
109 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed();
110 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration();
111 static void UtcDaliDepthLayoutConstraintLeft();
112 static void UtcDaliDepthLayoutConstraintRight();
113 static void UtcDaliDepthLayoutConstraintUp();
114 static void UtcDaliDepthLayoutConstraintDown();
115 static void UtcDaliDepthLayoutGetScrollToPosition();
116 static void UtcDaliDepthLayoutScrollDirection();
117
118 enum {
119   POSITIVE_TC_IDX = 0x01,
120   NEGATIVE_TC_IDX,
121 };
122
123 // Add test functionality for all APIs in the class (Positive and Negative)
124 extern "C" {
125   struct tet_testlist tet_testlist[] = {
126     { UtcDaliDepthLayoutNew, POSITIVE_TC_IDX },
127     { UtcDaliDepthLayoutScrollDirection, POSITIVE_TC_IDX },
128     { UtcDaliDepthLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
129     { UtcDaliDepthLayoutSetAndGetNumberOfRows, POSITIVE_TC_IDX },
130     { UtcDaliDepthLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
131     { UtcDaliDepthLayoutSetAndGetTiltAngle, POSITIVE_TC_IDX },
132     { UtcDaliDepthLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
133     { UtcDaliDepthLayoutSetAndGetBottomMarginFunction, POSITIVE_TC_IDX },
134     { UtcDaliDepthLayoutSetAndGetItemTiltAngle, POSITIVE_TC_IDX },
135     { UtcDaliDepthLayoutSetAndGetColumnPositionFunction, POSITIVE_TC_IDX },
136     { UtcDaliDepthLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
137     { UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
138     { UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
139     { UtcDaliDepthLayoutConstraintLeft, POSITIVE_TC_IDX },
140     { UtcDaliDepthLayoutConstraintRight, POSITIVE_TC_IDX },
141     { UtcDaliDepthLayoutConstraintUp, POSITIVE_TC_IDX },
142     { UtcDaliDepthLayoutConstraintDown, POSITIVE_TC_IDX },
143     { UtcDaliDepthLayoutGetScrollToPosition, POSITIVE_TC_IDX },
144     { NULL, 0 }
145   };
146 }
147
148 // Called only once before first test is run.
149 static void Startup()
150 {
151 }
152
153 // Called only once after last test is run
154 static void Cleanup()
155 {
156 }
157
158 static void UtcDaliDepthLayoutNew()
159 {
160   ToolkitTestApplication application;
161
162   // Create a depth layout
163   DepthLayoutPtr depthLayout = DepthLayout::New();
164
165   DALI_TEST_CHECK(depthLayout);
166 }
167
168 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns()
169 {
170   ToolkitTestApplication application;
171
172   // Create a depth layout
173   DepthLayoutPtr depthLayout = DepthLayout::New();
174
175   // Set the number of columns
176   depthLayout->SetNumberOfColumns(5);
177
178   // Check whether we get the correct number of columns
179   DALI_TEST_CHECK(depthLayout->GetNumberOfColumns() == 5);
180 }
181
182 static void UtcDaliDepthLayoutSetAndGetNumberOfRows()
183 {
184   ToolkitTestApplication application;
185
186   // Create a depth layout
187   DepthLayoutPtr depthLayout = DepthLayout::New();
188
189   // Set the number of rows
190   depthLayout->SetNumberOfRows(15);
191
192   // Check whether we get the correct number of rows
193   DALI_TEST_CHECK(depthLayout->GetNumberOfRows() == 15);
194 }
195
196 static void UtcDaliDepthLayoutSetAndGetRowSpacing()
197 {
198   ToolkitTestApplication application;
199
200   // Create a depth layout
201   DepthLayoutPtr depthLayout = DepthLayout::New();
202
203   // Set the row spacing
204   depthLayout->SetRowSpacing(30.0f);
205
206   // Check whether we get the correct row spacing
207   DALI_TEST_EQUALS(depthLayout->GetRowSpacing(), 30.0f, TEST_LOCATION );
208 }
209
210 static void UtcDaliDepthLayoutSetAndGetTiltAngle()
211 {
212   ToolkitTestApplication application;
213
214   // Create a depth layout
215   DepthLayoutPtr depthLayout = DepthLayout::New();
216
217   // Set the tilt angle
218   depthLayout->SetTiltAngle(Degree(25.0f));
219
220   // Check whether we get the correct tilt angle
221   DALI_TEST_EQUALS(float(depthLayout->GetTiltAngle()), 25.0f, 0.001f, TEST_LOCATION );
222 }
223
224 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction()
225 {
226   ToolkitTestApplication application;
227
228   // Create a depth layout
229   DepthLayoutPtr depthLayout = DepthLayout::New();
230
231   // Set the item size function
232   depthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunction);
233
234   // Check whether we get the correct item size function
235   DALI_TEST_CHECK(depthLayout->GetItemSizeFunction() == DepthLayoutItemSizeFunction);
236 }
237
238 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction()
239 {
240   ToolkitTestApplication application;
241
242   // Create a depth layout
243   DepthLayoutPtr depthLayout = DepthLayout::New();
244
245   // Set the bottom margin function
246   depthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunction);
247
248   // Check whether we get the correct bottom margin function
249   DALI_TEST_CHECK(depthLayout->GetBottomMarginFunction() == DepthLayoutBottomMarginFunction);
250 }
251
252 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle()
253 {
254   ToolkitTestApplication application;
255
256   // Create a depth layout
257   DepthLayoutPtr depthLayout = DepthLayout::New();
258
259   // Set the item's tilt angle
260   depthLayout->SetItemTiltAngle(Degree(5.0f));
261
262   // Check whether we get the correct item's tilt angle
263   DALI_TEST_EQUALS(float(depthLayout->GetItemTiltAngle()), 5.0f, 0.001f, TEST_LOCATION );
264 }
265
266 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction()
267 {
268   ToolkitTestApplication application;
269
270   // Create a depth layout
271   DepthLayoutPtr depthLayout = DepthLayout::New();
272
273   // Set the column position function
274   depthLayout->SetColumnPositionFunction(DepthLayoutColumnPositionFunction);
275
276   // Check whether we get the correct column position function
277   DALI_TEST_CHECK(depthLayout->GetColumnPositionFunction() == DepthLayoutColumnPositionFunction);
278 }
279
280 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor()
281 {
282   ToolkitTestApplication application;
283
284   // Create a depth layout
285   DepthLayoutPtr depthLayout = DepthLayout::New();
286
287   // Set the scroll speed factor
288   depthLayout->SetScrollSpeedFactor(0.05f);
289
290   // Check whether we get the correct scroll speed factor
291   DALI_TEST_EQUALS(depthLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
292 }
293
294 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed()
295 {
296   ToolkitTestApplication application;
297
298   // Create a depth layout
299   DepthLayoutPtr depthLayout = DepthLayout::New();
300
301   // Set the maximum swipe speed
302   depthLayout->SetMaximumSwipeSpeed(50.0f);
303
304   // Check whether we get the correct maximum swipe speed
305   DALI_TEST_EQUALS(depthLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
306 }
307
308 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration()
309 {
310   ToolkitTestApplication application;
311
312   // Create a depth layout
313   DepthLayoutPtr depthLayout = DepthLayout::New();
314
315   // Set the flick animaiton duration
316   depthLayout->SetItemFlickAnimationDuration(0.35f);
317
318   // Check whether we get the correct flick animaiton duration
319   DALI_TEST_EQUALS( depthLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
320 }
321
322 static void UtcDaliDepthLayoutConstraintLeft()
323 {
324   ToolkitTestApplication application;
325
326   // Create the ItemView actor
327   TestItemFactory factory;
328   ItemView view = ItemView::New(factory);
329   Vector3 vec(480.0f, 800.0f, 0.0f);
330   DepthLayoutPtr navigationLayout = DepthLayout::New();
331   navigationLayout->SetNumberOfColumns(6);
332
333   view.SetName("view actor");
334   view.AddLayout(*navigationLayout);
335   view.SetSize(vec);
336
337   Stage::GetCurrent().Add(view);
338   navigationLayout->SetOrientation(ControlOrientation::Left);
339   view.ActivateLayout(0, vec, 0.0f);
340
341   application.SendNotification();
342   application.Render(0);
343
344   // render 10 frames
345   for(int i = 0; i < 10; ++i)
346   {
347     application.Render(16); // 60hz frames
348   }
349
350   // Confirm: we have actors in the view and they are positioned some distance from the origin.
351   int nonZeroCount = 0;
352   int elementsFound = 0;
353   for(unsigned int i = 0; i < 10; i++)
354   {
355     Actor testActor = view.GetItem(i);
356     if (testActor)
357     {
358       elementsFound++;
359       Vector3 pos = testActor.GetCurrentPosition();
360
361       if (pos.LengthSquared() > 0.0f)
362       {
363         nonZeroCount++;
364       }
365     }
366   }
367
368   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
369   Stage::GetCurrent().Remove(view);
370 }
371
372 static void UtcDaliDepthLayoutConstraintRight()
373 {
374   ToolkitTestApplication application;
375
376   // Create the ItemView actor
377   TestItemFactory factory;
378   ItemView view = ItemView::New(factory);
379   Vector3 vec(480.0f, 800.0f, 0.0f);
380   DepthLayoutPtr navigationLayout = DepthLayout::New();
381   navigationLayout->SetNumberOfColumns(6);
382
383   view.SetName("view actor");
384   view.AddLayout(*navigationLayout);
385   view.SetSize(vec);
386
387   Stage::GetCurrent().Add(view);
388   navigationLayout->SetOrientation(ControlOrientation::Right);
389   view.ActivateLayout(0, vec, 0.0f);
390
391   application.SendNotification();
392   application.Render(0);
393
394   // render 10 frames
395   for(int i = 0; i < 10; ++i)
396   {
397     application.Render(16); // 60hz frames
398   }
399
400   // Confirm: we have actors in the view and they are positioned some distance from the origin.
401   int nonZeroCount = 0;
402   int elementsFound = 0;
403   for(unsigned int i = 0; i < 10; i++)
404   {
405     Actor testActor = view.GetItem(i);
406     if (testActor)
407     {
408       elementsFound++;
409       Vector3 pos = testActor.GetCurrentPosition();
410
411       if (pos.LengthSquared() > 0.0f)
412       {
413         nonZeroCount++;
414       }
415     }
416   }
417
418   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
419   Stage::GetCurrent().Remove(view);
420 }
421
422 static void UtcDaliDepthLayoutConstraintUp()
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   DepthLayoutPtr navigationLayout = DepthLayout::New();
431   navigationLayout->SetNumberOfColumns(6);
432
433   view.SetName("view actor");
434   view.AddLayout(*navigationLayout);
435   view.SetSize(vec);
436
437   Stage::GetCurrent().Add(view);
438   navigationLayout->SetOrientation(ControlOrientation::Up);
439   view.ActivateLayout(0, vec, 0.0f);
440
441   application.SendNotification();
442   application.Render(0);
443
444   // render 10 frames
445   for(int i = 0; i < 10; ++i)
446   {
447     application.Render(16); // 60hz frames
448   }
449
450   // Confirm: we have actors in the view and they are positioned some distance from the origin.
451   int nonZeroCount = 0;
452   int elementsFound = 0;
453   for(unsigned int i = 0; i < 10; i++)
454   {
455     Actor testActor = view.GetItem(i);
456     if (testActor)
457     {
458       elementsFound++;
459       Vector3 pos = testActor.GetCurrentPosition();
460
461       if (pos.LengthSquared() > 0.0f)
462       {
463         nonZeroCount++;
464       }
465     }
466   }
467
468   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
469   Stage::GetCurrent().Remove(view);
470 }
471
472 static void UtcDaliDepthLayoutConstraintDown()
473 {
474   ToolkitTestApplication application;
475
476   // Create the ItemView actor
477   TestItemFactory factory;
478   ItemView view = ItemView::New(factory);
479   Vector3 vec(480.0f, 800.0f, 0.0f);
480   DepthLayoutPtr navigationLayout = DepthLayout::New();
481   navigationLayout->SetNumberOfColumns(6);
482
483   view.SetName("view actor");
484   view.AddLayout(*navigationLayout);
485   view.SetSize(vec);
486
487   Stage::GetCurrent().Add(view);
488   navigationLayout->SetOrientation(ControlOrientation::Down);
489   view.ActivateLayout(0, vec, 0.0f);
490
491   application.SendNotification();
492   application.Render(0);
493
494   // render 10 frames
495   for(int i = 0; i < 10; ++i)
496   {
497     application.Render(16); // 60hz frames
498   }
499
500   // Confirm: we have actors in the view and they are positioned some distance from the origin.
501   int nonZeroCount = 0;
502   int elementsFound = 0;
503   for(unsigned int i = 0; i < 10; i++)
504   {
505     Actor testActor = view.GetItem(i);
506     if (testActor)
507     {
508       elementsFound++;
509       Vector3 pos = testActor.GetCurrentPosition();
510
511       if (pos.LengthSquared() > 0.0f)
512       {
513         nonZeroCount++;
514       }
515     }
516   }
517
518   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
519   Stage::GetCurrent().Remove(view);
520 }
521
522 static void UtcDaliDepthLayoutGetScrollToPosition()
523 {
524   ToolkitTestApplication application;
525
526   // Create the ItemView actor
527   TestItemFactory factory;
528   ItemView view = ItemView::New(factory);
529   Vector3 vec(480.0f, 800.0f, 0.0f);
530   DepthLayoutPtr layout = DepthLayout::New();
531
532   view.SetName("view actor");
533   view.AddLayout(*layout);
534   view.SetSize(vec);
535
536   Stage::GetCurrent().Add(view);
537   layout->SetOrientation(ControlOrientation::Up);
538   view.ActivateLayout(0, vec, 0.0f);
539
540   application.SendNotification();
541   application.Render(0);
542
543   // render 10 frames
544   for(int i = 0; i < 10; ++i)
545   {
546     application.Render(16); // 60hz frames
547   }
548
549   // Confirm: we have actors in the view.
550   std::vector<unsigned int> indices;
551   for(unsigned int i = 0; i < 10; i++)
552   {
553     Actor testActor = view.GetItem(i);
554     if (testActor)
555     {
556       indices.push_back(i);
557     }
558   }
559
560   try
561   {
562     if (!indices.empty())
563     {
564       const unsigned int firstTargetIndex = indices[indices.size()-1];
565       // scroll to last item
566       view.ScrollToItem(firstTargetIndex, 0.00f);
567       application.Render(16); // 60hz frames
568
569       std::size_t moveCount = 0;
570       for(std::size_t i = 0; i < indices.size(); i++)
571       {
572         float layoutPosBefore = view.GetCurrentLayoutPosition(i);
573         view.ScrollToItem(indices[i], 0.0f);
574
575         application.Render(16); // 60hz frame
576
577         float layoutPosAfter = view.GetCurrentLayoutPosition(i);
578
579         if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
580         {
581           ++moveCount;
582         }
583       }
584
585       DALI_TEST_CHECK((moveCount == indices.size()));
586     }
587   }
588   catch(...)
589   {
590     tet_result(TET_FAIL);
591   }
592
593   Stage::GetCurrent().Remove(view);
594 }
595
596 static void UtcDaliDepthLayoutScrollDirection()
597 {
598   ToolkitTestApplication application;
599
600   // Create the ItemView actor
601   TestItemFactory factory;
602   ItemView view = ItemView::New(factory);
603   Vector3 vec(480.0f, 800.0f, 0.0f);
604   DepthLayoutPtr navigationLayout = DepthLayout::New();
605
606   view.SetName("view actor");
607   view.AddLayout(*navigationLayout);
608   view.SetSize(vec);
609
610   Stage::GetCurrent().Add(view);
611   navigationLayout->SetOrientation(ControlOrientation::Left);
612   view.ActivateLayout(0, vec, 0.0f);
613
614   application.SendNotification();
615   application.Render(0);
616
617   ItemLayoutPtr layout = navigationLayout;
618
619   // render 10 frames
620   for(int i = 0; i < 10; ++i)
621   {
622     application.Render(16); // 60hz frames
623   }
624
625   navigationLayout->SetOrientation(ControlOrientation::Up);
626   view.ActivateLayout(0, vec, 0.0f);
627   application.SendNotification();
628   application.Render();
629
630   Degree deg = layout->GetScrollDirection();
631   DALI_TEST_CHECK(deg == 180.0f);
632
633   navigationLayout->SetOrientation(ControlOrientation::Down);
634   view.ActivateLayout(0, vec, 0.0f);
635   application.SendNotification();
636   application.Render();
637
638   deg = layout->GetScrollDirection();
639   DALI_TEST_CHECK((deg == 0.0f));
640
641   layout->SetOrientation(ControlOrientation::Left);
642   view.ActivateLayout(0, vec, 0.0f);
643   application.SendNotification();
644   application.Render();
645
646   deg = layout->GetScrollDirection();
647   DALI_TEST_CHECK(deg == 270.0f);
648
649   navigationLayout->SetOrientation(ControlOrientation::Right);
650   view.ActivateLayout(0, vec, 0.0f);
651   application.SendNotification();
652   application.Render();
653
654   deg = layout->GetScrollDirection();
655   DALI_TEST_CHECK(deg == 90.0f);
656
657   Stage::GetCurrent().Remove(view);
658 }