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