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-test-suite-utils.h>
28 using namespace Toolkit;
32 const unsigned int TOTAL_ITEM_NUMBER = 200;
34 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
36 float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
38 return Vector3(width, width, width);
43 static void Startup();
44 static void Cleanup();
46 // Implementation of ItemFactory for providing actors to ItemView
47 class TestItemFactory : public ItemFactory
53 * @param application class, stored as reference
59 public: // From ItemFactory
62 * Query the number of items available from the factory.
63 * The maximum available item has an ID of GetNumberOfItems() - 1.
65 virtual unsigned int GetNumberOfItems()
67 return TOTAL_ITEM_NUMBER;
71 * Create an Actor to represent a visible item.
73 * @return the created actor.
75 virtual Actor NewItem(unsigned int itemId)
77 // Create an test actor for this item
78 ImageActor actor = CreateSolidColorActor(Color::RED);
79 actor.SetSize(64.0f, 64.0f);
85 void (*tet_startup)() = Startup;
86 void (*tet_cleanup)() = Cleanup;
89 static void UtcDaliGridLayoutNew();
90 static void UtcDaliGridLayoutSetAndGetNumberOfColumns();
91 static void UtcDaliGridLayoutSetAndGetRowSpacing();
92 static void UtcDaliGridLayoutSetAndGetColumnSpacing();
93 static void UtcDaliGridLayoutSetAndGetTopMargin();
94 static void UtcDaliGridLayoutSetAndGetBottomMargin();
95 static void UtcDaliGridLayoutSetAndGetSideMargin();
96 static void UtcDaliGridLayoutSetAndGetZGap();
97 static void UtcDaliGridLayoutSetAndGetItemSizeFunction();
98 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor();
99 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed();
100 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration();
101 static void UtcDaliGridLayoutConstraintLeft();
102 static void UtcDaliGridLayoutConstraintRight();
103 static void UtcDaliGridLayoutConstraintUp();
104 static void UtcDaliGridLayoutConstraintDown();
105 static void UtcDaliGridLayoutScrollDirection();
108 POSITIVE_TC_IDX = 0x01,
112 // Add test functionality for all APIs in the class (Positive and Negative)
114 struct tet_testlist tet_testlist[] = {
115 { UtcDaliGridLayoutNew, POSITIVE_TC_IDX },
116 { UtcDaliGridLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
117 { UtcDaliGridLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
118 { UtcDaliGridLayoutSetAndGetColumnSpacing, POSITIVE_TC_IDX },
119 { UtcDaliGridLayoutSetAndGetTopMargin, POSITIVE_TC_IDX },
120 { UtcDaliGridLayoutSetAndGetBottomMargin, POSITIVE_TC_IDX },
121 { UtcDaliGridLayoutSetAndGetSideMargin, POSITIVE_TC_IDX },
122 { UtcDaliGridLayoutSetAndGetZGap, POSITIVE_TC_IDX },
123 { UtcDaliGridLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
124 { UtcDaliGridLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
125 { UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
126 { UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
127 { UtcDaliGridLayoutConstraintLeft, POSITIVE_TC_IDX },
128 { UtcDaliGridLayoutConstraintRight, POSITIVE_TC_IDX },
129 { UtcDaliGridLayoutConstraintUp, POSITIVE_TC_IDX },
130 { UtcDaliGridLayoutConstraintDown, POSITIVE_TC_IDX },
131 { UtcDaliGridLayoutScrollDirection, POSITIVE_TC_IDX },
136 // Called only once before first test is run.
137 static void Startup()
141 // Called only once after last test is run
142 static void Cleanup()
146 static void UtcDaliGridLayoutNew()
148 ToolkitTestApplication application;
150 // Create a grid layout
151 GridLayoutPtr gridLayout = GridLayout::New();
153 DALI_TEST_CHECK(gridLayout);
156 static void UtcDaliGridLayoutSetAndGetNumberOfColumns()
158 ToolkitTestApplication application;
160 // Create a grid layout
161 GridLayoutPtr gridLayout = GridLayout::New();
163 // Set the number of columns
164 gridLayout->SetNumberOfColumns(6);
166 // Check whether we get the correct number of columns
167 DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
170 static void UtcDaliGridLayoutSetAndGetRowSpacing()
172 ToolkitTestApplication application;
174 // Create a grid layout
175 GridLayoutPtr gridLayout = GridLayout::New();
177 // Set the row spacing
178 gridLayout->SetRowSpacing(10.0f);
180 // Check whether we get the correct row spacing
181 DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
184 static void UtcDaliGridLayoutSetAndGetColumnSpacing()
186 ToolkitTestApplication application;
188 // Create a grid layout
189 GridLayoutPtr gridLayout = GridLayout::New();
191 // Set the column spacing
192 gridLayout->SetColumnSpacing(10.0f);
194 // Check whether we get the correct column spacing
195 DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
198 static void UtcDaliGridLayoutSetAndGetTopMargin()
200 ToolkitTestApplication application;
202 // Create a grid layout
203 GridLayoutPtr gridLayout = GridLayout::New();
205 // Set the top margin
206 gridLayout->SetTopMargin(30.0f);
208 // Check whether we get the correct top margin
209 DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
212 static void UtcDaliGridLayoutSetAndGetBottomMargin()
214 ToolkitTestApplication application;
216 // Create a grid layout
217 GridLayoutPtr gridLayout = GridLayout::New();
219 // Set the bottom margin
220 gridLayout->SetBottomMargin(30.0f);
222 // Check whether we get the correct bottom margin
223 DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
226 static void UtcDaliGridLayoutSetAndGetSideMargin()
228 ToolkitTestApplication application;
230 // Create a grid layout
231 GridLayoutPtr gridLayout = GridLayout::New();
233 // Set the side margin
234 gridLayout->SetSideMargin(10.0f);
236 // Check whether we get the correct side margin
237 DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
240 static void UtcDaliGridLayoutSetAndGetZGap()
242 ToolkitTestApplication application;
244 // Create a grid layout
245 GridLayoutPtr gridLayout = GridLayout::New();
247 // Set the gap of items in the Z axis in different columns
248 gridLayout->SetZGap(5.0f);
250 // Check whether we get the correct Z gap
251 DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
254 static void UtcDaliGridLayoutSetAndGetItemSizeFunction()
256 ToolkitTestApplication application;
258 // Create a grid layout
259 GridLayoutPtr gridLayout = GridLayout::New();
261 // Set the item size function
262 gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
264 // Check whether we get the correct item size function
265 DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
268 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor()
270 ToolkitTestApplication application;
272 // Create a grid layout
273 GridLayoutPtr gridLayout = GridLayout::New();
275 // Set the scroll speed factor
276 gridLayout->SetScrollSpeedFactor(0.05f);
278 // Check whether we get the correct scroll speed factor
279 DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
282 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed()
284 ToolkitTestApplication application;
286 // Create a grid layout
287 GridLayoutPtr gridLayout = GridLayout::New();
289 // Set the maximum swipe speed
290 gridLayout->SetMaximumSwipeSpeed(50.0f);
292 // Check whether we get the correct maximum swipe speed
293 DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
296 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration()
298 ToolkitTestApplication application;
300 // Create a grid layout
301 GridLayoutPtr gridLayout = GridLayout::New();
303 // Set the flick animaiton duration
304 gridLayout->SetItemFlickAnimationDuration(0.35f);
306 // Check whether we get the correct flick animaiton duration
307 DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
310 static void UtcDaliGridLayoutConstraintLeft()
312 ToolkitTestApplication application;
314 // Create the ItemView actor
315 TestItemFactory factory;
316 ItemView view = ItemView::New(factory);
317 Vector3 vec(480.0f, 800.0f, 0.0f);
318 GridLayoutPtr gridLayout = GridLayout::New();
319 gridLayout->SetNumberOfColumns(6);
321 view.SetName("view actor");
322 view.AddLayout(*gridLayout);
325 Stage::GetCurrent().Add(view);
326 gridLayout->SetOrientation(ControlOrientation::Left);
327 view.ActivateLayout(0, vec, 0.0f);
329 application.SendNotification();
330 application.Render(0);
333 for(int i = 0; i < 10; ++i)
335 application.Render(16); // 60hz frames
338 // Confirm: we have actors in the view and they are positioned some distance from the origin.
339 int nonZeroCount = 0;
340 int elementsFound = 0;
341 for(unsigned int i = 0; i < 10; i++)
343 Actor testActor = view.GetItem(i);
347 Vector3 pos = testActor.GetCurrentPosition();
349 if (pos.LengthSquared() > 0.0f)
356 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
357 Stage::GetCurrent().Remove(view);
360 static void UtcDaliGridLayoutConstraintRight()
362 ToolkitTestApplication application;
364 // Create the ItemView actor
365 TestItemFactory factory;
366 ItemView view = ItemView::New(factory);
367 Vector3 vec(480.0f, 800.0f, 0.0f);
368 GridLayoutPtr gridLayout = GridLayout::New();
369 gridLayout->SetNumberOfColumns(6);
371 view.SetName("view actor");
372 view.AddLayout(*gridLayout);
375 Stage::GetCurrent().Add(view);
376 gridLayout->SetOrientation(ControlOrientation::Right);
377 view.ActivateLayout(0, vec, 0.0f);
379 application.SendNotification();
380 application.Render(0);
383 for(int i = 0; i < 10; ++i)
385 application.Render(16); // 60hz frames
388 // Confirm: we have actors in the view and they are positioned some distance from the origin.
389 int nonZeroCount = 0;
390 int elementsFound = 0;
391 for(unsigned int i = 0; i < 10; i++)
393 Actor testActor = view.GetItem(i);
397 Vector3 pos = testActor.GetCurrentPosition();
399 if (pos.LengthSquared() > 0.0f)
406 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
407 Stage::GetCurrent().Remove(view);
410 static void UtcDaliGridLayoutConstraintUp()
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 GridLayoutPtr gridLayout = GridLayout::New();
419 gridLayout->SetNumberOfColumns(6);
421 view.SetName("view actor");
422 view.AddLayout(*gridLayout);
425 Stage::GetCurrent().Add(view);
426 gridLayout->SetOrientation(ControlOrientation::Up);
427 view.ActivateLayout(0, vec, 0.0f);
429 application.SendNotification();
430 application.Render(0);
433 for(int i = 0; i < 10; ++i)
435 application.Render(16); // 60hz frames
438 // Confirm: we have actors in the view and they are positioned some distance from the origin.
439 int nonZeroCount = 0;
440 int elementsFound = 0;
441 for(unsigned int i = 0; i < 10; i++)
443 Actor testActor = view.GetItem(i);
447 Vector3 pos = testActor.GetCurrentPosition();
449 if (pos.LengthSquared() > 0.0f)
456 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
458 ItemLayoutPtr layout = gridLayout;
459 layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
460 int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
461 DALI_TEST_CHECK(nextItem == 1);
463 Stage::GetCurrent().Remove(view);
466 static void UtcDaliGridLayoutConstraintDown()
468 ToolkitTestApplication application;
470 // Create the ItemView actor
471 TestItemFactory factory;
472 ItemView view = ItemView::New(factory);
473 Vector3 vec(480.0f, 800.0f, 0.0f);
474 GridLayoutPtr gridLayout = GridLayout::New();
475 gridLayout->SetNumberOfColumns(6);
477 view.SetName("view actor");
478 view.AddLayout(*gridLayout);
481 Stage::GetCurrent().Add(view);
482 gridLayout->SetOrientation(ControlOrientation::Down);
483 view.ActivateLayout(0, vec, 0.0f);
485 application.SendNotification();
486 application.Render(0);
489 for(int i = 0; i < 10; ++i)
491 application.Render(16); // 60hz frames
494 // Confirm: we have actors in the view and they are positioned some distance from the origin.
495 int nonZeroCount = 0;
496 int elementsFound = 0;
497 for(unsigned int i = 0; i < 10; i++)
499 Actor testActor = view.GetItem(i);
503 Vector3 pos = testActor.GetCurrentPosition();
505 if (pos.LengthSquared() > 0.0f)
512 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
513 Stage::GetCurrent().Remove(view);
516 static void UtcDaliGridLayoutScrollDirection()
518 ToolkitTestApplication application;
520 // Create the ItemView actor
521 TestItemFactory factory;
522 ItemView view = ItemView::New(factory);
523 Vector3 vec(480.0f, 800.0f, 0.0f);
524 GridLayoutPtr gridLayout = GridLayout::New();
525 gridLayout->SetNumberOfColumns(6);
527 view.SetName("view actor");
528 view.AddLayout(*gridLayout);
531 Stage::GetCurrent().Add(view);
532 gridLayout->SetOrientation(ControlOrientation::Left);
533 view.ActivateLayout(0, vec, 0.0f);
535 application.SendNotification();
536 application.Render(0);
538 ItemLayoutPtr layout = gridLayout;
541 for(int i = 0; i < 10; ++i)
543 application.Render(16); // 60hz frames
546 gridLayout->SetOrientation(ControlOrientation::Up);
547 view.ActivateLayout(0, vec, 0.0f);
548 application.SendNotification();
549 application.Render();
551 Degree deg = layout->GetScrollDirection();
552 DALI_TEST_CHECK(deg == 0.0f);
554 gridLayout->SetOrientation(ControlOrientation::Down);
555 view.ActivateLayout(0, vec, 0.0f);
556 application.SendNotification();
557 application.Render();
559 deg = layout->GetScrollDirection();
560 DALI_TEST_CHECK((deg == 180.0f));
562 layout->SetOrientation(ControlOrientation::Left);
563 view.ActivateLayout(0, vec, 0.0f);
564 application.SendNotification();
565 application.Render();
567 deg = layout->GetScrollDirection();
568 DALI_TEST_CHECK(deg == 90.f);
570 gridLayout->SetOrientation(ControlOrientation::Right);
571 view.ActivateLayout(0, vec, 0.0f);
572 application.SendNotification();
573 application.Render();
575 deg = layout->GetScrollDirection();
576 DALI_TEST_CHECK(deg == 270.0f);
578 Stage::GetCurrent().Remove(view);