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