2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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
23 #include <dali/dali.h>
24 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit-test-suite-utils.h>
29 using namespace Toolkit;
33 const unsigned int TOTAL_ITEM_NUMBER = 100;
34 const char* TEST_IMAGE_FILE_NAME = DALI_IMAGE_DIR "gallery_image_01.jpg";
36 static bool gObjectCreatedCallBackCalled;
38 static void TestCallback(BaseHandle handle)
40 gObjectCreatedCallBackCalled = true;
45 static void Startup();
46 static void Cleanup();
49 void (*tet_startup)() = Startup;
50 void (*tet_cleanup)() = Cleanup;
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();
71 POSITIVE_TC_IDX = 0x01,
75 // Add test functionality for all APIs in the class (Positive and Negative)
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 },
98 // Called only once before first test is run.
103 // Called only once after last test is run
104 static void Cleanup()
108 // Implementation of ItemFactory for providing actors to ItemView
109 class TestItemFactory : public ItemFactory
115 * @param application class, stored as reference
121 public: // From ItemFactory
124 * Query the number of items available from the factory.
125 * The maximum available item has an ID of GetNumberOfItems() - 1.
127 virtual unsigned int GetNumberOfItems()
129 return TOTAL_ITEM_NUMBER;
133 * Create an Actor to represent a visible item.
135 * @return the created actor.
137 virtual Actor NewItem(unsigned int itemId)
139 // Create an image actor for this item
140 Image image = Image::New( TEST_IMAGE_FILE_NAME );
141 Actor actor = ImageActor::New(image);
147 static void UtcDaliItemViewNew()
149 ToolkitTestApplication application;
151 // Create the ItemView actor
152 TestItemFactory factory;
153 ItemView view = ItemView::New(factory);
155 DALI_TEST_CHECK(view);
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 );
161 gObjectCreatedCallBackCalled = false;
162 registry.ObjectCreatedSignal().Connect(&TestCallback);
164 TestItemFactory factory;
165 ItemView view = ItemView::New(factory);
167 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
170 static void UtcDaliItemViewDownCast()
172 ToolkitTestApplication application;
174 // Create the ItemView actor
175 TestItemFactory factory;
176 const ItemView itemViewConst = ItemView::New(factory);
177 ItemView itemView(itemViewConst);
179 BaseHandle handle(itemView);
181 ItemView newItemView = ItemView::DownCast( handle );
182 DALI_TEST_CHECK( itemView );
183 DALI_TEST_CHECK( newItemView == itemView );
186 static void UtcDaliItemViewAddAndGetLayout()
188 ToolkitTestApplication application;
190 // Create the ItemView actor
191 TestItemFactory factory;
192 ItemView view = ItemView::New(factory);
194 // Create a grid layout and add it to ItemView
195 GridLayoutPtr gridLayout = GridLayout::New();
196 view.AddLayout(*gridLayout);
198 // As we have added one layout, check the number of layout is now 1
199 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
201 // Create a depth layout and add it to ItemView
202 DepthLayoutPtr depthLayout = DepthLayout::New();
203 view.AddLayout(*depthLayout);
205 // As we have added another layout, check the number of layout is now 2
206 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
208 // Create a spiral layout and add it to ItemView
209 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
210 view.AddLayout(*spiralLayout);
212 // As we have added another layout, check the number of layout is now 3
213 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
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);
221 static void UtcDaliItemViewAddAndRemoveLayout()
223 ToolkitTestApplication application;
225 // Create the ItemView actor
226 TestItemFactory factory;
227 ItemView view = ItemView::New(factory);
229 // Create a grid layout and add it to ItemView
230 GridLayoutPtr gridLayout = GridLayout::New();
231 view.AddLayout(*gridLayout);
233 // As we have added one layout, check the number of layout is now 1
234 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
236 // Create a depth layout and add it to ItemView
237 DepthLayoutPtr depthLayout = DepthLayout::New();
238 view.AddLayout(*depthLayout);
240 // As we have added another layout, check the number of layout is now 2
241 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
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);
247 // Remove the grid layout
248 view.RemoveLayout(0);
250 // As we have removed the grid layout, check the number of layout is now 1
251 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
253 // Check we are getting the correct layout from ItemView
254 DALI_TEST_CHECK(view.GetLayout(0) == depthLayout);
256 // Remove the depth layout
257 view.RemoveLayout(0);
259 // As we also removed the depth layout, check the number of layout is now 0
260 DALI_TEST_CHECK(view.GetLayoutCount() == 0);
263 static void UtcDaliItemViewActivateLayoutAndGetActiveLayout()
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 GridLayoutPtr gridLayout = GridLayout::New();
273 view.AddLayout(*gridLayout);
275 // Create a depth layout and add it to ItemView
276 DepthLayoutPtr depthLayout = DepthLayout::New();
277 view.AddLayout(*depthLayout);
279 // Create a spiral layout and add it to ItemView
280 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
281 view.AddLayout(*spiralLayout);
283 // As we have added three layouts, check the number of layout is now 3
284 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
286 // Check there is no active layout at the moment
287 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
289 // Activate the depth layout
290 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
291 view.ActivateLayout(1, stageSize, 0.5f);
293 // Check the current active layout is the depth layout
294 DALI_TEST_CHECK(view.GetActiveLayout() == depthLayout);
296 // Activate the grid layout
297 view.ActivateLayout(0, stageSize, 0.5f);
299 // Check the current active layout is the grid layout
300 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
302 // Activate the spiral layout
303 view.ActivateLayout(2, stageSize, 0.5f);
305 // Check the current active layout is the spiral layout
306 DALI_TEST_CHECK(view.GetActiveLayout() == spiralLayout);
309 static void UtcDaliItemViewDeactivateCurrentLayout()
311 ToolkitTestApplication application;
313 // Create the ItemView actor
314 TestItemFactory factory;
315 ItemView view = ItemView::New(factory);
317 // Create a grid layout and add it to ItemView
318 GridLayoutPtr gridLayout = GridLayout::New();
319 view.AddLayout(*gridLayout);
321 // Check there is no active layout at the moment
322 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
324 // Activate the grid layout
325 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
326 view.ActivateLayout(0, stageSize, 0.5f);
328 // Check the current active layout is the grid layout
329 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
331 // Deactivate the current layout
332 view.DeactivateCurrentLayout();
334 // Check there is no active layout at the moment
335 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
338 static void UtcDaliItemViewGetItemAndGetItemId()
340 ToolkitTestApplication application;
342 // Create the ItemView actor
343 TestItemFactory factory;
344 ItemView view = ItemView::New(factory);
346 // Create a grid layout and add it to ItemView
347 GridLayoutPtr gridLayout = GridLayout::New();
348 view.AddLayout(*gridLayout);
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);
354 // Get the item given the item ID
355 Actor itemActor = view.GetItem(2);
357 // Check we are getting the correct Item ID given the specified actor
358 DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
361 static void UtcDaliItemViewRemoveItem()
363 ToolkitTestApplication application;
365 // Create the ItemView actor
366 TestItemFactory factory;
367 ItemView view = ItemView::New(factory);
369 // Create a grid layout and add it to ItemView
370 GridLayoutPtr gridLayout = GridLayout::New();
371 view.AddLayout(*gridLayout);
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);
377 // Get the item given the item ID 2 and 3
378 Actor oldItemActorID2 = view.GetItem(2);
379 Actor oldItemActorID3 = view.GetItem(3);
381 // Remove the item with ID 2
382 view.RemoveItem(2, 0.0f);
384 // Get the new item given the item ID 2
385 Actor newItemActorID2 = view.GetItem(2);
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);
393 static void UtcDaliItemViewGetCurrentLayoutPosition()
395 ToolkitTestApplication application;
397 // Create the ItemView actor
398 TestItemFactory factory;
399 ItemView view = ItemView::New(factory);
401 // Create a grid layout and add it to ItemView
402 GridLayoutPtr gridLayout = GridLayout::New();
403 view.AddLayout(*gridLayout);
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);
409 // Check the current layout position for the 10th items is 9.0f
410 DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
413 static void UtcDaliItemViewSetAndGetMinimumSwipeSpeed()
415 ToolkitTestApplication application;
417 // Create the ItemView actor
418 TestItemFactory factory;
419 ItemView view = ItemView::New(factory);
421 // Set the minimum swipe speed to be 1.5f
422 view.SetMinimumSwipeSpeed(1.5f);
424 // Check the minimum swipe speed is 1.5f
425 DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
428 static void UtcDaliItemViewSetAndGetMinimumSwipeDistance()
430 ToolkitTestApplication application;
432 // Create the ItemView actor
433 TestItemFactory factory;
434 ItemView view = ItemView::New(factory);
436 // Set the minimum swipe distance to be 2.5f
437 view.SetMinimumSwipeDistance(2.5f);
439 // Check the minimum swipe distance is 2.5f
440 DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
443 static void UtcDaliItemViewSetAndGetAnchoring()
445 ToolkitTestApplication application;
447 // Create the ItemView actor
448 TestItemFactory factory;
449 ItemView view = ItemView::New(factory);
451 // Disable the anchor animation
452 view.SetAnchoring(false);
454 // Check the anchor animation is disabled
455 DALI_TEST_CHECK(view.GetAnchoring() == false);
458 static void UtcDaliItemViewSetAndGetAnchoringDuration()
460 ToolkitTestApplication application;
462 // Create the ItemView actor
463 TestItemFactory factory;
464 ItemView view = ItemView::New(factory);
466 // Set the duration of anchor animation to be 1.5f
467 view.SetAnchoringDuration(1.5f);
469 // Check the duration of anchor animation is 1.5f
470 DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
473 static void UtcDaliItemViewSetAndGetRefreshInterval()
475 ToolkitTestApplication application;
477 // Create the ItemView actor
478 TestItemFactory factory;
479 ItemView view = ItemView::New(factory);
481 // Set the interval between refreshes to be 20
482 view.SetRefreshInterval(20);
484 // Check the interval between refreshes is 20
485 DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
488 static void UtcDaliItemViewScrollToItem()
490 ToolkitTestApplication application;
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();
498 view.SetName("view actor");
499 view.AddLayout(*layout);
502 Stage::GetCurrent().Add(view);
503 layout->SetOrientation(ControlOrientation::Up);
504 view.ActivateLayout(0, vec, 0.0f);
506 application.SendNotification();
507 application.Render(0);
510 for(int i = 0; i < 10; ++i)
512 application.Render(16); // 60hz frames
515 // Confirm: we have actors in the view.
516 std::vector<unsigned int> indices;
517 for(unsigned int i = 0; i < 10; i++)
519 Actor testActor = view.GetItem(i);
522 indices.push_back(i);
528 if (!indices.empty())
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)
535 application.Render(16); // 60hz frames
538 std::size_t moveCount = 0;
539 for(std::size_t i = 0; i < indices.size(); i++)
541 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
542 view.ScrollToItem(indices[i], 0.0f);
543 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
545 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
551 DALI_TEST_CHECK((moveCount == indices.size()));
556 tet_result(TET_FAIL);
559 Stage::GetCurrent().Remove(view);
562 static void UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep()
564 ToolkitTestApplication application;
566 // Create the ItemView actor
567 TestItemFactory factory;
568 ItemView view = ItemView::New(factory);
570 // Set the scroll distance step for the mouse wheel event to be 100.0f
571 view.SetMouseWheelScrollDistanceStep(100.0f);
573 // Check the scroll distance step is 100.0f
574 DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );