Add Wayland support.
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / item-view / utc-Dali-ItemView.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 <float.h>       // for FLT_MAX
22 #include <tet_api.h>
23
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26
27 #include <dali-toolkit-test-suite-utils.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 namespace
33 {
34 const unsigned int TOTAL_ITEM_NUMBER = 100;
35 const char* TEST_IMAGE_FILE_NAME = DALI_IMAGE_DIR "gallery_image_01.jpg";
36
37 static bool gObjectCreatedCallBackCalled;
38
39 static void TestCallback(BaseHandle handle)
40 {
41   gObjectCreatedCallBackCalled = true;
42 }
43
44 } // namespace
45
46 static void Startup();
47 static void Cleanup();
48
49 extern "C" {
50   void (*tet_startup)() = Startup;
51   void (*tet_cleanup)() = Cleanup;
52 }
53
54 static void UtcDaliItemViewNew();
55 static void UtcDaliItemViewDownCast();
56 static void UtcDaliItemViewAddAndGetLayout();
57 static void UtcDaliItemViewAddAndRemoveLayout();
58 static void UtcDaliItemViewActivateLayoutAndGetActiveLayout();
59 static void UtcDaliItemViewDeactivateCurrentLayout();
60 static void UtcDaliItemViewGetItemAndGetItemId();
61 static void UtcDaliItemViewRemoveItem();
62 static void UtcDaliItemViewGetCurrentLayoutPosition();
63 static void UtcDaliItemViewSetAndGetMinimumSwipeSpeed();
64 static void UtcDaliItemViewSetAndGetMinimumSwipeDistance();
65 static void UtcDaliItemViewSetAndGetAnchoring();
66 static void UtcDaliItemViewSetAndGetAnchoringDuration();
67 static void UtcDaliItemViewSetAndGetRefreshInterval();
68 static void UtcDaliItemViewScrollToItem();
69 static void UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep();
70
71 enum {
72   POSITIVE_TC_IDX = 0x01,
73   NEGATIVE_TC_IDX,
74 };
75
76 // Add test functionality for all APIs in the class (Positive and Negative)
77 extern "C" {
78   struct tet_testlist tet_testlist[] = {
79     { UtcDaliItemViewNew, POSITIVE_TC_IDX },
80     { UtcDaliItemViewDownCast, POSITIVE_TC_IDX },
81     { UtcDaliItemViewAddAndGetLayout, POSITIVE_TC_IDX },
82     { UtcDaliItemViewAddAndRemoveLayout, POSITIVE_TC_IDX },
83     { UtcDaliItemViewActivateLayoutAndGetActiveLayout, POSITIVE_TC_IDX },
84     { UtcDaliItemViewDeactivateCurrentLayout, POSITIVE_TC_IDX },
85     { UtcDaliItemViewGetItemAndGetItemId, POSITIVE_TC_IDX },
86     { UtcDaliItemViewRemoveItem, POSITIVE_TC_IDX },
87     { UtcDaliItemViewGetCurrentLayoutPosition, POSITIVE_TC_IDX },
88     { UtcDaliItemViewSetAndGetMinimumSwipeSpeed, POSITIVE_TC_IDX },
89     { UtcDaliItemViewSetAndGetMinimumSwipeDistance, POSITIVE_TC_IDX },
90     { UtcDaliItemViewSetAndGetAnchoring, POSITIVE_TC_IDX },
91     { UtcDaliItemViewSetAndGetAnchoringDuration, POSITIVE_TC_IDX },
92     { UtcDaliItemViewSetAndGetRefreshInterval, POSITIVE_TC_IDX },
93     { UtcDaliItemViewScrollToItem, POSITIVE_TC_IDX },
94     { UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep, POSITIVE_TC_IDX },
95     { NULL, 0 }
96   };
97 }
98
99 // Called only once before first test is run.
100 static void Startup()
101 {
102 }
103
104 // Called only once after last test is run
105 static void Cleanup()
106 {
107 }
108
109 // Implementation of ItemFactory for providing actors to ItemView
110 class TestItemFactory : public ItemFactory
111 {
112 public:
113
114   /**
115    * Constructor
116    * @param application class, stored as reference
117    */
118   TestItemFactory()
119   {
120   }
121
122 public: // From ItemFactory
123
124   /**
125    * Query the number of items available from the factory.
126    * The maximum available item has an ID of GetNumberOfItems() - 1.
127    */
128   virtual unsigned int GetNumberOfItems()
129   {
130     return TOTAL_ITEM_NUMBER;
131   }
132
133   /**
134    * Create an Actor to represent a visible item.
135    * @param itemId
136    * @return the created actor.
137    */
138   virtual Actor NewItem(unsigned int itemId)
139   {
140     // Create an image actor for this item
141     Image image = Image::New( TEST_IMAGE_FILE_NAME );
142     Actor actor = ImageActor::New(image);
143
144     return actor;
145   }
146 };
147
148 static void UtcDaliItemViewNew()
149 {
150   ToolkitTestApplication application;
151
152   // Create the ItemView actor
153   TestItemFactory factory;
154   ItemView view = ItemView::New(factory);
155
156   DALI_TEST_CHECK(view);
157
158   //Additional check to ensure object is created by checking if it's registered
159   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
160   DALI_TEST_CHECK( registry );
161
162   gObjectCreatedCallBackCalled = false;
163   registry.ObjectCreatedSignal().Connect(&TestCallback);
164   {
165     TestItemFactory factory;
166     ItemView view = ItemView::New(factory);
167   }
168   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
169 }
170
171 static void UtcDaliItemViewDownCast()
172 {
173   ToolkitTestApplication application;
174
175   // Create the ItemView actor
176   TestItemFactory factory;
177   const ItemView itemViewConst = ItemView::New(factory);
178   ItemView itemView(itemViewConst);
179
180   BaseHandle handle(itemView);
181
182   ItemView newItemView = ItemView::DownCast( handle );
183   DALI_TEST_CHECK( itemView );
184   DALI_TEST_CHECK( newItemView == itemView );
185 }
186
187 static void UtcDaliItemViewAddAndGetLayout()
188 {
189   ToolkitTestApplication application;
190
191   // Create the ItemView actor
192   TestItemFactory factory;
193   ItemView view = ItemView::New(factory);
194
195   // Create a grid layout and add it to ItemView
196   GridLayoutPtr gridLayout = GridLayout::New();
197   view.AddLayout(*gridLayout);
198
199   // As we have added one layout, check the number of layout is now 1
200   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
201
202   // Create a depth layout and add it to ItemView
203   DepthLayoutPtr depthLayout = DepthLayout::New();
204   view.AddLayout(*depthLayout);
205
206   // As we have added another layout, check the number of layout is now 2
207   DALI_TEST_CHECK(view.GetLayoutCount() == 2);
208
209   // Create a spiral layout and add it to ItemView
210   SpiralLayoutPtr spiralLayout = SpiralLayout::New();
211   view.AddLayout(*spiralLayout);
212
213   // As we have added another layout, check the number of layout is now 3
214   DALI_TEST_CHECK(view.GetLayoutCount() == 3);
215
216   // Check we are getting the correct layout from ItemView
217   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
218   DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
219   DALI_TEST_CHECK(view.GetLayout(2) == spiralLayout);
220 }
221
222 static void UtcDaliItemViewAddAndRemoveLayout()
223 {
224   ToolkitTestApplication application;
225
226   // Create the ItemView actor
227   TestItemFactory factory;
228   ItemView view = ItemView::New(factory);
229
230   // Create a grid layout and add it to ItemView
231   GridLayoutPtr gridLayout = GridLayout::New();
232   view.AddLayout(*gridLayout);
233
234   // As we have added one layout, check the number of layout is now 1
235   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
236
237   // Create a depth layout and add it to ItemView
238   DepthLayoutPtr depthLayout = DepthLayout::New();
239   view.AddLayout(*depthLayout);
240
241   // As we have added another layout, check the number of layout is now 2
242   DALI_TEST_CHECK(view.GetLayoutCount() == 2);
243
244   // Check we are getting the correct layout from ItemView
245   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
246   DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
247
248   // Remove the grid layout
249   view.RemoveLayout(0);
250
251   // As we have removed the grid layout, check the number of layout is now 1
252   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
253
254   // Check we are getting the correct layout from ItemView
255   DALI_TEST_CHECK(view.GetLayout(0) == depthLayout);
256
257   // Remove the depth layout
258   view.RemoveLayout(0);
259
260   // As we also removed the depth layout, check the number of layout is now 0
261   DALI_TEST_CHECK(view.GetLayoutCount() == 0);
262 }
263
264 static void UtcDaliItemViewActivateLayoutAndGetActiveLayout()
265 {
266   ToolkitTestApplication application;
267
268   // Create the ItemView actor
269   TestItemFactory factory;
270   ItemView view = ItemView::New(factory);
271
272   // Create a grid layout and add it to ItemView
273   GridLayoutPtr gridLayout = GridLayout::New();
274   view.AddLayout(*gridLayout);
275
276   // Create a depth layout and add it to ItemView
277   DepthLayoutPtr depthLayout = DepthLayout::New();
278   view.AddLayout(*depthLayout);
279
280   // Create a spiral layout and add it to ItemView
281   SpiralLayoutPtr spiralLayout = SpiralLayout::New();
282   view.AddLayout(*spiralLayout);
283
284   // As we have added three layouts, check the number of layout is now 3
285   DALI_TEST_CHECK(view.GetLayoutCount() == 3);
286
287   // Check there is no active layout at the moment
288   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
289
290   // Activate the depth layout
291   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
292   view.ActivateLayout(1, stageSize, 0.5f);
293
294   // Check the current active layout is the depth layout
295   DALI_TEST_CHECK(view.GetActiveLayout() == depthLayout);
296
297   // Activate the grid layout
298   view.ActivateLayout(0, stageSize, 0.5f);
299
300   // Check the current active layout is the grid layout
301   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
302
303   // Activate the spiral layout
304   view.ActivateLayout(2, stageSize, 0.5f);
305
306   // Check the current active layout is the spiral layout
307   DALI_TEST_CHECK(view.GetActiveLayout() == spiralLayout);
308 }
309
310 static void UtcDaliItemViewDeactivateCurrentLayout()
311 {
312   ToolkitTestApplication application;
313
314   // Create the ItemView actor
315   TestItemFactory factory;
316   ItemView view = ItemView::New(factory);
317
318   // Create a grid layout and add it to ItemView
319   GridLayoutPtr gridLayout = GridLayout::New();
320   view.AddLayout(*gridLayout);
321
322   // Check there is no active layout at the moment
323   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
324
325   // Activate the grid layout
326   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
327   view.ActivateLayout(0, stageSize, 0.5f);
328
329   // Check the current active layout is the grid layout
330   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
331
332   // Deactivate the current layout
333   view.DeactivateCurrentLayout();
334
335   // Check there is no active layout at the moment
336   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
337 }
338
339 static void UtcDaliItemViewGetItemAndGetItemId()
340 {
341   ToolkitTestApplication application;
342
343   // Create the ItemView actor
344   TestItemFactory factory;
345   ItemView view = ItemView::New(factory);
346
347   // Create a grid layout and add it to ItemView
348   GridLayoutPtr gridLayout = GridLayout::New();
349   view.AddLayout(*gridLayout);
350
351   // Activate the grid layout so that the items will be created and added to ItemView
352   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
353   view.ActivateLayout(0, stageSize, 0.5f);
354
355   // Get the item given the item ID
356   Actor itemActor = view.GetItem(2);
357
358   // Check we are getting the correct Item ID given the specified actor
359   DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
360 }
361
362 static void UtcDaliItemViewRemoveItem()
363 {
364   ToolkitTestApplication application;
365
366   // Create the ItemView actor
367   TestItemFactory factory;
368   ItemView view = ItemView::New(factory);
369
370   // Create a grid layout and add it to ItemView
371   GridLayoutPtr gridLayout = GridLayout::New();
372   view.AddLayout(*gridLayout);
373
374   // Activate the grid layout so that the items will be created and added to ItemView
375   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
376   view.ActivateLayout(0, stageSize, 0.5f);
377
378   // Get the item given the item ID 2 and 3
379   Actor oldItemActorID2 = view.GetItem(2);
380   Actor oldItemActorID3 = view.GetItem(3);
381
382   // Remove the item with ID 2
383   view.RemoveItem(2, 0.0f);
384
385   // Get the new item given the item ID 2
386   Actor newItemActorID2 = view.GetItem(2);
387
388   // Check the original item with item ID 2 was deleted and now item ID 2 represents the original item with ID 3
389   DALI_TEST_CHECK(view.GetItemId(newItemActorID2) == 2);
390   DALI_TEST_CHECK(oldItemActorID2 != newItemActorID2);
391   DALI_TEST_CHECK(newItemActorID2 = oldItemActorID3);
392 }
393
394 static void UtcDaliItemViewGetCurrentLayoutPosition()
395 {
396   ToolkitTestApplication application;
397
398   // Create the ItemView actor
399   TestItemFactory factory;
400   ItemView view = ItemView::New(factory);
401
402   // Create a grid layout and add it to ItemView
403   GridLayoutPtr gridLayout = GridLayout::New();
404   view.AddLayout(*gridLayout);
405
406   // Activate the grid layout so that the items will be created and added to ItemView
407   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
408   view.ActivateLayout(0, stageSize, 0.0f);
409
410   // Check the current layout position for the 10th items is 9.0f
411   DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
412 }
413
414 static void UtcDaliItemViewSetAndGetMinimumSwipeSpeed()
415 {
416   ToolkitTestApplication application;
417
418   // Create the ItemView actor
419   TestItemFactory factory;
420   ItemView view = ItemView::New(factory);
421
422   // Set the minimum swipe speed to be 1.5f
423   view.SetMinimumSwipeSpeed(1.5f);
424
425   // Check the minimum swipe speed is 1.5f
426   DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
427 }
428
429 static void UtcDaliItemViewSetAndGetMinimumSwipeDistance()
430 {
431   ToolkitTestApplication application;
432
433   // Create the ItemView actor
434   TestItemFactory factory;
435   ItemView view = ItemView::New(factory);
436
437   // Set the minimum swipe distance to be 2.5f
438   view.SetMinimumSwipeDistance(2.5f);
439
440   // Check the minimum swipe distance is 2.5f
441   DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
442 }
443
444 static void UtcDaliItemViewSetAndGetAnchoring()
445 {
446   ToolkitTestApplication application;
447
448   // Create the ItemView actor
449   TestItemFactory factory;
450   ItemView view = ItemView::New(factory);
451
452   // Disable the anchor animation
453   view.SetAnchoring(false);
454
455   // Check the anchor animation is disabled
456   DALI_TEST_CHECK(view.GetAnchoring() == false);
457 }
458
459 static void UtcDaliItemViewSetAndGetAnchoringDuration()
460 {
461   ToolkitTestApplication application;
462
463   // Create the ItemView actor
464   TestItemFactory factory;
465   ItemView view = ItemView::New(factory);
466
467   // Set the duration of anchor animation to be 1.5f
468   view.SetAnchoringDuration(1.5f);
469
470   // Check the duration of anchor animation is 1.5f
471   DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
472 }
473
474 static void UtcDaliItemViewSetAndGetRefreshInterval()
475 {
476   ToolkitTestApplication application;
477
478   // Create the ItemView actor
479   TestItemFactory factory;
480   ItemView view = ItemView::New(factory);
481
482   // Set the interval between refreshes to be 20
483   view.SetRefreshInterval(20);
484
485   // Check the interval between refreshes is 20
486   DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
487 }
488
489 static void UtcDaliItemViewScrollToItem()
490 {
491   ToolkitTestApplication application;
492
493   // Create the ItemView actor
494   TestItemFactory factory;
495   ItemView view = ItemView::New(factory);
496   Vector3 vec(480.0f, 800.0f, 0.0f);
497   GridLayoutPtr layout = GridLayout::New();
498
499   view.SetName("view actor");
500   view.AddLayout(*layout);
501   view.SetSize(vec);
502
503   Stage::GetCurrent().Add(view);
504   layout->SetOrientation(ControlOrientation::Up);
505   view.ActivateLayout(0, vec, 0.0f);
506
507   application.SendNotification();
508   application.Render(0);
509
510   // render 10 frames
511   for(int i = 0; i < 10; ++i)
512   {
513     application.Render(16); // 60hz frames
514   }
515
516   // Confirm: we have actors in the view.
517   std::vector<unsigned int> indices;
518   for(unsigned int i = 0; i < 10; i++)
519   {
520     Actor testActor = view.GetItem(i);
521     if (testActor)
522     {
523       indices.push_back(i);
524     }
525   }
526
527   try
528   {
529     if (!indices.empty())
530     {
531       const unsigned int firstTargetIndex = indices[indices.size()-1];
532       // scroll to last item
533       view.ScrollToItem(firstTargetIndex, 0.00f);
534       for(int i = 0; i < 10; ++i)
535       {
536         application.Render(16); // 60hz frames
537       }
538
539       std::size_t moveCount = 0;
540       for(std::size_t i = 0; i < indices.size(); i++)
541       {
542         float layoutPosBefore = view.GetCurrentLayoutPosition(i);
543         view.ScrollToItem(indices[i], 0.0f);
544         float layoutPosAfter = view.GetCurrentLayoutPosition(i);
545
546         if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
547         {
548           ++moveCount;
549         }
550       }
551
552       DALI_TEST_CHECK((moveCount == indices.size()));
553     }
554   }
555   catch(...)
556   {
557     tet_result(TET_FAIL);
558   }
559
560   Stage::GetCurrent().Remove(view);
561 }
562
563 static void UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep()
564 {
565   ToolkitTestApplication application;
566
567   // Create the ItemView actor
568   TestItemFactory factory;
569   ItemView view = ItemView::New(factory);
570
571   // Set the scroll distance step for the mouse wheel event to be 100.0f
572   view.SetMouseWheelScrollDistanceStep(100.0f);
573
574   // Check the scroll distance step is 100.0f
575   DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );
576 }