Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-ItemView.cpp
1 /*
2  * Copyright (c) 2014 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 #include <float.h>       // for FLT_MAX
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 using namespace Dali;
25 using namespace Toolkit;
26
27 namespace
28 {
29 const unsigned int TOTAL_ITEM_NUMBER = 100;
30 const char* TEST_IMAGE_FILE_NAME = "gallery_image_01.jpg";
31
32
33 // Implementation of ItemFactory for providing actors to ItemView
34 class TestItemFactory : public ItemFactory
35 {
36 public:
37
38   /**
39    * Constructor
40    * @param application class, stored as reference
41    */
42   TestItemFactory()
43   {
44   }
45
46 public: // From ItemFactory
47
48   /**
49    * Query the number of items available from the factory.
50    * The maximum available item has an ID of GetNumberOfItems() - 1.
51    */
52   virtual unsigned int GetNumberOfItems()
53   {
54     return TOTAL_ITEM_NUMBER;
55   }
56
57   /**
58    * Create an Actor to represent a visible item.
59    * @param itemId
60    * @return the created actor.
61    */
62   virtual Actor NewItem(unsigned int itemId)
63   {
64     // Create an image actor for this item
65     Image image = Image::New( TEST_IMAGE_FILE_NAME );
66     Actor actor = ImageActor::New(image);
67
68     return actor;
69   }
70 };
71
72 } // namespace
73
74
75
76 int UtcDaliItemViewAddAndGetLayout(void)
77 {
78   ToolkitTestApplication application;
79
80   // Create the ItemView actor
81   TestItemFactory factory;
82   ItemView view = ItemView::New(factory);
83
84   // Create a grid layout and add it to ItemView
85   GridLayoutPtr gridLayout = GridLayout::New();
86   view.AddLayout(*gridLayout);
87
88   // As we have added one layout, check the number of layout is now 1
89   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
90
91   // Create a depth layout and add it to ItemView
92   DepthLayoutPtr depthLayout = DepthLayout::New();
93   view.AddLayout(*depthLayout);
94
95   // As we have added another layout, check the number of layout is now 2
96   DALI_TEST_CHECK(view.GetLayoutCount() == 2);
97
98   // Create a spiral layout and add it to ItemView
99   SpiralLayoutPtr spiralLayout = SpiralLayout::New();
100   view.AddLayout(*spiralLayout);
101
102   // As we have added another layout, check the number of layout is now 3
103   DALI_TEST_CHECK(view.GetLayoutCount() == 3);
104
105   // Check we are getting the correct layout from ItemView
106   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
107   DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
108   DALI_TEST_CHECK(view.GetLayout(2) == spiralLayout);
109   END_TEST;
110 }
111
112 int UtcDaliItemViewAddAndRemoveLayout(void)
113 {
114   ToolkitTestApplication application;
115
116   // Create the ItemView actor
117   TestItemFactory factory;
118   ItemView view = ItemView::New(factory);
119
120   // Create a grid layout and add it to ItemView
121   GridLayoutPtr gridLayout = GridLayout::New();
122   view.AddLayout(*gridLayout);
123
124   // As we have added one layout, check the number of layout is now 1
125   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
126
127   // Create a depth layout and add it to ItemView
128   DepthLayoutPtr depthLayout = DepthLayout::New();
129   view.AddLayout(*depthLayout);
130
131   // As we have added another layout, check the number of layout is now 2
132   DALI_TEST_CHECK(view.GetLayoutCount() == 2);
133
134   // Check we are getting the correct layout from ItemView
135   DALI_TEST_CHECK(view.GetLayout(0) == gridLayout);
136   DALI_TEST_CHECK(view.GetLayout(1) == depthLayout);
137
138   // Remove the grid layout
139   view.RemoveLayout(0);
140
141   // As we have removed the grid layout, check the number of layout is now 1
142   DALI_TEST_CHECK(view.GetLayoutCount() == 1);
143
144   // Check we are getting the correct layout from ItemView
145   DALI_TEST_CHECK(view.GetLayout(0) == depthLayout);
146
147   // Remove the depth layout
148   view.RemoveLayout(0);
149
150   // As we also removed the depth layout, check the number of layout is now 0
151   DALI_TEST_CHECK(view.GetLayoutCount() == 0);
152   END_TEST;
153 }
154
155 int UtcDaliItemViewActivateLayoutAndGetActiveLayout(void)
156 {
157   ToolkitTestApplication application;
158
159   // Create the ItemView actor
160   TestItemFactory factory;
161   ItemView view = ItemView::New(factory);
162
163   // Create a grid layout and add it to ItemView
164   GridLayoutPtr gridLayout = GridLayout::New();
165   view.AddLayout(*gridLayout);
166
167   // Create a depth layout and add it to ItemView
168   DepthLayoutPtr depthLayout = DepthLayout::New();
169   view.AddLayout(*depthLayout);
170
171   // Create a spiral layout and add it to ItemView
172   SpiralLayoutPtr spiralLayout = SpiralLayout::New();
173   view.AddLayout(*spiralLayout);
174
175   // As we have added three layouts, check the number of layout is now 3
176   DALI_TEST_CHECK(view.GetLayoutCount() == 3);
177
178   // Check there is no active layout at the moment
179   DALI_TEST_CHECK(view.GetActiveLayout() == NULL);
180
181   // Activate the depth layout
182   Vector3 stageSize(Dali::Stage::GetCurrent().GetSize());
183   view.ActivateLayout(1, stageSize, 0.5f);
184
185   // Check the current active layout is the depth layout
186   DALI_TEST_CHECK(view.GetActiveLayout() == depthLayout);
187
188   // Activate the grid layout
189   view.ActivateLayout(0, stageSize, 0.5f);
190
191   // Check the current active layout is the grid layout
192   DALI_TEST_CHECK(view.GetActiveLayout() == gridLayout);
193
194   // Activate the spiral layout
195   view.ActivateLayout(2, stageSize, 0.5f);
196
197   // Check the current active layout is the spiral layout
198   DALI_TEST_CHECK(view.GetActiveLayout() == spiralLayout);
199   END_TEST;
200 }