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-test-suite-utils.h>
29 using namespace Toolkit;
33 const unsigned int TOTAL_ITEM_NUMBER = 200;
35 Vector3 GridLayoutItemSizeFunction(unsigned int numberOfColumns, float layoutWidth, float sideMargin, float columnSpacing)
37 float width = (layoutWidth - sideMargin * 2.0f - columnSpacing * static_cast<float>(numberOfColumns - 1)) / static_cast<float>(numberOfColumns);
39 return Vector3(width, width, width);
44 static void Startup();
45 static void Cleanup();
47 // Implementation of ItemFactory for providing actors to ItemView
48 class TestItemFactory : public ItemFactory
54 * @param application class, stored as reference
60 public: // From ItemFactory
63 * Query the number of items available from the factory.
64 * The maximum available item has an ID of GetNumberOfItems() - 1.
66 virtual unsigned int GetNumberOfItems()
68 return TOTAL_ITEM_NUMBER;
72 * Create an Actor to represent a visible item.
74 * @return the created actor.
76 virtual Actor NewItem(unsigned int itemId)
78 // Create an test actor for this item
79 ImageActor actor = CreateSolidColorActor(Color::RED);
80 actor.SetSize(64.0f, 64.0f);
86 void (*tet_startup)() = Startup;
87 void (*tet_cleanup)() = Cleanup;
90 static void UtcDaliGridLayoutNew();
91 static void UtcDaliGridLayoutSetAndGetNumberOfColumns();
92 static void UtcDaliGridLayoutSetAndGetRowSpacing();
93 static void UtcDaliGridLayoutSetAndGetColumnSpacing();
94 static void UtcDaliGridLayoutSetAndGetTopMargin();
95 static void UtcDaliGridLayoutSetAndGetBottomMargin();
96 static void UtcDaliGridLayoutSetAndGetSideMargin();
97 static void UtcDaliGridLayoutSetAndGetZGap();
98 static void UtcDaliGridLayoutSetAndGetItemSizeFunction();
99 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor();
100 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed();
101 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration();
102 static void UtcDaliGridLayoutConstraintLeft();
103 static void UtcDaliGridLayoutConstraintRight();
104 static void UtcDaliGridLayoutConstraintUp();
105 static void UtcDaliGridLayoutConstraintDown();
106 static void UtcDaliGridLayoutScrollDirection();
109 POSITIVE_TC_IDX = 0x01,
113 // Add test functionality for all APIs in the class (Positive and Negative)
115 struct tet_testlist tet_testlist[] = {
116 { UtcDaliGridLayoutNew, POSITIVE_TC_IDX },
117 { UtcDaliGridLayoutSetAndGetNumberOfColumns, POSITIVE_TC_IDX },
118 { UtcDaliGridLayoutSetAndGetRowSpacing, POSITIVE_TC_IDX },
119 { UtcDaliGridLayoutSetAndGetColumnSpacing, POSITIVE_TC_IDX },
120 { UtcDaliGridLayoutSetAndGetTopMargin, POSITIVE_TC_IDX },
121 { UtcDaliGridLayoutSetAndGetBottomMargin, POSITIVE_TC_IDX },
122 { UtcDaliGridLayoutSetAndGetSideMargin, POSITIVE_TC_IDX },
123 { UtcDaliGridLayoutSetAndGetZGap, POSITIVE_TC_IDX },
124 { UtcDaliGridLayoutSetAndGetItemSizeFunction, POSITIVE_TC_IDX },
125 { UtcDaliGridLayoutSetAndGetScrollSpeedFactor, POSITIVE_TC_IDX },
126 { UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed, POSITIVE_TC_IDX },
127 { UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration, POSITIVE_TC_IDX },
128 { UtcDaliGridLayoutConstraintLeft, POSITIVE_TC_IDX },
129 { UtcDaliGridLayoutConstraintRight, POSITIVE_TC_IDX },
130 { UtcDaliGridLayoutConstraintUp, POSITIVE_TC_IDX },
131 { UtcDaliGridLayoutConstraintDown, POSITIVE_TC_IDX },
132 { UtcDaliGridLayoutScrollDirection, POSITIVE_TC_IDX },
137 // Called only once before first test is run.
138 static void Startup()
142 // Called only once after last test is run
143 static void Cleanup()
147 static void UtcDaliGridLayoutNew()
149 ToolkitTestApplication application;
151 // Create a grid layout
152 GridLayoutPtr gridLayout = GridLayout::New();
154 DALI_TEST_CHECK(gridLayout);
157 static void UtcDaliGridLayoutSetAndGetNumberOfColumns()
159 ToolkitTestApplication application;
161 // Create a grid layout
162 GridLayoutPtr gridLayout = GridLayout::New();
164 // Set the number of columns
165 gridLayout->SetNumberOfColumns(6);
167 // Check whether we get the correct number of columns
168 DALI_TEST_CHECK(gridLayout->GetNumberOfColumns() == 6);
171 static void UtcDaliGridLayoutSetAndGetRowSpacing()
173 ToolkitTestApplication application;
175 // Create a grid layout
176 GridLayoutPtr gridLayout = GridLayout::New();
178 // Set the row spacing
179 gridLayout->SetRowSpacing(10.0f);
181 // Check whether we get the correct row spacing
182 DALI_TEST_EQUALS(gridLayout->GetRowSpacing(), 10.0f, TEST_LOCATION );
185 static void UtcDaliGridLayoutSetAndGetColumnSpacing()
187 ToolkitTestApplication application;
189 // Create a grid layout
190 GridLayoutPtr gridLayout = GridLayout::New();
192 // Set the column spacing
193 gridLayout->SetColumnSpacing(10.0f);
195 // Check whether we get the correct column spacing
196 DALI_TEST_EQUALS(gridLayout->GetColumnSpacing(), 10.0f, TEST_LOCATION );
199 static void UtcDaliGridLayoutSetAndGetTopMargin()
201 ToolkitTestApplication application;
203 // Create a grid layout
204 GridLayoutPtr gridLayout = GridLayout::New();
206 // Set the top margin
207 gridLayout->SetTopMargin(30.0f);
209 // Check whether we get the correct top margin
210 DALI_TEST_EQUALS(gridLayout->GetTopMargin(), 30.0f, TEST_LOCATION );
213 static void UtcDaliGridLayoutSetAndGetBottomMargin()
215 ToolkitTestApplication application;
217 // Create a grid layout
218 GridLayoutPtr gridLayout = GridLayout::New();
220 // Set the bottom margin
221 gridLayout->SetBottomMargin(30.0f);
223 // Check whether we get the correct bottom margin
224 DALI_TEST_EQUALS(gridLayout->GetBottomMargin(), 30.0f, TEST_LOCATION );
227 static void UtcDaliGridLayoutSetAndGetSideMargin()
229 ToolkitTestApplication application;
231 // Create a grid layout
232 GridLayoutPtr gridLayout = GridLayout::New();
234 // Set the side margin
235 gridLayout->SetSideMargin(10.0f);
237 // Check whether we get the correct side margin
238 DALI_TEST_EQUALS(gridLayout->GetSideMargin(), 10.0f, TEST_LOCATION );
241 static void UtcDaliGridLayoutSetAndGetZGap()
243 ToolkitTestApplication application;
245 // Create a grid layout
246 GridLayoutPtr gridLayout = GridLayout::New();
248 // Set the gap of items in the Z axis in different columns
249 gridLayout->SetZGap(5.0f);
251 // Check whether we get the correct Z gap
252 DALI_TEST_EQUALS(gridLayout->GetZGap(), 5.0f, TEST_LOCATION );
255 static void UtcDaliGridLayoutSetAndGetItemSizeFunction()
257 ToolkitTestApplication application;
259 // Create a grid layout
260 GridLayoutPtr gridLayout = GridLayout::New();
262 // Set the item size function
263 gridLayout->SetItemSizeFunction(GridLayoutItemSizeFunction);
265 // Check whether we get the correct item size function
266 DALI_TEST_CHECK(gridLayout->GetItemSizeFunction() == GridLayoutItemSizeFunction);
269 static void UtcDaliGridLayoutSetAndGetScrollSpeedFactor()
271 ToolkitTestApplication application;
273 // Create a grid layout
274 GridLayoutPtr gridLayout = GridLayout::New();
276 // Set the scroll speed factor
277 gridLayout->SetScrollSpeedFactor(0.05f);
279 // Check whether we get the correct scroll speed factor
280 DALI_TEST_EQUALS(gridLayout->GetScrollSpeedFactor(), 0.05f, TEST_LOCATION );
283 static void UtcDaliGridLayoutSetAndGetMaximumSwipeSpeed()
285 ToolkitTestApplication application;
287 // Create a grid layout
288 GridLayoutPtr gridLayout = GridLayout::New();
290 // Set the maximum swipe speed
291 gridLayout->SetMaximumSwipeSpeed(50.0f);
293 // Check whether we get the correct maximum swipe speed
294 DALI_TEST_EQUALS(gridLayout->GetMaximumSwipeSpeed(), 50.0f, TEST_LOCATION );
297 static void UtcDaliGridLayoutSetAndGetItemFlickAnimationDuration()
299 ToolkitTestApplication application;
301 // Create a grid layout
302 GridLayoutPtr gridLayout = GridLayout::New();
304 // Set the flick animaiton duration
305 gridLayout->SetItemFlickAnimationDuration(0.35f);
307 // Check whether we get the correct flick animaiton duration
308 DALI_TEST_EQUALS( gridLayout->GetItemFlickAnimationDuration(), 0.35f, TEST_LOCATION );
311 static void UtcDaliGridLayoutConstraintLeft()
313 ToolkitTestApplication application;
315 // Create the ItemView actor
316 TestItemFactory factory;
317 ItemView view = ItemView::New(factory);
318 Vector3 vec(480.0f, 800.0f, 0.0f);
319 GridLayoutPtr gridLayout = GridLayout::New();
320 gridLayout->SetNumberOfColumns(6);
322 view.SetName("view actor");
323 view.AddLayout(*gridLayout);
326 Stage::GetCurrent().Add(view);
327 gridLayout->SetOrientation(ControlOrientation::Left);
328 view.ActivateLayout(0, vec, 0.0f);
330 application.SendNotification();
331 application.Render(0);
334 for(int i = 0; i < 10; ++i)
336 application.Render(16); // 60hz frames
339 // Confirm: we have actors in the view and they are positioned some distance from the origin.
340 int nonZeroCount = 0;
341 int elementsFound = 0;
342 for(unsigned int i = 0; i < 10; i++)
344 Actor testActor = view.GetItem(i);
348 Vector3 pos = testActor.GetCurrentPosition();
350 if (pos.LengthSquared() > 0.0f)
357 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
358 Stage::GetCurrent().Remove(view);
361 static void UtcDaliGridLayoutConstraintRight()
363 ToolkitTestApplication application;
365 // Create the ItemView actor
366 TestItemFactory factory;
367 ItemView view = ItemView::New(factory);
368 Vector3 vec(480.0f, 800.0f, 0.0f);
369 GridLayoutPtr gridLayout = GridLayout::New();
370 gridLayout->SetNumberOfColumns(6);
372 view.SetName("view actor");
373 view.AddLayout(*gridLayout);
376 Stage::GetCurrent().Add(view);
377 gridLayout->SetOrientation(ControlOrientation::Right);
378 view.ActivateLayout(0, vec, 0.0f);
380 application.SendNotification();
381 application.Render(0);
384 for(int i = 0; i < 10; ++i)
386 application.Render(16); // 60hz frames
389 // Confirm: we have actors in the view and they are positioned some distance from the origin.
390 int nonZeroCount = 0;
391 int elementsFound = 0;
392 for(unsigned int i = 0; i < 10; i++)
394 Actor testActor = view.GetItem(i);
398 Vector3 pos = testActor.GetCurrentPosition();
400 if (pos.LengthSquared() > 0.0f)
407 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
408 Stage::GetCurrent().Remove(view);
411 static void UtcDaliGridLayoutConstraintUp()
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 GridLayoutPtr gridLayout = GridLayout::New();
420 gridLayout->SetNumberOfColumns(6);
422 view.SetName("view actor");
423 view.AddLayout(*gridLayout);
426 Stage::GetCurrent().Add(view);
427 gridLayout->SetOrientation(ControlOrientation::Up);
428 view.ActivateLayout(0, vec, 0.0f);
430 application.SendNotification();
431 application.Render(0);
434 for(int i = 0; i < 10; ++i)
436 application.Render(16); // 60hz frames
439 // Confirm: we have actors in the view and they are positioned some distance from the origin.
440 int nonZeroCount = 0;
441 int elementsFound = 0;
442 for(unsigned int i = 0; i < 10; i++)
444 Actor testActor = view.GetItem(i);
448 Vector3 pos = testActor.GetCurrentPosition();
450 if (pos.LengthSquared() > 0.0f)
457 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
459 ItemLayoutPtr layout = gridLayout;
460 layout->GetClosestOnScreenLayoutPosition(0, 0.0f, vec);
461 int nextItem = layout->GetNextFocusItemID(0, 10, Dali::Toolkit::Control::Right, false);
462 DALI_TEST_CHECK(nextItem == 1);
464 Stage::GetCurrent().Remove(view);
467 static void UtcDaliGridLayoutConstraintDown()
469 ToolkitTestApplication application;
471 // Create the ItemView actor
472 TestItemFactory factory;
473 ItemView view = ItemView::New(factory);
474 Vector3 vec(480.0f, 800.0f, 0.0f);
475 GridLayoutPtr gridLayout = GridLayout::New();
476 gridLayout->SetNumberOfColumns(6);
478 view.SetName("view actor");
479 view.AddLayout(*gridLayout);
482 Stage::GetCurrent().Add(view);
483 gridLayout->SetOrientation(ControlOrientation::Down);
484 view.ActivateLayout(0, vec, 0.0f);
486 application.SendNotification();
487 application.Render(0);
490 for(int i = 0; i < 10; ++i)
492 application.Render(16); // 60hz frames
495 // Confirm: we have actors in the view and they are positioned some distance from the origin.
496 int nonZeroCount = 0;
497 int elementsFound = 0;
498 for(unsigned int i = 0; i < 10; i++)
500 Actor testActor = view.GetItem(i);
504 Vector3 pos = testActor.GetCurrentPosition();
506 if (pos.LengthSquared() > 0.0f)
513 DALI_TEST_CHECK((elementsFound > 0) && (nonZeroCount == elementsFound));
514 Stage::GetCurrent().Remove(view);
517 static void UtcDaliGridLayoutScrollDirection()
519 ToolkitTestApplication application;
521 // Create the ItemView actor
522 TestItemFactory factory;
523 ItemView view = ItemView::New(factory);
524 Vector3 vec(480.0f, 800.0f, 0.0f);
525 GridLayoutPtr gridLayout = GridLayout::New();
526 gridLayout->SetNumberOfColumns(6);
528 view.SetName("view actor");
529 view.AddLayout(*gridLayout);
532 Stage::GetCurrent().Add(view);
533 gridLayout->SetOrientation(ControlOrientation::Left);
534 view.ActivateLayout(0, vec, 0.0f);
536 application.SendNotification();
537 application.Render(0);
539 ItemLayoutPtr layout = gridLayout;
542 for(int i = 0; i < 10; ++i)
544 application.Render(16); // 60hz frames
547 gridLayout->SetOrientation(ControlOrientation::Up);
548 view.ActivateLayout(0, vec, 0.0f);
549 application.SendNotification();
550 application.Render();
552 Degree deg = layout->GetScrollDirection();
553 DALI_TEST_CHECK(deg == 0.0f);
555 gridLayout->SetOrientation(ControlOrientation::Down);
556 view.ActivateLayout(0, vec, 0.0f);
557 application.SendNotification();
558 application.Render();
560 deg = layout->GetScrollDirection();
561 DALI_TEST_CHECK((deg == 180.0f));
563 layout->SetOrientation(ControlOrientation::Left);
564 view.ActivateLayout(0, vec, 0.0f);
565 application.SendNotification();
566 application.Render();
568 deg = layout->GetScrollDirection();
569 DALI_TEST_CHECK(deg == 90.f);
571 gridLayout->SetOrientation(ControlOrientation::Right);
572 view.ActivateLayout(0, vec, 0.0f);
573 application.SendNotification();
574 application.Render();
576 deg = layout->GetScrollDirection();
577 DALI_TEST_CHECK(deg == 270.0f);
579 Stage::GetCurrent().Remove(view);