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