License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / 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 <dali-toolkit-test-suite-utils.h>
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.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
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
93 void depth_layout_startup(void)
94 {
95 }
96
97 void depth_layout_cleanup(void)
98 {
99 }
100
101 int UtcDaliDepthLayoutNew(void)
102 {
103   ToolkitTestApplication application;
104
105   // Create a depth layout
106   DepthLayoutPtr depthLayout = DepthLayout::New();
107
108   DALI_TEST_CHECK(depthLayout);
109   END_TEST;
110 }
111
112 int UtcDaliDepthLayoutSetAndGetNumberOfColumns(void)
113 {
114   ToolkitTestApplication application;
115
116   // Create a depth layout
117   DepthLayoutPtr depthLayout = DepthLayout::New();
118
119   // Set the number of columns
120   depthLayout->SetNumberOfColumns(5);
121
122   // Check whether we get the correct number of columns
123   DALI_TEST_CHECK(depthLayout->GetNumberOfColumns() == 5);
124   END_TEST;
125 }
126
127 int UtcDaliDepthLayoutSetAndGetNumberOfRows(void)
128 {
129   ToolkitTestApplication application;
130
131   // Create a depth layout
132   DepthLayoutPtr depthLayout = DepthLayout::New();
133
134   // Set the number of rows
135   depthLayout->SetNumberOfRows(15);
136
137   // Check whether we get the correct number of rows
138   DALI_TEST_CHECK(depthLayout->GetNumberOfRows() == 15);
139   END_TEST;
140 }
141
142 int UtcDaliDepthLayoutSetAndGetRowSpacing(void)
143 {
144   ToolkitTestApplication application;
145
146   // Create a depth layout
147   DepthLayoutPtr depthLayout = DepthLayout::New();
148
149   // Set the row spacing
150   depthLayout->SetRowSpacing(30.0f);
151
152   // Check whether we get the correct row spacing
153   DALI_TEST_EQUALS(depthLayout->GetRowSpacing(), 30.0f, TEST_LOCATION );
154   END_TEST;
155 }
156
157 int UtcDaliDepthLayoutSetAndGetTiltAngle(void)
158 {
159   ToolkitTestApplication application;
160
161   // Create a depth layout
162   DepthLayoutPtr depthLayout = DepthLayout::New();
163
164   // Set the tilt angle
165   depthLayout->SetTiltAngle(Degree(25.0f));
166
167   // Check whether we get the correct tilt angle
168   DALI_TEST_EQUALS(float(depthLayout->GetTiltAngle()), 25.0f, 0.001f, TEST_LOCATION );
169   END_TEST;
170 }
171
172 int UtcDaliDepthLayoutSetAndGetItemSizeFunction(void)
173 {
174   ToolkitTestApplication application;
175
176   // Create a depth layout
177   DepthLayoutPtr depthLayout = DepthLayout::New();
178
179   // Set the item size function
180   depthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunction);
181
182   // Check whether we get the correct item size function
183   DALI_TEST_CHECK(depthLayout->GetItemSizeFunction() == DepthLayoutItemSizeFunction);
184   END_TEST;
185 }
186
187 int UtcDaliDepthLayoutSetAndGetBottomMarginFunction(void)
188 {
189   ToolkitTestApplication application;
190
191   // Create a depth layout
192   DepthLayoutPtr depthLayout = DepthLayout::New();
193
194   // Set the bottom margin function
195   depthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunction);
196
197   // Check whether we get the correct bottom margin function
198   DALI_TEST_CHECK(depthLayout->GetBottomMarginFunction() == DepthLayoutBottomMarginFunction);
199   END_TEST;
200 }
201
202 int UtcDaliDepthLayoutSetAndGetItemTiltAngle(void)
203 {
204   ToolkitTestApplication application;
205
206   // Create a depth layout
207   DepthLayoutPtr depthLayout = DepthLayout::New();
208
209   // Set the item's tilt angle
210   depthLayout->SetItemTiltAngle(Degree(5.0f));
211
212   // Check whether we get the correct item's tilt angle
213   DALI_TEST_EQUALS(float(depthLayout->GetItemTiltAngle()), 5.0f, 0.001f, TEST_LOCATION );
214   END_TEST;
215 }
216
217 int UtcDaliDepthLayoutSetAndGetColumnPositionFunction(void)
218 {
219   ToolkitTestApplication application;
220
221   // Create a depth layout
222   DepthLayoutPtr depthLayout = DepthLayout::New();
223
224   // Set the column position function
225   depthLayout->SetColumnPositionFunction(DepthLayoutColumnPositionFunction);
226
227   // Check whether we get the correct column position function
228   DALI_TEST_CHECK(depthLayout->GetColumnPositionFunction() == DepthLayoutColumnPositionFunction);
229   END_TEST;
230 }
231
232 int UtcDaliDepthLayoutSetAndGetScrollSpeedFactor(void)
233 {
234   ToolkitTestApplication application;
235
236   // Create a depth layout
237   DepthLayoutPtr depthLayout = DepthLayout::New();
238
239   // Set the scroll speed factor
240   depthLayout->SetScrollSpeedFactor(0.05f);
241
242   // Check whether we get the correct scroll speed factor
243   DALI_TEST_EQUALS(depthLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
244   END_TEST;
245 }
246
247 int UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed(void)
248 {
249   ToolkitTestApplication application;
250
251   // Create a depth layout
252   DepthLayoutPtr depthLayout = DepthLayout::New();
253
254   // Set the maximum swipe speed
255   depthLayout->SetMaximumSwipeSpeed(50.0f);
256
257   // Check whether we get the correct maximum swipe speed
258   DALI_TEST_EQUALS(depthLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
259   END_TEST;
260 }
261
262 int UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration(void)
263 {
264   ToolkitTestApplication application;
265
266   // Create a depth layout
267   DepthLayoutPtr depthLayout = DepthLayout::New();
268
269   // Set the flick animaiton duration
270   depthLayout->SetItemFlickAnimationDuration(0.35f);
271
272   // Check whether we get the correct flick animaiton duration
273   DALI_TEST_EQUALS( depthLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
274   END_TEST;
275 }
276
277 int UtcDaliDepthLayoutConstraintLeft(void)
278 {
279   ToolkitTestApplication application;
280
281   // Create the ItemView actor
282   TestItemFactory factory;
283   ItemView view = ItemView::New(factory);
284   Vector3 vec(480.0f, 800.0f, 0.0f);
285   DepthLayoutPtr navigationLayout = DepthLayout::New();
286   navigationLayout->SetNumberOfColumns(6);
287
288   view.SetName("view actor");
289   view.AddLayout(*navigationLayout);
290   view.SetSize(vec);
291
292   Stage::GetCurrent().Add(view);
293   navigationLayout->SetOrientation(ControlOrientation::Left);
294   view.ActivateLayout(0, vec, 0.0f);
295
296   application.SendNotification();
297   application.Render(0);
298
299   // render 10 frames
300   for(int i = 0; i < 10; ++i)
301   {
302     application.Render(16); // 60hz frames
303   }
304
305   // Confirm: we have actors in the view and they are positioned some distance from the origin.
306   int nonZeroCount = 0;
307   int elementsFound = 0;
308   for(unsigned int i = 0; i < 10; i++)
309   {
310     Actor testActor = view.GetItem(i);
311     if (testActor)
312     {
313       elementsFound++;
314       Vector3 pos = testActor.GetCurrentPosition();
315
316       if (pos.LengthSquared() > 0.0f)
317       {
318         nonZeroCount++;
319       }
320     }
321   }
322
323   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
324   Stage::GetCurrent().Remove(view);
325   END_TEST;
326 }
327
328 int UtcDaliDepthLayoutConstraintRight(void)
329 {
330   ToolkitTestApplication application;
331
332   // Create the ItemView actor
333   TestItemFactory factory;
334   ItemView view = ItemView::New(factory);
335   Vector3 vec(480.0f, 800.0f, 0.0f);
336   DepthLayoutPtr navigationLayout = DepthLayout::New();
337   navigationLayout->SetNumberOfColumns(6);
338
339   view.SetName("view actor");
340   view.AddLayout(*navigationLayout);
341   view.SetSize(vec);
342
343   Stage::GetCurrent().Add(view);
344   navigationLayout->SetOrientation(ControlOrientation::Right);
345   view.ActivateLayout(0, vec, 0.0f);
346
347   application.SendNotification();
348   application.Render(0);
349
350   // render 10 frames
351   for(int i = 0; i < 10; ++i)
352   {
353     application.Render(16); // 60hz frames
354   }
355
356   // Confirm: we have actors in the view and they are positioned some distance from the origin.
357   int nonZeroCount = 0;
358   int elementsFound = 0;
359   for(unsigned int i = 0; i < 10; i++)
360   {
361     Actor testActor = view.GetItem(i);
362     if (testActor)
363     {
364       elementsFound++;
365       Vector3 pos = testActor.GetCurrentPosition();
366
367       if (pos.LengthSquared() > 0.0f)
368       {
369         nonZeroCount++;
370       }
371     }
372   }
373
374   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
375   Stage::GetCurrent().Remove(view);
376   END_TEST;
377 }
378
379 int UtcDaliDepthLayoutConstraintUp(void)
380 {
381   ToolkitTestApplication application;
382
383   // Create the ItemView actor
384   TestItemFactory factory;
385   ItemView view = ItemView::New(factory);
386   Vector3 vec(480.0f, 800.0f, 0.0f);
387   DepthLayoutPtr navigationLayout = DepthLayout::New();
388   navigationLayout->SetNumberOfColumns(6);
389
390   view.SetName("view actor");
391   view.AddLayout(*navigationLayout);
392   view.SetSize(vec);
393
394   Stage::GetCurrent().Add(view);
395   navigationLayout->SetOrientation(ControlOrientation::Up);
396   view.ActivateLayout(0, vec, 0.0f);
397
398   application.SendNotification();
399   application.Render(0);
400
401   // render 10 frames
402   for(int i = 0; i < 10; ++i)
403   {
404     application.Render(16); // 60hz frames
405   }
406
407   // Confirm: we have actors in the view and they are positioned some distance from the origin.
408   int nonZeroCount = 0;
409   int elementsFound = 0;
410   for(unsigned int i = 0; i < 10; i++)
411   {
412     Actor testActor = view.GetItem(i);
413     if (testActor)
414     {
415       elementsFound++;
416       Vector3 pos = testActor.GetCurrentPosition();
417
418       if (pos.LengthSquared() > 0.0f)
419       {
420         nonZeroCount++;
421       }
422     }
423   }
424
425   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
426   Stage::GetCurrent().Remove(view);
427   END_TEST;
428 }
429
430 int UtcDaliDepthLayoutConstraintDown(void)
431 {
432   ToolkitTestApplication application;
433
434   // Create the ItemView actor
435   TestItemFactory factory;
436   ItemView view = ItemView::New(factory);
437   Vector3 vec(480.0f, 800.0f, 0.0f);
438   DepthLayoutPtr navigationLayout = DepthLayout::New();
439   navigationLayout->SetNumberOfColumns(6);
440
441   view.SetName("view actor");
442   view.AddLayout(*navigationLayout);
443   view.SetSize(vec);
444
445   Stage::GetCurrent().Add(view);
446   navigationLayout->SetOrientation(ControlOrientation::Down);
447   view.ActivateLayout(0, vec, 0.0f);
448
449   application.SendNotification();
450   application.Render(0);
451
452   // render 10 frames
453   for(int i = 0; i < 10; ++i)
454   {
455     application.Render(16); // 60hz frames
456   }
457
458   // Confirm: we have actors in the view and they are positioned some distance from the origin.
459   int nonZeroCount = 0;
460   int elementsFound = 0;
461   for(unsigned int i = 0; i < 10; i++)
462   {
463     Actor testActor = view.GetItem(i);
464     if (testActor)
465     {
466       elementsFound++;
467       Vector3 pos = testActor.GetCurrentPosition();
468
469       if (pos.LengthSquared() > 0.0f)
470       {
471         nonZeroCount++;
472       }
473     }
474   }
475
476   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
477   Stage::GetCurrent().Remove(view);
478   END_TEST;
479 }
480
481 int UtcDaliDepthLayoutGetScrollToPosition(void)
482 {
483   ToolkitTestApplication application;
484
485   // Create the ItemView actor
486   TestItemFactory factory;
487   ItemView view = ItemView::New(factory);
488   Vector3 vec(480.0f, 800.0f, 0.0f);
489   DepthLayoutPtr layout = DepthLayout::New();
490
491   view.SetName("view actor");
492   view.AddLayout(*layout);
493   view.SetSize(vec);
494
495   Stage::GetCurrent().Add(view);
496   layout->SetOrientation(ControlOrientation::Up);
497   view.ActivateLayout(0, vec, 0.0f);
498
499   application.SendNotification();
500   application.Render(0);
501
502   // render 10 frames
503   for(int i = 0; i < 10; ++i)
504   {
505     application.Render(16); // 60hz frames
506   }
507
508   // Confirm: we have actors in the view.
509   std::vector<unsigned int> indices;
510   for(unsigned int i = 0; i < 10; i++)
511   {
512     Actor testActor = view.GetItem(i);
513     if (testActor)
514     {
515       indices.push_back(i);
516     }
517   }
518
519   try
520   {
521     if (!indices.empty())
522     {
523       const unsigned int firstTargetIndex = indices[indices.size()-1];
524       // scroll to last item
525       view.ScrollToItem(firstTargetIndex, 0.00f);
526       application.Render(16); // 60hz frames
527
528       std::size_t moveCount = 0;
529       for(std::size_t i = 0; i < indices.size(); i++)
530       {
531         float layoutPosBefore = view.GetCurrentLayoutPosition(i);
532         view.ScrollToItem(indices[i], 0.0f);
533
534         application.Render(16); // 60hz frame
535
536         float layoutPosAfter = view.GetCurrentLayoutPosition(i);
537
538         if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
539         {
540           ++moveCount;
541         }
542       }
543
544       DALI_TEST_CHECK((moveCount == indices.size()));
545     }
546   }
547   catch(...)
548   {
549     tet_result(TET_FAIL);
550   }
551
552   Stage::GetCurrent().Remove(view);
553   END_TEST;
554 }
555
556 int UtcDaliDepthLayoutScrollDirection(void)
557 {
558   ToolkitTestApplication application;
559
560   // Create the ItemView actor
561   TestItemFactory factory;
562   ItemView view = ItemView::New(factory);
563   Vector3 vec(480.0f, 800.0f, 0.0f);
564   DepthLayoutPtr navigationLayout = DepthLayout::New();
565
566   view.SetName("view actor");
567   view.AddLayout(*navigationLayout);
568   view.SetSize(vec);
569
570   Stage::GetCurrent().Add(view);
571   navigationLayout->SetOrientation(ControlOrientation::Left);
572   view.ActivateLayout(0, vec, 0.0f);
573
574   application.SendNotification();
575   application.Render(0);
576
577   ItemLayoutPtr layout = navigationLayout;
578
579   // render 10 frames
580   for(int i = 0; i < 10; ++i)
581   {
582     application.Render(16); // 60hz frames
583   }
584
585   navigationLayout->SetOrientation(ControlOrientation::Up);
586   view.ActivateLayout(0, vec, 0.0f);
587   application.SendNotification();
588   application.Render();
589
590   Degree deg = layout->GetScrollDirection();
591   DALI_TEST_CHECK(deg == 180.0f);
592
593   navigationLayout->SetOrientation(ControlOrientation::Down);
594   view.ActivateLayout(0, vec, 0.0f);
595   application.SendNotification();
596   application.Render();
597
598   deg = layout->GetScrollDirection();
599   DALI_TEST_CHECK((deg == 0.0f));
600
601   layout->SetOrientation(ControlOrientation::Left);
602   view.ActivateLayout(0, vec, 0.0f);
603   application.SendNotification();
604   application.Render();
605
606   deg = layout->GetScrollDirection();
607   DALI_TEST_CHECK(deg == 270.0f);
608
609   navigationLayout->SetOrientation(ControlOrientation::Right);
610   view.ActivateLayout(0, vec, 0.0f);
611   application.SendNotification();
612   application.Render();
613
614   deg = layout->GetScrollDirection();
615   DALI_TEST_CHECK(deg == 90.0f);
616
617   Stage::GetCurrent().Remove(view);
618   END_TEST;
619 }