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