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