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