[dali_1.0.1] Merge branch 'tizen'
[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().GetText(), 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   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
195   tet_infoline(" UtcDaliPopupSetTitle");
196
197   // Create the Popup actor
198   Popup popup = Popup::New();
199   Stage::GetCurrent().Add( popup );
200   // Put in show state so it's layer is connected to popup (for ancestor check).
201   popup.SetState(Popup::POPUP_SHOW, 0.0f);
202
203   TextView titleActor = TextView::New();
204   titleActor.SetText("title");
205
206   DALI_TEST_CHECK( !popup.GetTitle() );
207   popup.SetTitle(titleActor);
208   DALI_TEST_CHECK( popup.GetTitle() == titleActor );
209   DALI_TEST_CHECK( (popup.GetTitle()) && (popup.GetTitle().GetText() == "title") );
210   // verify titleActor is actually inside popup, and not elsewhere on stage, or off even.
211   DALI_TEST_CHECK( HasAncestor(titleActor, popup) );
212
213   TextView titleActor2 = TextView::New();
214   titleActor2.SetText("anothertitle");
215   popup.SetTitle(titleActor2);
216   DALI_TEST_CHECK( popup.GetTitle() != titleActor );
217   DALI_TEST_CHECK( popup.GetTitle() == titleActor2 );
218   DALI_TEST_CHECK( (popup.GetTitle()) && (popup.GetTitle().GetText() == "anothertitle") );
219   // verify titleActor is actually inside popup, and not elsewhere on stage, or off even.
220   DALI_TEST_CHECK( HasAncestor(titleActor2, popup) );
221   END_TEST;
222 }
223
224 int UtcDaliPopupSetTitleText(void)
225 {
226   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
227   tet_infoline(" UtcDaliPopupSetTitleText");
228
229   // Create the Popup actor
230   Popup popup = Popup::New();
231   Stage::GetCurrent().Add( popup );
232   // Put in show state so it's layer is connected to popup (for ancestor check).
233   popup.SetState(Popup::POPUP_SHOW, 0.0f);
234
235   TextView titleActor = TextView::New();
236   titleActor.SetText("title");
237
238   DALI_TEST_CHECK( !popup.GetTitle() );
239   popup.SetTitle(titleActor);
240   DALI_TEST_CHECK( popup.GetTitle() == titleActor );
241   DALI_TEST_CHECK( (popup.GetTitle()) && (popup.GetTitle().GetText() == "title") );
242   // verify titleActor is actually inside popup, and not elsewhere on stage, or off even.
243   DALI_TEST_CHECK( HasAncestor(titleActor, popup) );
244
245   // this text should replace titleImage actor.
246   popup.SetTitle("newtext");
247   DALI_TEST_CHECK( popup.GetTitle() != titleActor );
248   DALI_TEST_CHECK( (popup.GetTitle()) && (popup.GetTitle().GetText() == "newtext") );
249   // verify titleActor is no longer inside popup. (been displaced by newtext actor)
250   DALI_TEST_CHECK( !HasAncestor(titleActor, popup) );
251   END_TEST;
252 }
253
254 int UtcDaliPopupAddButton(void)
255 {
256   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
257   tet_infoline(" UtcDaliPopupAddButton");
258
259   // Create the Popup actor
260   Popup popup = Popup::New();
261   Stage::GetCurrent().Add( popup );
262   // Put in show state so it's layer is connected to popup (for ancestor check).
263   popup.SetState(Popup::POPUP_SHOW, 0.0f);
264
265   PushButton button = PushButton::New();
266   DALI_TEST_CHECK( !HasAncestor(button, popup) );
267   popup.AddButton(button);
268   // Hide and then re-show popup to cause button to be rearranged and added to popup.
269   popup.SetState( Popup::POPUP_HIDE, 0.0f );
270   popup.SetState( Popup::POPUP_SHOW, 0.0f );
271   DALI_TEST_CHECK( HasAncestor(button, popup) );
272   END_TEST;
273 }
274
275 int UtcDaliPopupSetState(void)
276 {
277   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
278   tet_infoline(" UtcDaliPopupSetState");
279
280   // Create the Popup actor
281   Popup popup = Popup::New();
282   Stage::GetCurrent().Add( popup );
283
284   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
285   popup.SetBackgroundImage(backgroundImage);
286
287   // Showing/Hiding popup, results in all child Actors being
288   // connected/disconnected from the stage.
289   DALI_TEST_CHECK( !backgroundImage.OnStage() );
290   popup.SetState(Popup::POPUP_SHOW, 0.0f);
291   DALI_TEST_CHECK( backgroundImage.OnStage() );
292   DALI_TEST_EQUALS( Popup::POPUP_SHOW, popup.GetState(), TEST_LOCATION );
293   popup.SetState(Popup::POPUP_HIDE, 0.0f);
294   DALI_TEST_CHECK( !backgroundImage.OnStage() );
295   DALI_TEST_EQUALS( Popup::POPUP_HIDE, popup.GetState(), TEST_LOCATION );
296   END_TEST;
297 }
298
299 int UtcDaliPopupSetStateSlow(void)
300 {
301   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
302   tet_infoline(" UtcDaliPopupSetStateSlow");
303
304   // Create the Popup actor
305   Popup popup = Popup::New();
306   Stage::GetCurrent().Add( popup );
307
308   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
309   popup.SetBackgroundImage(backgroundImage);
310
311   // Showing/Hiding popup, results in all child Actors being
312   // connected/disconnected from the stage.
313   DALI_TEST_CHECK( !backgroundImage.OnStage() );
314   popup.SetState(Popup::POPUP_SHOW, 0.0f);
315   DALI_TEST_CHECK( backgroundImage.OnStage() );
316
317   // Hide slowly
318   popup.SetState(Popup::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   END_TEST;
329 }
330
331
332
333 int UtcDaliPopupShowHide(void)
334 {
335   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
336   tet_infoline(" UtcDaliPopupShowHide");
337
338   // Create the Popup actor
339   Popup popup = Popup::New();
340   Stage::GetCurrent().Add( popup );
341   popup.HiddenSignal().Connect( &OnPopupHidden );
342
343   ImageActor backgroundImage = CreateSolidColorActor( Color::RED );
344   popup.SetBackgroundImage(backgroundImage);
345
346   PushButton button1 = PushButton::New();
347   PushButton button2 = PushButton::New();
348   button1.SetSize(DEFAULT_BUTTON_SIZE);
349   popup.AddButton(button1);
350   button2.SetSize(DEFAULT_BUTTON_SIZE);
351   popup.AddButton(button2);
352
353   // Showing/Hiding popup, results in all child Actors being
354   // connected/disconnected from the stage.
355   DALI_TEST_CHECK( !backgroundImage.OnStage() );
356
357   // Show
358   // Note: in most popup animation implementations show would result in
359   // popup being onstage immediately following Show(). However we can't
360   // assume for all. e.g. If one creates a animation with a delay.
361   popup.Show();
362
363   // Wait for a while (allow animation to complete), and then check state.
364   for(int i = 0; i < RENDER_ANIMATION_TEST_DURATION_FRAMES; i++)
365   {
366     application.SendNotification();
367     application.Render(RENDER_FRAME_INTERVAL);
368   }
369
370   DALI_TEST_CHECK( backgroundImage.OnStage() );
371
372   // Hide
373   gHidden = false;
374   popup.Hide();
375
376   // Wait for a while (allow animation to complete), and then check state.
377   for(int i = 0; i < RENDER_ANIMATION_TEST_DURATION_FRAMES; i++)
378   {
379     application.SendNotification();
380     application.Render(RENDER_FRAME_INTERVAL);
381   }
382
383   DALI_TEST_CHECK( !backgroundImage.OnStage() );
384   DALI_TEST_CHECK( gHidden );
385   END_TEST;
386 }
387
388 int UtcDaliPopupShowHideTail(void)
389 {
390   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
391   tet_infoline(" UtcDaliPopupShowHideTail");
392
393   // Create the Popup actor
394   Popup popup = Popup::New();
395   Stage::GetCurrent().Add( popup );
396   popup.SetState(Popup::POPUP_SHOW, 0.0f);
397
398   popup.HideTail();
399   int withoutTailCount = DescendentCount(popup);
400
401   popup.ShowTail(ParentOrigin::BOTTOM_CENTER);
402   int withTailCount = DescendentCount(popup);
403
404   // There should be more actors if the Tail has been added.
405   DALI_TEST_CHECK( withTailCount > withoutTailCount );
406
407   // Hide again
408   popup.HideTail();
409   int withoutTailCount2 = DescendentCount(popup);
410
411   DALI_TEST_CHECK( withTailCount > withoutTailCount2 );
412   END_TEST;
413 }
414
415 int UtcDaliPopupOnTouchedOutside(void)
416 {
417   ToolkitTestApplication application;  // Exceptions require ToolkitTestApplication
418   tet_infoline(" UtcDaliPopupOnTouchedOutside");
419
420   // Create the Popup actor
421   Popup popup = Popup::New();
422   Stage::GetCurrent().Add( popup );
423   popup.SetParentOrigin(ParentOrigin::CENTER);
424   popup.SetAnchorPoint(ParentOrigin::CENTER);
425   popup.SetState(Popup::POPUP_SHOW, 0.0f);
426   popup.OutsideTouchedSignal().Connect( &OnPopupTouchedOutside );
427
428   application.SendNotification();
429   application.Render();
430
431   gTouchedOutside = false;
432   Dali::Integration::TouchEvent event;
433
434   event = Dali::Integration::TouchEvent();
435   event.AddPoint( pointDownOutside );
436   application.ProcessEvent( event );
437
438   application.SendNotification();
439   application.Render();
440
441   event = Dali::Integration::TouchEvent();
442   event.AddPoint( pointUpOutside );
443   application.ProcessEvent( event );
444
445   application.SendNotification();
446   application.Render();
447
448   DALI_TEST_CHECK(gTouchedOutside);
449   END_TEST;
450 }