Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-Popup.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
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/integration-api/events/touch-event-integ.h>
27 #include <dali-toolkit/dali-toolkit.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 void utc_dali_toolkit_popup_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_popup_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42 namespace
43 {
44 static bool gObjectCreatedCallBackCalled;
45
46 static void TestCallback(BaseHandle handle)
47 {
48   gObjectCreatedCallBackCalled = true;
49 }
50
51 const int RENDER_FRAME_INTERVAL = 10;                          ///< Duration of each frame in ms.
52 const int RENDER_ANIMATION_TEST_DURATION_MS = 1000;            ///< 1000ms to test animation
53 const int RENDER_ANIMATION_TEST_DURATION_FRAMES = RENDER_ANIMATION_TEST_DURATION_MS / RENDER_FRAME_INTERVAL; ///< equivalent frames.
54 const Vector3 DEFAULT_BUTTON_SIZE(100.0f, 50.0f, 0.0f);
55 const Dali::TouchPoint pointDownOutside( 0, TouchPoint::Down, 10.0f, 10.0f );
56 const Dali::TouchPoint pointUpOutside( 0, TouchPoint::Up, 10.0f, 10.0f );
57
58 /**
59  * Counts how many descendents root Actor has, including
60  * itself.
61  *
62  * @param[in] root The root actor to count from.
63  * @return The number of descendents including root actor itself.
64  */
65 int DescendentCount(const Actor& root)
66 {
67   unsigned int numChildren = root.GetChildCount();
68
69   int count = 1;
70
71   for(unsigned int i=0; i<numChildren; ++i)
72   {
73     count += DescendentCount(root.GetChildAt(i));
74   }
75
76   return count;
77 }
78
79 bool HasAncestor(Actor child, Actor ancestor)
80 {
81   while(child && child != ancestor)
82   {
83     child = child.GetParent();
84   }
85
86   return (child == ancestor);
87 }
88
89
90 static bool gHidden = false;
91
92 static void OnPopupHidden()
93 {
94   gHidden = true;
95 }
96
97 static bool gTouchedOutside;
98
99 static void OnPopupTouchedOutside()
100 {
101   gTouchedOutside = true;
102 }
103
104
105 } // anon namespace
106
107 int UtcDaliPopupNew(void)
108 {
109   ToolkitTestApplication application;
110   tet_infoline(" UtcDaliPopupNew");
111
112   // Create the Popup actor
113   Popup popup;
114
115   DALI_TEST_CHECK( !popup );
116
117   popup = Popup::New();
118
119   DALI_TEST_CHECK( popup );
120
121   Popup popup2(popup);
122
123   DALI_TEST_CHECK( popup2 == popup );
124
125   //Additional check to ensure object is created by checking if it's registered
126   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
127   DALI_TEST_CHECK( registry );
128
129   gObjectCreatedCallBackCalled = false;
130   registry.ObjectCreatedSignal().Connect( &TestCallback );
131   {
132     Popup popup = Popup::New();
133   }
134   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
135   END_TEST;
136 }
137
138 int UtcDaliPopupDestructor(void)
139 {
140   ToolkitTestApplication application;
141
142   Popup* popup = new Popup();
143   delete popup;
144
145   DALI_TEST_CHECK( true );
146   END_TEST;
147 }
148
149 int UtcDaliPopupDownCast(void)
150 {
151   ToolkitTestApplication application;
152
153   Handle handle = Popup::New();
154
155   Popup popup = Popup::DownCast( handle );
156
157   DALI_TEST_CHECK( popup == handle );
158   END_TEST;
159 }
160
161 int UtcDaliPopoupSetProperty(void)
162 {
163   tet_infoline("UtcDaliPopoupSetProperty: ");
164   ToolkitTestApplication application;
165
166   Popup popup = Popup::New();
167
168   //Test properties
169   std::string testString = "Hello World";
170   popup.SetProperty(popup.GetPropertyIndex("title"), testString);
171   DALI_TEST_EQUALS( testString, popup.GetTitle(), TEST_LOCATION );
172   END_TEST;
173 }
174
175
176 int UtcDaliPopupSetBackgroundImage(void)
177 {
178   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
179   tet_infoline(" UtcDaliPopupSetBackgroundImage");
180
181   // Create the Popup actor
182   Popup popup = Popup::New();
183   Stage::GetCurrent().Add( popup );
184
185   ImageActor image = CreateSolidColorActor( Color::RED );
186   DALI_TEST_CHECK( !image.GetParent() );
187   popup.SetBackgroundImage(image);
188   DALI_TEST_CHECK( image.GetParent() );
189   END_TEST;
190 }
191
192 int UtcDaliPopupSetTitle(void)
193 {
194   // TODO
195   END_TEST;
196 }
197
198 int UtcDaliPopupAddButton(void)
199 {
200   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
201   tet_infoline(" UtcDaliPopupAddButton");
202
203   // Create the Popup actor
204   Popup popup = Popup::New();
205   Stage::GetCurrent().Add( popup );
206   // Put in show state so it's layer is connected to popup (for ancestor check).
207   popup.SetState(Popup::POPUP_SHOW, 0.0f);
208
209   PushButton button = PushButton::New();
210   DALI_TEST_CHECK( !HasAncestor(button, popup) );
211   popup.AddButton(button);
212   // Hide and then re-show popup to cause button to be rearranged and added to popup.
213   popup.SetState( Popup::POPUP_HIDE, 0.0f );
214   popup.SetState( Popup::POPUP_SHOW, 0.0f );
215   DALI_TEST_CHECK( HasAncestor(button, popup) );
216   END_TEST;
217 }
218
219 int UtcDaliPopupSetState(void)
220 {
221   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
222   tet_infoline(" UtcDaliPopupSetState");
223
224   // Create the Popup actor
225   Popup popup = Popup::New();
226   Stage::GetCurrent().Add( popup );
227
228   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
229   popup.SetBackgroundImage(backgroundImage);
230
231   // Showing/Hiding popup, results in all child Actors being
232   // connected/disconnected from the stage.
233   DALI_TEST_CHECK( !backgroundImage.OnStage() );
234   popup.SetState(Popup::POPUP_SHOW, 0.0f);
235   DALI_TEST_CHECK( backgroundImage.OnStage() );
236   DALI_TEST_EQUALS( Popup::POPUP_SHOW, popup.GetState(), TEST_LOCATION );
237   popup.SetState(Popup::POPUP_HIDE, 0.0f);
238   DALI_TEST_CHECK( !backgroundImage.OnStage() );
239   DALI_TEST_EQUALS( Popup::POPUP_HIDE, popup.GetState(), TEST_LOCATION );
240   END_TEST;
241 }
242
243 int UtcDaliPopupSetStateSlow(void)
244 {
245   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
246   tet_infoline(" UtcDaliPopupSetStateSlow");
247
248   // Create the Popup actor
249   Popup popup = Popup::New();
250   Stage::GetCurrent().Add( popup );
251
252   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
253   popup.SetBackgroundImage(backgroundImage);
254
255   // Showing/Hiding popup, results in all child Actors being
256   // connected/disconnected from the stage.
257   DALI_TEST_CHECK( !backgroundImage.OnStage() );
258   popup.SetState(Popup::POPUP_SHOW, 0.0f);
259   DALI_TEST_CHECK( backgroundImage.OnStage() );
260
261   // Hide slowly
262   popup.SetState(Popup::POPUP_HIDE);
263
264   // Wait for a while (allow animation to complete), and then check state.
265   for(int i = 0; i < RENDER_ANIMATION_TEST_DURATION_FRAMES; i++)
266   {
267     application.SendNotification();
268     application.Render(RENDER_FRAME_INTERVAL);
269   }
270
271   DALI_TEST_CHECK( !backgroundImage.OnStage() );
272   END_TEST;
273 }
274
275
276
277 int UtcDaliPopupShowHide(void)
278 {
279   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
280   tet_infoline(" UtcDaliPopupShowHide");
281
282   // Create the Popup actor
283   Popup popup = Popup::New();
284   Stage::GetCurrent().Add( popup );
285   popup.HiddenSignal().Connect( &OnPopupHidden );
286
287   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
288   popup.SetBackgroundImage(backgroundImage);
289
290   PushButton button1 = PushButton::New();
291   PushButton button2 = PushButton::New();
292   button1.SetSize(DEFAULT_BUTTON_SIZE);
293   popup.AddButton(button1);
294   button2.SetSize(DEFAULT_BUTTON_SIZE);
295   popup.AddButton(button2);
296
297   // Showing/Hiding popup, results in all child Actors being
298   // connected/disconnected from the stage.
299   DALI_TEST_CHECK( !backgroundImage.OnStage() );
300
301   // Show
302   // Note: in most popup animation implementations show would result in
303   // popup being onstage immediately following Show(). However we can't
304   // assume for all. e.g. If one creates a animation with a delay.
305   popup.Show();
306
307   // Wait for a while (allow animation to complete), and then check state.
308   for(int i = 0; i < RENDER_ANIMATION_TEST_DURATION_FRAMES; i++)
309   {
310     application.SendNotification();
311     application.Render(RENDER_FRAME_INTERVAL);
312   }
313
314   DALI_TEST_CHECK( backgroundImage.OnStage() );
315
316   // Hide
317   gHidden = false;
318   popup.Hide();
319
320   // Wait for a while (allow animation to complete), and then check state.
321   for(int i = 0; i < RENDER_ANIMATION_TEST_DURATION_FRAMES; i++)
322   {
323     application.SendNotification();
324     application.Render(RENDER_FRAME_INTERVAL);
325   }
326
327   DALI_TEST_CHECK( !backgroundImage.OnStage() );
328   DALI_TEST_CHECK( gHidden );
329   END_TEST;
330 }
331
332 int UtcDaliPopupShowHideTail(void)
333 {
334   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
335   tet_infoline(" UtcDaliPopupShowHideTail");
336
337   // Create the Popup actor
338   Popup popup = Popup::New();
339   Stage::GetCurrent().Add( popup );
340   popup.SetState(Popup::POPUP_SHOW, 0.0f);
341
342   popup.HideTail();
343   int withoutTailCount = DescendentCount(popup);
344
345   popup.ShowTail(ParentOrigin::BOTTOM_CENTER);
346   int withTailCount = DescendentCount(popup);
347
348   // There should be more actors if the Tail has been added.
349   DALI_TEST_CHECK( withTailCount > withoutTailCount );
350
351   // Hide again
352   popup.HideTail();
353   int withoutTailCount2 = DescendentCount(popup);
354
355   DALI_TEST_CHECK( withTailCount > withoutTailCount2 );
356   END_TEST;
357 }
358
359 int UtcDaliPopupOnTouchedOutside(void)
360 {
361   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
362   tet_infoline(" UtcDaliPopupOnTouchedOutside");
363
364   // Create the Popup actor
365   Popup popup = Popup::New();
366   Stage::GetCurrent().Add( popup );
367   popup.SetParentOrigin(ParentOrigin::CENTER);
368   popup.SetAnchorPoint(ParentOrigin::CENTER);
369   popup.SetState(Popup::POPUP_SHOW, 0.0f);
370   popup.OutsideTouchedSignal().Connect( &OnPopupTouchedOutside );
371
372   application.SendNotification();
373   application.Render();
374
375   gTouchedOutside = false;
376   Dali::Integration::TouchEvent event;
377
378   event = Dali::Integration::TouchEvent();
379   event.AddPoint( pointDownOutside );
380   application.ProcessEvent( event );
381
382   application.SendNotification();
383   application.Render();
384
385   event = Dali::Integration::TouchEvent();
386   event.AddPoint( pointUpOutside );
387   application.ProcessEvent( event );
388
389   application.SendNotification();
390   application.Render();
391
392   DALI_TEST_CHECK(gTouchedOutside);
393   END_TEST;
394 }