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