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