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/public-api/dali-core.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
27 #include <dali-toolkit-test-suite-utils.h>
30 using namespace Toolkit;
34 const unsigned int TOTAL_ITEM_NUMBER = 200;
37 static void Startup();
38 static void Cleanup();
40 // Implementation of ItemFactory for providing actors to ItemView
41 class TestItemFactory : public ItemFactory
47 * @param application class, stored as reference
53 public: // From ItemFactory
56 * Query the number of items available from the factory.
57 * The maximum available item has an ID of GetNumberOfItems() - 1.
59 virtual unsigned int GetNumberOfItems()
61 return TOTAL_ITEM_NUMBER;
65 * Create an Actor to represent a visible item.
67 * @return the created actor.
69 virtual Actor NewItem(unsigned int itemId)
71 // Create an test actor for this item
72 ImageActor actor = CreateSolidColorActor(Color::RED);
73 actor.SetSize(64.0f, 64.0f);
80 void (*tet_startup)() = Startup;
81 void (*tet_cleanup)() = Cleanup;
84 static void UtcDaliNavigationLayoutNew();
85 static void UtcDaliNavigationLayoutColumns();
86 static void UtcDaliNavigationLayoutSetGetOrientation();
87 static void UtcDaliNavigationLayoutTestConstraintLeft();
88 static void UtcDaliNavigationLayoutTestConstraintRight();
89 static void UtcDaliNavigationLayoutTestConstraintUp();
90 static void UtcDaliNavigationLayoutTestConstraintDown();
91 static void UtcDaliNavigationLayoutScrollDirection();
92 static void UtcDaliNavigationLayoutSetGetColumnSpacing();
93 static void UtcDaliNavigationLayoutSetGetTopMargin();
94 static void UtcDaliNavigationLayoutSetGetBottomMargin();
95 static void UtcDaliNavigationLayoutSetGetScrollSpeedFactor();
96 static void UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed();
97 static void UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration();
98 static void UtcDaliNavigationLayoutGetScrollToPosition();
102 POSITIVE_TC_IDX = 0x01,
106 // Add test functionality for all APIs in the class (Positive and Negative)
108 struct tet_testlist tet_testlist[] = {
109 { UtcDaliNavigationLayoutNew, POSITIVE_TC_IDX },
110 { UtcDaliNavigationLayoutColumns, POSITIVE_TC_IDX },
111 { UtcDaliNavigationLayoutSetGetOrientation, POSITIVE_TC_IDX },
112 { UtcDaliNavigationLayoutTestConstraintLeft, POSITIVE_TC_IDX },
113 { UtcDaliNavigationLayoutTestConstraintRight, POSITIVE_TC_IDX },
114 { UtcDaliNavigationLayoutTestConstraintUp, POSITIVE_TC_IDX },
115 { UtcDaliNavigationLayoutTestConstraintDown, POSITIVE_TC_IDX },
116 { UtcDaliNavigationLayoutScrollDirection, POSITIVE_TC_IDX },
117 { UtcDaliNavigationLayoutSetGetColumnSpacing, POSITIVE_TC_IDX },
118 { UtcDaliNavigationLayoutSetGetTopMargin, POSITIVE_TC_IDX },
119 { UtcDaliNavigationLayoutSetGetBottomMargin, POSITIVE_TC_IDX },
120 { UtcDaliNavigationLayoutSetGetScrollSpeedFactor, POSITIVE_TC_IDX },
121 { UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
122 { UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
123 { UtcDaliNavigationLayoutGetScrollToPosition, POSITIVE_TC_IDX },
128 // Called only once before first test is run.
129 static void Startup()
133 // Called only once after last test is run
134 static void Cleanup()
139 // Positive test case for a method
140 static void UtcDaliNavigationLayoutNew()
142 ToolkitTestApplication application;
144 // Create a navigation layout
145 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
146 navigationLayout->SetNumberOfColumns(6);
147 DALI_TEST_CHECK(navigationLayout);
150 static void UtcDaliNavigationLayoutColumns()
152 ToolkitTestApplication application;
153 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
155 navigationLayout->SetNumberOfColumns(6);
156 // Check whether we get the correct number of columns
157 DALI_TEST_CHECK(navigationLayout->GetNumberOfColumns() == 6);
160 static void UtcDaliNavigationLayoutSetGetOrientation()
162 ToolkitTestApplication application;
163 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
165 navigationLayout->SetNumberOfColumns(6);
166 navigationLayout->SetOrientation(ControlOrientation::Right);
167 DALI_TEST_CHECK(navigationLayout->GetOrientation() == ControlOrientation::Right);
170 static void UtcDaliNavigationLayoutTestConstraintLeft()
172 ToolkitTestApplication application;
174 // Create the ItemView actor
175 TestItemFactory factory;
176 ItemView view = ItemView::New(factory);
177 Vector3 vec(480.0f, 800.0f, 0.0f);
178 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
179 navigationLayout->SetNumberOfColumns(6);
181 view.SetName("view actor");
182 view.AddLayout(*navigationLayout);
185 Stage::GetCurrent().Add(view);
186 navigationLayout->SetOrientation(ControlOrientation::Left);
187 view.ActivateLayout(0, vec, 0.0f);
189 application.SendNotification();
190 application.Render(0);
193 for(int i = 0; i < 10; ++i)
195 application.Render(16); // 60hz frames
198 // Confirm: we have actors in the view and all of them is positioned at X = 0
199 // and the series is monotonely decreasing.
200 int nonZeroXCount = 0;
201 int elementsFound = 0;
202 int wrongDirectionCount = 0;
203 float prevY = FLT_MAX;
204 for(unsigned int i = 0; i < 10; i++)
206 Actor testActor = view.GetItem(i);
210 Vector3 pos = testActor.GetCurrentPosition();
219 wrongDirectionCount++;
226 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroXCount == 0) && (wrongDirectionCount == 0));
227 Stage::GetCurrent().Remove(view);
230 static void UtcDaliNavigationLayoutTestConstraintRight()
232 ToolkitTestApplication application;
234 // Create the ItemView actor
235 TestItemFactory factory;
236 ItemView view = ItemView::New(factory);
237 Vector3 vec(480.0f, 800.0f, 0.0f);
238 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
239 navigationLayout->SetNumberOfColumns(6);
241 view.SetName("view actor");
242 view.AddLayout(*navigationLayout);
245 Stage::GetCurrent().Add(view);
246 navigationLayout->SetOrientation(ControlOrientation::Right);
247 view.ActivateLayout(0, vec, 0.0f);
249 application.SendNotification();
250 application.Render(0);
253 for(int i = 0; i < 10; ++i)
255 application.Render(16); // 60hz frames
258 // Confirm: we have actors in the view and all of them is positioned at X = 0
259 // and the series is monotonely increasing.
260 int nonZeroXCount = 0;
261 int elementsFound = 0;
262 int wrongDirectionCount = 0;
263 float prevY = -FLT_MAX;
264 for(unsigned int i = 0; i < 10; i++)
266 Actor testActor = view.GetItem(i);
270 Vector3 pos = testActor.GetCurrentPosition();
279 wrongDirectionCount++;
286 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroXCount == 0) && (wrongDirectionCount == 0));
287 Stage::GetCurrent().Remove(view);
290 static void UtcDaliNavigationLayoutTestConstraintUp()
292 ToolkitTestApplication application;
294 // Create the ItemView actor
295 TestItemFactory factory;
296 ItemView view = ItemView::New(factory);
297 Vector3 vec(480.0f, 800.0f, 0.0f);
298 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
299 navigationLayout->SetNumberOfColumns(6);
301 view.SetName("view actor");
302 view.AddLayout(*navigationLayout);
305 Stage::GetCurrent().Add(view);
306 navigationLayout->SetOrientation(ControlOrientation::Up);
307 view.ActivateLayout(0, vec, 0.0f);
309 application.SendNotification();
310 application.Render(0);
313 for(int i = 0; i < 10; ++i)
315 application.Render(16); // 60hz frames
318 // Confirm: we have actors in the view and all of them is positioned at X = 0
319 // and the series is monotonely decreasing.
320 int nonZeroYCount = 0;
321 int elementsFound = 0;
322 int wrongDirectionCount = 0;
323 float prevX = -FLT_MAX;
324 for(unsigned int i = 0; i < 10; i++)
326 Actor testActor = view.GetItem(i);
330 Vector3 pos = testActor.GetCurrentPosition();
339 wrongDirectionCount++;
346 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroYCount == 0) && (wrongDirectionCount == 0));
347 Stage::GetCurrent().Remove(view);
350 static void UtcDaliNavigationLayoutTestConstraintDown()
352 ToolkitTestApplication application;
354 // Create the ItemView actor
355 TestItemFactory factory;
356 ItemView view = ItemView::New(factory);
357 Vector3 vec(480.0f, 800.0f, 0.0f);
358 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
359 navigationLayout->SetNumberOfColumns(6);
361 view.SetName("view actor");
362 view.AddLayout(*navigationLayout);
365 Stage::GetCurrent().Add(view);
366 navigationLayout->SetOrientation(ControlOrientation::Down);
367 view.ActivateLayout(0, vec, 0.0f);
369 application.SendNotification();
370 application.Render(0);
373 for(int i = 0; i < 10; ++i)
375 application.Render(16); // 60hz frames
378 // Confirm: we have actors in the view and all of them is positioned at X = 0
379 // and the series is monotonely decreasing.
380 int nonZeroYCount = 0;
381 int elementsFound = 0;
382 int wrongDirectionCount = 0;
383 float prevX = FLT_MAX;
384 for(unsigned int i = 0; i < 10; i++)
386 Actor testActor = view.GetItem(i);
390 Vector3 pos = testActor.GetCurrentPosition();
399 wrongDirectionCount++;
406 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroYCount == 0) && (wrongDirectionCount == 0));
407 Stage::GetCurrent().Remove(view);
411 static void UtcDaliNavigationLayoutScrollDirection()
413 ToolkitTestApplication application;
415 // Create the ItemView actor
416 TestItemFactory factory;
417 ItemView view = ItemView::New(factory);
418 Vector3 vec(480.0f, 800.0f, 0.0f);
419 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
420 navigationLayout->SetNumberOfColumns(6);
422 view.SetName("view actor");
423 view.AddLayout(*navigationLayout);
426 Stage::GetCurrent().Add(view);
427 navigationLayout->SetOrientation(ControlOrientation::Left);
428 view.ActivateLayout(0, vec, 0.0f);
430 application.SendNotification();
431 application.Render(0);
433 ItemLayoutPtr layout = navigationLayout;
436 for(int i = 0; i < 10; ++i)
438 application.Render(16); // 60hz frames
441 navigationLayout->SetOrientation(ControlOrientation::Up);
442 view.ActivateLayout(0, vec, 0.0f);
443 application.SendNotification();
444 application.Render();
446 Degree deg = layout->GetScrollDirection();
447 DALI_TEST_CHECK(deg == (180.0f - 45.0f));
449 navigationLayout->SetOrientation(ControlOrientation::Down);
450 view.ActivateLayout(0, vec, 0.0f);
451 application.SendNotification();
452 application.Render();
454 deg = layout->GetScrollDirection();
455 DALI_TEST_CHECK((deg == -45.0f));
457 layout->SetOrientation(ControlOrientation::Left);
458 view.ActivateLayout(0, vec, 0.0f);
459 application.SendNotification();
460 application.Render();
462 deg = layout->GetScrollDirection();
463 DALI_TEST_CHECK(deg == (270.0f - 45.0f));
465 navigationLayout->SetOrientation(ControlOrientation::Right);
466 view.ActivateLayout(0, vec, 0.0f);
467 application.SendNotification();
468 application.Render();
470 deg = layout->GetScrollDirection();
471 DALI_TEST_CHECK(deg == (90.0f - 45.0f));
473 Stage::GetCurrent().Remove(view);
476 static void UtcDaliNavigationLayoutSetGetColumnSpacing()
478 ToolkitTestApplication application;
479 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
480 const float testValue = 11.0f;
482 navigationLayout->SetNumberOfColumns(6);
483 navigationLayout->SetColumnSpacing(testValue);
484 DALI_TEST_CHECK(navigationLayout->GetColumnSpacing() == testValue);
487 static void UtcDaliNavigationLayoutSetGetTopMargin()
489 ToolkitTestApplication application;
490 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
491 const float testValue = 11.0f;
493 navigationLayout->SetNumberOfColumns(6);
494 navigationLayout->SetTopMargin(testValue);
495 DALI_TEST_CHECK(navigationLayout->GetTopMargin() == testValue);
498 static void UtcDaliNavigationLayoutSetGetBottomMargin()
500 ToolkitTestApplication application;
501 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
502 const float testValue = 12.0f;
504 navigationLayout->SetNumberOfColumns(6);
505 navigationLayout->SetBottomMargin(testValue);
506 DALI_TEST_CHECK(navigationLayout->GetBottomMargin() == testValue);
509 static void UtcDaliNavigationLayoutSetGetScrollSpeedFactor()
511 ToolkitTestApplication application;
512 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
513 const float testValue = 15.0f;
515 navigationLayout->SetNumberOfColumns(6);
516 navigationLayout->SetScrollSpeedFactor(testValue);
517 DALI_TEST_CHECK(navigationLayout->GetScrollSpeedFactor() == testValue);
520 static void UtcDaliNavigationLayoutSetGetMaximumSwipeSpeed()
522 ToolkitTestApplication application;
523 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
524 const float testValue = 10.0f;
526 navigationLayout->SetNumberOfColumns(6);
527 navigationLayout->SetMaximumSwipeSpeed(testValue);
528 DALI_TEST_CHECK(navigationLayout->GetMaximumSwipeSpeed() == testValue);
531 static void UtcDaliNavigationLayoutSetAndGetItemFlickAnimationDuration()
533 ToolkitTestApplication application;
535 // Create a navigation layout
536 NavigationLayoutPtr navigationLayout = NavigationLayout::New();
538 // Set the flick animaiton duration
539 navigationLayout->SetItemFlickAnimationDuration(0.35f);
541 // Check whether we get the correct flick animaiton duration
542 DALI_TEST_EQUALS( navigationLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
545 static void UtcDaliNavigationLayoutGetScrollToPosition()
547 ToolkitTestApplication application;
549 // Create the ItemView actor
550 TestItemFactory factory;
551 ItemView view = ItemView::New(factory);
552 Vector3 vec(480.0f, 800.0f, 0.0f);
553 NavigationLayoutPtr layout = NavigationLayout::New();
555 view.SetName("view actor");
556 view.AddLayout(*layout);
559 Stage::GetCurrent().Add(view);
560 layout->SetOrientation(ControlOrientation::Up);
561 view.ActivateLayout(0, vec, 0.0f);
563 application.SendNotification();
564 application.Render(0);
567 for(int i = 0; i < 10; ++i)
569 application.Render(16); // 60hz frames
572 // Confirm: we have actors in the view.
573 std::vector<unsigned int> indices;
574 for(unsigned int i = 0; i < 10; i++)
576 Actor testActor = view.GetItem(i);
579 indices.push_back(i);
585 if (!indices.empty())
587 const unsigned int firstTargetIndex = indices[indices.size()-1];
588 // scroll to last item
589 view.ScrollToItem(firstTargetIndex, 0.00f);
590 application.Render(16); // 60hz frames
592 std::size_t moveCount = 0;
593 for(std::size_t i = 0; i < indices.size(); i++)
595 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
596 view.ScrollToItem(indices[i], 0.0f);
598 application.Render(16); // 60hz frame
600 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
602 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
608 DALI_TEST_CHECK((moveCount == indices.size()));
613 tet_result(TET_FAIL);
616 Stage::GetCurrent().Remove(view);