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.
18 #include <float.h> // for FLT_MAX
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali-toolkit-test-suite-utils.h>
26 using namespace Toolkit;
30 const unsigned int TOTAL_ITEM_NUMBER = 200;
32 Vector3 DepthLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth)
34 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * 0.8f;
35 return Vector3(width, width, width);
38 float DepthLayoutBottomMarginFunction(float layoutHeight)
40 return layoutHeight * 0.25f;
43 float DepthLayoutColumnPositionFunction(unsigned int numberOfColumns, unsigned int columnNumber, const Vector3& itemSize, float layoutWidth)
45 float availableSpace = layoutWidth - itemSize.width * numberOfColumns;
46 float leftMargin = availableSpace / numberOfColumns * 0.5f;
47 float columnPosition = leftMargin + itemSize.width * 0.5f + columnNumber * (itemSize.width + availableSpace / numberOfColumns);
48 return columnPosition - layoutWidth * 0.5f;
52 static void Startup();
53 static void Cleanup();
55 // Implementation of ItemFactory for providing actors to ItemView
56 class TestItemFactory : public ItemFactory
62 * @param application class, stored as reference
68 public: // From ItemFactory
71 * Query the number of items available from the factory.
72 * The maximum available item has an ID of GetNumberOfItems() - 1.
74 virtual unsigned int GetNumberOfItems()
76 return TOTAL_ITEM_NUMBER;
80 * Create an Actor to represent a visible item.
82 * @return the created actor.
84 virtual Actor NewItem(unsigned int itemId)
86 // Create an test actor for this item
87 ImageActor actor = CreateSolidColorActor(Color::RED);
88 actor.SetSize(64.0f, 64.0f);
95 void (*tet_startup)() = Startup;
96 void (*tet_cleanup)() = Cleanup;
99 static void UtcDaliDepthLayoutNew();
100 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns();
101 static void UtcDaliDepthLayoutSetAndGetNumberOfRows();
102 static void UtcDaliDepthLayoutSetAndGetRowSpacing();
103 static void UtcDaliDepthLayoutSetAndGetTiltAngle();
104 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction();
105 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction();
106 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle();
107 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction();
108 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor();
109 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed();
110 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration();
111 static void UtcDaliDepthLayoutConstraintLeft();
112 static void UtcDaliDepthLayoutConstraintRight();
113 static void UtcDaliDepthLayoutConstraintUp();
114 static void UtcDaliDepthLayoutConstraintDown();
115 static void UtcDaliDepthLayoutGetScrollToPosition();
116 static void UtcDaliDepthLayoutScrollDirection();
119 POSITIVE_TC_IDX = 0x01,
123 // Add test functionality for all APIs in the class (Positive and Negative)
125 struct tet_testlist tet_testlist[] = {
126 { UtcDaliDepthLayoutNew, POSITIVE_TC_IDX },
127 { UtcDaliDepthLayoutScrollDirection, POSITIVE_TC_IDX },
128 { UtcDaliDepthLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
129 { UtcDaliDepthLayoutSetAndGetNumberOfRows, POSITIVE_TC_IDX },
130 { UtcDaliDepthLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
131 { UtcDaliDepthLayoutSetAndGetTiltAngle, POSITIVE_TC_IDX },
132 { UtcDaliDepthLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
133 { UtcDaliDepthLayoutSetAndGetBottomMarginFunction, POSITIVE_TC_IDX },
134 { UtcDaliDepthLayoutSetAndGetItemTiltAngle, POSITIVE_TC_IDX },
135 { UtcDaliDepthLayoutSetAndGetColumnPositionFunction, POSITIVE_TC_IDX },
136 { UtcDaliDepthLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
137 { UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
138 { UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
139 { UtcDaliDepthLayoutConstraintLeft, POSITIVE_TC_IDX },
140 { UtcDaliDepthLayoutConstraintRight, POSITIVE_TC_IDX },
141 { UtcDaliDepthLayoutConstraintUp, POSITIVE_TC_IDX },
142 { UtcDaliDepthLayoutConstraintDown, POSITIVE_TC_IDX },
143 { UtcDaliDepthLayoutGetScrollToPosition, POSITIVE_TC_IDX },
148 // Called only once before first test is run.
149 static void Startup()
153 // Called only once after last test is run
154 static void Cleanup()
158 static void UtcDaliDepthLayoutNew()
160 ToolkitTestApplication application;
162 // Create a depth layout
163 DepthLayoutPtr depthLayout = DepthLayout::New();
165 DALI_TEST_CHECK(depthLayout);
168 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns()
170 ToolkitTestApplication application;
172 // Create a depth layout
173 DepthLayoutPtr depthLayout = DepthLayout::New();
175 // Set the number of columns
176 depthLayout->SetNumberOfColumns(5);
178 // Check whether we get the correct number of columns
179 DALI_TEST_CHECK(depthLayout->GetNumberOfColumns() == 5);
182 static void UtcDaliDepthLayoutSetAndGetNumberOfRows()
184 ToolkitTestApplication application;
186 // Create a depth layout
187 DepthLayoutPtr depthLayout = DepthLayout::New();
189 // Set the number of rows
190 depthLayout->SetNumberOfRows(15);
192 // Check whether we get the correct number of rows
193 DALI_TEST_CHECK(depthLayout->GetNumberOfRows() == 15);
196 static void UtcDaliDepthLayoutSetAndGetRowSpacing()
198 ToolkitTestApplication application;
200 // Create a depth layout
201 DepthLayoutPtr depthLayout = DepthLayout::New();
203 // Set the row spacing
204 depthLayout->SetRowSpacing(30.0f);
206 // Check whether we get the correct row spacing
207 DALI_TEST_EQUALS(depthLayout->GetRowSpacing(), 30.0f, TEST_LOCATION );
210 static void UtcDaliDepthLayoutSetAndGetTiltAngle()
212 ToolkitTestApplication application;
214 // Create a depth layout
215 DepthLayoutPtr depthLayout = DepthLayout::New();
217 // Set the tilt angle
218 depthLayout->SetTiltAngle(Degree(25.0f));
220 // Check whether we get the correct tilt angle
221 DALI_TEST_EQUALS(float(depthLayout->GetTiltAngle()), 25.0f, 0.001f, TEST_LOCATION );
224 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction()
226 ToolkitTestApplication application;
228 // Create a depth layout
229 DepthLayoutPtr depthLayout = DepthLayout::New();
231 // Set the item size function
232 depthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunction);
234 // Check whether we get the correct item size function
235 DALI_TEST_CHECK(depthLayout->GetItemSizeFunction() == DepthLayoutItemSizeFunction);
238 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction()
240 ToolkitTestApplication application;
242 // Create a depth layout
243 DepthLayoutPtr depthLayout = DepthLayout::New();
245 // Set the bottom margin function
246 depthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunction);
248 // Check whether we get the correct bottom margin function
249 DALI_TEST_CHECK(depthLayout->GetBottomMarginFunction() == DepthLayoutBottomMarginFunction);
252 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle()
254 ToolkitTestApplication application;
256 // Create a depth layout
257 DepthLayoutPtr depthLayout = DepthLayout::New();
259 // Set the item's tilt angle
260 depthLayout->SetItemTiltAngle(Degree(5.0f));
262 // Check whether we get the correct item's tilt angle
263 DALI_TEST_EQUALS(float(depthLayout->GetItemTiltAngle()), 5.0f, 0.001f, TEST_LOCATION );
266 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction()
268 ToolkitTestApplication application;
270 // Create a depth layout
271 DepthLayoutPtr depthLayout = DepthLayout::New();
273 // Set the column position function
274 depthLayout->SetColumnPositionFunction(DepthLayoutColumnPositionFunction);
276 // Check whether we get the correct column position function
277 DALI_TEST_CHECK(depthLayout->GetColumnPositionFunction() == DepthLayoutColumnPositionFunction);
280 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor()
282 ToolkitTestApplication application;
284 // Create a depth layout
285 DepthLayoutPtr depthLayout = DepthLayout::New();
287 // Set the scroll speed factor
288 depthLayout->SetScrollSpeedFactor(0.05f);
290 // Check whether we get the correct scroll speed factor
291 DALI_TEST_EQUALS(depthLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
294 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed()
296 ToolkitTestApplication application;
298 // Create a depth layout
299 DepthLayoutPtr depthLayout = DepthLayout::New();
301 // Set the maximum swipe speed
302 depthLayout->SetMaximumSwipeSpeed(50.0f);
304 // Check whether we get the correct maximum swipe speed
305 DALI_TEST_EQUALS(depthLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
308 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration()
310 ToolkitTestApplication application;
312 // Create a depth layout
313 DepthLayoutPtr depthLayout = DepthLayout::New();
315 // Set the flick animaiton duration
316 depthLayout->SetItemFlickAnimationDuration(0.35f);
318 // Check whether we get the correct flick animaiton duration
319 DALI_TEST_EQUALS( depthLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
322 static void UtcDaliDepthLayoutConstraintLeft()
324 ToolkitTestApplication application;
326 // Create the ItemView actor
327 TestItemFactory factory;
328 ItemView view = ItemView::New(factory);
329 Vector3 vec(480.0f, 800.0f, 0.0f);
330 DepthLayoutPtr navigationLayout = DepthLayout::New();
331 navigationLayout->SetNumberOfColumns(6);
333 view.SetName("view actor");
334 view.AddLayout(*navigationLayout);
337 Stage::GetCurrent().Add(view);
338 navigationLayout->SetOrientation(ControlOrientation::Left);
339 view.ActivateLayout(0, vec, 0.0f);
341 application.SendNotification();
342 application.Render(0);
345 for(int i = 0; i < 10; ++i)
347 application.Render(16); // 60hz frames
350 // Confirm: we have actors in the view and they are positioned some distance from the origin.
351 int nonZeroCount = 0;
352 int elementsFound = 0;
353 for(unsigned int i = 0; i < 10; i++)
355 Actor testActor = view.GetItem(i);
359 Vector3 pos = testActor.GetCurrentPosition();
361 if (pos.LengthSquared() > 0.0f)
368 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
369 Stage::GetCurrent().Remove(view);
372 static void UtcDaliDepthLayoutConstraintRight()
374 ToolkitTestApplication application;
376 // Create the ItemView actor
377 TestItemFactory factory;
378 ItemView view = ItemView::New(factory);
379 Vector3 vec(480.0f, 800.0f, 0.0f);
380 DepthLayoutPtr navigationLayout = DepthLayout::New();
381 navigationLayout->SetNumberOfColumns(6);
383 view.SetName("view actor");
384 view.AddLayout(*navigationLayout);
387 Stage::GetCurrent().Add(view);
388 navigationLayout->SetOrientation(ControlOrientation::Right);
389 view.ActivateLayout(0, vec, 0.0f);
391 application.SendNotification();
392 application.Render(0);
395 for(int i = 0; i < 10; ++i)
397 application.Render(16); // 60hz frames
400 // Confirm: we have actors in the view and they are positioned some distance from the origin.
401 int nonZeroCount = 0;
402 int elementsFound = 0;
403 for(unsigned int i = 0; i < 10; i++)
405 Actor testActor = view.GetItem(i);
409 Vector3 pos = testActor.GetCurrentPosition();
411 if (pos.LengthSquared() > 0.0f)
418 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
419 Stage::GetCurrent().Remove(view);
422 static void UtcDaliDepthLayoutConstraintUp()
424 ToolkitTestApplication application;
426 // Create the ItemView actor
427 TestItemFactory factory;
428 ItemView view = ItemView::New(factory);
429 Vector3 vec(480.0f, 800.0f, 0.0f);
430 DepthLayoutPtr navigationLayout = DepthLayout::New();
431 navigationLayout->SetNumberOfColumns(6);
433 view.SetName("view actor");
434 view.AddLayout(*navigationLayout);
437 Stage::GetCurrent().Add(view);
438 navigationLayout->SetOrientation(ControlOrientation::Up);
439 view.ActivateLayout(0, vec, 0.0f);
441 application.SendNotification();
442 application.Render(0);
445 for(int i = 0; i < 10; ++i)
447 application.Render(16); // 60hz frames
450 // Confirm: we have actors in the view and they are positioned some distance from the origin.
451 int nonZeroCount = 0;
452 int elementsFound = 0;
453 for(unsigned int i = 0; i < 10; i++)
455 Actor testActor = view.GetItem(i);
459 Vector3 pos = testActor.GetCurrentPosition();
461 if (pos.LengthSquared() > 0.0f)
468 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
469 Stage::GetCurrent().Remove(view);
472 static void UtcDaliDepthLayoutConstraintDown()
474 ToolkitTestApplication application;
476 // Create the ItemView actor
477 TestItemFactory factory;
478 ItemView view = ItemView::New(factory);
479 Vector3 vec(480.0f, 800.0f, 0.0f);
480 DepthLayoutPtr navigationLayout = DepthLayout::New();
481 navigationLayout->SetNumberOfColumns(6);
483 view.SetName("view actor");
484 view.AddLayout(*navigationLayout);
487 Stage::GetCurrent().Add(view);
488 navigationLayout->SetOrientation(ControlOrientation::Down);
489 view.ActivateLayout(0, vec, 0.0f);
491 application.SendNotification();
492 application.Render(0);
495 for(int i = 0; i < 10; ++i)
497 application.Render(16); // 60hz frames
500 // Confirm: we have actors in the view and they are positioned some distance from the origin.
501 int nonZeroCount = 0;
502 int elementsFound = 0;
503 for(unsigned int i = 0; i < 10; i++)
505 Actor testActor = view.GetItem(i);
509 Vector3 pos = testActor.GetCurrentPosition();
511 if (pos.LengthSquared() > 0.0f)
518 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
519 Stage::GetCurrent().Remove(view);
522 static void UtcDaliDepthLayoutGetScrollToPosition()
524 ToolkitTestApplication application;
526 // Create the ItemView actor
527 TestItemFactory factory;
528 ItemView view = ItemView::New(factory);
529 Vector3 vec(480.0f, 800.0f, 0.0f);
530 DepthLayoutPtr layout = DepthLayout::New();
532 view.SetName("view actor");
533 view.AddLayout(*layout);
536 Stage::GetCurrent().Add(view);
537 layout->SetOrientation(ControlOrientation::Up);
538 view.ActivateLayout(0, vec, 0.0f);
540 application.SendNotification();
541 application.Render(0);
544 for(int i = 0; i < 10; ++i)
546 application.Render(16); // 60hz frames
549 // Confirm: we have actors in the view.
550 std::vector<unsigned int> indices;
551 for(unsigned int i = 0; i < 10; i++)
553 Actor testActor = view.GetItem(i);
556 indices.push_back(i);
562 if (!indices.empty())
564 const unsigned int firstTargetIndex = indices[indices.size()-1];
565 // scroll to last item
566 view.ScrollToItem(firstTargetIndex, 0.00f);
567 application.Render(16); // 60hz frames
569 std::size_t moveCount = 0;
570 for(std::size_t i = 0; i < indices.size(); i++)
572 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
573 view.ScrollToItem(indices[i], 0.0f);
575 application.Render(16); // 60hz frame
577 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
579 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
585 DALI_TEST_CHECK((moveCount == indices.size()));
590 tet_result(TET_FAIL);
593 Stage::GetCurrent().Remove(view);
596 static void UtcDaliDepthLayoutScrollDirection()
598 ToolkitTestApplication application;
600 // Create the ItemView actor
601 TestItemFactory factory;
602 ItemView view = ItemView::New(factory);
603 Vector3 vec(480.0f, 800.0f, 0.0f);
604 DepthLayoutPtr navigationLayout = DepthLayout::New();
606 view.SetName("view actor");
607 view.AddLayout(*navigationLayout);
610 Stage::GetCurrent().Add(view);
611 navigationLayout->SetOrientation(ControlOrientation::Left);
612 view.ActivateLayout(0, vec, 0.0f);
614 application.SendNotification();
615 application.Render(0);
617 ItemLayoutPtr layout = navigationLayout;
620 for(int i = 0; i < 10; ++i)
622 application.Render(16); // 60hz frames
625 navigationLayout->SetOrientation(ControlOrientation::Up);
626 view.ActivateLayout(0, vec, 0.0f);
627 application.SendNotification();
628 application.Render();
630 Degree deg = layout->GetScrollDirection();
631 DALI_TEST_CHECK(deg == 180.0f);
633 navigationLayout->SetOrientation(ControlOrientation::Down);
634 view.ActivateLayout(0, vec, 0.0f);
635 application.SendNotification();
636 application.Render();
638 deg = layout->GetScrollDirection();
639 DALI_TEST_CHECK((deg == 0.0f));
641 layout->SetOrientation(ControlOrientation::Left);
642 view.ActivateLayout(0, vec, 0.0f);
643 application.SendNotification();
644 application.Render();
646 deg = layout->GetScrollDirection();
647 DALI_TEST_CHECK(deg == 270.0f);
649 navigationLayout->SetOrientation(ControlOrientation::Right);
650 view.ActivateLayout(0, vec, 0.0f);
651 application.SendNotification();
652 application.Render();
654 deg = layout->GetScrollDirection();
655 DALI_TEST_CHECK(deg == 90.0f);
657 Stage::GetCurrent().Remove(view);