DALi Version 1.0.28
[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 = Image::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   GridLayoutPtr gridLayout = GridLayout::New();
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
153   // Check we are getting the correct layout from ItemView
154   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
155   END_TEST;
156 }
157
158 int UtcDaliItemViewAddAndRemoveLayout(void)
159 {
160   ToolkitTestApplication application;
161
162   // Create the ItemView actor
163   TestItemFactory factory;
164   ItemView view = ItemView::New(factory);
165
166   // Create a grid layout and add it to ItemView
167   GridLayoutPtr gridLayout = GridLayout::New();
168   view.AddLayout(*gridLayout);
169
170   // As we have added one layout, check the number of layout is now 1
171   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
172
173   // Check we are getting the correct layout from ItemView
174   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
175
176   // Remove the grid layout
177   view.RemoveLayout(0);
178
179   // As we have removed the grid layout, check the number of layout is now 0
180   DALI_TEST_CHECK(view.GetLayoutCount() == 0);
181
182   END_TEST;
183 }
184
185 int UtcDaliItemViewActivateLayoutAndGetActiveLayout(void)
186 {
187   ToolkitTestApplication application;
188
189   // Create the ItemView actor
190   TestItemFactory factory;
191   ItemView view = ItemView::New(factory);
192
193   // Create a grid layout and add it to ItemView
194   GridLayoutPtr gridLayout = GridLayout::New();
195   view.AddLayout(*gridLayout);
196
197   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
198
199   // Check there is no active layout at the moment
200   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
201
202   // Activate the grid layout
203   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
204   view.ActivateLayout(0, stageSize, 0.5f);
205
206   // Check the current active layout is the grid layout
207   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
208
209   END_TEST;
210 }
211
212 int UtcDaliItemViewDeactivateCurrentLayout(void)
213 {
214   ToolkitTestApplication application;
215
216   // Create the ItemView actor
217   TestItemFactory factory;
218   ItemView view = ItemView::New(factory);
219
220   // Create a grid layout and add it to ItemView
221   GridLayoutPtr gridLayout = GridLayout::New();
222   view.AddLayout(*gridLayout);
223
224   // Check there is no active layout at the moment
225   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
226
227   // Activate the grid layout
228   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
229   view.ActivateLayout(0, stageSize, 0.5f);
230
231   // Check the current active layout is the grid layout
232   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
233
234   // Deactivate the current layout
235   view.DeactivateCurrentLayout();
236
237   // Check there is no active layout at the moment
238   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
239   END_TEST;
240 }
241
242 int UtcDaliItemViewGetItemAndGetItemId(void)
243 {
244   ToolkitTestApplication application;
245
246   // Create the ItemView actor
247   TestItemFactory factory;
248   ItemView view = ItemView::New(factory);
249
250   // Create a grid layout and add it to ItemView
251   GridLayoutPtr gridLayout = GridLayout::New();
252   view.AddLayout(*gridLayout);
253
254   // Activate the grid layout so that the items will be created and added to ItemView
255   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
256   view.ActivateLayout(0, stageSize, 0.5f);
257
258   // Get the item given the item ID
259   Actor itemActor = view.GetItem(2);
260
261   // Check we are getting the correct Item ID given the specified actor
262   DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
263   END_TEST;
264 }
265
266 int UtcDaliItemViewRemoveItem(void)
267 {
268   ToolkitTestApplication application;
269
270   // Create the ItemView actor
271   TestItemFactory factory;
272   ItemView view = ItemView::New(factory);
273
274   // Create a grid layout and add it to ItemView
275   GridLayoutPtr gridLayout = GridLayout::New();
276   view.AddLayout(*gridLayout);
277
278   // Activate the grid layout so that the items will be created and added to ItemView
279   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
280   view.ActivateLayout(0, stageSize, 0.5f);
281
282   // Get the item given the item ID 2 and 3
283   Actor oldItemActorID2 = view.GetItem(2);
284   Actor oldItemActorID3 = view.GetItem(3);
285
286   // Remove the item with ID 2
287   view.RemoveItem(2, 0.0f);
288
289   // Get the new item given the item ID 2
290   Actor newItemActorID2 = view.GetItem(2);
291
292   // Check the original item with item ID 2 was deleted and now item ID 2 represents the original item with ID 3
293   DALI_TEST_CHECK(view.GetItemId(newItemActorID2) == 2);
294   DALI_TEST_CHECK(oldItemActorID2 != newItemActorID2);
295   DALI_TEST_CHECK(newItemActorID2 = oldItemActorID3);
296   END_TEST;
297 }
298
299 int UtcDaliItemViewGetCurrentLayoutPosition(void)
300 {
301   ToolkitTestApplication application;
302
303   // Create the ItemView actor
304   TestItemFactory factory;
305   ItemView view = ItemView::New(factory);
306
307   // Create a grid layout and add it to ItemView
308   GridLayoutPtr gridLayout = GridLayout::New();
309   view.AddLayout(*gridLayout);
310
311   // Activate the grid layout so that the items will be created and added to ItemView
312   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
313   view.ActivateLayout(0, stageSize, 0.0f);
314
315   // Check the current layout position for the 10th items is 9.0f
316   DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
317   END_TEST;
318 }
319
320 int UtcDaliItemViewSetAndGetMinimumSwipeSpeed(void)
321 {
322   ToolkitTestApplication application;
323
324   // Create the ItemView actor
325   TestItemFactory factory;
326   ItemView view = ItemView::New(factory);
327
328   // Set the minimum swipe speed to be 1.5f
329   view.SetMinimumSwipeSpeed(1.5f);
330
331   // Check the minimum swipe speed is 1.5f
332   DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
333   END_TEST;
334 }
335
336 int UtcDaliItemViewSetAndGetMinimumSwipeDistance(void)
337 {
338   ToolkitTestApplication application;
339
340   // Create the ItemView actor
341   TestItemFactory factory;
342   ItemView view = ItemView::New(factory);
343
344   // Set the minimum swipe distance to be 2.5f
345   view.SetMinimumSwipeDistance(2.5f);
346
347   // Check the minimum swipe distance is 2.5f
348   DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
349   END_TEST;
350 }
351
352 int UtcDaliItemViewSetAndGetAnchoring(void)
353 {
354   ToolkitTestApplication application;
355
356   // Create the ItemView actor
357   TestItemFactory factory;
358   ItemView view = ItemView::New(factory);
359
360   // Disable the anchor animation
361   view.SetAnchoring(false);
362
363   // Check the anchor animation is disabled
364   DALI_TEST_CHECK(view.GetAnchoring() == false);
365   END_TEST;
366 }
367
368 int UtcDaliItemViewSetAndGetAnchoringDuration(void)
369 {
370   ToolkitTestApplication application;
371
372   // Create the ItemView actor
373   TestItemFactory factory;
374   ItemView view = ItemView::New(factory);
375
376   // Set the duration of anchor animation to be 1.5f
377   view.SetAnchoringDuration(1.5f);
378
379   // Check the duration of anchor animation is 1.5f
380   DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
381   END_TEST;
382 }
383
384 int UtcDaliItemViewSetAndGetRefreshInterval(void)
385 {
386   ToolkitTestApplication application;
387
388   // Create the ItemView actor
389   TestItemFactory factory;
390   ItemView view = ItemView::New(factory);
391
392   // Set the interval between refreshes to be 20
393   view.SetRefreshInterval(20);
394
395   // Check the interval between refreshes is 20
396   DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
397   END_TEST;
398 }
399
400 int UtcDaliItemViewScrollToItem(void)
401 {
402   ToolkitTestApplication application;
403
404   // Create the ItemView actor
405   TestItemFactory factory;
406   ItemView view = ItemView::New(factory);
407   Vector3 vec(480.0f, 800.0f, 0.0f);
408   GridLayoutPtr layout = GridLayout::New();
409
410   view.SetName("view actor");
411   view.AddLayout(*layout);
412   view.SetSize(vec);
413
414   Stage::GetCurrent().Add(view);
415   layout->SetOrientation(ControlOrientation::Up);
416   view.ActivateLayout(0, vec, 0.0f);
417
418   application.SendNotification();
419   application.Render(0);
420
421   // render 10 frames
422   for(int i = 0; i < 10; ++i)
423   {
424     application.Render(16); // 60hz frames
425   }
426
427   // Confirm: we have actors in the view.
428   std::vector<unsigned int> indices;
429   for(unsigned int i = 0; i < 10; i++)
430   {
431     Actor testActor = view.GetItem(i);
432     if (testActor)
433     {
434       indices.push_back(i);
435     }
436   }
437
438   try
439   {
440     if (!indices.empty())
441     {
442       const unsigned int firstTargetIndex = indices[indices.size()-1];
443       // scroll to last item
444       view.ScrollToItem(firstTargetIndex, 0.00f);
445       for(int i = 0; i < 10; ++i)
446       {
447         application.Render(16); // 60hz frames
448       }
449
450       std::size_t moveCount = 0;
451       for(std::size_t i = 0; i < indices.size(); i++)
452       {
453         float layoutPosBefore = view.GetCurrentLayoutPosition(i);
454         view.ScrollToItem(indices[i], 0.0f);
455         float layoutPosAfter = view.GetCurrentLayoutPosition(i);
456
457         if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
458         {
459           ++moveCount;
460         }
461       }
462
463       DALI_TEST_CHECK((moveCount == indices.size()));
464     }
465   }
466   catch(...)
467   {
468     tet_result(TET_FAIL);
469   }
470
471   Stage::GetCurrent().Remove(view);
472   END_TEST;
473 }
474
475 int UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep(void)
476 {
477   ToolkitTestApplication application;
478
479   // Create the ItemView actor
480   TestItemFactory factory;
481   ItemView view = ItemView::New(factory);
482
483   // Set the scroll distance step for the mouse wheel event to be 100.0f
484   view.SetMouseWheelScrollDistanceStep(100.0f);
485
486   // Check the scroll distance step is 100.0f
487   DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );
488   END_TEST;
489 }