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