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.
21 #include <float.h> // for FLT_MAX
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit-test-suite-utils.h>
30 using namespace Toolkit;
34 const unsigned int TOTAL_ITEM_NUMBER = 100;
35 const char* TEST_IMAGE_FILE_NAME = DALI_IMAGE_DIR "gallery_image_01.jpg";
37 static bool gObjectCreatedCallBackCalled;
39 static void TestCallback(BaseHandle handle)
41 gObjectCreatedCallBackCalled = true;
46 static void Startup();
47 static void Cleanup();
50 void (*tet_startup)() = Startup;
51 void (*tet_cleanup)() = Cleanup;
54 static void UtcDaliItemViewNew();
55 static void UtcDaliItemViewDownCast();
56 static void UtcDaliItemViewAddAndGetLayout();
57 static void UtcDaliItemViewAddAndRemoveLayout();
58 static void UtcDaliItemViewActivateLayoutAndGetActiveLayout();
59 static void UtcDaliItemViewDeactivateCurrentLayout();
60 static void UtcDaliItemViewGetItemAndGetItemId();
61 static void UtcDaliItemViewRemoveItem();
62 static void UtcDaliItemViewGetCurrentLayoutPosition();
63 static void UtcDaliItemViewSetAndGetMinimumSwipeSpeed();
64 static void UtcDaliItemViewSetAndGetMinimumSwipeDistance();
65 static void UtcDaliItemViewSetAndGetAnchoring();
66 static void UtcDaliItemViewSetAndGetAnchoringDuration();
67 static void UtcDaliItemViewSetAndGetRefreshInterval();
68 static void UtcDaliItemViewScrollToItem();
69 static void UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep();
72 POSITIVE_TC_IDX = 0x01,
76 // Add test functionality for all APIs in the class (Positive and Negative)
78 struct tet_testlist tet_testlist[] = {
79 { UtcDaliItemViewNew, POSITIVE_TC_IDX },
80 { UtcDaliItemViewDownCast, POSITIVE_TC_IDX },
81 { UtcDaliItemViewAddAndGetLayout, POSITIVE_TC_IDX },
82 { UtcDaliItemViewAddAndRemoveLayout, POSITIVE_TC_IDX },
83 { UtcDaliItemViewActivateLayoutAndGetActiveLayout, POSITIVE_TC_IDX },
84 { UtcDaliItemViewDeactivateCurrentLayout, POSITIVE_TC_IDX },
85 { UtcDaliItemViewGetItemAndGetItemId, POSITIVE_TC_IDX },
86 { UtcDaliItemViewRemoveItem, POSITIVE_TC_IDX },
87 { UtcDaliItemViewGetCurrentLayoutPosition, POSITIVE_TC_IDX },
88 { UtcDaliItemViewSetAndGetMinimumSwipeSpeed, POSITIVE_TC_IDX },
89 { UtcDaliItemViewSetAndGetMinimumSwipeDistance, POSITIVE_TC_IDX },
90 { UtcDaliItemViewSetAndGetAnchoring, POSITIVE_TC_IDX },
91 { UtcDaliItemViewSetAndGetAnchoringDuration, POSITIVE_TC_IDX },
92 { UtcDaliItemViewSetAndGetRefreshInterval, POSITIVE_TC_IDX },
93 { UtcDaliItemViewScrollToItem, POSITIVE_TC_IDX },
94 { UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep, POSITIVE_TC_IDX },
99 // Called only once before first test is run.
100 static void Startup()
104 // Called only once after last test is run
105 static void Cleanup()
109 // Implementation of ItemFactory for providing actors to ItemView
110 class TestItemFactory : public ItemFactory
116 * @param application class, stored as reference
122 public: // From ItemFactory
125 * Query the number of items available from the factory.
126 * The maximum available item has an ID of GetNumberOfItems() - 1.
128 virtual unsigned int GetNumberOfItems()
130 return TOTAL_ITEM_NUMBER;
134 * Create an Actor to represent a visible item.
136 * @return the created actor.
138 virtual Actor NewItem(unsigned int itemId)
140 // Create an image actor for this item
141 Image image = Image::New( TEST_IMAGE_FILE_NAME );
142 Actor actor = ImageActor::New(image);
148 static void UtcDaliItemViewNew()
150 ToolkitTestApplication application;
152 // Create the ItemView actor
153 TestItemFactory factory;
154 ItemView view = ItemView::New(factory);
156 DALI_TEST_CHECK(view);
158 //Additional check to ensure object is created by checking if it's registered
159 ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
160 DALI_TEST_CHECK( registry );
162 gObjectCreatedCallBackCalled = false;
163 registry.ObjectCreatedSignal().Connect(&TestCallback);
165 TestItemFactory factory;
166 ItemView view = ItemView::New(factory);
168 DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
171 static void UtcDaliItemViewDownCast()
173 ToolkitTestApplication application;
175 // Create the ItemView actor
176 TestItemFactory factory;
177 const ItemView itemViewConst = ItemView::New(factory);
178 ItemView itemView(itemViewConst);
180 BaseHandle handle(itemView);
182 ItemView newItemView = ItemView::DownCast( handle );
183 DALI_TEST_CHECK( itemView );
184 DALI_TEST_CHECK( newItemView == itemView );
187 static void UtcDaliItemViewAddAndGetLayout()
189 ToolkitTestApplication application;
191 // Create the ItemView actor
192 TestItemFactory factory;
193 ItemView view = ItemView::New(factory);
195 // Create a grid layout and add it to ItemView
196 GridLayoutPtr gridLayout = GridLayout::New();
197 view.AddLayout(*gridLayout);
199 // As we have added one layout, check the number of layout is now 1
200 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
202 // Create a depth layout and add it to ItemView
203 DepthLayoutPtr depthLayout = DepthLayout::New();
204 view.AddLayout(*depthLayout);
206 // As we have added another layout, check the number of layout is now 2
207 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
209 // Create a spiral layout and add it to ItemView
210 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
211 view.AddLayout(*spiralLayout);
213 // As we have added another layout, check the number of layout is now 3
214 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
216 // Check we are getting the correct layout from ItemView
217 DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
218 DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
219 DALI_TEST_CHECK(view.GetLayout(2) == spiralLayout);
222 static void UtcDaliItemViewAddAndRemoveLayout()
224 ToolkitTestApplication application;
226 // Create the ItemView actor
227 TestItemFactory factory;
228 ItemView view = ItemView::New(factory);
230 // Create a grid layout and add it to ItemView
231 GridLayoutPtr gridLayout = GridLayout::New();
232 view.AddLayout(*gridLayout);
234 // As we have added one layout, check the number of layout is now 1
235 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
237 // Create a depth layout and add it to ItemView
238 DepthLayoutPtr depthLayout = DepthLayout::New();
239 view.AddLayout(*depthLayout);
241 // As we have added another layout, check the number of layout is now 2
242 DALI_TEST_CHECK(view.GetLayoutCount() == 2);
244 // Check we are getting the correct layout from ItemView
245 DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
246 DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
248 // Remove the grid layout
249 view.RemoveLayout(0);
251 // As we have removed the grid layout, check the number of layout is now 1
252 DALI_TEST_CHECK(view.GetLayoutCount() == 1);
254 // Check we are getting the correct layout from ItemView
255 DALI_TEST_CHECK(view.GetLayout(0) == depthLayout);
257 // Remove the depth layout
258 view.RemoveLayout(0);
260 // As we also removed the depth layout, check the number of layout is now 0
261 DALI_TEST_CHECK(view.GetLayoutCount() == 0);
264 static void UtcDaliItemViewActivateLayoutAndGetActiveLayout()
266 ToolkitTestApplication application;
268 // Create the ItemView actor
269 TestItemFactory factory;
270 ItemView view = ItemView::New(factory);
272 // Create a grid layout and add it to ItemView
273 GridLayoutPtr gridLayout = GridLayout::New();
274 view.AddLayout(*gridLayout);
276 // Create a depth layout and add it to ItemView
277 DepthLayoutPtr depthLayout = DepthLayout::New();
278 view.AddLayout(*depthLayout);
280 // Create a spiral layout and add it to ItemView
281 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
282 view.AddLayout(*spiralLayout);
284 // As we have added three layouts, check the number of layout is now 3
285 DALI_TEST_CHECK(view.GetLayoutCount() == 3);
287 // Check there is no active layout at the moment
288 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
290 // Activate the depth layout
291 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
292 view.ActivateLayout(1, stageSize, 0.5f);
294 // Check the current active layout is the depth layout
295 DALI_TEST_CHECK(view.GetActiveLayout() == depthLayout);
297 // Activate the grid layout
298 view.ActivateLayout(0, stageSize, 0.5f);
300 // Check the current active layout is the grid layout
301 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
303 // Activate the spiral layout
304 view.ActivateLayout(2, stageSize, 0.5f);
306 // Check the current active layout is the spiral layout
307 DALI_TEST_CHECK(view.GetActiveLayout() == spiralLayout);
310 static void UtcDaliItemViewDeactivateCurrentLayout()
312 ToolkitTestApplication application;
314 // Create the ItemView actor
315 TestItemFactory factory;
316 ItemView view = ItemView::New(factory);
318 // Create a grid layout and add it to ItemView
319 GridLayoutPtr gridLayout = GridLayout::New();
320 view.AddLayout(*gridLayout);
322 // Check there is no active layout at the moment
323 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
325 // Activate the grid layout
326 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
327 view.ActivateLayout(0, stageSize, 0.5f);
329 // Check the current active layout is the grid layout
330 DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
332 // Deactivate the current layout
333 view.DeactivateCurrentLayout();
335 // Check there is no active layout at the moment
336 DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
339 static void UtcDaliItemViewGetItemAndGetItemId()
341 ToolkitTestApplication application;
343 // Create the ItemView actor
344 TestItemFactory factory;
345 ItemView view = ItemView::New(factory);
347 // Create a grid layout and add it to ItemView
348 GridLayoutPtr gridLayout = GridLayout::New();
349 view.AddLayout(*gridLayout);
351 // Activate the grid layout so that the items will be created and added to ItemView
352 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
353 view.ActivateLayout(0, stageSize, 0.5f);
355 // Get the item given the item ID
356 Actor itemActor = view.GetItem(2);
358 // Check we are getting the correct Item ID given the specified actor
359 DALI_TEST_CHECK(view.GetItemId(itemActor) == 2);
362 static void UtcDaliItemViewRemoveItem()
364 ToolkitTestApplication application;
366 // Create the ItemView actor
367 TestItemFactory factory;
368 ItemView view = ItemView::New(factory);
370 // Create a grid layout and add it to ItemView
371 GridLayoutPtr gridLayout = GridLayout::New();
372 view.AddLayout(*gridLayout);
374 // Activate the grid layout so that the items will be created and added to ItemView
375 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
376 view.ActivateLayout(0, stageSize, 0.5f);
378 // Get the item given the item ID 2 and 3
379 Actor oldItemActorID2 = view.GetItem(2);
380 Actor oldItemActorID3 = view.GetItem(3);
382 // Remove the item with ID 2
383 view.RemoveItem(2, 0.0f);
385 // Get the new item given the item ID 2
386 Actor newItemActorID2 = view.GetItem(2);
388 // Check the original item with item ID 2 was deleted and now item ID 2 represents the original item with ID 3
389 DALI_TEST_CHECK(view.GetItemId(newItemActorID2) == 2);
390 DALI_TEST_CHECK(oldItemActorID2 != newItemActorID2);
391 DALI_TEST_CHECK(newItemActorID2 = oldItemActorID3);
394 static void UtcDaliItemViewGetCurrentLayoutPosition()
396 ToolkitTestApplication application;
398 // Create the ItemView actor
399 TestItemFactory factory;
400 ItemView view = ItemView::New(factory);
402 // Create a grid layout and add it to ItemView
403 GridLayoutPtr gridLayout = GridLayout::New();
404 view.AddLayout(*gridLayout);
406 // Activate the grid layout so that the items will be created and added to ItemView
407 Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
408 view.ActivateLayout(0, stageSize, 0.0f);
410 // Check the current layout position for the 10th items is 9.0f
411 DALI_TEST_EQUALS(view.GetCurrentLayoutPosition(9), 9.0f, TEST_LOCATION );
414 static void UtcDaliItemViewSetAndGetMinimumSwipeSpeed()
416 ToolkitTestApplication application;
418 // Create the ItemView actor
419 TestItemFactory factory;
420 ItemView view = ItemView::New(factory);
422 // Set the minimum swipe speed to be 1.5f
423 view.SetMinimumSwipeSpeed(1.5f);
425 // Check the minimum swipe speed is 1.5f
426 DALI_TEST_EQUALS(view.GetMinimumSwipeSpeed(), 1.5f, TEST_LOCATION );
429 static void UtcDaliItemViewSetAndGetMinimumSwipeDistance()
431 ToolkitTestApplication application;
433 // Create the ItemView actor
434 TestItemFactory factory;
435 ItemView view = ItemView::New(factory);
437 // Set the minimum swipe distance to be 2.5f
438 view.SetMinimumSwipeDistance(2.5f);
440 // Check the minimum swipe distance is 2.5f
441 DALI_TEST_EQUALS(view.GetMinimumSwipeDistance(), 2.5f, TEST_LOCATION );
444 static void UtcDaliItemViewSetAndGetAnchoring()
446 ToolkitTestApplication application;
448 // Create the ItemView actor
449 TestItemFactory factory;
450 ItemView view = ItemView::New(factory);
452 // Disable the anchor animation
453 view.SetAnchoring(false);
455 // Check the anchor animation is disabled
456 DALI_TEST_CHECK(view.GetAnchoring() == false);
459 static void UtcDaliItemViewSetAndGetAnchoringDuration()
461 ToolkitTestApplication application;
463 // Create the ItemView actor
464 TestItemFactory factory;
465 ItemView view = ItemView::New(factory);
467 // Set the duration of anchor animation to be 1.5f
468 view.SetAnchoringDuration(1.5f);
470 // Check the duration of anchor animation is 1.5f
471 DALI_TEST_EQUALS(view.GetAnchoringDuration(), 1.5f, TEST_LOCATION );
474 static void UtcDaliItemViewSetAndGetRefreshInterval()
476 ToolkitTestApplication application;
478 // Create the ItemView actor
479 TestItemFactory factory;
480 ItemView view = ItemView::New(factory);
482 // Set the interval between refreshes to be 20
483 view.SetRefreshInterval(20);
485 // Check the interval between refreshes is 20
486 DALI_TEST_CHECK(view.GetRefreshInterval() == 20);
489 static void UtcDaliItemViewScrollToItem()
491 ToolkitTestApplication application;
493 // Create the ItemView actor
494 TestItemFactory factory;
495 ItemView view = ItemView::New(factory);
496 Vector3 vec(480.0f, 800.0f, 0.0f);
497 GridLayoutPtr layout = GridLayout::New();
499 view.SetName("view actor");
500 view.AddLayout(*layout);
503 Stage::GetCurrent().Add(view);
504 layout->SetOrientation(ControlOrientation::Up);
505 view.ActivateLayout(0, vec, 0.0f);
507 application.SendNotification();
508 application.Render(0);
511 for(int i = 0; i < 10; ++i)
513 application.Render(16); // 60hz frames
516 // Confirm: we have actors in the view.
517 std::vector<unsigned int> indices;
518 for(unsigned int i = 0; i < 10; i++)
520 Actor testActor = view.GetItem(i);
523 indices.push_back(i);
529 if (!indices.empty())
531 const unsigned int firstTargetIndex = indices[indices.size()-1];
532 // scroll to last item
533 view.ScrollToItem(firstTargetIndex, 0.00f);
534 for(int i = 0; i < 10; ++i)
536 application.Render(16); // 60hz frames
539 std::size_t moveCount = 0;
540 for(std::size_t i = 0; i < indices.size(); i++)
542 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
543 view.ScrollToItem(indices[i], 0.0f);
544 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
546 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
552 DALI_TEST_CHECK((moveCount == indices.size()));
557 tet_result(TET_FAIL);
560 Stage::GetCurrent().Remove(view);
563 static void UtcDaliItemViewSetAndGetMouseWheelScrollDistanceStep()
565 ToolkitTestApplication application;
567 // Create the ItemView actor
568 TestItemFactory factory;
569 ItemView view = ItemView::New(factory);
571 // Set the scroll distance step for the mouse wheel event to be 100.0f
572 view.SetMouseWheelScrollDistanceStep(100.0f);
574 // Check the scroll distance step is 100.0f
575 DALI_TEST_EQUALS(view.GetMouseWheelScrollDistanceStep(), 100.0f, TEST_LOCATION );