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