cb506a95dce753612d754587620735371329d716
[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/dali.h>
27 #include <dali-toolkit/dali-toolkit.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 void utc_dali_toolkit_item_view_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_item_view_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42 namespace
43 {
44 const unsigned int TOTAL_ITEM_NUMBER = 100;
45 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
46
47 static bool gObjectCreatedCallBackCalled;
48
49 static void TestCallback(BaseHandle handle)
50 {
51   gObjectCreatedCallBackCalled = true;
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 image actor for this item
87     Image image = Image::New( TEST_IMAGE_FILE_NAME );
88     Actor actor = ImageActor::New(image);
89
90     return actor;
91   }
92 };
93
94 } // namespace
95
96
97 int UtcDaliItemViewNew(void)
98 {
99   ToolkitTestApplication application;
100
101   // Create the ItemView actor
102   TestItemFactory factory;
103   ItemView view = ItemView::New(factory);
104
105   DALI_TEST_CHECK(view);
106
107   //Additional check to ensure object is created by checking if it's registered
108   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
109   DALI_TEST_CHECK( registry );
110
111   gObjectCreatedCallBackCalled = false;
112   registry.ObjectCreatedSignal().Connect(&TestCallback);
113   {
114     TestItemFactory factory;
115     ItemView view = ItemView::New(factory);
116   }
117   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
118   END_TEST;
119 }
120
121 int UtcDaliItemViewDownCast(void)
122 {
123   ToolkitTestApplication application;
124
125   // Create the ItemView actor
126   TestItemFactory factory;
127   const ItemView itemViewConst = ItemView::New(factory);
128   ItemView itemView(itemViewConst);
129
130   BaseHandle handle(itemView);
131
132   ItemView newItemView = ItemView::DownCast( handle );
133   DALI_TEST_CHECK( itemView );
134   DALI_TEST_CHECK( newItemView == itemView );
135   END_TEST;
136 }
137
138 int UtcDaliItemViewAddAndGetLayout(void)
139 {
140   ToolkitTestApplication application;
141
142   // Create the ItemView actor
143   TestItemFactory factory;
144   ItemView view = ItemView::New(factory);
145
146   // Create a grid layout and add it to ItemView
147   GridLayoutPtr gridLayout = GridLayout::New();
148   view.AddLayout(*gridLayout);
149
150   // As we have added one layout, check the number of layout is now 1
151   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
152
153
154   // Check we are getting the correct layout from ItemView
155   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
156   END_TEST;
157 }
158
159 int UtcDaliItemViewAddAndRemoveLayout(void)
160 {
161   ToolkitTestApplication application;
162
163   // Create the ItemView actor
164   TestItemFactory factory;
165   ItemView view = ItemView::New(factory);
166
167   // Create a grid layout and add it to ItemView
168   GridLayoutPtr gridLayout = GridLayout::New();
169   view.AddLayout(*gridLayout);
170
171   // As we have added one layout, check the number of layout is now 1
172   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
173
174   // Check we are getting the correct layout from ItemView
175   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
176
177   // Remove the grid layout
178   view.RemoveLayout(0);
179
180   // As we have removed the grid layout, check the number of layout is now 0
181   DALI_TEST_CHECK(view.GetLayoutCount() == 0);
182
183   END_TEST;
184 }
185
186 int UtcDaliItemViewActivateLayoutAndGetActiveLayout(void)
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   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
199
200   // Check there is no active layout at the moment
201   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
202
203   // Activate the grid layout
204   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
205   view.ActivateLayout(0, stageSize, 0.5f);
206
207   // Check the current active layout is the grid layout
208   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
209
210   END_TEST;
211 }
212
213 int UtcDaliItemViewDeactivateCurrentLayout(void)
214 {
215   ToolkitTestApplication application;
216
217   // Create the ItemView actor
218   TestItemFactory factory;
219   ItemView view = ItemView::New(factory);
220
221   // Create a grid layout and add it to ItemView
222   GridLayoutPtr gridLayout = GridLayout::New();
223   view.AddLayout(*gridLayout);
224
225   // Check there is no active layout at the moment
226   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
227
228   // Activate the grid layout
229   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
230   view.ActivateLayout(0, stageSize, 0.5f);
231
232   // Check the current active layout is the grid layout
233   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
234
235   // Deactivate the current layout
236   view.DeactivateCurrentLayout();
237
238   // Check there is no active layout at the moment
239   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
240   END_TEST;
241 }
242
243 int UtcDaliItemViewGetItemAndGetItemId(void)
244 {
245   ToolkitTestApplication application;
246
247   // Create the ItemView actor
248   TestItemFactory factory;
249   ItemView view = ItemView::New(factory);
250
251   // Create a grid layout and add it to ItemView
252   GridLayoutPtr gridLayout = GridLayout::New();
253   view.AddLayout(*gridLayout);
254
255   // Activate the grid layout so that the items will be created and added to ItemView
256   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
257   view.ActivateLayout(0, stageSize, 0.5f);
258
259   // Get the item given the item ID
260   Actor itemActor = view.GetItem(2);
261
262   // Check we are getting the correct Item ID given the specified actor
263   DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
264   END_TEST;
265 }
266
267 int UtcDaliItemViewRemoveItem(void)
268 {
269   ToolkitTestApplication application;
270
271   // Create the ItemView actor
272   TestItemFactory factory;
273   ItemView view = ItemView::New(factory);
274
275   // Create a grid layout and add it to ItemView
276   GridLayoutPtr gridLayout = GridLayout::New();
277   view.AddLayout(*gridLayout);
278
279   // Activate the grid layout so that the items will be created and added to ItemView
280   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
281   view.ActivateLayout(0, stageSize, 0.5f);
282
283   // Get the item given the item ID 2 and 3
284   Actor oldItemActorID2 = view.GetItem(2);
285   Actor oldItemActorID3 = view.GetItem(3);
286
287   // Remove the item with ID 2
288   view.RemoveItem(2, 0.0f);
289
290   // Get the new item given the item ID 2
291   Actor newItemActorID2 = view.GetItem(2);
292
293   // Check the original item with item ID 2 was deleted and now item ID 2 represents the original item with ID 3
294   DALI_TEST_CHECK(view.GetItemId(newItemActorID2) == 2);
295   DALI_TEST_CHECK(oldItemActorID2 != newItemActorID2);
296   DALI_TEST_CHECK(newItemActorID2 = oldItemActorID3);
297   END_TEST;
298 }
299
300 int UtcDaliItemViewGetCurrentLayoutPosition(void)
301 {
302   ToolkitTestApplication application;
303
304   // Create the ItemView actor
305   TestItemFactory factory;
306   ItemView view = ItemView::New(factory);
307
308   // Create a grid layout and add it to ItemView
309   GridLayoutPtr gridLayout = GridLayout::New();
310   view.AddLayout(*gridLayout);
311
312   // Activate the grid layout so that the items will be created and added to ItemView
313   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
314   view.ActivateLayout(0, stageSize, 0.0f);
315
316   // Check the current layout position for the 10th items is 9.0f
317   DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
318   END_TEST;
319 }
320
321 int UtcDaliItemViewSetAndGetMinimumSwipeSpeed(void)
322 {
323   ToolkitTestApplication application;
324
325   // Create the ItemView actor
326   TestItemFactory factory;
327   ItemView view = ItemView::New(factory);
328
329   // Set the minimum swipe speed to be 1.5f
330   view.SetMinimumSwipeSpeed(1.5f);
331
332   // Check the minimum swipe speed is 1.5f
333   DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
334   END_TEST;
335 }
336
337 int UtcDaliItemViewSetAndGetMinimumSwipeDistance(void)
338 {
339   ToolkitTestApplication application;
340
341   // Create the ItemView actor
342   TestItemFactory factory;
343   ItemView view = ItemView::New(factory);
344
345   // Set the minimum swipe distance to be 2.5f
346   view.SetMinimumSwipeDistance(2.5f);
347
348   // Check the minimum swipe distance is 2.5f
349   DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
350   END_TEST;
351 }
352
353 int UtcDaliItemViewSetAndGetAnchoring(void)
354 {
355   ToolkitTestApplication application;
356
357   // Create the ItemView actor
358   TestItemFactory factory;
359   ItemView view = ItemView::New(factory);
360
361   // Disable the anchor animation
362   view.SetAnchoring(false);
363
364   // Check the anchor animation is disabled
365   DALI_TEST_CHECK(view.GetAnchoring() == false);
366   END_TEST;
367 }
368
369 int UtcDaliItemViewSetAndGetAnchoringDuration(void)
370 {
371   ToolkitTestApplication application;
372
373   // Create the ItemView actor
374   TestItemFactory factory;
375   ItemView view = ItemView::New(factory);
376
377   // Set the duration of anchor animation to be 1.5f
378   view.SetAnchoringDuration(1.5f);
379
380   // Check the duration of anchor animation is 1.5f
381   DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
382   END_TEST;
383 }
384
385 int UtcDaliItemViewSetAndGetRefreshInterval(void)
386 {
387   ToolkitTestApplication application;
388
389   // Create the ItemView actor
390   TestItemFactory factory;
391   ItemView view = ItemView::New(factory);
392
393   // Set the interval between refreshes to be 20
394   view.SetRefreshInterval(20);
395
396   // Check the interval between refreshes is 20
397   DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
398   END_TEST;
399 }
400
401 int UtcDaliItemViewScrollToItem(void)
402 {
403   ToolkitTestApplication application;
404
405   // Create the ItemView actor
406   TestItemFactory factory;
407   ItemView view = ItemView::New(factory);
408   Vector3 vec(480.0f, 800.0f, 0.0f);
409   GridLayoutPtr layout = GridLayout::New();
410
411   view.SetName("view actor");
412   view.AddLayout(*layout);
413   view.SetSize(vec);
414
415   Stage::GetCurrent().Add(view);
416   layout->SetOrientation(ControlOrientation::Up);
417   view.ActivateLayout(0, vec, 0.0f);
418
419   application.SendNotification();
420   application.Render(0);
421
422   // render 10 frames
423   for(int i = 0; i < 10; ++i)
424   {
425     application.Render(16); // 60hz frames
426   }
427
428   // Confirm: we have actors in the view.
429   std::vector<unsigned int> indices;
430   for(unsigned int i = 0; i < 10; i++)
431   {
432     Actor testActor = view.GetItem(i);
433     if (testActor)
434     {
435       indices.push_back(i);
436     }
437   }
438
439   try
440   {
441     if (!indices.empty())
442     {
443       const unsigned int firstTargetIndex = indices[indices.size()-1];
444       // scroll to last item
445       view.ScrollToItem(firstTargetIndex, 0.00f);
446       for(int i = 0; i < 10; ++i)
447       {
448         application.Render(16); // 60hz frames
449       }
450
451       std::size_t moveCount = 0;
452       for(std::size_t i = 0; i < indices.size(); i++)
453       {
454         float layoutPosBefore = view.GetCurrentLayoutPosition(i);
455         view.ScrollToItem(indices[i], 0.0f);
456         float layoutPosAfter = view.GetCurrentLayoutPosition(i);
457
458         if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
459         {
460           ++moveCount;
461         }
462       }
463
464       DALI_TEST_CHECK((moveCount == indices.size()));
465     }
466   }
467   catch(...)
468   {
469     tet_result(TET_FAIL);
470   }
471
472   Stage::GetCurrent().Remove(view);
473   END_TEST;
474 }
475
476 int UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep(void)
477 {
478   ToolkitTestApplication application;
479
480   // Create the ItemView actor
481   TestItemFactory factory;
482   ItemView view = ItemView::New(factory);
483
484   // Set the scroll distance step for the mouse wheel event to be 100.0f
485   view.SetMouseWheelScrollDistanceStep(100.0f);
486
487   // Check the scroll distance step is 100.0f
488   DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );
489   END_TEST;
490 }