2 * Copyright (c) 2016 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 // 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;
33 const unsigned int TOTAL_ITEM_NUMBER = 200;
34 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
37 // Implementation of ItemFactory for providing actors to ItemView
38 class TestItemFactory : public ItemFactory
44 * @param application class, stored as reference
53 virtual ~TestItemFactory()
57 public: // From ItemFactory
60 * Query the number of items available from the factory.
61 * The maximum available item has an ID of GetNumberOfItems() - 1.
63 virtual unsigned int GetNumberOfItems()
65 return TOTAL_ITEM_NUMBER;
69 * Create an Actor to represent a visible item.
71 * @return the created actor.
73 virtual Actor NewItem(unsigned int itemId)
75 // Create a renderable actor for this item
76 Image image = ResourceImage::New( TEST_IMAGE_FILE_NAME );
77 Actor actor = CreateRenderableActor(image);
85 typedef IntrusivePtr<TestItemLayout> TestItemLayoutPtr;
87 // Implementation of ItemLayout
88 class TestItemLayout : public ItemLayout
100 * Virtual destructor.
102 virtual ~TestItemLayout()
107 * Create a new grid layout.
109 static TestItemLayoutPtr New()
111 return TestItemLayoutPtr(new TestItemLayout());
114 public: // From ItemLayout
117 * Query the minimum valid layout position; this is a negative value.
119 * When scrolling, the first item will move within the range 0 to GetMinimumLayoutPosition().
120 * @param[in] numberOfItems The current number of items in the layout.
121 * @param[in] layoutSize The size of the layout area.
122 * @return The minimum layout position.
124 virtual float GetMinimumLayoutPosition(unsigned int numberOfItems, Vector3 layoutSize) const
130 * Query the closest anchor position for the given layout position.
132 * This anchor position is the position where all the items in the layout are aligned to
133 * their rounded layout positions in integer.
134 * @param[in] layoutPosition The layout position.
135 * @return The closest anchor position for the given layout position.
137 virtual float GetClosestAnchorPosition(float layoutPosition) const
143 * Query the layout position for the first item in the layout to move to when the layout
144 * needs to scroll to a particular item.
146 * @param[in] itemId The ID of an item in the layout.
147 * @return The layout position for the first item in the layout to move to.
149 virtual float GetItemScrollToPosition(unsigned int itemId) const
155 * Query the items within a given layout-area.
157 * @param[in] firstItemPosition The layout-position of the first item in the layout.
158 * @param[in] layoutSize The size of the layout area.
159 * @return The ID of the first & last visible item.
161 virtual ItemRange GetItemsWithinArea(float firstItemPosition, Vector3 layoutSize) const
163 return ItemRange(0, 10);
167 * Query the number of items that should be reserved, for scrolling purposes.
169 * @param[in] layoutSize The size of the layout area.
170 * @return The number of extra items.
172 virtual unsigned int GetReserveItemCount(Vector3 layoutSize) const
178 * Retrieve the default size of an item in the layout.
180 * @param[in] itemId The ID of an item in the layout.
181 * @param[in] layoutSize The layout size
182 * @param[out] itemSize The target size of an item.
184 virtual void GetDefaultItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
189 * @brief Query the scroll direction of the layout.
190 * @return The scroll direction in degrees.
192 virtual Degree GetScrollDirection() const
194 return Degree( 0.0f );
198 * @brief Query the scroll speed factor of the layout while dragging.
199 * @return The scroll speed factor of the layout.
201 virtual float GetScrollSpeedFactor() const
207 * @brief Query the maximum swipe speed in pixels per second.
208 * @return speed The maximum swipe speed.
210 virtual float GetMaximumSwipeSpeed() const
216 * @brief Get the duration of the flick animation in second.
217 * @return The duration of the flick animation.
219 virtual float GetItemFlickAnimationDuration() const
225 * @brief Applies constraints defined by the layout to an actor.
227 * @param[in] actor The actor to constrain.
228 * @param[in] itemId The ID of the item represented by the actor.
229 * @param[in] layoutSize the current size of the item view instance.
230 * @param[in] itemViewActor The item view instance which requests the application of constraints.
232 virtual void ApplyConstraints( Actor& actor, const int itemId, const Vector3& layoutSize, const Actor& itemViewActor )
237 * @brief Gets the position of a given item
239 * @param[in] itemID id of the item we want to get its position
240 * @param[in] currentLayoutPosition the current layout position of the item view instance
241 * @param[in] layoutSize the current size of the item view instance
242 * @return The item position (x,y,z)
244 virtual Vector3 GetItemPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize) const
246 return Vector3::ZERO;
252 int UtcDaliItemLayoutSetAndGetOrientation(void)
254 ToolkitTestApplication application;
256 // Create the ItemView actor
257 TestItemFactory factory;
258 ItemView view = ItemView::New(factory);
260 // Create a grid layout and add it to ItemView
261 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
262 view.AddLayout(*gridLayout);
264 // Set the orientation of the layout to be horizontal from left to right
265 ItemLayoutPtr layout = view.GetLayout(0);
267 DALI_TEST_CHECK(gridLayout == layout);
269 layout->SetOrientation(ControlOrientation::Left);
271 // Check the orientation of the layout is horizontal from left to right
272 DALI_TEST_CHECK(layout->GetOrientation() == ControlOrientation::Left);
274 Vector3 itemSize(100.0f, 100.0f, 100.0f);
275 layout->SetItemSize(itemSize);
278 layout->GetItemSize(0u, Vector3(Stage::GetCurrent().GetSize()), itemSize1);
280 DALI_TEST_CHECK(itemSize == itemSize1);
282 float position = layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(Stage::GetCurrent().GetSize()));
284 DALI_TEST_EQUALS(position, 0.0f, TEST_LOCATION);
286 int focusItem = layout->GetNextFocusItemID(0, TOTAL_ITEM_NUMBER, Control::KeyboardFocus::LEFT, true);
288 DALI_TEST_CHECK(focusItem != 0);
290 float flickSpeedFactor = layout->GetFlickSpeedFactor();
292 DALI_TEST_CHECK(flickSpeedFactor != 0.0f);
294 // White box test here: -( itemId / NoOfItemsPerRow(default 4) ) * NoOfItemsPerRow
295 DALI_TEST_EQUALS( -1.0f, gridLayout->GetItemScrollToPosition( 1 ), TEST_LOCATION );
297 ItemLayoutPtr depthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
298 view.AddLayout(*depthLayout);
300 layout = view.GetLayout(1);
301 DALI_TEST_CHECK(depthLayout == layout);
303 ItemLayoutPtr listLayout = DefaultItemLayout::New( DefaultItemLayout::LIST );
304 view.AddLayout(*listLayout);
306 layout = view.GetLayout(2);
307 DALI_TEST_CHECK(listLayout == layout);
309 ItemLayoutPtr spiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
310 view.AddLayout(*spiralLayout);
312 layout = view.GetLayout(3);
313 DALI_TEST_CHECK(spiralLayout == layout);
317 int UtcDaliItemLayoutGetExtension(void)
319 ToolkitTestApplication application;
321 ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
322 DALI_TEST_CHECK( gridLayout );
323 DALI_TEST_CHECK( !gridLayout->GetExtension() );
328 int UtcDaliItemLayoutGetClosestOnScreenLayoutPosition(void)
330 ToolkitTestApplication application;
332 TestItemLayoutPtr layout = TestItemLayout::New();
333 DALI_TEST_CHECK( layout );
334 DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3::ZERO), 0.0f, TEST_LOCATION );
335 DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(-800.0f, -1200.0f, 0.0f)), 0.0f, TEST_LOCATION );
340 int UtcDaliItemLayoutGetNextFocusItemID(void)
342 ToolkitTestApplication application;
344 TestItemLayoutPtr layout = TestItemLayout::New();
345 DALI_TEST_CHECK( layout );
346 DALI_TEST_EQUALS(layout->GetNextFocusItemID(0, 100, Control::KeyboardFocus::LEFT, true), 99, TEST_LOCATION );
347 DALI_TEST_EQUALS(layout->GetNextFocusItemID(110, 100, Control::KeyboardFocus::RIGHT, true), 0, TEST_LOCATION );
352 int UtcDaliItemRangeIntersection(void)
354 ToolkitTestApplication application;
356 unsigned int uBeginItemFirst = 100u, uEndItemFirst = 300u;
357 unsigned int uBeginItemSecond = 290u, uEndItemSecond = 400;
358 unsigned int uInterBeginCheck=290u , uInterEndCheck=301u;
359 bool bIsInThisRange = false, bOutOfThisRange = false;
361 Toolkit::ItemRange objItemRangeFirst(uBeginItemFirst, uEndItemFirst);
362 Toolkit::ItemRange objItemRangeSecond(uBeginItemSecond, uEndItemSecond);
363 ItemRange itmInterSect = objItemRangeFirst.Intersection(objItemRangeSecond);
365 bIsInThisRange = itmInterSect.Within(uInterBeginCheck);
366 DALI_TEST_EQUALS(bIsInThisRange, true, TEST_LOCATION );
368 bOutOfThisRange = itmInterSect.Within(uInterEndCheck);
369 DALI_TEST_EQUALS(bOutOfThisRange, false, TEST_LOCATION );