2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <float.h> // for FLT_MAX
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>
26 #include <dali-toolkit/dali-toolkit.h>
29 using namespace Toolkit;
31 void utc_dali_toolkit_item_view_startup(void)
33 test_return_value = TET_UNDEF;
36 void utc_dali_toolkit_item_view_cleanup(void)
38 test_return_value = TET_PASS;
43 const unsigned int TOTAL_ITEM_NUMBER = 100;
44 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
46 static bool gObjectCreatedCallBackCalled;
48 static void TestCallback(BaseHandle handle)
50 gObjectCreatedCallBackCalled = true;
54 // Implementation of ItemFactory for providing actors to ItemView
55 class TestItemFactory : public ItemFactory
61 * @param application class, stored as reference
67 public: // From ItemFactory
70 * Query the number of items available from the factory.
71 * The maximum available item has an ID of GetNumberOfItems() - 1.
73 virtual unsigned int GetNumberOfItems()
75 return TOTAL_ITEM_NUMBER;
79 * Create an Actor to represent a visible item.
81 * @return the created actor.
83 virtual Actor NewItem(unsigned int itemId)
85 // Create an image actor for this item
86 Image image = ResourceImage::New( TEST_IMAGE_FILE_NAME );
87 Actor actor = ImageActor::New(image);
96 int UtcDaliItemViewNew(void)
98 ToolkitTestApplication application;
100 // Create the ItemView actor
101 TestItemFactory factory;
102 ItemView view = ItemView::New(factory);
104 DALI_TEST_CHECK(view);
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 );
110 gObjectCreatedCallBackCalled = false;
111 registry.ObjectCreatedSignal().Connect(&TestCallback);
113 TestItemFactory factory;
114 ItemView view = ItemView::New(factory);
116 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
120 int UtcDaliItemViewDownCast(void)
122 ToolkitTestApplication application;
124 // Create the ItemView actor
125 TestItemFactory factory;
126 const ItemView itemViewConst = ItemView::New(factory);
127 ItemView itemView(itemViewConst);
129 BaseHandle handle(itemView);
131 ItemView newItemView = ItemView::DownCast( handle );
132 DALI_TEST_CHECK( itemView );
133 DALI_TEST_CHECK( newItemView == itemView );
137 int UtcDaliItemViewAddAndGetLayout(void)
139 ToolkitTestApplication application;
141 // Create the ItemView actor
142 TestItemFactory factory;
143 ItemView view = ItemView::New(factory);
145 // Create a grid layout and add it to ItemView
146 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
147 view.AddLayout(*gridLayout);
149 // As we have added one layout, check the number of layout is now 1
150 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
152 // Create a depth layout and add it to ItemView
153 ItemLayoutPtr depthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
154 view.AddLayout(*depthLayout);
156 // As we have added another layout, check the number of layout is now 2
157 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
159 // Create a spiral layout and add it to ItemView
160 ItemLayoutPtr spiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
161 view.AddLayout(*spiralLayout);
163 // As we have added another layout, check the number of layout is now 3
164 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
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);
173 int UtcDaliItemViewAddAndRemoveLayout(void)
175 ToolkitTestApplication application;
177 // Create the ItemView actor
178 TestItemFactory factory;
179 ItemView view = ItemView::New(factory);
181 // Create a grid layout and add it to ItemView
182 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
183 view.AddLayout(*gridLayout);
185 // As we have added one layout, check the number of layout is now 1
186 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
188 // Create a depth layout and add it to ItemView
189 ItemLayoutPtr depthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
190 view.AddLayout(*depthLayout);
192 // As we have added another layout, check the number of layout is now 2
193 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
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);
199 // Remove the grid layout
200 view.RemoveLayout(0);
202 // As we have removed the grid layout, check the number of layout is now 1
203 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
205 // Check we are getting the correct layout from ItemView
206 DALI_TEST_CHECK(view.GetLayout(0) == depthLayout);
208 // Remove the depth layout
209 view.RemoveLayout(0);
211 // As we also removed the depth layout, check the number of layout is now 0
212 DALI_TEST_CHECK(view.GetLayoutCount() == 0);
216 int UtcDaliItemViewActivateLayoutAndGetActiveLayout(void)
218 ToolkitTestApplication application;
220 // Create the ItemView actor
221 TestItemFactory factory;
222 ItemView view = ItemView::New(factory);
224 // Create a grid layout and add it to ItemView
225 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
226 view.AddLayout(*gridLayout);
228 // Create a depth layout and add it to ItemView
229 ItemLayoutPtr depthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
230 view.AddLayout(*depthLayout);
232 // Create a spiral layout and add it to ItemView
233 ItemLayoutPtr spiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
234 view.AddLayout(*spiralLayout);
236 // As we have added three layouts, check the number of layout is now 3
237 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
239 // Check there is no active layout at the moment
240 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
242 // Activate the depth layout
243 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
244 view.ActivateLayout(1, stageSize, 0.5f);
246 // Check the current active layout is the depth layout
247 DALI_TEST_CHECK(view.GetActiveLayout() == depthLayout);
249 // Activate the grid layout
250 view.ActivateLayout(0, stageSize, 0.5f);
252 // Check the current active layout is the grid layout
253 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
255 // Activate the spiral layout
256 view.ActivateLayout(2, stageSize, 0.5f);
258 // Check the current active layout is the spiral layout
259 DALI_TEST_CHECK(view.GetActiveLayout() == spiralLayout);
263 int UtcDaliItemViewDeactivateCurrentLayout(void)
265 ToolkitTestApplication application;
267 // Create the ItemView actor
268 TestItemFactory factory;
269 ItemView view = ItemView::New(factory);
271 // Create a grid layout and add it to ItemView
272 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
273 view.AddLayout(*gridLayout);
275 // Check there is no active layout at the moment
276 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
278 // Activate the grid layout
279 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
280 view.ActivateLayout(0, stageSize, 0.5f);
282 // Check the current active layout is the grid layout
283 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
285 // Deactivate the current layout
286 view.DeactivateCurrentLayout();
288 // Check there is no active layout at the moment
289 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
293 int UtcDaliItemViewGetItemAndGetItemId(void)
295 ToolkitTestApplication application;
297 // Create the ItemView actor
298 TestItemFactory factory;
299 ItemView view = ItemView::New(factory);
301 // Create a grid layout and add it to ItemView
302 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
303 view.AddLayout(*gridLayout);
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);
309 // Get the item given the item ID
310 Actor itemActor = view.GetItem(2);
312 // Check we are getting the correct Item ID given the specified actor
313 DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
317 int UtcDaliItemViewRemoveItem(void)
319 ToolkitTestApplication application;
321 // Create the ItemView actor
322 TestItemFactory factory;
323 ItemView view = ItemView::New(factory);
325 // Create a grid layout and add it to ItemView
326 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
327 view.AddLayout(*gridLayout);
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);
333 // Get the item given the item ID 2 and 3
334 Actor oldItemActorID2 = view.GetItem(2);
335 Actor oldItemActorID3 = view.GetItem(3);
337 // Remove the item with ID 2
338 view.RemoveItem(2, 0.0f);
340 // Get the new item given the item ID 2
341 Actor newItemActorID2 = view.GetItem(2);
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);
350 int UtcDaliItemViewGetCurrentLayoutPosition(void)
352 ToolkitTestApplication application;
354 // Create the ItemView actor
355 TestItemFactory factory;
356 ItemView view = ItemView::New(factory);
358 // Create a grid layout and add it to ItemView
359 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
360 view.AddLayout(*gridLayout);
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);
366 // Check the current layout position for the 10th items is 9.0f
367 DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
371 int UtcDaliItemViewSetAndGetMinimumSwipeSpeed(void)
373 ToolkitTestApplication application;
375 // Create the ItemView actor
376 TestItemFactory factory;
377 ItemView view = ItemView::New(factory);
379 // Set the minimum swipe speed to be 1.5f
380 view.SetMinimumSwipeSpeed(1.5f);
382 // Check the minimum swipe speed is 1.5f
383 DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
387 int UtcDaliItemViewSetAndGetMinimumSwipeDistance(void)
389 ToolkitTestApplication application;
391 // Create the ItemView actor
392 TestItemFactory factory;
393 ItemView view = ItemView::New(factory);
395 // Set the minimum swipe distance to be 2.5f
396 view.SetMinimumSwipeDistance(2.5f);
398 // Check the minimum swipe distance is 2.5f
399 DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
403 int UtcDaliItemViewSetAndGetAnchoring(void)
405 ToolkitTestApplication application;
407 // Create the ItemView actor
408 TestItemFactory factory;
409 ItemView view = ItemView::New(factory);
411 // Disable the anchor animation
412 view.SetAnchoring(false);
414 // Check the anchor animation is disabled
415 DALI_TEST_CHECK(view.GetAnchoring() == false);
419 int UtcDaliItemViewSetAndGetAnchoringDuration(void)
421 ToolkitTestApplication application;
423 // Create the ItemView actor
424 TestItemFactory factory;
425 ItemView view = ItemView::New(factory);
427 // Set the duration of anchor animation to be 1.5f
428 view.SetAnchoringDuration(1.5f);
430 // Check the duration of anchor animation is 1.5f
431 DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
435 int UtcDaliItemViewSetAndGetRefreshInterval(void)
437 ToolkitTestApplication application;
439 // Create the ItemView actor
440 TestItemFactory factory;
441 ItemView view = ItemView::New(factory);
443 // Set the interval between refreshes to be 20
444 view.SetRefreshInterval(20);
448 // Check the interval between refreshes is 20
449 DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
453 int UtcDaliItemViewScrollToItem(void)
455 ToolkitTestApplication application;
457 // Create the ItemView actor
458 TestItemFactory factory;
459 ItemView view = ItemView::New(factory);
460 Vector3 vec(480.0f, 800.0f, 0.0f);
461 ItemLayoutPtr layout = DefaultItemLayout::New( DefaultItemLayout::GRID );
463 view.SetName("view actor");
464 view.AddLayout(*layout);
467 Stage::GetCurrent().Add(view);
468 layout->SetOrientation(ControlOrientation::Up);
469 view.ActivateLayout(0, vec, 0.0f);
471 application.SendNotification();
472 application.Render(0);
475 for(int i = 0; i < 10; ++i)
477 application.Render(16); // 60hz frames
480 // Confirm: we have actors in the view.
481 std::vector<unsigned int> indices;
482 for(unsigned int i = 0; i < 10; i++)
484 Actor testActor = view.GetItem(i);
487 indices.push_back(i);
493 if (!indices.empty())
495 const unsigned int firstTargetIndex = indices[indices.size()-1];
496 // scroll to last item
497 view.ScrollToItem(firstTargetIndex, 0.00f);
498 for(int i = 0; i < 10; ++i)
500 application.Render(16); // 60hz frames
503 std::size_t moveCount = 0;
504 for(std::size_t i = 0; i < indices.size(); i++)
506 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
507 view.ScrollToItem(indices[i], 0.0f);
508 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
510 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
516 DALI_TEST_CHECK((moveCount == indices.size()));
521 tet_result(TET_FAIL);
524 Stage::GetCurrent().Remove(view);
528 int UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep(void)
530 ToolkitTestApplication application;
532 // Create the ItemView actor
533 TestItemFactory factory;
534 ItemView view = ItemView::New(factory);
536 // Set the scroll distance step for the mouse wheel event to be 100.0f
537 view.SetMouseWheelScrollDistanceStep(100.0f);
539 // Check the scroll distance step is 100.0f
540 DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );
544 int UtcDaliItemViewInsertItemP(void)
546 ToolkitTestApplication application;
548 // Create the ItemView actor
549 TestItemFactory factory;
550 ItemView view = ItemView::New(factory);
552 // Create a grid layout and add it to ItemView
553 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
554 view.AddLayout(*gridLayout);
556 // Activate the grid layout so that the items will be created and added to ItemView
557 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
558 view.ActivateLayout(0, stageSize, 0.5f);
560 // Get the item given the item ID
561 Actor itemActor = view.GetItem(2);
563 ItemId id = view.GetItemId( itemActor );
565 // Check we are getting the correct Item ID given the specified actor
566 DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
568 Actor newActor = Actor::New();
570 view.InsertItem(Item(id, newActor), 0.5f);
572 DALI_TEST_CHECK(view.GetItem(2) == newActor);
576 int UtcDaliItemViewInsertItemsP(void)
578 ToolkitTestApplication application;
580 // Create the ItemView actor
581 TestItemFactory factory;
582 ItemView view = ItemView::New(factory);
584 // Create a grid layout and add it to ItemView
585 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
586 view.AddLayout(*gridLayout);
588 // Activate the grid layout so that the items will be created and added to ItemView
589 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
590 view.ActivateLayout(0, stageSize, 0.5f);
592 unsigned int itemCount = view.GetChildCount();
594 ItemContainer insertList;
596 for( unsigned int i = 0u; i < 10; ++i )
598 Actor child = view.GetChildAt( i );
599 Actor newActor = Actor::New();
600 newActor.SetName("Inserted");
601 insertList.push_back( Item( view.GetItemId(child), newActor ) );
604 if( !insertList.empty() )
606 view.InsertItems( insertList, 0.5f );
609 DALI_TEST_CHECK(view.GetChildCount() == itemCount + 10);
611 ItemIdContainer removeList;
613 for( unsigned int i = 0u; i < view.GetChildCount(); ++i )
615 Actor child = view.GetChildAt( i );
617 if( child.GetName() == "Inserted" )
619 removeList.push_back( view.GetItemId(child) );
623 if( ! removeList.empty() )
625 view.RemoveItems( removeList, 0.5f );
628 DALI_TEST_CHECK(view.GetChildCount() == itemCount);
632 int UtcDaliItemViewReplaceItemP(void)
634 ToolkitTestApplication application;
636 // Create the ItemView actor
637 TestItemFactory factory;
638 ItemView view = ItemView::New(factory);
640 // Create a grid layout and add it to ItemView
641 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
642 view.AddLayout(*gridLayout);
644 // Activate the grid layout so that the items will be created and added to ItemView
645 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
646 view.ActivateLayout(0, stageSize, 0.5f);
648 Actor newActor = Actor::New();
650 view.ReplaceItem( Item( 0, newActor ), 0.5f );
652 DALI_TEST_CHECK(view.GetItem(0) == newActor);
656 int UtcDaliItemViewReplaceItemsP(void)
658 ToolkitTestApplication application;
660 // Create the ItemView actor
661 TestItemFactory factory;
662 ItemView view = ItemView::New(factory);
664 // Create a grid layout and add it to ItemView
665 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
666 view.AddLayout(*gridLayout);
668 // Activate the grid layout so that the items will be created and added to ItemView
669 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
670 view.ActivateLayout(0, stageSize, 0.5f);
672 ItemContainer replaceList;
674 for( unsigned int i = 0u; i < 10; ++i )
676 Actor child = view.GetChildAt( i );
677 Actor newActor = Actor::New();
678 newActor.SetName("Replaced");
680 replaceList.push_back( Item( view.GetItemId(child), newActor ) );
683 if( !replaceList.empty() )
685 view.ReplaceItems( replaceList, 0.5f );
688 DALI_TEST_CHECK(view.GetItem(0).GetName() == "Replaced");
689 DALI_TEST_CHECK(view.GetItem(8).GetName() == "Replaced");
693 int UtcDaliItemViewGetItemsRangeP(void)
695 ToolkitTestApplication application;
697 // Create the ItemView actor
698 TestItemFactory factory;
699 ItemView view = ItemView::New(factory);
701 // Create a grid layout and add it to ItemView
702 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
703 view.AddLayout(*gridLayout);
705 // Activate the grid layout so that the items will be created and added to ItemView
706 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
707 view.ActivateLayout(0, stageSize, 0.5f);
709 ItemRange itemRange(0, 0);
711 view.GetItemsRange(itemRange);
713 DALI_TEST_CHECK(itemRange.Within(0));
717 int UtcDaliItemViewSetItemsAnchorPointP(void)
719 ToolkitTestApplication application;
721 // Create the ItemView actor
722 TestItemFactory factory;
723 ItemView view = ItemView::New(factory);
725 // Create a grid layout and add it to ItemView
726 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
727 view.AddLayout(*gridLayout);
729 // Activate the grid layout so that the items will be created and added to ItemView
730 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
731 view.ActivateLayout(0, stageSize, 0.5f);
733 Vector3 anchorPoint(10.0f, 10.0f, 0.0f);
735 view.SetItemsAnchorPoint(anchorPoint);
737 DALI_TEST_CHECK(view.GetItemsAnchorPoint() == anchorPoint);
738 DALI_TEST_CHECK(view.GetItem(0).GetCurrentAnchorPoint() == anchorPoint);
742 int UtcDaliItemViewSetItemsParentOriginP(void)
744 ToolkitTestApplication application;
746 // Create the ItemView actor
747 TestItemFactory factory;
748 ItemView view = ItemView::New(factory);
750 // Create a grid layout and add it to ItemView
751 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
752 view.AddLayout(*gridLayout);
754 // Activate the grid layout so that the items will be created and added to ItemView
755 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
756 view.ActivateLayout(0, stageSize, 0.5f);
758 Vector3 parentOrigin(10.0f, 10.0f, 0.0f);
760 view.SetItemsParentOrigin(parentOrigin);
762 DALI_TEST_CHECK(view.GetItemsParentOrigin() == parentOrigin);
763 DALI_TEST_CHECK(view.GetItem(0).GetCurrentParentOrigin() == parentOrigin);