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