Adding new test harness
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / item-view / utc-Dali-GridLayout.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 <float.h>       // for FLT_MAX
21 #include <tet_api.h>
22
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit-test-suite-utils.h>
26
27 using namespace Dali;
28 using namespace Toolkit;
29
30 namespace
31 {
32 const unsigned int TOTAL_ITEM_NUMBER = 200;
33
34 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
35 {
36   float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
37
38   return Vector3(width, width, width);
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    * @param application class, stored as reference
54    */
55   TestItemFactory()
56   {
57   }
58
59 public: // From ItemFactory
60
61   /**
62    * Query the number of items available from the factory.
63    * The maximum available item has an ID of GetNumberOfItems() - 1.
64    */
65   virtual unsigned int GetNumberOfItems()
66   {
67     return TOTAL_ITEM_NUMBER;
68   }
69
70   /**
71    * Create an Actor to represent a visible item.
72    * @param itemId
73    * @return the created actor.
74    */
75   virtual Actor NewItem(unsigned int itemId)
76   {
77     // Create an test actor for this item
78     ImageActor actor = CreateSolidColorActor(Color::RED);
79     actor.SetSize(64.0f, 64.0f);
80     return actor;
81   }
82 };
83
84 extern "C" {
85   void (*tet_startup)() = Startup;
86   void (*tet_cleanup)() = Cleanup;
87 }
88
89 static void UtcDaliGridLayoutNew();
90 static void UtcDaliGridLayoutSetAndGetNumberOfColumns();
91 static void UtcDaliGridLayoutSetAndGetRowSpacing();
92 static void UtcDaliGridLayoutSetAndGetColumnSpacing();
93 static void UtcDaliGridLayoutSetAndGetTopMargin();
94 static void UtcDaliGridLayoutSetAndGetBottomMargin();
95 static void UtcDaliGridLayoutSetAndGetSideMargin();
96 static void UtcDaliGridLayoutSetAndGetZGap();
97 static void UtcDaliGridLayoutSetAndGetItemSizeFunction();
98 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor();
99 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed();
100 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration();
101 static void UtcDaliGridLayoutConstraintLeft();
102 static void UtcDaliGridLayoutConstraintRight();
103 static void UtcDaliGridLayoutConstraintUp();
104 static void UtcDaliGridLayoutConstraintDown();
105 static void UtcDaliGridLayoutScrollDirection();
106
107 enum {
108   POSITIVE_TC_IDX = 0x01,
109   NEGATIVE_TC_IDX,
110 };
111
112 // Add test functionality for all APIs in the class (Positive and Negative)
113 extern "C" {
114   struct tet_testlist tet_testlist[] = {
115     { UtcDaliGridLayoutNew, POSITIVE_TC_IDX },
116     { UtcDaliGridLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
117     { UtcDaliGridLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
118     { UtcDaliGridLayoutSetAndGetColumnSpacing, POSITIVE_TC_IDX },
119     { UtcDaliGridLayoutSetAndGetTopMargin, POSITIVE_TC_IDX },
120     { UtcDaliGridLayoutSetAndGetBottomMargin, POSITIVE_TC_IDX },
121     { UtcDaliGridLayoutSetAndGetSideMargin, POSITIVE_TC_IDX },
122     { UtcDaliGridLayoutSetAndGetZGap, POSITIVE_TC_IDX },
123     { UtcDaliGridLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
124     { UtcDaliGridLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
125     { UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
126     { UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
127     { UtcDaliGridLayoutConstraintLeft, POSITIVE_TC_IDX },
128     { UtcDaliGridLayoutConstraintRight, POSITIVE_TC_IDX },
129     { UtcDaliGridLayoutConstraintUp, POSITIVE_TC_IDX },
130     { UtcDaliGridLayoutConstraintDown, POSITIVE_TC_IDX },
131     { UtcDaliGridLayoutScrollDirection, POSITIVE_TC_IDX },
132     { NULL, 0 }
133   };
134 }
135
136 // Called only once before first test is run.
137 static void Startup()
138 {
139 }
140
141 // Called only once after last test is run
142 static void Cleanup()
143 {
144 }
145
146 static void UtcDaliGridLayoutNew()
147 {
148   ToolkitTestApplication application;
149
150   // Create a grid layout
151   GridLayoutPtr gridLayout = GridLayout::New();
152
153   DALI_TEST_CHECK(gridLayout);
154 }
155
156 static void UtcDaliGridLayoutSetAndGetNumberOfColumns()
157 {
158   ToolkitTestApplication application;
159
160   // Create a grid layout
161   GridLayoutPtr gridLayout = GridLayout::New();
162
163   // Set the number of columns
164   gridLayout->SetNumberOfColumns(6);
165
166   // Check whether we get the correct number of columns
167   DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
168 }
169
170 static void UtcDaliGridLayoutSetAndGetRowSpacing()
171 {
172   ToolkitTestApplication application;
173
174   // Create a grid layout
175   GridLayoutPtr gridLayout = GridLayout::New();
176
177   // Set the row spacing
178   gridLayout->SetRowSpacing(10.0f);
179
180   // Check whether we get the correct row spacing
181   DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
182 }
183
184 static void UtcDaliGridLayoutSetAndGetColumnSpacing()
185 {
186   ToolkitTestApplication application;
187
188   // Create a grid layout
189   GridLayoutPtr gridLayout = GridLayout::New();
190
191   // Set the column spacing
192   gridLayout->SetColumnSpacing(10.0f);
193
194   // Check whether we get the correct column spacing
195   DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
196 }
197
198 static void UtcDaliGridLayoutSetAndGetTopMargin()
199 {
200   ToolkitTestApplication application;
201
202   // Create a grid layout
203   GridLayoutPtr gridLayout = GridLayout::New();
204
205   // Set the top margin
206   gridLayout->SetTopMargin(30.0f);
207
208   // Check whether we get the correct top margin
209   DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
210 }
211
212 static void UtcDaliGridLayoutSetAndGetBottomMargin()
213 {
214   ToolkitTestApplication application;
215
216   // Create a grid layout
217   GridLayoutPtr gridLayout = GridLayout::New();
218
219   // Set the bottom margin
220   gridLayout->SetBottomMargin(30.0f);
221
222   // Check whether we get the correct bottom margin
223   DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
224 }
225
226 static void UtcDaliGridLayoutSetAndGetSideMargin()
227 {
228   ToolkitTestApplication application;
229
230   // Create a grid layout
231   GridLayoutPtr gridLayout = GridLayout::New();
232
233   // Set the side margin
234   gridLayout->SetSideMargin(10.0f);
235
236   // Check whether we get the correct side margin
237   DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
238 }
239
240 static void UtcDaliGridLayoutSetAndGetZGap()
241 {
242   ToolkitTestApplication application;
243
244   // Create a grid layout
245   GridLayoutPtr gridLayout = GridLayout::New();
246
247   // Set the gap of items in the Z axis in different columns
248   gridLayout->SetZGap(5.0f);
249
250   // Check whether we get the correct Z gap
251   DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
252 }
253
254 static void UtcDaliGridLayoutSetAndGetItemSizeFunction()
255 {
256   ToolkitTestApplication application;
257
258   // Create a grid layout
259   GridLayoutPtr gridLayout = GridLayout::New();
260
261   // Set the item size function
262   gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
263
264   // Check whether we get the correct item size function
265   DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
266 }
267
268 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor()
269 {
270   ToolkitTestApplication application;
271
272   // Create a grid layout
273   GridLayoutPtr gridLayout = GridLayout::New();
274
275   // Set the scroll speed factor
276   gridLayout->SetScrollSpeedFactor(0.05f);
277
278   // Check whether we get the correct scroll speed factor
279   DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
280 }
281
282 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed()
283 {
284   ToolkitTestApplication application;
285
286   // Create a grid layout
287   GridLayoutPtr gridLayout = GridLayout::New();
288
289   // Set the maximum swipe speed
290   gridLayout->SetMaximumSwipeSpeed(50.0f);
291
292   // Check whether we get the correct maximum swipe speed
293   DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
294 }
295
296 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration()
297 {
298   ToolkitTestApplication application;
299
300   // Create a grid layout
301   GridLayoutPtr gridLayout = GridLayout::New();
302
303   // Set the flick animaiton duration
304   gridLayout->SetItemFlickAnimationDuration(0.35f);
305
306   // Check whether we get the correct flick animaiton duration
307   DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
308 }
309
310 static void UtcDaliGridLayoutConstraintLeft()
311 {
312   ToolkitTestApplication application;
313
314   // Create the ItemView actor
315   TestItemFactory factory;
316   ItemView view = ItemView::New(factory);
317   Vector3 vec(480.0f, 800.0f, 0.0f);
318   GridLayoutPtr gridLayout = GridLayout::New();
319   gridLayout->SetNumberOfColumns(6);
320
321   view.SetName("view actor");
322   view.AddLayout(*gridLayout);
323   view.SetSize(vec);
324
325   Stage::GetCurrent().Add(view);
326   gridLayout->SetOrientation(ControlOrientation::Left);
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 UtcDaliGridLayoutConstraintRight()
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   GridLayoutPtr gridLayout = GridLayout::New();
369   gridLayout->SetNumberOfColumns(6);
370
371   view.SetName("view actor");
372   view.AddLayout(*gridLayout);
373   view.SetSize(vec);
374
375   Stage::GetCurrent().Add(view);
376   gridLayout->SetOrientation(ControlOrientation::Right);
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 UtcDaliGridLayoutConstraintUp()
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   GridLayoutPtr gridLayout = GridLayout::New();
419   gridLayout->SetNumberOfColumns(6);
420
421   view.SetName("view actor");
422   view.AddLayout(*gridLayout);
423   view.SetSize(vec);
424
425   Stage::GetCurrent().Add(view);
426   gridLayout->SetOrientation(ControlOrientation::Up);
427   view.ActivateLayout(0, vec, 0.0f);
428
429   application.SendNotification();
430   application.Render(0);
431
432   // render 10 frames
433   for(int i = 0; i < 10; ++i)
434   {
435     application.Render(16); // 60hz frames
436   }
437
438   // Confirm: we have actors in the view and they are positioned some distance from the origin.
439   int nonZeroCount = 0;
440   int elementsFound = 0;
441   for(unsigned int i = 0; i < 10; i++)
442   {
443     Actor testActor = view.GetItem(i);
444     if (testActor)
445     {
446       elementsFound++;
447       Vector3 pos = testActor.GetCurrentPosition();
448
449       if (pos.LengthSquared() > 0.0f)
450       {
451         nonZeroCount++;
452       }
453     }
454   }
455
456   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
457
458   ItemLayoutPtr layout = gridLayout;
459   layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
460   int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
461   DALI_TEST_CHECK(nextItem == 1);
462
463   Stage::GetCurrent().Remove(view);
464 }
465
466 static void UtcDaliGridLayoutConstraintDown()
467 {
468   ToolkitTestApplication application;
469
470   // Create the ItemView actor
471   TestItemFactory factory;
472   ItemView view = ItemView::New(factory);
473   Vector3 vec(480.0f, 800.0f, 0.0f);
474   GridLayoutPtr gridLayout = GridLayout::New();
475   gridLayout->SetNumberOfColumns(6);
476
477   view.SetName("view actor");
478   view.AddLayout(*gridLayout);
479   view.SetSize(vec);
480
481   Stage::GetCurrent().Add(view);
482   gridLayout->SetOrientation(ControlOrientation::Down);
483   view.ActivateLayout(0, vec, 0.0f);
484
485   application.SendNotification();
486   application.Render(0);
487
488   // render 10 frames
489   for(int i = 0; i < 10; ++i)
490   {
491     application.Render(16); // 60hz frames
492   }
493
494   // Confirm: we have actors in the view and they are positioned some distance from the origin.
495   int nonZeroCount = 0;
496   int elementsFound = 0;
497   for(unsigned int i = 0; i < 10; i++)
498   {
499     Actor testActor = view.GetItem(i);
500     if (testActor)
501     {
502       elementsFound++;
503       Vector3 pos = testActor.GetCurrentPosition();
504
505       if (pos.LengthSquared() > 0.0f)
506       {
507         nonZeroCount++;
508       }
509     }
510   }
511
512   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
513   Stage::GetCurrent().Remove(view);
514 }
515
516 static void UtcDaliGridLayoutScrollDirection()
517 {
518   ToolkitTestApplication application;
519
520   // Create the ItemView actor
521   TestItemFactory factory;
522   ItemView view = ItemView::New(factory);
523   Vector3 vec(480.0f, 800.0f, 0.0f);
524   GridLayoutPtr gridLayout = GridLayout::New();
525   gridLayout->SetNumberOfColumns(6);
526
527   view.SetName("view actor");
528   view.AddLayout(*gridLayout);
529   view.SetSize(vec);
530
531   Stage::GetCurrent().Add(view);
532   gridLayout->SetOrientation(ControlOrientation::Left);
533   view.ActivateLayout(0, vec, 0.0f);
534
535   application.SendNotification();
536   application.Render(0);
537
538   ItemLayoutPtr layout = gridLayout;
539
540   // render 10 frames
541   for(int i = 0; i < 10; ++i)
542   {
543     application.Render(16); // 60hz frames
544   }
545
546   gridLayout->SetOrientation(ControlOrientation::Up);
547   view.ActivateLayout(0, vec, 0.0f);
548   application.SendNotification();
549   application.Render();
550
551   Degree deg = layout->GetScrollDirection();
552   DALI_TEST_CHECK(deg == 0.0f);
553
554   gridLayout->SetOrientation(ControlOrientation::Down);
555   view.ActivateLayout(0, vec, 0.0f);
556   application.SendNotification();
557   application.Render();
558
559   deg = layout->GetScrollDirection();
560   DALI_TEST_CHECK((deg == 180.0f));
561
562   layout->SetOrientation(ControlOrientation::Left);
563   view.ActivateLayout(0, vec, 0.0f);
564   application.SendNotification();
565   application.Render();
566
567   deg = layout->GetScrollDirection();
568   DALI_TEST_CHECK(deg == 90.f);
569
570   gridLayout->SetOrientation(ControlOrientation::Right);
571   view.ActivateLayout(0, vec, 0.0f);
572   application.SendNotification();
573   application.Render();
574
575   deg = layout->GetScrollDirection();
576   DALI_TEST_CHECK(deg == 270.0f);
577
578   Stage::GetCurrent().Remove(view);
579 }