[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-toolkit.git] / automated-tests / dali-test-suite / navigation-frame / utc-Dali-NavigationControl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/dali.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit-test-suite-utils.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 static void Startup();
30 static void Cleanup();
31
32 namespace
33 {
34
35 static bool gObjectCreatedCallBackCalled;
36 static void TestCallback(BaseHandle handle)
37 {
38   gObjectCreatedCallBackCalled = true;
39 }
40
41 }
42
43 extern "C" {
44   void (*tet_startup)() = Startup;
45   void (*tet_cleanup)() = Cleanup;
46 }
47
48 static void UtcDaliNavigationControlNew();
49 static void UtcDaliNavigationControlDownCast();
50 static void UtcDaliNavigationControlPushItem();
51 static void UtcDaliNavigationControlPopItem();
52 static void UtcDaliNavigationControlGetItemCount();
53 static void UtcDaliNavigationControlGetItem();
54 static void UtcDaliNavigationControlGetCurrentItem();
55 static void UtcDaliNavigationControlSetBackground();
56 static void UtcDaliNavigationControlCreateNavigationToolBar();
57 static void UtcDaliNavigationControlCreateNavigationTitleBar();
58
59 enum {
60   POSITIVE_TC_IDX = 0x01,
61   NEGATIVE_TC_IDX,
62 };
63
64 // Add test functionality for all APIs in the class (Positive and Negative)
65 extern "C" {
66   struct tet_testlist tet_testlist[] = {
67     { UtcDaliNavigationControlNew, POSITIVE_TC_IDX },
68     { UtcDaliNavigationControlDownCast, POSITIVE_TC_IDX },
69     { UtcDaliNavigationControlPushItem, POSITIVE_TC_IDX },
70     { UtcDaliNavigationControlPopItem, POSITIVE_TC_IDX },
71     { UtcDaliNavigationControlGetItemCount, POSITIVE_TC_IDX },
72     { UtcDaliNavigationControlGetItem, POSITIVE_TC_IDX },
73     { UtcDaliNavigationControlGetCurrentItem, POSITIVE_TC_IDX },
74     { UtcDaliNavigationControlSetBackground, POSITIVE_TC_IDX },
75     { UtcDaliNavigationControlCreateNavigationToolBar, POSITIVE_TC_IDX },
76     { UtcDaliNavigationControlCreateNavigationTitleBar, POSITIVE_TC_IDX },
77     { NULL, 0 }
78   };
79 }
80
81 // Called only once before first test is run.
82 static void Startup()
83 {
84 }
85
86 // Called only once after last test is run
87 static void Cleanup()
88 {
89 }
90
91
92 static void UtcDaliNavigationControlNew()
93 {
94   ToolkitTestApplication application;
95   tet_infoline("UtcDaliNavigationControlNew");
96
97   NavigationControl naviControl;
98   // Check that this handle is uninitialized
99   DALI_TEST_CHECK( !naviControl );
100
101   naviControl = NavigationControl::New();
102   // Check that the Dali resource is successfully created
103   DALI_TEST_CHECK( naviControl );
104
105   NavigationControl naviControl2( naviControl );
106   DALI_TEST_CHECK( naviControl2 == naviControl );
107
108   //Additional check to ensure object is created by checking whether it is registered
109   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
110   DALI_TEST_CHECK( registry );
111   gObjectCreatedCallBackCalled = false;
112   registry.ObjectCreatedSignal().Connect( TestCallback );
113   {
114     NavigationControl naviControl = NavigationControl::New();
115   }
116   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
117 }
118
119 static void UtcDaliNavigationControlDownCast()
120 {
121   ToolkitTestApplication application;
122   tet_infoline( "UtcDaliNavigationControlDownCast" );
123
124   NavigationControl naviControl = NavigationControl::New();
125   BaseHandle handle( naviControl );
126
127   NavigationControl newNaviControl = NavigationControl::DownCast( handle );
128   DALI_TEST_CHECK( naviControl );
129   DALI_TEST_CHECK( newNaviControl == naviControl );
130 }
131
132 static void UtcDaliNavigationControlPushItem()
133 {
134   ToolkitTestApplication application;
135   tet_infoline( "UtcDaliNavigationControlPushItem" );
136
137   // Create a NavigationControl object, and add it to stage
138   NavigationControl naviControl = NavigationControl::New();
139   Stage::GetCurrent().Add(naviControl);
140   // Check there is no item in the stack
141   DALI_TEST_CHECK( naviControl.GetItemCount() == 0 );
142
143   // Create two NavigationItem objects
144   Page firstItem = Page::New();
145   Page secondItem = Page::New();
146
147   // Push the first item into stack
148   naviControl.PushItem( firstItem );
149   // Check the item count in stack
150   DALI_TEST_CHECK( naviControl.GetItemCount() == 1 );
151   // Check the current item
152   DALI_TEST_CHECK( naviControl.GetCurrentItem() == firstItem );
153   // Check that the newly pushed item is displayed on stage
154   DALI_TEST_CHECK( firstItem.OnStage() );
155
156   // Push the second item into stack
157   naviControl.PushItem( secondItem );
158   // Check the item count in stack
159   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
160   // Check the current item
161   DALI_TEST_CHECK( naviControl.GetCurrentItem() == secondItem );
162   // Check the bottom item in the stack
163   DALI_TEST_CHECK( naviControl.GetItem(0) == firstItem );
164   // Check that the previous item is off stage
165   DALI_TEST_CHECK( !firstItem.OnStage() );
166   // Check that the newly pushed item is displayed on stage
167   DALI_TEST_CHECK( secondItem.OnStage() );
168
169   Page thirdItem;
170   Page fourthItem(secondItem);
171   naviControl.PushItem( thirdItem );
172   // Check that an uninitialized item cannot be pushed into the stack
173   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
174   naviControl.PushItem( fourthItem );
175   // Check that an duplicated item with the current item cannot be pushed into the stack
176   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
177   // Check that the current item and the item on the stage is still the secondItem
178   DALI_TEST_CHECK( naviControl.GetCurrentItem() == secondItem );
179   DALI_TEST_CHECK( secondItem.OnStage() );
180 }
181
182 static void UtcDaliNavigationControlPopItem()
183 {
184   ToolkitTestApplication application;
185   tet_infoline( "UtcDaliNavigationControlPopItem" );
186
187   // Create a NavigationControl object, and add it to stage
188   NavigationControl naviControl = NavigationControl::New();
189   Stage::GetCurrent().Add(naviControl);
190   // Create three NavigationItem objects
191   Page firstItem = Page::New();
192   Page secondItem = Page::New();
193   Page thirdItem = Page::New();
194   naviControl.PushItem( firstItem );
195   naviControl.PushItem( secondItem );
196   naviControl.PushItem( thirdItem );
197
198   DALI_TEST_CHECK( naviControl.GetItemCount() == 3 );
199
200   // pop an item out from the stack
201   Page poppedItem = naviControl.PopItem();
202   // check that the item count is decrease by one
203   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
204   // check that the item popped out is the thirdItem
205   DALI_TEST_CHECK( poppedItem == thirdItem );
206   // check that the item popped out is disappeared from the stage
207   DALI_TEST_CHECK( !poppedItem.OnStage() );
208   // check that the new top item is displayed on the stage
209   DALI_TEST_CHECK( secondItem.OnStage() );
210
211   // repeat the above steps again
212   poppedItem = naviControl.PopItem();
213   DALI_TEST_CHECK( naviControl.GetItemCount() == 1 );
214   DALI_TEST_CHECK( poppedItem == secondItem );
215   DALI_TEST_CHECK( !poppedItem.OnStage() );
216   DALI_TEST_CHECK( firstItem.OnStage() );
217
218   // check that the bottom-most item can not be popped out from the stack
219   poppedItem = naviControl.PopItem();
220   // when trying to pop the bottom-most item, it returns an uninitialized handle and does nothing else
221   DALI_TEST_CHECK( !poppedItem );
222   DALI_TEST_CHECK( naviControl.GetItemCount() == 1 );
223   DALI_TEST_CHECK( firstItem.OnStage() );
224 }
225
226 static void UtcDaliNavigationControlGetItemCount()
227 {
228   ToolkitTestApplication application;
229   tet_infoline( "UtcDaliNavigationControlGetItemCount" );
230
231   // Create a NavigationControl object
232   NavigationControl naviControl = NavigationControl::New();
233   // Create three NavigationItem objects
234   Page firstItem = Page::New();
235   Page secondItem = Page::New();
236   Page thirdItem = Page::New();
237
238   DALI_TEST_CHECK( naviControl.GetItemCount() == 0 );
239   naviControl.PushItem( firstItem );
240   DALI_TEST_CHECK( naviControl.GetItemCount() == 1 );
241   naviControl.PushItem( secondItem );
242   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
243   naviControl.PushItem( thirdItem );
244   DALI_TEST_CHECK( naviControl.GetItemCount() == 3 );
245   naviControl.PopItem();
246   DALI_TEST_CHECK( naviControl.GetItemCount() == 2 );
247   naviControl.PopItem();
248   DALI_TEST_CHECK( naviControl.GetItemCount() == 1 );
249 }
250
251 static void UtcDaliNavigationControlGetItem()
252 {
253   ToolkitTestApplication application;
254   tet_infoline( "UtcDaliNavigationControlGetItem" );
255
256   // Create a NavigationControl object
257   NavigationControl naviControl = NavigationControl::New();
258   // Create three NavigationItem objects and push them into stack
259   Page firstItem = Page::New();
260   Page secondItem = Page::New();
261   Page thirdItem = Page::New();
262   naviControl.PushItem( firstItem );
263   naviControl.PushItem( secondItem );
264   naviControl.PushItem( thirdItem );
265
266   // check every item by get it by index
267   DALI_TEST_CHECK( naviControl.GetItem(0) == firstItem );
268   DALI_TEST_CHECK( naviControl.GetItem(1) == secondItem );
269   DALI_TEST_CHECK( naviControl.GetItem(2) == thirdItem);
270 }
271
272 static void UtcDaliNavigationControlGetCurrentItem()
273 {
274   ToolkitTestApplication application;
275   tet_infoline( "UtcDaliNavigationControlGetCurrentItem" );
276
277   // Create a NavigationControl object
278   NavigationControl naviControl = NavigationControl::New();
279   // Create three NavigationItem objects
280   Page firstItem = Page::New();
281   Page secondItem = Page::New();
282   Page thirdItem = Page::New();
283
284   naviControl.PushItem( firstItem );
285   DALI_TEST_CHECK( naviControl.GetCurrentItem() == firstItem );
286   naviControl.PushItem( secondItem );
287   DALI_TEST_CHECK( naviControl.GetCurrentItem() == secondItem );
288   naviControl.PushItem( thirdItem );
289   DALI_TEST_CHECK( naviControl.GetCurrentItem() == thirdItem );
290   naviControl.PopItem();
291   DALI_TEST_CHECK( naviControl.GetCurrentItem() == secondItem );
292   naviControl.PopItem();
293   DALI_TEST_CHECK( naviControl.GetCurrentItem() == firstItem );
294 }
295
296 static void UtcDaliNavigationControlSetBackground()
297 {
298   ToolkitTestApplication application;
299   tet_infoline( "UtcDaliNavigationControlSetBackground" );
300
301   try
302   {
303     NavigationControl naviControl = NavigationControl::New();
304     Stage::GetCurrent().Add( naviControl );
305
306     ImageActor background = CreateSolidColorActor( Color::RED );
307     naviControl.SetBackground( background );
308   }
309   catch (Dali::DaliException& e)
310   {
311     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
312     DALI_TEST_EQUALS(e.mCondition, "segmentIndex+1 < mKnots.size() && segmentIndex < mKnots.size()", TEST_LOCATION);
313     tet_result(TET_FAIL);
314   }
315
316   tet_result(TET_PASS);
317 }
318
319 static void UtcDaliNavigationControlCreateNavigationToolBar()
320 {
321   ToolkitTestApplication application;
322   tet_infoline( "UtcDaliNavigationControlCreateNavigationToolBar" );
323
324   ImageActor background = CreateSolidColorActor( Color::RED );
325   Stage stage = Stage::GetCurrent();
326
327   NavigationControl naviControl = NavigationControl::New();
328   stage.Add( naviControl );
329
330   Toolkit::NaviToolBarStyle toolbarStyle( background, 720, 98, 496, 182, 72, 16, 63, 26);
331
332   naviControl.CreateNavigationToolBar( toolbarStyle, toolbarStyle);
333
334   Page naviItem = Page::New();
335   PushButton firstControl = PushButton::New();
336   naviItem.AddControlToToolBar(firstControl, Alignment::HorizontalLeft);
337   PushButton secondControl = PushButton::New();
338   naviItem.AddControlToToolBar(secondControl, Alignment::HorizontalCenter);
339   PushButton thirdControl = PushButton::New();
340   naviItem.AddControlToToolBar(thirdControl, Alignment::HorizontalCenter);
341   PushButton fourthControl = PushButton::New();
342   naviItem.AddControlToToolBar(fourthControl, Alignment::HorizontalRight);
343   PushButton fifthControl = PushButton::New();
344   naviItem.AddControlToToolBar(fifthControl, Alignment::HorizontalRight);
345
346   naviControl.PushItem( naviItem );
347
348   DALI_TEST_CHECK( firstControl.OnStage() );
349   // Can add multiple controls to the central group
350   DALI_TEST_CHECK( secondControl.OnStage() );
351   DALI_TEST_CHECK( thirdControl.OnStage() );
352   // Can only have one control in the side groups
353   DALI_TEST_CHECK( !fourthControl.OnStage() );
354   DALI_TEST_CHECK( fifthControl.OnStage() );
355
356 }
357
358 static void UtcDaliNavigationControlCreateNavigationTitleBar()
359 {
360   ToolkitTestApplication application;
361   tet_infoline( "UtcDaliNavigationControlCreateNavigationTitleBar" );
362
363   ImageActor background = CreateSolidColorActor( Color::RED );
364   TextStyle textStyle;
365   Stage stage = Stage::GetCurrent();
366
367   NavigationControl naviControl = NavigationControl::New();
368   stage.Add( naviControl );
369
370   Toolkit::NaviTitleBarStyle titleBarStyle( background, textStyle, textStyle, 720, 111, 68, 48, 34, 16, 11, 45, 63, 26, 14, 22 );
371   naviControl.CreateNavigationTitleBar( titleBarStyle, titleBarStyle );
372
373   Page naviItem = Page::New();
374
375   PushButton firstControl = PushButton::New();
376   naviItem.AddControlToTitleBar( firstControl );
377   PushButton secondControl = PushButton::New();
378   naviItem.AddControlToTitleBar( secondControl );
379
380   Actor titleIcon = Actor::New();
381   naviItem.SetTitleIcon( titleIcon );
382
383   naviControl.PushItem( naviItem );
384
385   DALI_TEST_CHECK( firstControl.OnStage() );
386   DALI_TEST_CHECK( secondControl.OnStage() );
387   DALI_TEST_CHECK( titleIcon.OnStage() );
388 }