Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-RollLayout.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 <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27 const unsigned int TOTAL_ITEM_NUMBER = 200;
28
29 Vector3 RollLayoutItemSizeFunction(float layoutWidth, float layoutHeight, float rowSpacing)
30 {
31   float height = (layoutHeight - rowSpacing) * 0.5f;
32   return Vector3(layoutWidth, height, height);
33 }
34
35 } // namespace
36
37
38 // Implementation of ItemFactory for providing actors to ItemView
39 class TestItemFactory : public ItemFactory
40 {
41 public:
42
43   /**
44    * Constructor
45    */
46   TestItemFactory()
47   {
48   }
49
50 public: // From ItemFactory
51
52   /**
53    * Query the number of items available from the factory.
54    * The maximum available item has an ID of GetNumberOfItems() - 1.
55    */
56   virtual unsigned int GetNumberOfItems()
57   {
58     return TOTAL_ITEM_NUMBER;
59   }
60
61   /**
62    * Create an Actor to represent a visible item.
63    * @param itemId
64    * @return the created actor.
65    */
66   virtual Actor NewItem(unsigned int itemId)
67   {
68     // Create an test actor for this item
69     ImageActor actor = CreateSolidColorActor(Color::RED);
70     actor.SetSize(64.0f, 64.0f);
71     return actor;
72   }
73 };
74
75 void dali_roll_layout_startup(void)
76 {
77   test_return_value = TET_UNDEF;
78 }
79
80 void dali_roll_layout_cleanup(void)
81 {
82   test_return_value = TET_PASS;
83 }
84
85
86
87 int UtcDaliRollLayoutNew(void)
88 {
89   ToolkitTestApplication application;
90
91   // Create a roll layout
92   RollLayoutPtr rollLayout = RollLayout::New();
93
94   DALI_TEST_CHECK(rollLayout);
95   END_TEST;
96 }
97
98 int UtcDaliRollLayoutSetAndGetRowSpacing(void)
99 {
100   ToolkitTestApplication application;
101
102   // Create a roll layout
103   RollLayoutPtr rollLayout = RollLayout::New();
104
105   // Set the row spacing
106   rollLayout->SetRowSpacing(10.0f);
107
108   // Check whether we get the correct row spacing
109   DALI_TEST_EQUALS(rollLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
110   END_TEST;
111 }
112
113 int UtcDaliRollLayoutSetAndGetItemSizeFunction(void)
114 {
115   ToolkitTestApplication application;
116
117   // Create a roll layout
118   RollLayoutPtr rollLayout = RollLayout::New();
119
120   // Set the item size function
121   rollLayout->SetItemSizeFunction(RollLayoutItemSizeFunction);
122
123   // Check whether we get the correct item size function
124   DALI_TEST_CHECK(rollLayout->GetItemSizeFunction() == RollLayoutItemSizeFunction);
125   END_TEST;
126 }
127
128 int UtcDaliRollLayoutSetAndGetScrollSpeedFactor(void)
129 {
130   ToolkitTestApplication application;
131
132   // Create a roll layout
133   RollLayoutPtr rollLayout = RollLayout::New();
134
135   // Set the scroll speed factor
136   rollLayout->SetScrollSpeedFactor(0.05f);
137
138   // Check whether we get the correct scroll speed factor
139   DALI_TEST_EQUALS(rollLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
140   END_TEST;
141 }
142
143 int UtcDaliRollLayoutSetAndGetMaximumSwipeSpeed(void)
144 {
145   ToolkitTestApplication application;
146
147   // Create a roll layout
148   RollLayoutPtr rollLayout = RollLayout::New();
149
150   // Set the maximum swipe speed
151   rollLayout->SetMaximumSwipeSpeed(50.0f);
152
153   // Check whether we get the correct maximum swipe speed
154   DALI_TEST_EQUALS(rollLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
155   END_TEST;
156 }
157
158 int UtcDaliRollLayoutSetAndGetItemFlickAnimationDuration(void)
159 {
160   ToolkitTestApplication application;
161
162   // Create a roll layout
163   RollLayoutPtr rollLayout = RollLayout::New();
164
165   // Set the flick animaiton duration
166   rollLayout->SetItemFlickAnimationDuration(0.35f);
167
168   // Check whether we get the correct flick animaiton duration
169   DALI_TEST_EQUALS( rollLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
170   END_TEST;
171 }
172
173 int UtcDaliRollLayoutConstraintLeft(void)
174 {
175   ToolkitTestApplication application;
176
177   // Create the ItemView actor
178   TestItemFactory factory;
179   ItemView view = ItemView::New(factory);
180   Vector3 vec(480.0f, 800.0f, 0.0f);
181   RollLayoutPtr rollLayout = RollLayout::New();
182
183   view.SetName("view actor");
184   view.AddLayout(*rollLayout);
185   view.SetSize(vec);
186
187   Stage::GetCurrent().Add(view);
188   rollLayout->SetOrientation(ControlOrientation::Left);
189   view.ActivateLayout(0, vec, 0.0f);
190
191   application.SendNotification();
192   application.Render(0);
193
194   // render 10 frames
195   for(int i = 0; i < 10; ++i)
196   {
197     application.Render(16); // 60hz frames
198   }
199
200   // Confirm: we have actors in the view and they are positioned some distance from the origin.
201   int nonZeroCount = 0;
202   int elementsFound = 0;
203   for(unsigned int i = 0; i < 10; i++)
204   {
205     Actor testActor = view.GetItem(i);
206     if (testActor)
207     {
208       elementsFound++;
209       Vector3 pos = testActor.GetCurrentPosition();
210
211       if (pos.LengthSquared() > 0.0f)
212       {
213         nonZeroCount++;
214       }
215     }
216   }
217
218   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
219   Stage::GetCurrent().Remove(view);
220   END_TEST;
221 }
222
223 int UtcDaliRollLayoutConstraintRight(void)
224 {
225   ToolkitTestApplication application;
226
227   // Create the ItemView actor
228   TestItemFactory factory;
229   ItemView view = ItemView::New(factory);
230   Vector3 vec(480.0f, 800.0f, 0.0f);
231   RollLayoutPtr rollLayout = RollLayout::New();
232
233   view.SetName("view actor");
234   view.AddLayout(*rollLayout);
235   view.SetSize(vec);
236
237   Stage::GetCurrent().Add(view);
238   rollLayout->SetOrientation(ControlOrientation::Right);
239   view.ActivateLayout(0, vec, 0.0f);
240
241   application.SendNotification();
242   application.Render(0);
243
244   // render 10 frames
245   for(int i = 0; i < 10; ++i)
246   {
247     application.Render(16); // 60hz frames
248   }
249
250   // Confirm: we have actors in the view and they are positioned some distance from the origin.
251   int nonZeroCount = 0;
252   int elementsFound = 0;
253   for(unsigned int i = 0; i < 10; i++)
254   {
255     Actor testActor = view.GetItem(i);
256     if (testActor)
257     {
258       elementsFound++;
259       Vector3 pos = testActor.GetCurrentPosition();
260
261       if (pos.LengthSquared() > 0.0f)
262       {
263         nonZeroCount++;
264       }
265     }
266   }
267
268   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
269   Stage::GetCurrent().Remove(view);
270   END_TEST;
271 }
272
273 int UtcDaliRollLayoutConstraintUp(void)
274 {
275   ToolkitTestApplication application;
276
277   // Create the ItemView actor
278   TestItemFactory factory;
279   ItemView view = ItemView::New(factory);
280   Vector3 vec(480.0f, 800.0f, 0.0f);
281   RollLayoutPtr rollLayout = RollLayout::New();
282
283   view.SetName("view actor");
284   view.AddLayout(*rollLayout);
285   view.SetSize(vec);
286
287   Stage::GetCurrent().Add(view);
288   rollLayout->SetOrientation(ControlOrientation::Up);
289   view.ActivateLayout(0, vec, 0.0f);
290
291   application.SendNotification();
292   application.Render(0);
293
294   // render 10 frames
295   for(int i = 0; i < 10; ++i)
296   {
297     application.Render(16); // 60hz frames
298   }
299
300   // Confirm: we have actors in the view and they are positioned some distance from the origin.
301   int nonZeroCount = 0;
302   int elementsFound = 0;
303   for(unsigned int i = 0; i < 10; i++)
304   {
305     Actor testActor = view.GetItem(i);
306     if (testActor)
307     {
308       elementsFound++;
309       Vector3 pos = testActor.GetCurrentPosition();
310
311       if (pos.LengthSquared() > 0.0f)
312       {
313         nonZeroCount++;
314       }
315     }
316   }
317
318   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
319   Stage::GetCurrent().Remove(view);
320   END_TEST;
321 }
322
323 int UtcDaliRollLayoutConstraintDown(void)
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   RollLayoutPtr rollLayout = RollLayout::New();
332
333   view.SetName("view actor");
334   view.AddLayout(*rollLayout);
335   view.SetSize(vec);
336
337   Stage::GetCurrent().Add(view);
338   rollLayout->SetOrientation(ControlOrientation::Down);
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   END_TEST;
371 }
372
373 int UtcDaliRollLayoutScrollDirection(void)
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   RollLayoutPtr rollLayout = RollLayout::New();
382
383   view.SetName("view actor");
384   view.AddLayout(*rollLayout);
385   view.SetSize(vec);
386
387   Stage::GetCurrent().Add(view);
388   rollLayout->SetOrientation(ControlOrientation::Left);
389   view.ActivateLayout(0, vec, 0.0f);
390
391   application.SendNotification();
392   application.Render(0);
393
394   ItemLayoutPtr layout = rollLayout;
395
396   // render 10 frames
397   for(int i = 0; i < 10; ++i)
398   {
399     application.Render(16); // 60hz frames
400   }
401
402   rollLayout->SetOrientation(ControlOrientation::Up);
403   view.ActivateLayout(0, vec, 0.0f);
404   application.SendNotification();
405   application.Render();
406
407   Degree deg = layout->GetScrollDirection();
408   DALI_TEST_CHECK(deg == 0.0f);
409
410   rollLayout->SetOrientation(ControlOrientation::Down);
411   view.ActivateLayout(0, vec, 0.0f);
412   application.SendNotification();
413   application.Render();
414
415   deg = layout->GetScrollDirection();
416   DALI_TEST_CHECK((deg == 180.0f));
417
418   layout->SetOrientation(ControlOrientation::Left);
419   view.ActivateLayout(0, vec, 0.0f);
420   application.SendNotification();
421   application.Render();
422
423   deg = layout->GetScrollDirection();
424   DALI_TEST_CHECK(deg == 90.f);
425
426   rollLayout->SetOrientation(ControlOrientation::Right);
427   view.ActivateLayout(0, vec, 0.0f);
428   application.SendNotification();
429   application.Render();
430
431   deg = layout->GetScrollDirection();
432   DALI_TEST_CHECK(deg == 270.0f);
433
434   Stage::GetCurrent().Remove(view);
435   END_TEST;
436 }