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/public-api/dali-core.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
26 #include <dali-toolkit-test-suite-utils.h>
29 using namespace Toolkit;
33 const unsigned int TOTAL_ITEM_NUMBER = 200;
36 static void Startup();
37 static void Cleanup();
39 // Implementation of ItemFactory for providing actors to ItemView
40 class TestItemFactory : public ItemFactory
46 * @param application class, stored as reference
52 public: // From ItemFactory
55 * Query the number of items available from the factory.
56 * The maximum available item has an ID of GetNumberOfItems() - 1.
58 virtual unsigned int GetNumberOfItems()
60 return TOTAL_ITEM_NUMBER;
64 * Create an Actor to represent a visible item.
66 * @return the created actor.
68 virtual Actor NewItem(unsigned int itemId)
70 // Create an test actor for this item
71 ImageActor actor = CreateSolidColorActor(Color::RED);
72 actor.SetSize(64.0f, 64.0f);
79 void (*tet_startup)() = Startup;
80 void (*tet_cleanup)() = Cleanup;
83 static void UtcDaliNavigationLayoutNew();
84 static void UtcDaliNavigationLayoutColumns();
85 static void UtcDaliNavigationLayoutSetGetOrientation();
86 static void UtcDaliNavigationLayoutTestConstraintLeft();
87 static void UtcDaliNavigationLayoutTestConstraintRight();
88 static void UtcDaliNavigationLayoutTestConstraintUp();
89 static void UtcDaliNavigationLayoutTestConstraintDown();
90 static void UtcDaliNavigationLayoutScrollDirection();
91 static void UtcDaliNavigationLayoutSetGetColumnSpacing();
92 static void UtcDaliNavigationLayoutSetGetTopMargin();
93 static void UtcDaliNavigationLayoutSetGetBottomMargin();
94 static void UtcDaliNavigationLayoutSetGetScrollSpeedFactor();
95 static void UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed();
96 static void UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration();
97 static void UtcDaliNavigationLayoutGetScrollToPosition();
101 POSITIVE_TC_IDX = 0x01,
105 // Add test functionality for all APIs in the class (Positive and Negative)
107 struct tet_testlist tet_testlist[] = {
108 { UtcDaliNavigationLayoutNew, POSITIVE_TC_IDX },
109 { UtcDaliNavigationLayoutColumns, POSITIVE_TC_IDX },
110 { UtcDaliNavigationLayoutSetGetOrientation, POSITIVE_TC_IDX },
111 { UtcDaliNavigationLayoutTestConstraintLeft, POSITIVE_TC_IDX },
112 { UtcDaliNavigationLayoutTestConstraintRight, POSITIVE_TC_IDX },
113 { UtcDaliNavigationLayoutTestConstraintUp, POSITIVE_TC_IDX },
114 { UtcDaliNavigationLayoutTestConstraintDown, POSITIVE_TC_IDX },
115 { UtcDaliNavigationLayoutScrollDirection, POSITIVE_TC_IDX },
116 { UtcDaliNavigationLayoutSetGetColumnSpacing, POSITIVE_TC_IDX },
117 { UtcDaliNavigationLayoutSetGetTopMargin, POSITIVE_TC_IDX },
118 { UtcDaliNavigationLayoutSetGetBottomMargin, POSITIVE_TC_IDX },
119 { UtcDaliNavigationLayoutSetGetScrollSpeedFactor, POSITIVE_TC_IDX },
120 { UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
121 { UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
122 { UtcDaliNavigationLayoutGetScrollToPosition, POSITIVE_TC_IDX },
127 // Called only once before first test is run.
128 static void Startup()
132 // Called only once after last test is run
133 static void Cleanup()
138 // Positive test case for a method
139 static void UtcDaliNavigationLayoutNew()
141 ToolkitTestApplication application;
143 // Create a navigation layout
144 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
145 navigationLayout->SetNumberOfColumns(6);
146 DALI_TEST_CHECK(navigationLayout);
149 static void UtcDaliNavigationLayoutColumns()
151 ToolkitTestApplication application;
152 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
154 navigationLayout->SetNumberOfColumns(6);
155 // Check whether we get the correct number of columns
156 DALI_TEST_CHECK(navigationLayout->GetNumberOfColumns() == 6);
159 static void UtcDaliNavigationLayoutSetGetOrientation()
161 ToolkitTestApplication application;
162 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
164 navigationLayout->SetNumberOfColumns(6);
165 navigationLayout->SetOrientation(ControlOrientation::Right);
166 DALI_TEST_CHECK(navigationLayout->GetOrientation() == ControlOrientation::Right);
169 static void UtcDaliNavigationLayoutTestConstraintLeft()
171 ToolkitTestApplication application;
173 // Create the ItemView actor
174 TestItemFactory factory;
175 ItemView view = ItemView::New(factory);
176 Vector3 vec(480.0f, 800.0f, 0.0f);
177 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
178 navigationLayout->SetNumberOfColumns(6);
180 view.SetName("view actor");
181 view.AddLayout(*navigationLayout);
184 Stage::GetCurrent().Add(view);
185 navigationLayout->SetOrientation(ControlOrientation::Left);
186 view.ActivateLayout(0, vec, 0.0f);
188 application.SendNotification();
189 application.Render(0);
192 for(int i = 0; i < 10; ++i)
194 application.Render(16); // 60hz frames
197 // Confirm: we have actors in the view and all of them is positioned at X = 0
198 // and the series is monotonely decreasing.
199 int nonZeroXCount = 0;
200 int elementsFound = 0;
201 int wrongDirectionCount = 0;
202 float prevY = FLT_MAX;
203 for(unsigned int i = 0; i < 10; i++)
205 Actor testActor = view.GetItem(i);
209 Vector3 pos = testActor.GetCurrentPosition();
218 wrongDirectionCount++;
225 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroXCount == 0) && (wrongDirectionCount == 0));
226 Stage::GetCurrent().Remove(view);
229 static void UtcDaliNavigationLayoutTestConstraintRight()
231 ToolkitTestApplication application;
233 // Create the ItemView actor
234 TestItemFactory factory;
235 ItemView view = ItemView::New(factory);
236 Vector3 vec(480.0f, 800.0f, 0.0f);
237 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
238 navigationLayout->SetNumberOfColumns(6);
240 view.SetName("view actor");
241 view.AddLayout(*navigationLayout);
244 Stage::GetCurrent().Add(view);
245 navigationLayout->SetOrientation(ControlOrientation::Right);
246 view.ActivateLayout(0, vec, 0.0f);
248 application.SendNotification();
249 application.Render(0);
252 for(int i = 0; i < 10; ++i)
254 application.Render(16); // 60hz frames
257 // Confirm: we have actors in the view and all of them is positioned at X = 0
258 // and the series is monotonely increasing.
259 int nonZeroXCount = 0;
260 int elementsFound = 0;
261 int wrongDirectionCount = 0;
262 float prevY = -FLT_MAX;
263 for(unsigned int i = 0; i < 10; i++)
265 Actor testActor = view.GetItem(i);
269 Vector3 pos = testActor.GetCurrentPosition();
278 wrongDirectionCount++;
285 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroXCount == 0) && (wrongDirectionCount == 0));
286 Stage::GetCurrent().Remove(view);
289 static void UtcDaliNavigationLayoutTestConstraintUp()
291 ToolkitTestApplication application;
293 // Create the ItemView actor
294 TestItemFactory factory;
295 ItemView view = ItemView::New(factory);
296 Vector3 vec(480.0f, 800.0f, 0.0f);
297 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
298 navigationLayout->SetNumberOfColumns(6);
300 view.SetName("view actor");
301 view.AddLayout(*navigationLayout);
304 Stage::GetCurrent().Add(view);
305 navigationLayout->SetOrientation(ControlOrientation::Up);
306 view.ActivateLayout(0, vec, 0.0f);
308 application.SendNotification();
309 application.Render(0);
312 for(int i = 0; i < 10; ++i)
314 application.Render(16); // 60hz frames
317 // Confirm: we have actors in the view and all of them is positioned at X = 0
318 // and the series is monotonely decreasing.
319 int nonZeroYCount = 0;
320 int elementsFound = 0;
321 int wrongDirectionCount = 0;
322 float prevX = -FLT_MAX;
323 for(unsigned int i = 0; i < 10; i++)
325 Actor testActor = view.GetItem(i);
329 Vector3 pos = testActor.GetCurrentPosition();
338 wrongDirectionCount++;
345 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroYCount == 0) && (wrongDirectionCount == 0));
346 Stage::GetCurrent().Remove(view);
349 static void UtcDaliNavigationLayoutTestConstraintDown()
351 ToolkitTestApplication application;
353 // Create the ItemView actor
354 TestItemFactory factory;
355 ItemView view = ItemView::New(factory);
356 Vector3 vec(480.0f, 800.0f, 0.0f);
357 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
358 navigationLayout->SetNumberOfColumns(6);
360 view.SetName("view actor");
361 view.AddLayout(*navigationLayout);
364 Stage::GetCurrent().Add(view);
365 navigationLayout->SetOrientation(ControlOrientation::Down);
366 view.ActivateLayout(0, vec, 0.0f);
368 application.SendNotification();
369 application.Render(0);
372 for(int i = 0; i < 10; ++i)
374 application.Render(16); // 60hz frames
377 // Confirm: we have actors in the view and all of them is positioned at X = 0
378 // and the series is monotonely decreasing.
379 int nonZeroYCount = 0;
380 int elementsFound = 0;
381 int wrongDirectionCount = 0;
382 float prevX = FLT_MAX;
383 for(unsigned int i = 0; i < 10; i++)
385 Actor testActor = view.GetItem(i);
389 Vector3 pos = testActor.GetCurrentPosition();
398 wrongDirectionCount++;
405 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroYCount == 0) && (wrongDirectionCount == 0));
406 Stage::GetCurrent().Remove(view);
410 static void UtcDaliNavigationLayoutScrollDirection()
412 ToolkitTestApplication application;
414 // Create the ItemView actor
415 TestItemFactory factory;
416 ItemView view = ItemView::New(factory);
417 Vector3 vec(480.0f, 800.0f, 0.0f);
418 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
419 navigationLayout->SetNumberOfColumns(6);
421 view.SetName("view actor");
422 view.AddLayout(*navigationLayout);
425 Stage::GetCurrent().Add(view);
426 navigationLayout->SetOrientation(ControlOrientation::Left);
427 view.ActivateLayout(0, vec, 0.0f);
429 application.SendNotification();
430 application.Render(0);
432 ItemLayoutPtr layout = navigationLayout;
435 for(int i = 0; i < 10; ++i)
437 application.Render(16); // 60hz frames
440 navigationLayout->SetOrientation(ControlOrientation::Up);
441 view.ActivateLayout(0, vec, 0.0f);
442 application.SendNotification();
443 application.Render();
445 Degree deg = layout->GetScrollDirection();
446 DALI_TEST_CHECK(deg == (180.0f - 45.0f));
448 navigationLayout->SetOrientation(ControlOrientation::Down);
449 view.ActivateLayout(0, vec, 0.0f);
450 application.SendNotification();
451 application.Render();
453 deg = layout->GetScrollDirection();
454 DALI_TEST_CHECK((deg == -45.0f));
456 layout->SetOrientation(ControlOrientation::Left);
457 view.ActivateLayout(0, vec, 0.0f);
458 application.SendNotification();
459 application.Render();
461 deg = layout->GetScrollDirection();
462 DALI_TEST_CHECK(deg == (270.0f - 45.0f));
464 navigationLayout->SetOrientation(ControlOrientation::Right);
465 view.ActivateLayout(0, vec, 0.0f);
466 application.SendNotification();
467 application.Render();
469 deg = layout->GetScrollDirection();
470 DALI_TEST_CHECK(deg == (90.0f - 45.0f));
472 Stage::GetCurrent().Remove(view);
475 static void UtcDaliNavigationLayoutSetGetColumnSpacing()
477 ToolkitTestApplication application;
478 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
479 const float testValue = 11.0f;
481 navigationLayout->SetNumberOfColumns(6);
482 navigationLayout->SetColumnSpacing(testValue);
483 DALI_TEST_CHECK(navigationLayout->GetColumnSpacing() == testValue);
486 static void UtcDaliNavigationLayoutSetGetTopMargin()
488 ToolkitTestApplication application;
489 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
490 const float testValue = 11.0f;
492 navigationLayout->SetNumberOfColumns(6);
493 navigationLayout->SetTopMargin(testValue);
494 DALI_TEST_CHECK(navigationLayout->GetTopMargin() == testValue);
497 static void UtcDaliNavigationLayoutSetGetBottomMargin()
499 ToolkitTestApplication application;
500 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
501 const float testValue = 12.0f;
503 navigationLayout->SetNumberOfColumns(6);
504 navigationLayout->SetBottomMargin(testValue);
505 DALI_TEST_CHECK(navigationLayout->GetBottomMargin() == testValue);
508 static void UtcDaliNavigationLayoutSetGetScrollSpeedFactor()
510 ToolkitTestApplication application;
511 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
512 const float testValue = 15.0f;
514 navigationLayout->SetNumberOfColumns(6);
515 navigationLayout->SetScrollSpeedFactor(testValue);
516 DALI_TEST_CHECK(navigationLayout->GetScrollSpeedFactor() == testValue);
519 static void UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed()
521 ToolkitTestApplication application;
522 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
523 const float testValue = 10.0f;
525 navigationLayout->SetNumberOfColumns(6);
526 navigationLayout->SetMaximumSwipeSpeed(testValue);
527 DALI_TEST_CHECK(navigationLayout->GetMaximumSwipeSpeed() == testValue);
530 static void UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration()
532 ToolkitTestApplication application;
534 // Create a navigation layout
535 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
537 // Set the flick animaiton duration
538 navigationLayout->SetItemFlickAnimationDuration(0.35f);
540 // Check whether we get the correct flick animaiton duration
541 DALI_TEST_EQUALS( navigationLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
544 static void UtcDaliNavigationLayoutGetScrollToPosition()
546 ToolkitTestApplication application;
548 // Create the ItemView actor
549 TestItemFactory factory;
550 ItemView view = ItemView::New(factory);
551 Vector3 vec(480.0f, 800.0f, 0.0f);
552 NavigationLayoutPtr layout = NavigationLayout::New();
554 view.SetName("view actor");
555 view.AddLayout(*layout);
558 Stage::GetCurrent().Add(view);
559 layout->SetOrientation(ControlOrientation::Up);
560 view.ActivateLayout(0, vec, 0.0f);
562 application.SendNotification();
563 application.Render(0);
566 for(int i = 0; i < 10; ++i)
568 application.Render(16); // 60hz frames
571 // Confirm: we have actors in the view.
572 std::vector<unsigned int> indices;
573 for(unsigned int i = 0; i < 10; i++)
575 Actor testActor = view.GetItem(i);
578 indices.push_back(i);
584 if (!indices.empty())
586 const unsigned int firstTargetIndex = indices[indices.size()-1];
587 // scroll to last item
588 view.ScrollToItem(firstTargetIndex, 0.00f);
589 application.Render(16); // 60hz frames
591 std::size_t moveCount = 0;
592 for(std::size_t i = 0; i < indices.size(); i++)
594 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
595 view.ScrollToItem(indices[i], 0.0f);
597 application.Render(16); // 60hz frame
599 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
601 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
607 DALI_TEST_CHECK((moveCount == indices.size()));
612 tet_result(TET_FAIL);
615 Stage::GetCurrent().Remove(view);