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