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.
19 #include <float.h> // for FLT_MAX
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit-test-suite-utils.h>
27 using namespace Toolkit;
31 const unsigned int TOTAL_ITEM_NUMBER = 200;
33 Vector3 DepthLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth)
35 float width = (layoutWidth / static_cast<float>(numberOfColumns + 1)) * 0.8f;
36 return Vector3(width, width, width);
39 float DepthLayoutBottomMarginFunction(float layoutHeight)
41 return layoutHeight * 0.25f;
44 float DepthLayoutColumnPositionFunction(unsigned int numberOfColumns, unsigned int columnNumber, const Vector3& itemSize, float layoutWidth)
46 float availableSpace = layoutWidth - itemSize.width * numberOfColumns;
47 float leftMargin = availableSpace / numberOfColumns * 0.5f;
48 float columnPosition = leftMargin + itemSize.width * 0.5f + columnNumber * (itemSize.width + availableSpace / numberOfColumns);
49 return columnPosition - layoutWidth * 0.5f;
53 static void Startup();
54 static void Cleanup();
56 // Implementation of ItemFactory for providing actors to ItemView
57 class TestItemFactory : public ItemFactory
63 * @param application class, stored as reference
69 public: // From ItemFactory
72 * Query the number of items available from the factory.
73 * The maximum available item has an ID of GetNumberOfItems() - 1.
75 virtual unsigned int GetNumberOfItems()
77 return TOTAL_ITEM_NUMBER;
81 * Create an Actor to represent a visible item.
83 * @return the created actor.
85 virtual Actor NewItem(unsigned int itemId)
87 // Create an test actor for this item
88 ImageActor actor = CreateSolidColorActor(Color::RED);
89 actor.SetSize(64.0f, 64.0f);
96 void (*tet_startup)() = Startup;
97 void (*tet_cleanup)() = Cleanup;
100 static void UtcDaliDepthLayoutNew();
101 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns();
102 static void UtcDaliDepthLayoutSetAndGetNumberOfRows();
103 static void UtcDaliDepthLayoutSetAndGetRowSpacing();
104 static void UtcDaliDepthLayoutSetAndGetTiltAngle();
105 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction();
106 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction();
107 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle();
108 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction();
109 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor();
110 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed();
111 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration();
112 static void UtcDaliDepthLayoutConstraintLeft();
113 static void UtcDaliDepthLayoutConstraintRight();
114 static void UtcDaliDepthLayoutConstraintUp();
115 static void UtcDaliDepthLayoutConstraintDown();
116 static void UtcDaliDepthLayoutGetScrollToPosition();
117 static void UtcDaliDepthLayoutScrollDirection();
120 POSITIVE_TC_IDX = 0x01,
124 // Add test functionality for all APIs in the class (Positive and Negative)
126 struct tet_testlist tet_testlist[] = {
127 { UtcDaliDepthLayoutNew, POSITIVE_TC_IDX },
128 { UtcDaliDepthLayoutScrollDirection, POSITIVE_TC_IDX },
129 { UtcDaliDepthLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
130 { UtcDaliDepthLayoutSetAndGetNumberOfRows, POSITIVE_TC_IDX },
131 { UtcDaliDepthLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
132 { UtcDaliDepthLayoutSetAndGetTiltAngle, POSITIVE_TC_IDX },
133 { UtcDaliDepthLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
134 { UtcDaliDepthLayoutSetAndGetBottomMarginFunction, POSITIVE_TC_IDX },
135 { UtcDaliDepthLayoutSetAndGetItemTiltAngle, POSITIVE_TC_IDX },
136 { UtcDaliDepthLayoutSetAndGetColumnPositionFunction, POSITIVE_TC_IDX },
137 { UtcDaliDepthLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
138 { UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
139 { UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
140 { UtcDaliDepthLayoutConstraintLeft, POSITIVE_TC_IDX },
141 { UtcDaliDepthLayoutConstraintRight, POSITIVE_TC_IDX },
142 { UtcDaliDepthLayoutConstraintUp, POSITIVE_TC_IDX },
143 { UtcDaliDepthLayoutConstraintDown, POSITIVE_TC_IDX },
144 { UtcDaliDepthLayoutGetScrollToPosition, POSITIVE_TC_IDX },
149 // Called only once before first test is run.
150 static void Startup()
154 // Called only once after last test is run
155 static void Cleanup()
159 static void UtcDaliDepthLayoutNew()
161 ToolkitTestApplication application;
163 // Create a depth layout
164 DepthLayoutPtr depthLayout = DepthLayout::New();
166 DALI_TEST_CHECK(depthLayout);
169 static void UtcDaliDepthLayoutSetAndGetNumberOfColumns()
171 ToolkitTestApplication application;
173 // Create a depth layout
174 DepthLayoutPtr depthLayout = DepthLayout::New();
176 // Set the number of columns
177 depthLayout->SetNumberOfColumns(5);
179 // Check whether we get the correct number of columns
180 DALI_TEST_CHECK(depthLayout->GetNumberOfColumns() == 5);
183 static void UtcDaliDepthLayoutSetAndGetNumberOfRows()
185 ToolkitTestApplication application;
187 // Create a depth layout
188 DepthLayoutPtr depthLayout = DepthLayout::New();
190 // Set the number of rows
191 depthLayout->SetNumberOfRows(15);
193 // Check whether we get the correct number of rows
194 DALI_TEST_CHECK(depthLayout->GetNumberOfRows() == 15);
197 static void UtcDaliDepthLayoutSetAndGetRowSpacing()
199 ToolkitTestApplication application;
201 // Create a depth layout
202 DepthLayoutPtr depthLayout = DepthLayout::New();
204 // Set the row spacing
205 depthLayout->SetRowSpacing(30.0f);
207 // Check whether we get the correct row spacing
208 DALI_TEST_EQUALS(depthLayout->GetRowSpacing(), 30.0f, TEST_LOCATION );
211 static void UtcDaliDepthLayoutSetAndGetTiltAngle()
213 ToolkitTestApplication application;
215 // Create a depth layout
216 DepthLayoutPtr depthLayout = DepthLayout::New();
218 // Set the tilt angle
219 depthLayout->SetTiltAngle(Degree(25.0f));
221 // Check whether we get the correct tilt angle
222 DALI_TEST_EQUALS(float(depthLayout->GetTiltAngle()), 25.0f, 0.001f, TEST_LOCATION );
225 static void UtcDaliDepthLayoutSetAndGetItemSizeFunction()
227 ToolkitTestApplication application;
229 // Create a depth layout
230 DepthLayoutPtr depthLayout = DepthLayout::New();
232 // Set the item size function
233 depthLayout->SetItemSizeFunction(DepthLayoutItemSizeFunction);
235 // Check whether we get the correct item size function
236 DALI_TEST_CHECK(depthLayout->GetItemSizeFunction() == DepthLayoutItemSizeFunction);
239 static void UtcDaliDepthLayoutSetAndGetBottomMarginFunction()
241 ToolkitTestApplication application;
243 // Create a depth layout
244 DepthLayoutPtr depthLayout = DepthLayout::New();
246 // Set the bottom margin function
247 depthLayout->SetBottomMarginFunction(DepthLayoutBottomMarginFunction);
249 // Check whether we get the correct bottom margin function
250 DALI_TEST_CHECK(depthLayout->GetBottomMarginFunction() == DepthLayoutBottomMarginFunction);
253 static void UtcDaliDepthLayoutSetAndGetItemTiltAngle()
255 ToolkitTestApplication application;
257 // Create a depth layout
258 DepthLayoutPtr depthLayout = DepthLayout::New();
260 // Set the item's tilt angle
261 depthLayout->SetItemTiltAngle(Degree(5.0f));
263 // Check whether we get the correct item's tilt angle
264 DALI_TEST_EQUALS(float(depthLayout->GetItemTiltAngle()), 5.0f, 0.001f, TEST_LOCATION );
267 static void UtcDaliDepthLayoutSetAndGetColumnPositionFunction()
269 ToolkitTestApplication application;
271 // Create a depth layout
272 DepthLayoutPtr depthLayout = DepthLayout::New();
274 // Set the column position function
275 depthLayout->SetColumnPositionFunction(DepthLayoutColumnPositionFunction);
277 // Check whether we get the correct column position function
278 DALI_TEST_CHECK(depthLayout->GetColumnPositionFunction() == DepthLayoutColumnPositionFunction);
281 static void UtcDaliDepthLayoutSetAndGetScrollSpeedFactor()
283 ToolkitTestApplication application;
285 // Create a depth layout
286 DepthLayoutPtr depthLayout = DepthLayout::New();
288 // Set the scroll speed factor
289 depthLayout->SetScrollSpeedFactor(0.05f);
291 // Check whether we get the correct scroll speed factor
292 DALI_TEST_EQUALS(depthLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
295 static void UtcDaliDepthLayoutSetAndGetMaximumSwipeSpeed()
297 ToolkitTestApplication application;
299 // Create a depth layout
300 DepthLayoutPtr depthLayout = DepthLayout::New();
302 // Set the maximum swipe speed
303 depthLayout->SetMaximumSwipeSpeed(50.0f);
305 // Check whether we get the correct maximum swipe speed
306 DALI_TEST_EQUALS(depthLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
309 static void UtcDaliDepthLayoutSetAndGetItemFlickAnimationDuration()
311 ToolkitTestApplication application;
313 // Create a depth layout
314 DepthLayoutPtr depthLayout = DepthLayout::New();
316 // Set the flick animaiton duration
317 depthLayout->SetItemFlickAnimationDuration(0.35f);
319 // Check whether we get the correct flick animaiton duration
320 DALI_TEST_EQUALS( depthLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
323 static void UtcDaliDepthLayoutConstraintLeft()
325 ToolkitTestApplication application;
327 // Create the ItemView actor
328 TestItemFactory factory;
329 ItemView view = ItemView::New(factory);
330 Vector3 vec(480.0f, 800.0f, 0.0f);
331 DepthLayoutPtr navigationLayout = DepthLayout::New();
332 navigationLayout->SetNumberOfColumns(6);
334 view.SetName("view actor");
335 view.AddLayout(*navigationLayout);
338 Stage::GetCurrent().Add(view);
339 navigationLayout->SetOrientation(ControlOrientation::Left);
340 view.ActivateLayout(0, vec, 0.0f);
342 application.SendNotification();
343 application.Render(0);
346 for(int i = 0; i < 10; ++i)
348 application.Render(16); // 60hz frames
351 // Confirm: we have actors in the view and they are positioned some distance from the origin.
352 int nonZeroCount = 0;
353 int elementsFound = 0;
354 for(unsigned int i = 0; i < 10; i++)
356 Actor testActor = view.GetItem(i);
360 Vector3 pos = testActor.GetCurrentPosition();
362 if (pos.LengthSquared() > 0.0f)
369 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
370 Stage::GetCurrent().Remove(view);
373 static void UtcDaliDepthLayoutConstraintRight()
375 ToolkitTestApplication application;
377 // Create the ItemView actor
378 TestItemFactory factory;
379 ItemView view = ItemView::New(factory);
380 Vector3 vec(480.0f, 800.0f, 0.0f);
381 DepthLayoutPtr navigationLayout = DepthLayout::New();
382 navigationLayout->SetNumberOfColumns(6);
384 view.SetName("view actor");
385 view.AddLayout(*navigationLayout);
388 Stage::GetCurrent().Add(view);
389 navigationLayout->SetOrientation(ControlOrientation::Right);
390 view.ActivateLayout(0, vec, 0.0f);
392 application.SendNotification();
393 application.Render(0);
396 for(int i = 0; i < 10; ++i)
398 application.Render(16); // 60hz frames
401 // Confirm: we have actors in the view and they are positioned some distance from the origin.
402 int nonZeroCount = 0;
403 int elementsFound = 0;
404 for(unsigned int i = 0; i < 10; i++)
406 Actor testActor = view.GetItem(i);
410 Vector3 pos = testActor.GetCurrentPosition();
412 if (pos.LengthSquared() > 0.0f)
419 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
420 Stage::GetCurrent().Remove(view);
423 static void UtcDaliDepthLayoutConstraintUp()
425 ToolkitTestApplication application;
427 // Create the ItemView actor
428 TestItemFactory factory;
429 ItemView view = ItemView::New(factory);
430 Vector3 vec(480.0f, 800.0f, 0.0f);
431 DepthLayoutPtr navigationLayout = DepthLayout::New();
432 navigationLayout->SetNumberOfColumns(6);
434 view.SetName("view actor");
435 view.AddLayout(*navigationLayout);
438 Stage::GetCurrent().Add(view);
439 navigationLayout->SetOrientation(ControlOrientation::Up);
440 view.ActivateLayout(0, vec, 0.0f);
442 application.SendNotification();
443 application.Render(0);
446 for(int i = 0; i < 10; ++i)
448 application.Render(16); // 60hz frames
451 // Confirm: we have actors in the view and they are positioned some distance from the origin.
452 int nonZeroCount = 0;
453 int elementsFound = 0;
454 for(unsigned int i = 0; i < 10; i++)
456 Actor testActor = view.GetItem(i);
460 Vector3 pos = testActor.GetCurrentPosition();
462 if (pos.LengthSquared() > 0.0f)
469 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
470 Stage::GetCurrent().Remove(view);
473 static void UtcDaliDepthLayoutConstraintDown()
475 ToolkitTestApplication application;
477 // Create the ItemView actor
478 TestItemFactory factory;
479 ItemView view = ItemView::New(factory);
480 Vector3 vec(480.0f, 800.0f, 0.0f);
481 DepthLayoutPtr navigationLayout = DepthLayout::New();
482 navigationLayout->SetNumberOfColumns(6);
484 view.SetName("view actor");
485 view.AddLayout(*navigationLayout);
488 Stage::GetCurrent().Add(view);
489 navigationLayout->SetOrientation(ControlOrientation::Down);
490 view.ActivateLayout(0, vec, 0.0f);
492 application.SendNotification();
493 application.Render(0);
496 for(int i = 0; i < 10; ++i)
498 application.Render(16); // 60hz frames
501 // Confirm: we have actors in the view and they are positioned some distance from the origin.
502 int nonZeroCount = 0;
503 int elementsFound = 0;
504 for(unsigned int i = 0; i < 10; i++)
506 Actor testActor = view.GetItem(i);
510 Vector3 pos = testActor.GetCurrentPosition();
512 if (pos.LengthSquared() > 0.0f)
519 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
520 Stage::GetCurrent().Remove(view);
523 static void UtcDaliDepthLayoutGetScrollToPosition()
525 ToolkitTestApplication application;
527 // Create the ItemView actor
528 TestItemFactory factory;
529 ItemView view = ItemView::New(factory);
530 Vector3 vec(480.0f, 800.0f, 0.0f);
531 DepthLayoutPtr layout = DepthLayout::New();
533 view.SetName("view actor");
534 view.AddLayout(*layout);
537 Stage::GetCurrent().Add(view);
538 layout->SetOrientation(ControlOrientation::Up);
539 view.ActivateLayout(0, vec, 0.0f);
541 application.SendNotification();
542 application.Render(0);
545 for(int i = 0; i < 10; ++i)
547 application.Render(16); // 60hz frames
550 // Confirm: we have actors in the view.
551 std::vector<unsigned int> indices;
552 for(unsigned int i = 0; i < 10; i++)
554 Actor testActor = view.GetItem(i);
557 indices.push_back(i);
563 if (!indices.empty())
565 const unsigned int firstTargetIndex = indices[indices.size()-1];
566 // scroll to last item
567 view.ScrollToItem(firstTargetIndex, 0.00f);
568 application.Render(16); // 60hz frames
570 std::size_t moveCount = 0;
571 for(std::size_t i = 0; i < indices.size(); i++)
573 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
574 view.ScrollToItem(indices[i], 0.0f);
576 application.Render(16); // 60hz frame
578 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
580 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
586 DALI_TEST_CHECK((moveCount == indices.size()));
591 tet_result(TET_FAIL);
594 Stage::GetCurrent().Remove(view);
597 static void UtcDaliDepthLayoutScrollDirection()
599 ToolkitTestApplication application;
601 // Create the ItemView actor
602 TestItemFactory factory;
603 ItemView view = ItemView::New(factory);
604 Vector3 vec(480.0f, 800.0f, 0.0f);
605 DepthLayoutPtr navigationLayout = DepthLayout::New();
607 view.SetName("view actor");
608 view.AddLayout(*navigationLayout);
611 Stage::GetCurrent().Add(view);
612 navigationLayout->SetOrientation(ControlOrientation::Left);
613 view.ActivateLayout(0, vec, 0.0f);
615 application.SendNotification();
616 application.Render(0);
618 ItemLayoutPtr layout = navigationLayout;
621 for(int i = 0; i < 10; ++i)
623 application.Render(16); // 60hz frames
626 navigationLayout->SetOrientation(ControlOrientation::Up);
627 view.ActivateLayout(0, vec, 0.0f);
628 application.SendNotification();
629 application.Render();
631 Degree deg = layout->GetScrollDirection();
632 DALI_TEST_CHECK(deg == 180.0f);
634 navigationLayout->SetOrientation(ControlOrientation::Down);
635 view.ActivateLayout(0, vec, 0.0f);
636 application.SendNotification();
637 application.Render();
639 deg = layout->GetScrollDirection();
640 DALI_TEST_CHECK((deg == 0.0f));
642 layout->SetOrientation(ControlOrientation::Left);
643 view.ActivateLayout(0, vec, 0.0f);
644 application.SendNotification();
645 application.Render();
647 deg = layout->GetScrollDirection();
648 DALI_TEST_CHECK(deg == 270.0f);
650 navigationLayout->SetOrientation(ControlOrientation::Right);
651 view.ActivateLayout(0, vec, 0.0f);
652 application.SendNotification();
653 application.Render();
655 deg = layout->GetScrollDirection();
656 DALI_TEST_CHECK(deg == 90.0f);
658 Stage::GetCurrent().Remove(view);