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.
19 #include <float.h> // for FLT_MAX
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
26 #include <dali-toolkit/dali-toolkit.h>
29 using namespace Toolkit;
31 void utc_dali_toolkit_grid_layout_startup(void)
33 test_return_value = TET_UNDEF;
36 void utc_dali_toolkit_grid_layout_cleanup(void)
38 test_return_value = TET_PASS;
45 const unsigned int TOTAL_ITEM_NUMBER = 200;
47 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
49 float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
51 return Vector3(width, width, width);
55 // Implementation of ItemFactory for providing actors to ItemView
56 class TestItemFactory : public ItemFactory
62 * @param application class, stored as reference
68 public: // From ItemFactory
71 * Query the number of items available from the factory.
72 * The maximum available item has an ID of GetNumberOfItems() - 1.
74 virtual unsigned int GetNumberOfItems()
76 return TOTAL_ITEM_NUMBER;
80 * Create an Actor to represent a visible item.
82 * @return the created actor.
84 virtual Actor NewItem(unsigned int itemId)
86 // Create an test actor for this item
87 ImageActor actor = CreateSolidColorActor(Color::RED);
88 actor.SetSize(64.0f, 64.0f);
97 int UtcDaliGridLayoutNew(void)
99 ToolkitTestApplication application;
101 // Create a grid layout
102 GridLayoutPtr gridLayout = GridLayout::New();
104 DALI_TEST_CHECK(gridLayout);
108 int UtcDaliGridLayoutSetAndGetNumberOfColumns(void)
110 ToolkitTestApplication application;
112 // Create a grid layout
113 GridLayoutPtr gridLayout = GridLayout::New();
115 // Set the number of columns
116 gridLayout->SetNumberOfColumns(6);
118 // Check whether we get the correct number of columns
119 DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
123 int UtcDaliGridLayoutSetAndGetRowSpacing(void)
125 ToolkitTestApplication application;
127 // Create a grid layout
128 GridLayoutPtr gridLayout = GridLayout::New();
130 // Set the row spacing
131 gridLayout->SetRowSpacing(10.0f);
133 // Check whether we get the correct row spacing
134 DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
138 int UtcDaliGridLayoutSetAndGetColumnSpacing(void)
140 ToolkitTestApplication application;
142 // Create a grid layout
143 GridLayoutPtr gridLayout = GridLayout::New();
145 // Set the column spacing
146 gridLayout->SetColumnSpacing(10.0f);
148 // Check whether we get the correct column spacing
149 DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
153 int UtcDaliGridLayoutSetAndGetTopMargin(void)
155 ToolkitTestApplication application;
157 // Create a grid layout
158 GridLayoutPtr gridLayout = GridLayout::New();
160 // Set the top margin
161 gridLayout->SetTopMargin(30.0f);
163 // Check whether we get the correct top margin
164 DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
168 int UtcDaliGridLayoutSetAndGetBottomMargin(void)
170 ToolkitTestApplication application;
172 // Create a grid layout
173 GridLayoutPtr gridLayout = GridLayout::New();
175 // Set the bottom margin
176 gridLayout->SetBottomMargin(30.0f);
178 // Check whether we get the correct bottom margin
179 DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
183 int UtcDaliGridLayoutSetAndGetSideMargin(void)
185 ToolkitTestApplication application;
187 // Create a grid layout
188 GridLayoutPtr gridLayout = GridLayout::New();
190 // Set the side margin
191 gridLayout->SetSideMargin(10.0f);
193 // Check whether we get the correct side margin
194 DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
198 int UtcDaliGridLayoutSetAndGetZGap(void)
200 ToolkitTestApplication application;
202 // Create a grid layout
203 GridLayoutPtr gridLayout = GridLayout::New();
205 // Set the gap of items in the Z axis in different columns
206 gridLayout->SetZGap(5.0f);
208 // Check whether we get the correct Z gap
209 DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
213 int UtcDaliGridLayoutSetAndGetItemSizeFunction(void)
215 ToolkitTestApplication application;
217 // Create a grid layout
218 GridLayoutPtr gridLayout = GridLayout::New();
220 // Set the item size function
221 gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
223 // Check whether we get the correct item size function
224 DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
228 int UtcDaliGridLayoutSetAndGetScrollSpeedFactor(void)
230 ToolkitTestApplication application;
232 // Create a grid layout
233 GridLayoutPtr gridLayout = GridLayout::New();
235 // Set the scroll speed factor
236 gridLayout->SetScrollSpeedFactor(0.05f);
238 // Check whether we get the correct scroll speed factor
239 DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
243 int UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed(void)
245 ToolkitTestApplication application;
247 // Create a grid layout
248 GridLayoutPtr gridLayout = GridLayout::New();
250 // Set the maximum swipe speed
251 gridLayout->SetMaximumSwipeSpeed(50.0f);
253 // Check whether we get the correct maximum swipe speed
254 DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
258 int UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration(void)
260 ToolkitTestApplication application;
262 // Create a grid layout
263 GridLayoutPtr gridLayout = GridLayout::New();
265 // Set the flick animaiton duration
266 gridLayout->SetItemFlickAnimationDuration(0.35f);
268 // Check whether we get the correct flick animaiton duration
269 DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
273 int UtcDaliGridLayoutConstraintLeft(void)
275 ToolkitTestApplication application;
277 // Create the ItemView actor
278 TestItemFactory factory;
279 ItemView view = ItemView::New(factory);
280 Vector3 vec(480.0f, 800.0f, 0.0f);
281 GridLayoutPtr gridLayout = GridLayout::New();
282 gridLayout->SetNumberOfColumns(6);
284 view.SetName("view actor");
285 view.AddLayout(*gridLayout);
288 Stage::GetCurrent().Add(view);
289 gridLayout->SetOrientation(ControlOrientation::Left);
290 view.ActivateLayout(0, vec, 0.0f);
292 application.SendNotification();
293 application.Render(0);
296 for(int i = 0; i < 10; ++i)
298 application.Render(16); // 60hz frames
301 // Confirm: we have actors in the view and they are positioned some distance from the origin.
302 int nonZeroCount = 0;
303 int elementsFound = 0;
304 for(unsigned int i = 0; i < 10; i++)
306 Actor testActor = view.GetItem(i);
310 Vector3 pos = testActor.GetCurrentPosition();
312 if (pos.LengthSquared() > 0.0f)
319 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
320 Stage::GetCurrent().Remove(view);
324 int UtcDaliGridLayoutConstraintRight(void)
326 ToolkitTestApplication application;
328 // Create the ItemView actor
329 TestItemFactory factory;
330 ItemView view = ItemView::New(factory);
331 Vector3 vec(480.0f, 800.0f, 0.0f);
332 GridLayoutPtr gridLayout = GridLayout::New();
333 gridLayout->SetNumberOfColumns(6);
335 view.SetName("view actor");
336 view.AddLayout(*gridLayout);
339 Stage::GetCurrent().Add(view);
340 gridLayout->SetOrientation(ControlOrientation::Right);
341 view.ActivateLayout(0, vec, 0.0f);
343 application.SendNotification();
344 application.Render(0);
347 for(int i = 0; i < 10; ++i)
349 application.Render(16); // 60hz frames
352 // Confirm: we have actors in the view and they are positioned some distance from the origin.
353 int nonZeroCount = 0;
354 int elementsFound = 0;
355 for(unsigned int i = 0; i < 10; i++)
357 Actor testActor = view.GetItem(i);
361 Vector3 pos = testActor.GetCurrentPosition();
363 if (pos.LengthSquared() > 0.0f)
370 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
371 Stage::GetCurrent().Remove(view);
375 int UtcDaliGridLayoutConstraintUp(void)
377 ToolkitTestApplication application;
379 // Create the ItemView actor
380 TestItemFactory factory;
381 ItemView view = ItemView::New(factory);
382 Vector3 vec(480.0f, 800.0f, 0.0f);
383 GridLayoutPtr gridLayout = GridLayout::New();
384 gridLayout->SetNumberOfColumns(6);
386 view.SetName("view actor");
387 view.AddLayout(*gridLayout);
390 Stage::GetCurrent().Add(view);
391 gridLayout->SetOrientation(ControlOrientation::Up);
392 view.ActivateLayout(0, vec, 0.0f);
394 application.SendNotification();
395 application.Render(0);
398 for(int i = 0; i < 10; ++i)
400 application.Render(16); // 60hz frames
403 // Confirm: we have actors in the view and they are positioned some distance from the origin.
404 int nonZeroCount = 0;
405 int elementsFound = 0;
406 for(unsigned int i = 0; i < 10; i++)
408 Actor testActor = view.GetItem(i);
412 Vector3 pos = testActor.GetCurrentPosition();
414 if (pos.LengthSquared() > 0.0f)
421 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
423 ItemLayoutPtr layout = gridLayout;
424 layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
425 int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
426 DALI_TEST_CHECK(nextItem == 1);
428 Stage::GetCurrent().Remove(view);
432 int UtcDaliGridLayoutConstraintDown(void)
434 ToolkitTestApplication application;
436 // Create the ItemView actor
437 TestItemFactory factory;
438 ItemView view = ItemView::New(factory);
439 Vector3 vec(480.0f, 800.0f, 0.0f);
440 GridLayoutPtr gridLayout = GridLayout::New();
441 gridLayout->SetNumberOfColumns(6);
443 view.SetName("view actor");
444 view.AddLayout(*gridLayout);
447 Stage::GetCurrent().Add(view);
448 gridLayout->SetOrientation(ControlOrientation::Down);
449 view.ActivateLayout(0, vec, 0.0f);
451 application.SendNotification();
452 application.Render(0);
455 for(int i = 0; i < 10; ++i)
457 application.Render(16); // 60hz frames
460 // Confirm: we have actors in the view and they are positioned some distance from the origin.
461 int nonZeroCount = 0;
462 int elementsFound = 0;
463 for(unsigned int i = 0; i < 10; i++)
465 Actor testActor = view.GetItem(i);
469 Vector3 pos = testActor.GetCurrentPosition();
471 if (pos.LengthSquared() > 0.0f)
478 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
479 Stage::GetCurrent().Remove(view);
483 int UtcDaliGridLayoutScrollDirection(void)
485 ToolkitTestApplication application;
487 // Create the ItemView actor
488 TestItemFactory factory;
489 ItemView view = ItemView::New(factory);
490 Vector3 vec(480.0f, 800.0f, 0.0f);
491 GridLayoutPtr gridLayout = GridLayout::New();
492 gridLayout->SetNumberOfColumns(6);
494 view.SetName("view actor");
495 view.AddLayout(*gridLayout);
498 Stage::GetCurrent().Add(view);
499 gridLayout->SetOrientation(ControlOrientation::Left);
500 view.ActivateLayout(0, vec, 0.0f);
502 application.SendNotification();
503 application.Render(0);
505 ItemLayoutPtr layout = gridLayout;
508 for(int i = 0; i < 10; ++i)
510 application.Render(16); // 60hz frames
513 gridLayout->SetOrientation(ControlOrientation::Up);
514 view.ActivateLayout(0, vec, 0.0f);
515 application.SendNotification();
516 application.Render();
518 Degree deg = layout->GetScrollDirection();
519 DALI_TEST_CHECK(deg == 0.0f);
521 gridLayout->SetOrientation(ControlOrientation::Down);
522 view.ActivateLayout(0, vec, 0.0f);
523 application.SendNotification();
524 application.Render();
526 deg = layout->GetScrollDirection();
527 DALI_TEST_CHECK((deg == 180.0f));
529 layout->SetOrientation(ControlOrientation::Left);
530 view.ActivateLayout(0, vec, 0.0f);
531 application.SendNotification();
532 application.Render();
534 deg = layout->GetScrollDirection();
535 DALI_TEST_CHECK(deg == 90.f);
537 gridLayout->SetOrientation(ControlOrientation::Right);
538 view.ActivateLayout(0, vec, 0.0f);
539 application.SendNotification();
540 application.Render();
542 deg = layout->GetScrollDirection();
543 DALI_TEST_CHECK(deg == 270.0f);
545 Stage::GetCurrent().Remove(view);