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