Merge "Update keyboard focus direction enum for Control" into tizen
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ItemLayout.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <iostream>
19 #include <stdlib.h>
20
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>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29 using namespace Toolkit;
30
31 namespace
32 {
33 const unsigned int TOTAL_ITEM_NUMBER = 200;
34 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
35
36
37 // Implementation of ItemFactory for providing actors to ItemView
38 class TestItemFactory : public ItemFactory
39 {
40 public:
41
42   /**
43    * Constructor
44    * @param application class, stored as reference
45    */
46   TestItemFactory()
47   {
48   }
49
50   /**
51    * Virtual destructor.
52    */
53   virtual ~TestItemFactory()
54   {
55   }
56
57 public: // From ItemFactory
58
59   /**
60    * Query the number of items available from the factory.
61    * The maximum available item has an ID of GetNumberOfItems() - 1.
62    */
63   virtual unsigned int GetNumberOfItems()
64   {
65     return TOTAL_ITEM_NUMBER;
66   }
67
68   /**
69    * Create an Actor to represent a visible item.
70    * @param itemId
71    * @return the created actor.
72    */
73   virtual Actor NewItem(unsigned int itemId)
74   {
75     // Create an image actor for this item
76     Image image = ResourceImage::New( TEST_IMAGE_FILE_NAME );
77     Actor actor = ImageActor::New(image);
78
79     return actor;
80   }
81 };
82
83 class TestItemLayout;
84
85 typedef IntrusivePtr<TestItemLayout> TestItemLayoutPtr;
86
87 // Implementation of ItemLayout
88 class TestItemLayout : public ItemLayout
89 {
90 public:
91
92   /**
93    * Constructor
94    */
95   TestItemLayout()
96   {
97   }
98
99   /**
100    * Virtual destructor.
101    */
102   virtual ~TestItemLayout()
103   {
104   }
105
106   /**
107    * Create a new grid layout.
108    */
109   static TestItemLayoutPtr New()
110   {
111     return TestItemLayoutPtr(new TestItemLayout());
112   }
113
114 public: // From ItemLayout
115
116   /**
117    * Query the minimum valid layout position; this is a negative value.
118    *
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.
123    */
124   virtual float GetMinimumLayoutPosition(unsigned int numberOfItems, Vector3 layoutSize) const
125   {
126     return 0.0f;
127   }
128
129   /**
130    * Query the closest anchor position for the given layout position.
131    *
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.
136    */
137   virtual float GetClosestAnchorPosition(float layoutPosition) const
138   {
139     return 0.0f;
140   }
141
142   /**
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.
145    *
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.
148    */
149   virtual float GetItemScrollToPosition(unsigned int itemId) const
150   {
151     return 0.0f;
152   }
153
154   /**
155    * Query the items within a given layout-area.
156    *
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.
160    */
161   virtual ItemRange GetItemsWithinArea(float firstItemPosition, Vector3 layoutSize) const
162   {
163     return ItemRange(0, 10);
164   }
165
166   /**
167    * Query the number of items that should be reserved, for scrolling purposes.
168    *
169    * @param[in] layoutSize The size of the layout area.
170    * @return The number of extra items.
171    */
172   virtual unsigned int GetReserveItemCount(Vector3 layoutSize) const
173   {
174     return 0;
175   }
176
177   /**
178    * Retrieve the default size of an item in the layout.
179    *
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.
183    */
184   virtual void GetDefaultItemSize( unsigned int itemId, const Vector3& layoutSize, Vector3& itemSize ) const
185   {
186   }
187
188   /**
189    * Retrieve the resize animation in the layout.
190    *
191    * @param[in] animation The resize animation, not owned by the layout
192    * @param[in] actor The actor to animate
193    * @param [in] size The target size.
194    * @param [in] durationSeconds The duration of the resizing.
195    */
196   virtual void GetResizeAnimation(Animation& animation, Actor actor, Vector3 size, float durationSeconds) const
197   {
198   }
199
200   /**
201    * @brief Query the scroll direction of the layout.
202    * @return The scroll direction in degrees.
203    */
204   virtual Degree GetScrollDirection() const
205   {
206     return Degree( 0.0f );
207   }
208
209   /**
210    * @brief Query the scroll speed factor of the layout while dragging.
211    * @return The scroll speed factor of the layout.
212    */
213   virtual float GetScrollSpeedFactor() const
214   {
215     return 0;
216   }
217
218   /**
219    * @brief Query the maximum swipe speed in pixels per second.
220    * @return speed The maximum swipe speed.
221    */
222   virtual float GetMaximumSwipeSpeed() const
223   {
224     return 0;
225   }
226
227   /**
228    * @brief Get the duration of the flick animation in second.
229    * @return The duration of the flick animation.
230    */
231   virtual float GetItemFlickAnimationDuration() const
232   {
233     return 0;
234   }
235
236   /*
237    * @brief Applies constraints defined by the layout to an actor.
238    *
239    * @param[in] actor The actor to constrain.
240    * @param[in] itemId The ID of the item represented by the actor.
241    * @param[in] layoutSize the current size of the item view instance.
242    * @param[in] itemViewActor The item view instance which requests the application of constraints.
243    */
244   virtual void ApplyConstraints( Actor& actor, const int itemId, const Vector3& layoutSize, const Actor& itemViewActor )
245   {
246   }
247
248   /**
249    * @brief Gets the position of a given item
250    *
251    * @param[in] itemID id of the item we want to get its position
252    * @param[in] currentLayoutPosition the current layout position of the item view instance
253    * @param[in] layoutSize the current size of the item view instance
254    * @return The item position (x,y,z)
255    */
256   virtual Vector3 GetItemPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize) const
257   {
258     return Vector3::ZERO;
259   }
260 };
261
262 } // namespace
263
264 int UtcDaliItemLayoutSetAndGetOrientation(void)
265 {
266   ToolkitTestApplication application;
267
268   // Create the ItemView actor
269   TestItemFactory factory;
270   ItemView view = ItemView::New(factory);
271
272   // Create a grid layout and add it to ItemView
273   ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
274   view.AddLayout(*gridLayout);
275
276   // Set the orientation of the layout to be horizontal from left to right
277   ItemLayoutPtr layout = view.GetLayout(0);
278
279   DALI_TEST_CHECK(gridLayout == layout);
280
281   layout->SetOrientation(ControlOrientation::Left);
282
283   // Check the orientation of the layout is horizontal from left to right
284   DALI_TEST_CHECK(layout->GetOrientation() == ControlOrientation::Left);
285
286   Vector3 itemSize(100.0f, 100.0f, 100.0f);
287   layout->SetItemSize(itemSize);
288
289   Vector3 itemSize1;
290   layout->GetItemSize(0u, Vector3(Stage::GetCurrent().GetSize()), itemSize1);
291
292   DALI_TEST_CHECK(itemSize == itemSize1);
293
294   float position = layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(Stage::GetCurrent().GetSize()));
295
296   DALI_TEST_EQUALS(position, 0.0f, TEST_LOCATION);
297
298   int focusItem = layout->GetNextFocusItemID(0, TOTAL_ITEM_NUMBER, Control::KeyboardFocus::LEFT, true);
299
300   DALI_TEST_CHECK(focusItem != 0);
301
302   float flickSpeedFactor = layout->GetFlickSpeedFactor();
303
304   DALI_TEST_CHECK(flickSpeedFactor != 0.0f);
305
306   ItemLayoutPtr depthLayout = DefaultItemLayout::New( DefaultItemLayout::DEPTH );
307   view.AddLayout(*depthLayout);
308
309   layout = view.GetLayout(1);
310   DALI_TEST_CHECK(depthLayout == layout);
311
312   ItemLayoutPtr listLayout = DefaultItemLayout::New( DefaultItemLayout::LIST );
313   view.AddLayout(*listLayout);
314
315   layout = view.GetLayout(2);
316   DALI_TEST_CHECK(listLayout == layout);
317
318   ItemLayoutPtr spiralLayout = DefaultItemLayout::New( DefaultItemLayout::SPIRAL );
319   view.AddLayout(*spiralLayout);
320
321   layout = view.GetLayout(3);
322   DALI_TEST_CHECK(spiralLayout == layout);
323   END_TEST;
324 }
325
326 int UtcDaliItemLayoutGetExtension(void)
327 {
328   ToolkitTestApplication application;
329
330   ItemLayoutPtr gridLayout = DefaultItemLayout::New( DefaultItemLayout::GRID );
331   DALI_TEST_CHECK( gridLayout );
332   DALI_TEST_CHECK( !gridLayout->GetExtension() );
333
334   END_TEST;
335 }
336
337 int UtcDaliItemLayoutGetClosestOnScreenLayoutPosition(void)
338 {
339   ToolkitTestApplication application;
340
341   TestItemLayoutPtr layout = TestItemLayout::New();
342   DALI_TEST_CHECK( layout );
343   DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3::ZERO), 0.0f, TEST_LOCATION );
344   DALI_TEST_EQUALS(layout->GetClosestOnScreenLayoutPosition(0, 0.0f, Vector3(-800.0f, -1200.0f, 0.0f)), 0.0f, TEST_LOCATION );
345
346   END_TEST;
347 }
348
349 int UtcDaliItemLayoutGetNextFocusItemID(void)
350 {
351   ToolkitTestApplication application;
352
353   TestItemLayoutPtr layout = TestItemLayout::New();
354   DALI_TEST_CHECK( layout );
355   DALI_TEST_EQUALS(layout->GetNextFocusItemID(0, 100, Control::KeyboardFocus::LEFT, true), 99, TEST_LOCATION );
356   DALI_TEST_EQUALS(layout->GetNextFocusItemID(110, 100, Control::KeyboardFocus::RIGHT, true), 0, TEST_LOCATION );
357
358   END_TEST;
359 }
360