(StyleManager) Add style monitor signal into StyleManager
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / 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 #include <stdlib.h>
19 #include <float.h>       // for FLT_MAX
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29 using namespace Toolkit;
30
31 void utc_dali_toolkit_grid_layout_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void utc_dali_toolkit_grid_layout_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41
42 namespace
43 {
44
45 const unsigned int TOTAL_ITEM_NUMBER = 200;
46
47 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
48 {
49   float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
50
51   return Vector3(width, width, width);
52 }
53
54
55 // Implementation of ItemFactory for providing actors to ItemView
56 class TestItemFactory : public ItemFactory
57 {
58 public:
59
60   /**
61    * Constructor
62    * @param application class, stored as reference
63    */
64   TestItemFactory()
65   {
66   }
67
68 public: // From ItemFactory
69
70   /**
71    * Query the number of items available from the factory.
72    * The maximum available item has an ID of GetNumberOfItems() - 1.
73    */
74   virtual unsigned int GetNumberOfItems()
75   {
76     return TOTAL_ITEM_NUMBER;
77   }
78
79   /**
80    * Create an Actor to represent a visible item.
81    * @param itemId
82    * @return the created actor.
83    */
84   virtual Actor NewItem(unsigned int itemId)
85   {
86     // Create an test actor for this item
87     ImageActor actor = CreateSolidColorActor(Color::RED);
88     actor.SetSize(64.0f, 64.0f);
89     return actor;
90   }
91 };
92
93 } // namespace
94
95
96
97 int UtcDaliGridLayoutNew(void)
98 {
99   ToolkitTestApplication application;
100
101   // Create a grid layout
102   GridLayoutPtr gridLayout = GridLayout::New();
103
104   DALI_TEST_CHECK(gridLayout);
105   END_TEST;
106 }
107
108 int UtcDaliGridLayoutSetAndGetNumberOfColumns(void)
109 {
110   ToolkitTestApplication application;
111
112   // Create a grid layout
113   GridLayoutPtr gridLayout = GridLayout::New();
114
115   // Set the number of columns
116   gridLayout->SetNumberOfColumns(6);
117
118   // Check whether we get the correct number of columns
119   DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
120   END_TEST;
121 }
122
123 int UtcDaliGridLayoutSetAndGetRowSpacing(void)
124 {
125   ToolkitTestApplication application;
126
127   // Create a grid layout
128   GridLayoutPtr gridLayout = GridLayout::New();
129
130   // Set the row spacing
131   gridLayout->SetRowSpacing(10.0f);
132
133   // Check whether we get the correct row spacing
134   DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
135   END_TEST;
136 }
137
138 int UtcDaliGridLayoutSetAndGetColumnSpacing(void)
139 {
140   ToolkitTestApplication application;
141
142   // Create a grid layout
143   GridLayoutPtr gridLayout = GridLayout::New();
144
145   // Set the column spacing
146   gridLayout->SetColumnSpacing(10.0f);
147
148   // Check whether we get the correct column spacing
149   DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
150   END_TEST;
151 }
152
153 int UtcDaliGridLayoutSetAndGetTopMargin(void)
154 {
155   ToolkitTestApplication application;
156
157   // Create a grid layout
158   GridLayoutPtr gridLayout = GridLayout::New();
159
160   // Set the top margin
161   gridLayout->SetTopMargin(30.0f);
162
163   // Check whether we get the correct top margin
164   DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
165   END_TEST;
166 }
167
168 int UtcDaliGridLayoutSetAndGetBottomMargin(void)
169 {
170   ToolkitTestApplication application;
171
172   // Create a grid layout
173   GridLayoutPtr gridLayout = GridLayout::New();
174
175   // Set the bottom margin
176   gridLayout->SetBottomMargin(30.0f);
177
178   // Check whether we get the correct bottom margin
179   DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
180   END_TEST;
181 }
182
183 int UtcDaliGridLayoutSetAndGetSideMargin(void)
184 {
185   ToolkitTestApplication application;
186
187   // Create a grid layout
188   GridLayoutPtr gridLayout = GridLayout::New();
189
190   // Set the side margin
191   gridLayout->SetSideMargin(10.0f);
192
193   // Check whether we get the correct side margin
194   DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
195   END_TEST;
196 }
197
198 int UtcDaliGridLayoutSetAndGetZGap(void)
199 {
200   ToolkitTestApplication application;
201
202   // Create a grid layout
203   GridLayoutPtr gridLayout = GridLayout::New();
204
205   // Set the gap of items in the Z axis in different columns
206   gridLayout->SetZGap(5.0f);
207
208   // Check whether we get the correct Z gap
209   DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
210   END_TEST;
211 }
212
213 int UtcDaliGridLayoutSetAndGetItemSizeFunction(void)
214 {
215   ToolkitTestApplication application;
216
217   // Create a grid layout
218   GridLayoutPtr gridLayout = GridLayout::New();
219
220   // Set the item size function
221   gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
222
223   // Check whether we get the correct item size function
224   DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
225   END_TEST;
226 }
227
228 int UtcDaliGridLayoutSetAndGetScrollSpeedFactor(void)
229 {
230   ToolkitTestApplication application;
231
232   // Create a grid layout
233   GridLayoutPtr gridLayout = GridLayout::New();
234
235   // Set the scroll speed factor
236   gridLayout->SetScrollSpeedFactor(0.05f);
237
238   // Check whether we get the correct scroll speed factor
239   DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
240   END_TEST;
241 }
242
243 int UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed(void)
244 {
245   ToolkitTestApplication application;
246
247   // Create a grid layout
248   GridLayoutPtr gridLayout = GridLayout::New();
249
250   // Set the maximum swipe speed
251   gridLayout->SetMaximumSwipeSpeed(50.0f);
252
253   // Check whether we get the correct maximum swipe speed
254   DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
255   END_TEST;
256 }
257
258 int UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration(void)
259 {
260   ToolkitTestApplication application;
261
262   // Create a grid layout
263   GridLayoutPtr gridLayout = GridLayout::New();
264
265   // Set the flick animaiton duration
266   gridLayout->SetItemFlickAnimationDuration(0.35f);
267
268   // Check whether we get the correct flick animaiton duration
269   DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
270   END_TEST;
271 }
272
273 int UtcDaliGridLayoutConstraintLeft(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   GridLayoutPtr gridLayout = GridLayout::New();
282   gridLayout->SetNumberOfColumns(6);
283
284   view.SetName("view actor");
285   view.AddLayout(*gridLayout);
286   view.SetSize(vec);
287
288   Stage::GetCurrent().Add(view);
289   gridLayout->SetOrientation(ControlOrientation::Left);
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 UtcDaliGridLayoutConstraintRight(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   GridLayoutPtr gridLayout = GridLayout::New();
333   gridLayout->SetNumberOfColumns(6);
334
335   view.SetName("view actor");
336   view.AddLayout(*gridLayout);
337   view.SetSize(vec);
338
339   Stage::GetCurrent().Add(view);
340   gridLayout->SetOrientation(ControlOrientation::Right);
341   view.ActivateLayout(0, vec, 0.0f);
342
343   application.SendNotification();
344   application.Render(0);
345
346   // render 10 frames
347   for(int i = 0; i < 10; ++i)
348   {
349     application.Render(16); // 60hz frames
350   }
351
352   // Confirm: we have actors in the view and they are positioned some distance from the origin.
353   int nonZeroCount = 0;
354   int elementsFound = 0;
355   for(unsigned int i = 0; i < 10; i++)
356   {
357     Actor testActor = view.GetItem(i);
358     if (testActor)
359     {
360       elementsFound++;
361       Vector3 pos = testActor.GetCurrentPosition();
362
363       if (pos.LengthSquared() > 0.0f)
364       {
365         nonZeroCount++;
366       }
367     }
368   }
369
370   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
371   Stage::GetCurrent().Remove(view);
372   END_TEST;
373 }
374
375 int UtcDaliGridLayoutConstraintUp(void)
376 {
377   ToolkitTestApplication application;
378
379   // Create the ItemView actor
380   TestItemFactory factory;
381   ItemView view = ItemView::New(factory);
382   Vector3 vec(480.0f, 800.0f, 0.0f);
383   GridLayoutPtr gridLayout = GridLayout::New();
384   gridLayout->SetNumberOfColumns(6);
385
386   view.SetName("view actor");
387   view.AddLayout(*gridLayout);
388   view.SetSize(vec);
389
390   Stage::GetCurrent().Add(view);
391   gridLayout->SetOrientation(ControlOrientation::Up);
392   view.ActivateLayout(0, vec, 0.0f);
393
394   application.SendNotification();
395   application.Render(0);
396
397   // render 10 frames
398   for(int i = 0; i < 10; ++i)
399   {
400     application.Render(16); // 60hz frames
401   }
402
403   // Confirm: we have actors in the view and they are positioned some distance from the origin.
404   int nonZeroCount = 0;
405   int elementsFound = 0;
406   for(unsigned int i = 0; i < 10; i++)
407   {
408     Actor testActor = view.GetItem(i);
409     if (testActor)
410     {
411       elementsFound++;
412       Vector3 pos = testActor.GetCurrentPosition();
413
414       if (pos.LengthSquared() > 0.0f)
415       {
416         nonZeroCount++;
417       }
418     }
419   }
420
421   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
422
423   ItemLayoutPtr layout = gridLayout;
424   layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
425   int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
426   DALI_TEST_CHECK(nextItem == 1);
427
428   Stage::GetCurrent().Remove(view);
429   END_TEST;
430 }
431
432 int UtcDaliGridLayoutConstraintDown(void)
433 {
434   ToolkitTestApplication application;
435
436   // Create the ItemView actor
437   TestItemFactory factory;
438   ItemView view = ItemView::New(factory);
439   Vector3 vec(480.0f, 800.0f, 0.0f);
440   GridLayoutPtr gridLayout = GridLayout::New();
441   gridLayout->SetNumberOfColumns(6);
442
443   view.SetName("view actor");
444   view.AddLayout(*gridLayout);
445   view.SetSize(vec);
446
447   Stage::GetCurrent().Add(view);
448   gridLayout->SetOrientation(ControlOrientation::Down);
449   view.ActivateLayout(0, vec, 0.0f);
450
451   application.SendNotification();
452   application.Render(0);
453
454   // render 10 frames
455   for(int i = 0; i < 10; ++i)
456   {
457     application.Render(16); // 60hz frames
458   }
459
460   // Confirm: we have actors in the view and they are positioned some distance from the origin.
461   int nonZeroCount = 0;
462   int elementsFound = 0;
463   for(unsigned int i = 0; i < 10; i++)
464   {
465     Actor testActor = view.GetItem(i);
466     if (testActor)
467     {
468       elementsFound++;
469       Vector3 pos = testActor.GetCurrentPosition();
470
471       if (pos.LengthSquared() > 0.0f)
472       {
473         nonZeroCount++;
474       }
475     }
476   }
477
478   DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
479   Stage::GetCurrent().Remove(view);
480   END_TEST;
481 }
482
483 int UtcDaliGridLayoutScrollDirection(void)
484 {
485   ToolkitTestApplication application;
486
487   // Create the ItemView actor
488   TestItemFactory factory;
489   ItemView view = ItemView::New(factory);
490   Vector3 vec(480.0f, 800.0f, 0.0f);
491   GridLayoutPtr gridLayout = GridLayout::New();
492   gridLayout->SetNumberOfColumns(6);
493
494   view.SetName("view actor");
495   view.AddLayout(*gridLayout);
496   view.SetSize(vec);
497
498   Stage::GetCurrent().Add(view);
499   gridLayout->SetOrientation(ControlOrientation::Left);
500   view.ActivateLayout(0, vec, 0.0f);
501
502   application.SendNotification();
503   application.Render(0);
504
505   ItemLayoutPtr layout = gridLayout;
506
507   // render 10 frames
508   for(int i = 0; i < 10; ++i)
509   {
510     application.Render(16); // 60hz frames
511   }
512
513   gridLayout->SetOrientation(ControlOrientation::Up);
514   view.ActivateLayout(0, vec, 0.0f);
515   application.SendNotification();
516   application.Render();
517
518   Degree deg = layout->GetScrollDirection();
519   DALI_TEST_CHECK(deg == 0.0f);
520
521   gridLayout->SetOrientation(ControlOrientation::Down);
522   view.ActivateLayout(0, vec, 0.0f);
523   application.SendNotification();
524   application.Render();
525
526   deg = layout->GetScrollDirection();
527   DALI_TEST_CHECK((deg == 180.0f));
528
529   layout->SetOrientation(ControlOrientation::Left);
530   view.ActivateLayout(0, vec, 0.0f);
531   application.SendNotification();
532   application.Render();
533
534   deg = layout->GetScrollDirection();
535   DALI_TEST_CHECK(deg == 90.f);
536
537   gridLayout->SetOrientation(ControlOrientation::Right);
538   view.ActivateLayout(0, vec, 0.0f);
539   application.SendNotification();
540   application.Render();
541
542   deg = layout->GetScrollDirection();
543   DALI_TEST_CHECK(deg == 270.0f);
544
545   Stage::GetCurrent().Remove(view);
546   END_TEST;
547 }