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