(AutomatedTest) Fix ImfManager stub to match recent changes
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-PushButton.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_pushbutton_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_pushbutton_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42 namespace
43 {
44
45 static bool gPushButtonToggleState = false;
46 bool PushButtonToggled( Button button, bool toggled )
47 {
48   gPushButtonToggleState = toggled && ( toggled == static_cast<PushButton&>( button ).IsToggled() );
49   return true;
50 }
51
52 static bool gPushButtonPressed = false;
53
54 static bool PushButtonPressed( Button button )
55 {
56   gPushButtonPressed = true;
57   return true;
58 }
59
60 static bool gPushButtonReleased = false;
61
62 static bool PushButtonReleased( Button button )
63 {
64   gPushButtonReleased = true;
65   return true;
66 }
67
68 const Dali::TouchPoint pointDownInside( 0, TouchPoint::Down, 240, 400 );
69 const Dali::TouchPoint pointUpInside( 0, TouchPoint::Up, 240, 400 );
70 const Dali::TouchPoint pointLeave( 0, TouchPoint::Leave, 240, 400 );
71 const Dali::TouchPoint pointEnter( 0, TouchPoint::Motion, 240, 400 );
72 const Dali::TouchPoint pointMotionOut( 0, TouchPoint::Motion, 10, 10 );
73 const Dali::TouchPoint pointDownOutside( 0, TouchPoint::Down, 10, 10 );
74 const Dali::TouchPoint pointUpOutside( 0, TouchPoint::Up, 10, 10 );
75
76 Image CreateSolidColorImage( const Vector4& color, unsigned int width, unsigned int height )
77 {
78   BitmapImage imageData = BitmapImage::New( width, height, Pixel::RGBA8888 );
79
80   // Create the image
81   PixelBuffer* pixbuf = imageData.GetBuffer();
82   unsigned int size = width * height;
83
84   for( size_t i = 0; i < size; i++ )
85     {
86       pixbuf[i*4+0] = 0xFF * color.r;
87       pixbuf[i*4+1] = 0xFF * color.g;
88       pixbuf[i*4+2] = 0xFF * color.b;
89       pixbuf[i*4+3] = 0xFF * color.a;
90     }
91
92   imageData.Update();
93
94   return imageData;
95 }
96
97 } //namespace
98
99
100 int UtcDaliPushButtonSetGetAutoRepeating(void)
101 {
102   ToolkitTestApplication application;
103   tet_infoline(" UtcDaliPushButtonSetGetAutoRepeating");
104
105   PushButton pushButton = PushButton::New();
106
107   pushButton.SetAutoRepeating( true );
108
109   DALI_TEST_CHECK( pushButton.IsAutoRepeating() );
110
111   pushButton.SetAutoRepeating( false );
112
113   DALI_TEST_CHECK( !pushButton.IsAutoRepeating() );
114
115   pushButton.SetAutoRepeating( true );
116
117   DALI_TEST_CHECK( pushButton.IsAutoRepeating() );
118   END_TEST;
119 }
120
121 int UtcDaliPushButtonSetGetToggleButton(void)
122 {
123   ToolkitTestApplication application;
124   tet_infoline(" UtcDaliPushButtonSetGetToggleButton");
125
126   PushButton pushButton = PushButton::New();
127
128   pushButton.SetToggleButton( true );
129
130   DALI_TEST_CHECK( pushButton.IsToggleButton() );
131
132   pushButton.SetToggleButton( false );
133
134   DALI_TEST_CHECK( !pushButton.IsToggleButton() );
135
136   pushButton.SetToggleButton( true );
137
138   DALI_TEST_CHECK( pushButton.IsToggleButton() );
139   END_TEST;
140 }
141
142 int UtcDaliPushButtonSetGetAutoRepeatingAndToggleButton(void)
143 {
144   ToolkitTestApplication application;
145   tet_infoline(" UtcDaliPushButtonSetGetAutoRepeatingAndToggleButton");
146
147   PushButton pushButton = PushButton::New();
148
149   pushButton.SetAutoRepeating( true );
150   pushButton.SetToggleButton( true );
151
152   DALI_TEST_CHECK( pushButton.IsToggleButton() );
153   DALI_TEST_CHECK( !pushButton.IsAutoRepeating() );
154
155   pushButton.SetToggleButton( true );
156   pushButton.SetAutoRepeating( true );
157
158   DALI_TEST_CHECK( pushButton.IsAutoRepeating() );
159   DALI_TEST_CHECK( !pushButton.IsToggleButton() );
160   END_TEST;
161 }
162
163 int UtcDaliPushButtonSetGetToggled01(void)
164 {
165   ToolkitTestApplication application;
166   tet_infoline(" UtcDaliPushButtonSetGetToggled01");
167
168   PushButton pushButton = PushButton::New();
169
170   pushButton.SetToggleButton( true );
171   pushButton.ToggledSignal().Connect( &PushButtonToggled );
172
173   gPushButtonToggleState = false;
174   pushButton.SetToggled( true );
175
176   DALI_TEST_CHECK( pushButton.IsToggled() );
177   DALI_TEST_CHECK( gPushButtonToggleState );
178
179   pushButton.SetToggled( false );
180
181   DALI_TEST_CHECK( !pushButton.IsToggled() );
182   DALI_TEST_CHECK( !gPushButtonToggleState );
183
184   pushButton.SetToggled( true );
185
186   DALI_TEST_CHECK( pushButton.IsToggled() );
187   DALI_TEST_CHECK( gPushButtonToggleState );
188   END_TEST;
189 }
190
191 int UtcDaliPushButtonSetGetToggled02(void)
192 {
193   ToolkitTestApplication application;
194   tet_infoline(" UtcDaliPushButtonSetGetToggled02");
195
196   PushButton pushButton = PushButton::New();
197
198   pushButton.SetToggleButton( false );
199   pushButton.ToggledSignal().Connect( &PushButtonToggled );
200
201   gPushButtonToggleState = false;
202   pushButton.SetToggled( true );
203
204   DALI_TEST_CHECK( !pushButton.IsToggled() );
205   DALI_TEST_CHECK( !gPushButtonToggleState );
206
207   pushButton.SetToggled( false );
208
209   DALI_TEST_CHECK( !pushButton.IsToggled() );
210   DALI_TEST_CHECK( !gPushButtonToggleState );
211
212   pushButton.SetToggled( true );
213
214   DALI_TEST_CHECK( !pushButton.IsToggled() );
215   DALI_TEST_CHECK( !gPushButtonToggleState );
216   END_TEST;
217 }
218
219 int UtcDaliPushButtonSetGetAutorepeatingDelayValues01(void)
220 {
221   ToolkitTestApplication application;
222   tet_infoline(" UtcDaliPushButtonSetGetAutorepeatingDelayValues01");
223
224   PushButton pushButton = PushButton::New();
225
226   pushButton.SetAutoRepeating( true );
227
228   pushButton.SetInitialAutoRepeatingDelay( 1.f );
229   DALI_TEST_EQUALS( pushButton.GetInitialAutoRepeatingDelay(), 1.f, TEST_LOCATION );
230
231   pushButton.SetNextAutoRepeatingDelay( 1.f );
232   DALI_TEST_EQUALS( pushButton.GetNextAutoRepeatingDelay(), 1.f, TEST_LOCATION );
233   END_TEST;
234 }
235
236 int UtcDaliPushButtonSetGetAutorepeatingDelayValues02(void)
237 {
238   ToolkitTestApplication application;
239   tet_infoline(" UtcDaliPushButtonSetGetAutorepeatingDelayValues02");
240
241   PushButton pushButton = PushButton::New();
242
243   bool assert1( false );
244   bool assert2( false );
245
246   pushButton.SetAutoRepeating( true );
247
248   try
249   {
250     pushButton.SetInitialAutoRepeatingDelay( -1.f );
251   }
252   catch( Dali::DaliException& e )
253   {
254     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
255     DALI_TEST_EQUALS(e.mCondition, "initialAutoRepeatingDelay > 0.f", TEST_LOCATION);
256     assert1 = true;
257   }
258
259   try
260   {
261     pushButton.SetNextAutoRepeatingDelay( -1.f );
262   }
263   catch( Dali::DaliException& e )
264   {
265     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
266     DALI_TEST_EQUALS(e.mCondition, "nextAutoRepeatingDelay > 0.f", TEST_LOCATION);
267     assert2 = true;
268   }
269
270   DALI_TEST_CHECK( assert1 && assert2 );
271   END_TEST;
272 }
273
274 int UtcDaliPushButtonSetImages(void)
275 {
276   ToolkitTestApplication application;
277   tet_infoline(" UtcDaliPushButtonSetImages");
278
279   Actor imageActor;
280
281   Image image01 = CreateSolidColorImage( Color::RED, 10, 10 );
282   ImageActor imageActor01 = CreateSolidColorActor( Color::RED );
283   imageActor01.SetSize( 20.f, 20.f );
284
285   Image image02 = CreateSolidColorImage( Color::RED, 30, 30 );
286   ImageActor imageActor02 = CreateSolidColorActor( Color::RED );
287   imageActor02.SetSize( 40.f, 40.f );
288
289   Image image03 = CreateSolidColorImage( Color::RED, 50, 50 );
290   ImageActor imageActor03 = CreateSolidColorActor( Color::RED );
291   imageActor03.SetSize( 60.f, 60.f );
292
293   Image image04 = CreateSolidColorImage( Color::RED, 70, 70 );
294   ImageActor imageActor04 = CreateSolidColorActor( Color::RED );
295   imageActor04.SetSize( 80.f, 80.f );
296
297   Image image05 = CreateSolidColorImage( Color::RED, 90, 90 );
298   ImageActor imageActor05 = CreateSolidColorActor( Color::RED );
299   imageActor05.SetSize( 100.f, 100.f );
300
301   Vector3 size;
302   PushButton pushButton = PushButton::New();
303
304   application.SendNotification();
305   application.Render();
306
307   // Just check if check box button size changes when a bigger image is set.
308
309   pushButton.SetButtonImage( image01 );
310
311   application.SendNotification();
312   application.Render();
313
314   size = pushButton.GetButtonImage().GetCurrentSize();
315
316   DALI_TEST_EQUALS( size.width, 10.f, TEST_LOCATION );
317   DALI_TEST_EQUALS( size.height, 10.f, TEST_LOCATION );
318
319   pushButton.SetButtonImage( imageActor01 );
320
321   application.SendNotification();
322   application.Render();
323
324   size = pushButton.GetButtonImage().GetCurrentSize();
325
326   DALI_TEST_EQUALS( size.width, 20.f, TEST_LOCATION );
327   DALI_TEST_EQUALS( size.height, 20.f, TEST_LOCATION );
328
329   pushButton.SetBackgroundImage( image02 );
330
331   application.SendNotification();
332   application.Render();
333
334   size = pushButton.GetBackgroundImage().GetCurrentSize();
335
336   DALI_TEST_EQUALS( size.width, 30.f, TEST_LOCATION );
337   DALI_TEST_EQUALS( size.height, 30.f, TEST_LOCATION );
338
339   pushButton.SetBackgroundImage( imageActor02 );
340
341   application.SendNotification();
342   application.Render();
343
344   size = pushButton.GetBackgroundImage().GetCurrentSize();
345
346   DALI_TEST_EQUALS( size.width, 40.f, TEST_LOCATION );
347   DALI_TEST_EQUALS( size.height, 40.f, TEST_LOCATION );
348
349   pushButton.SetPressedImage( image03 );
350
351   application.SendNotification();
352   application.Render();
353
354   size = pushButton.GetPressedImage().GetCurrentSize();
355
356   DALI_TEST_EQUALS( size.width, 50.f, TEST_LOCATION );
357   DALI_TEST_EQUALS( size.height, 50.f, TEST_LOCATION );
358
359   pushButton.SetPressedImage( imageActor03 );
360
361   application.SendNotification();
362   application.Render();
363
364   size = pushButton.GetPressedImage().GetCurrentSize();
365
366   DALI_TEST_EQUALS( size.width, 60.f, TEST_LOCATION );
367   DALI_TEST_EQUALS( size.height, 60.f, TEST_LOCATION );
368
369   pushButton.SetDimmedBackgroundImage( image04 );
370
371   application.SendNotification();
372   application.Render();
373
374   size = pushButton.GetDimmedBackgroundImage().GetCurrentSize();
375
376   DALI_TEST_EQUALS( size.width, 70.f, TEST_LOCATION );
377   DALI_TEST_EQUALS( size.height, 70.f, TEST_LOCATION );
378
379   pushButton.SetDimmedBackgroundImage( imageActor04 );
380
381   application.SendNotification();
382   application.Render();
383
384   size = pushButton.GetDimmedBackgroundImage().GetCurrentSize();
385
386   DALI_TEST_EQUALS( size.width, 80.f, TEST_LOCATION );
387   DALI_TEST_EQUALS( size.height, 80.f, TEST_LOCATION );
388
389   pushButton.SetDimmedImage( image05 );
390
391   application.SendNotification();
392   application.Render();
393
394   size = pushButton.GetDimmedImage().GetCurrentSize();
395
396   DALI_TEST_EQUALS( size.width, 90.f, TEST_LOCATION );
397   DALI_TEST_EQUALS( size.height, 90.f, TEST_LOCATION );
398
399   pushButton.SetDimmedImage( imageActor05 );
400
401   application.SendNotification();
402   application.Render();
403
404   size = pushButton.GetDimmedImage().GetCurrentSize();
405
406   DALI_TEST_EQUALS( size.width, 100.f, TEST_LOCATION );
407   DALI_TEST_EQUALS( size.height, 100.f, TEST_LOCATION );
408   END_TEST;
409 }
410
411 int UtcDaliPushButtonSetLabelText(void)
412 {
413   ToolkitTestApplication application;
414   tet_infoline(" UtcDaliPushButtonSetLabelText");
415
416   const std::string STR( "Hola!" );
417
418   PushButton pushButton = PushButton::New();
419
420   application.SendNotification();
421   application.Render();
422
423   TextView textView;
424
425   pushButton.SetLabelText( STR );
426
427   textView = TextView::DownCast( pushButton.GetLabelText() );
428   DALI_TEST_CHECK( STR == textView.GetText() );
429
430   TextView text = TextView::New( STR );
431   pushButton.SetLabelText( text );
432
433   textView = TextView::DownCast( pushButton.GetLabelText() );
434   DALI_TEST_CHECK( STR == textView.GetText() );
435   END_TEST;
436 }
437
438 int UtcDaliPushButtonPressed(void)
439 {
440   ToolkitTestApplication application;
441   tet_infoline(" UtcDaliPushButtonPressed");
442
443   PushButton pushButton = PushButton::New();
444   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
445   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
446   pushButton.SetPosition( 240, 400 );
447   pushButton.SetSize( 100, 100 );
448
449   Stage::GetCurrent().Add( pushButton );
450
451   application.SendNotification();
452   application.Render();
453
454   gPushButtonPressed = false;
455
456   // connect to its touch signal
457   pushButton.PressedSignal().Connect( &PushButtonPressed );
458
459   Dali::Integration::TouchEvent eventDown;
460   eventDown.AddPoint( pointDownInside );
461
462   // flush the queue and render once
463   application.SendNotification();
464   application.Render();
465   application.ProcessEvent( eventDown );
466
467   DALI_TEST_CHECK( gPushButtonPressed );
468   END_TEST;
469 }
470
471 int UtcDaliPushButtonReleased(void)
472 {
473   ToolkitTestApplication application;
474   tet_infoline(" UtcDaliPushButtonReleased");
475
476   PushButton pushButton = PushButton::New();
477   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
478   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
479   pushButton.SetPosition( 240, 400 );
480   pushButton.SetSize( 100, 100 );
481
482   Stage::GetCurrent().Add( pushButton );
483
484   application.SendNotification();
485   application.Render();
486
487   // connect to its touch signal
488   pushButton.ReleasedSignal().Connect( &PushButtonReleased );
489
490   Dali::Integration::TouchEvent event;
491
492   // Test1. Touch point down and up inside the button.
493
494   gPushButtonReleased = false;
495   event = Dali::Integration::TouchEvent();
496   event.AddPoint( pointDownInside );
497   application.ProcessEvent( event );
498
499   event = Dali::Integration::TouchEvent();
500   event.AddPoint( pointUpInside );
501   application.ProcessEvent( event );
502
503   DALI_TEST_CHECK( gPushButtonReleased );
504
505   // Test2. Touch point down and up outside the button.
506
507   gPushButtonReleased = false;
508   event = Dali::Integration::TouchEvent();
509   event.AddPoint( pointDownOutside );
510   application.ProcessEvent( event );
511
512   event = Dali::Integration::TouchEvent();
513   event.AddPoint( pointUpOutside );
514   application.ProcessEvent( event );
515
516   DALI_TEST_CHECK( !gPushButtonReleased );
517
518   // Test3. Touch point down inside and up outside the button.
519
520   gPushButtonReleased = false;
521   event = Dali::Integration::TouchEvent();
522   event.AddPoint( pointDownInside );
523   application.ProcessEvent( event );
524
525   event = Dali::Integration::TouchEvent();
526   event.AddPoint( pointLeave );
527   application.ProcessEvent( event );
528
529   event = Dali::Integration::TouchEvent();
530   event.AddPoint( pointUpOutside );
531   application.ProcessEvent( event );
532
533   DALI_TEST_CHECK( gPushButtonReleased );
534
535   // Test4. Touch point down outside and up inside the button.
536
537   gPushButtonReleased = false;
538   event = Dali::Integration::TouchEvent();
539   event.AddPoint( pointDownOutside );
540   application.ProcessEvent( event );
541
542   event = Dali::Integration::TouchEvent();
543   event.AddPoint( pointEnter );
544   application.ProcessEvent( event );
545
546   event = Dali::Integration::TouchEvent();
547   event.AddPoint( pointUpInside );
548   application.ProcessEvent( event );
549
550   DALI_TEST_CHECK( !gPushButtonReleased );
551   END_TEST;
552 }
553
554 int UtcDaliPushButtonToggled(void)
555 {
556   ToolkitTestApplication application;
557   tet_infoline(" UtcDaliPushButtonToggled");
558
559   PushButton pushButton = PushButton::New();
560   pushButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
561   pushButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
562   pushButton.SetPosition( 240, 400 );
563   pushButton.SetSize( 100, 100 );
564
565   Stage::GetCurrent().Add( pushButton );
566
567   application.SendNotification();
568   application.Render();
569
570   // connect to its touch signal
571   pushButton.ToggledSignal().Connect( &PushButtonToggled );
572
573   Dali::Integration::TouchEvent event;
574
575   // Test1. No toggle button.
576
577   gPushButtonToggleState = false;
578   event = Dali::Integration::TouchEvent();
579   event.AddPoint( pointDownInside );
580   application.ProcessEvent( event );
581
582   event = Dali::Integration::TouchEvent();
583   event.AddPoint( pointUpInside );
584   application.ProcessEvent( event );
585
586   DALI_TEST_CHECK( !gPushButtonToggleState );
587
588   // Set toggle property.
589   pushButton.SetToggleButton( true );
590
591   // Test2. Touch point down and up inside the button twice.
592   gPushButtonToggleState = false;
593   event = Dali::Integration::TouchEvent();
594   event.AddPoint( pointDownInside );
595   application.ProcessEvent( event );
596
597   event = Dali::Integration::TouchEvent();
598   event.AddPoint( pointUpInside );
599   application.ProcessEvent( event );
600
601   DALI_TEST_CHECK( gPushButtonToggleState );
602
603   event = Dali::Integration::TouchEvent();
604   event.AddPoint( pointDownInside );
605   application.ProcessEvent( event );
606
607   event = Dali::Integration::TouchEvent();
608   event.AddPoint( pointUpInside );
609   application.ProcessEvent( event );
610
611   DALI_TEST_CHECK( !gPushButtonToggleState );
612
613   // Test3. Touch point down and up outside the button.
614
615   gPushButtonToggleState = false;
616   event = Dali::Integration::TouchEvent();
617   event.AddPoint( pointDownOutside );
618   application.ProcessEvent( event );
619
620   event = Dali::Integration::TouchEvent();
621   event.AddPoint( pointUpOutside );
622   application.ProcessEvent( event );
623
624   DALI_TEST_CHECK( !gPushButtonToggleState );
625
626   // Test4. Touch point down inside and up outside the button.
627
628   gPushButtonToggleState = false;
629   event = Dali::Integration::TouchEvent();
630   event.AddPoint( pointDownInside );
631   application.ProcessEvent( event );
632
633   event = Dali::Integration::TouchEvent();
634   event.AddPoint( pointLeave );
635   application.ProcessEvent( event );
636
637   event = Dali::Integration::TouchEvent();
638   event.AddPoint( pointUpOutside );
639   application.ProcessEvent( event );
640
641   DALI_TEST_CHECK( !gPushButtonToggleState );
642
643   // Test5. Touch point down outside and up inside the button.
644
645   gPushButtonToggleState = false;
646   event = Dali::Integration::TouchEvent();
647   event.AddPoint( pointDownOutside );
648   application.ProcessEvent( event );
649
650   event = Dali::Integration::TouchEvent();
651   event.AddPoint( pointEnter );
652   application.ProcessEvent( event );
653
654   event = Dali::Integration::TouchEvent();
655   event.AddPoint( pointUpInside );
656   application.ProcessEvent( event );
657
658   DALI_TEST_CHECK( !gPushButtonToggleState );
659   END_TEST;
660 }