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;
36 Vector3 SpiralLayoutItemSizeFunction(const Vector3& layoutSize)
38 float width = layoutSize.width * 0.2f;
39 return Vector3(width, width, width);
42 float SpiralLayoutSpiralRadiusFunction(const Vector3& layoutSize)
44 return layoutSize.width * 0.5f;
48 static void Startup();
49 static void Cleanup();
51 // Implementation of ItemFactory for providing actors to ItemView
52 class TestItemFactory : public ItemFactory
58 * @param application class, stored as reference
64 public: // From ItemFactory
67 * Query the number of items available from the factory.
68 * The maximum available item has an ID of GetNumberOfItems() - 1.
70 virtual unsigned int GetNumberOfItems()
72 return TOTAL_ITEM_NUMBER;
76 * Create an Actor to represent a visible item.
78 * @return the created actor.
80 virtual Actor NewItem(unsigned int itemId)
82 // Create an test actor for this item
83 ImageActor actor = CreateSolidColorActor(Color::RED);
84 actor.SetSize(64.0f, 64.0f);
91 void (*tet_startup)() = Startup;
92 void (*tet_cleanup)() = Cleanup;
95 static void UtcDaliSpiralLayoutNew();
96 static void UtcDaliSpiralSetAndGetItemSizeFunction();
97 static void UtcDaliSpiralSetAndGetItemSpacing();
98 static void UtcDaliSpiralSetAndGetRevolutionDistance();
99 static void UtcDaliSpiralSetAndGetSpiralRadiusFunction();
100 static void UtcDaliSpiralSetAndGetTopItemAlignment();
101 static void UtcDaliSpiralSetAndGetScrollSpeedFactor();
102 static void UtcDaliSpiralSetAndGetMaximumSwipeSpeed();
103 static void UtcDaliSpiralLayoutSetAndGetItemFlickAnimationDuration();
104 static void UtcDaliSpiralLayoutConstraintLeft();
105 static void UtcDaliSpiralLayoutConstraintRight();
106 static void UtcDaliSpiralLayoutConstraintUp();
107 static void UtcDaliSpiralLayoutConstraintDown();
108 static void UtcDaliSpiralLayoutGetScrollToPosition();
109 static void UtcDaliSpiralLayoutScrollDirection();
112 POSITIVE_TC_IDX = 0x01,
116 // Add test functionality for all APIs in the class (Positive and Negative)
118 struct tet_testlist tet_testlist[] = {
119 { UtcDaliSpiralLayoutScrollDirection, POSITIVE_TC_IDX },
120 { UtcDaliSpiralLayoutNew, POSITIVE_TC_IDX },
121 { UtcDaliSpiralSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
122 { UtcDaliSpiralSetAndGetItemSpacing, POSITIVE_TC_IDX },
123 { UtcDaliSpiralSetAndGetRevolutionDistance, POSITIVE_TC_IDX },
124 { UtcDaliSpiralSetAndGetSpiralRadiusFunction, POSITIVE_TC_IDX },
125 { UtcDaliSpiralSetAndGetTopItemAlignment, POSITIVE_TC_IDX },
126 { UtcDaliSpiralSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
127 { UtcDaliSpiralSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
128 { UtcDaliSpiralLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
129 { UtcDaliSpiralLayoutConstraintLeft, POSITIVE_TC_IDX },
130 { UtcDaliSpiralLayoutConstraintRight, POSITIVE_TC_IDX },
131 { UtcDaliSpiralLayoutConstraintUp, POSITIVE_TC_IDX },
132 { UtcDaliSpiralLayoutConstraintDown, POSITIVE_TC_IDX },
133 { UtcDaliSpiralLayoutGetScrollToPosition, POSITIVE_TC_IDX },
138 // Called only once before first test is run.
139 static void Startup()
143 // Called only once after last test is run
144 static void Cleanup()
148 static void UtcDaliSpiralLayoutNew()
150 ToolkitTestApplication application;
152 // Create a spiral layout
153 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
155 DALI_TEST_CHECK(spiralLayout);
158 static void UtcDaliSpiralSetAndGetItemSizeFunction()
160 ToolkitTestApplication application;
162 // Create a spiral layout
163 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
165 // Set the item size function
166 spiralLayout->SetItemSizeFunction(SpiralLayoutItemSizeFunction);
168 // Check whether we get the correct item size function
169 DALI_TEST_CHECK(spiralLayout->GetItemSizeFunction() == SpiralLayoutItemSizeFunction);
172 static void UtcDaliSpiralSetAndGetItemSpacing()
174 ToolkitTestApplication application;
176 // Create a spiral layout
177 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
179 // Set the item spacing
180 spiralLayout->SetItemSpacing(Radian(0.6f));
182 // Check whether we get the correct item spacing
183 DALI_TEST_EQUALS(spiralLayout->GetItemSpacing(), 0.6f, TEST_LOCATION );
186 static void UtcDaliSpiralSetAndGetRevolutionDistance()
188 ToolkitTestApplication application;
190 // Create a spiral layout
191 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
193 // Set the revolution distance
194 spiralLayout->SetRevolutionDistance(150.0f);
196 // Check whether we get the correct revolution distance
197 DALI_TEST_EQUALS(spiralLayout->GetRevolutionDistance(), 150.0f, TEST_LOCATION );
200 static void UtcDaliSpiralSetAndGetSpiralRadiusFunction()
202 ToolkitTestApplication application;
204 // Create a spiral layout
205 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
207 // Set the spiral radius function
208 spiralLayout->SetSpiralRadiusFunction(SpiralLayoutSpiralRadiusFunction);
210 // Check whether we get the correct spiral radius function
211 DALI_TEST_CHECK(spiralLayout->GetSpiralRadiusFunction() == SpiralLayoutSpiralRadiusFunction);
214 static void UtcDaliSpiralSetAndGetTopItemAlignment()
216 ToolkitTestApplication application;
218 // Create a spiral layout
219 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
221 // Set the alignment of the top item
222 spiralLayout->SetTopItemAlignment(-0.25f);
224 // Check whether we get the correct alignment of the top item
225 DALI_TEST_EQUALS(spiralLayout->GetTopItemAlignment(), -0.25f, TEST_LOCATION );
228 static void UtcDaliSpiralSetAndGetScrollSpeedFactor()
230 ToolkitTestApplication application;
232 // Create a spiral layout
233 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
235 // Set the scroll speed factor
236 spiralLayout->SetScrollSpeedFactor(0.05f);
238 // Check whether we get the correct scroll speed factor
239 DALI_TEST_EQUALS(spiralLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
242 static void UtcDaliSpiralSetAndGetMaximumSwipeSpeed()
244 ToolkitTestApplication application;
246 // Create a spiral layout
247 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
249 // Set the maximum swipe speed
250 spiralLayout->SetMaximumSwipeSpeed(50.0f);
252 // Check whether we get the correct maximum swipe speed
253 DALI_TEST_EQUALS(spiralLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
256 static void UtcDaliSpiralLayoutSetAndGetItemFlickAnimationDuration()
258 ToolkitTestApplication application;
260 // Create a spiral layout
261 SpiralLayoutPtr spiralLayout = SpiralLayout::New();
263 // Set the flick animaiton duration
264 spiralLayout->SetItemFlickAnimationDuration(0.35f);
266 // Check whether we get the correct flick animaiton duration
267 DALI_TEST_EQUALS( spiralLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
270 static void UtcDaliSpiralLayoutConstraintLeft()
272 ToolkitTestApplication application;
274 // Create the ItemView actor
275 TestItemFactory factory;
276 ItemView view = ItemView::New(factory);
277 Vector3 vec(480.0f, 800.0f, 0.0f);
278 SpiralLayoutPtr layout = SpiralLayout::New();
280 view.SetName("view actor");
281 view.AddLayout(*layout);
284 Stage::GetCurrent().Add(view);
285 layout->SetOrientation(ControlOrientation::Left);
286 view.ActivateLayout(0, vec, 0.0f);
288 application.SendNotification();
289 application.Render(0);
292 for(int i = 0; i < 10; ++i)
294 application.Render(16); // 60hz frames
297 // Confirm: we have actors in the view and they are positioned some distance from the origin.
298 int nonZeroCount = 0;
299 int elementsFound = 0;
300 for(unsigned int i = 0; i < 10; i++)
302 Actor testActor = view.GetItem(i);
306 Vector3 pos = testActor.GetCurrentPosition();
308 if (pos.LengthSquared() > 0.0f)
315 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
316 Stage::GetCurrent().Remove(view);
319 static void UtcDaliSpiralLayoutConstraintRight()
321 ToolkitTestApplication application;
323 // Create the ItemView actor
324 TestItemFactory factory;
325 ItemView view = ItemView::New(factory);
326 Vector3 vec(480.0f, 800.0f, 0.0f);
327 SpiralLayoutPtr layout = SpiralLayout::New();
329 view.SetName("view actor");
330 view.AddLayout(*layout);
333 Stage::GetCurrent().Add(view);
334 layout->SetOrientation(ControlOrientation::Right);
335 view.ActivateLayout(0, vec, 0.0f);
337 application.SendNotification();
338 application.Render(0);
341 for(int i = 0; i < 10; ++i)
343 application.Render(16); // 60hz frames
346 // Confirm: we have actors in the view and they are positioned some distance from the origin.
347 int nonZeroCount = 0;
348 int elementsFound = 0;
349 for(unsigned int i = 0; i < 10; i++)
351 Actor testActor = view.GetItem(i);
355 Vector3 pos = testActor.GetCurrentPosition();
357 if (pos.LengthSquared() > 0.0f)
364 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
365 Stage::GetCurrent().Remove(view);
368 static void UtcDaliSpiralLayoutConstraintUp()
370 ToolkitTestApplication application;
372 // Create the ItemView actor
373 TestItemFactory factory;
374 ItemView view = ItemView::New(factory);
375 Vector3 vec(480.0f, 800.0f, 0.0f);
376 SpiralLayoutPtr layout = SpiralLayout::New();
378 view.SetName("view actor");
379 view.AddLayout(*layout);
382 Stage::GetCurrent().Add(view);
383 layout->SetOrientation(ControlOrientation::Up);
384 view.ActivateLayout(0, vec, 0.0f);
386 application.SendNotification();
387 application.Render(0);
390 for(int i = 0; i < 10; ++i)
392 application.Render(16); // 60hz frames
395 // Confirm: we have actors in the view and they are positioned some distance from the origin.
396 int nonZeroCount = 0;
397 int elementsFound = 0;
398 for(unsigned int i = 0; i < 10; i++)
400 Actor testActor = view.GetItem(i);
404 Vector3 pos = testActor.GetCurrentPosition();
406 if (pos.LengthSquared() > 0.0f)
413 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
415 layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
416 int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
417 DALI_TEST_CHECK(nextItem == 1);
419 Stage::GetCurrent().Remove(view);
422 static void UtcDaliSpiralLayoutConstraintDown()
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 SpiralLayoutPtr layout = SpiralLayout::New();
432 view.SetName("view actor");
433 view.AddLayout(*layout);
436 Stage::GetCurrent().Add(view);
437 layout->SetOrientation(ControlOrientation::Down);
438 view.ActivateLayout(0, vec, 0.0f);
440 application.SendNotification();
441 application.Render(0);
444 for(int i = 0; i < 10; ++i)
446 application.Render(16); // 60hz frames
449 // Confirm: we have actors in the view and they are positioned some distance from the origin.
450 int nonZeroCount = 0;
451 int elementsFound = 0;
452 for(unsigned int i = 0; i < 10; i++)
454 Actor testActor = view.GetItem(i);
458 Vector3 pos = testActor.GetCurrentPosition();
460 if (pos.LengthSquared() > 0.0f)
467 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
468 Stage::GetCurrent().Remove(view);
471 static void UtcDaliSpiralLayoutScrollDirection()
473 ToolkitTestApplication application;
475 // Create the ItemView actor
476 TestItemFactory factory;
477 ItemView view = ItemView::New(factory);
478 Vector3 vec(480.0f, 800.0f, 0.0f);
479 SpiralLayoutPtr navigationLayout = SpiralLayout::New();
481 view.SetName("view actor");
482 view.AddLayout(*navigationLayout);
485 Stage::GetCurrent().Add(view);
486 navigationLayout->SetOrientation(ControlOrientation::Left);
487 view.ActivateLayout(0, vec, 0.0f);
489 application.SendNotification();
490 application.Render(0);
492 ItemLayoutPtr layout = navigationLayout;
495 for(int i = 0; i < 10; ++i)
497 application.Render(16); // 60hz frames
500 navigationLayout->SetOrientation(ControlOrientation::Up);
501 view.ActivateLayout(0, vec, 0.0f);
502 application.SendNotification();
503 application.Render();
505 Degree deg = layout->GetScrollDirection();
506 DALI_TEST_CHECK(deg == -45.0f);
508 navigationLayout->SetOrientation(ControlOrientation::Down);
509 view.ActivateLayout(0, vec, 0.0f);
510 application.SendNotification();
511 application.Render();
513 deg = layout->GetScrollDirection();
514 DALI_TEST_CHECK((deg == 180.0f - 45.0f));
516 layout->SetOrientation(ControlOrientation::Left);
517 view.ActivateLayout(0, vec, 0.0f);
518 application.SendNotification();
519 application.Render();
521 deg = layout->GetScrollDirection();
522 DALI_TEST_CHECK(deg == 45.0f);
524 navigationLayout->SetOrientation(ControlOrientation::Right);
525 view.ActivateLayout(0, vec, 0.0f);
526 application.SendNotification();
527 application.Render();
529 deg = layout->GetScrollDirection();
530 DALI_TEST_CHECK(deg == (270.0f - 45.0f));
532 Stage::GetCurrent().Remove(view);
535 static void UtcDaliSpiralLayoutGetScrollToPosition()
537 ToolkitTestApplication application;
539 // Create the ItemView actor
540 TestItemFactory factory;
541 ItemView view = ItemView::New(factory);
542 Vector3 vec(480.0f, 800.0f, 0.0f);
543 SpiralLayoutPtr layout = SpiralLayout::New();
545 view.SetName("view actor");
546 view.AddLayout(*layout);
549 Stage::GetCurrent().Add(view);
550 layout->SetOrientation(ControlOrientation::Up);
551 view.ActivateLayout(0, vec, 0.0f);
553 application.SendNotification();
554 application.Render(0);
557 for(int i = 0; i < 10; ++i)
559 application.Render(16); // 60hz frames
562 // Confirm: we have actors in the view.
563 std::vector<unsigned int> indices;
564 for(unsigned int i = 0; i < 10; i++)
566 Actor testActor = view.GetItem(i);
569 indices.push_back(i);
575 if (!indices.empty())
577 const unsigned int firstTargetIndex = indices[indices.size()-1];
578 // scroll to last item
579 view.ScrollToItem(firstTargetIndex, 0.00f);
580 application.Render(16); // 60hz frames
582 std::size_t moveCount = 0;
583 for(std::size_t i = 0; i < indices.size(); i++)
585 float layoutPosBefore = view.GetCurrentLayoutPosition(i);
586 view.ScrollToItem(indices[i], 0.0f);
588 application.Render(16); // 60hz frame
590 float layoutPosAfter = view.GetCurrentLayoutPosition(i);
592 if (fabs(layoutPosBefore-layoutPosAfter) <= FLT_EPSILON)
598 DALI_TEST_CHECK((moveCount == indices.size()));
603 tet_result(TET_FAIL);
606 Stage::GetCurrent().Remove(view);