Changes after touch changes to Core
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / 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
20 #include <stdlib.h>
21 #include <dali-toolkit/internal/controls/buttons/push-button-impl.h>
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali/integration-api/events/touch-event-integ.h>
25
26
27 using namespace Dali;
28 using namespace Toolkit;
29
30 namespace
31 {
32
33 static bool gPushButtonSelectedState = false;
34 bool PushButtonSelected( Button button, bool selected )
35 {
36   gPushButtonSelectedState = selected && ( selected == static_cast<PushButton&>( button ).IsSelected() );
37   return true;
38 }
39
40
41 Dali::Integration::Point GetPointDownInside()
42 {
43   Dali::Integration::Point point;
44   point.SetState( PointState::DOWN );
45   point.SetScreenPosition( Vector2( 240, 400 ) );
46   return point;
47 }
48
49 Dali::Integration::Point GetPointUpInside()
50 {
51   Dali::Integration::Point point;
52   point.SetState( PointState::UP );
53   point.SetScreenPosition( Vector2( 240, 400 ) );
54   return point;
55 }
56
57 Dali::Integration::Point GetPointMotionOut()
58 {
59   Dali::Integration::Point point;
60   point.SetState( PointState::MOTION );
61   point.SetScreenPosition( Vector2( 10, 10 ) );
62   return point;
63 }
64
65 Dali::Integration::Point GetPointUpOutside()
66 {
67   Dali::Integration::Point point;
68   point.SetState( PointState::UP );
69   point.SetScreenPosition( Vector2( 10, 10 ) );
70   return point;
71 }
72
73 } // namespace
74
75
76 //////////////////////////////////////////////////////////
77
78 namespace
79 {
80 static bool gOnTouchPointInterrupted = false;
81 } //namespace
82
83 namespace Dali
84 {
85
86 namespace Toolkit
87 {
88
89 namespace Internal
90 {
91 class TETButton;
92 }
93
94 /**
95  * Creates a PushButton to test if interrupt events are handled correctly.
96  */
97 class TETButton : public PushButton
98 {
99 public:
100   // PushButton Pressed
101   typedef Signal< bool ( PushButton ) > ButtonSignalType;
102
103   ButtonSignalType& PressedSignal();
104
105   /**
106    * Default constructor.
107    */
108   TETButton();
109
110   /**
111    * Copy constructor.
112    */
113   TETButton( const PushButton& button );
114
115   /**
116    * Assignment operator.
117    */
118   TETButton& operator=( const TETButton& button );
119
120   /**
121    * Creates and initializes a new button.
122    */
123   static TETButton New();
124
125   /**
126    * Down cast to TETButton.
127    */
128   static TETButton DownCast( BaseHandle handle );
129
130   /**
131    * Creates a handle using the Toolkit::Internal implementation.
132    * @param[in]  implementation  The Control implementation.
133    */
134   TETButton( Internal::TETButton& implementation );
135
136   /**
137    * Allows the creation of this Control from an Internal::CustomActor pointer.
138    * @param[in]  internal  A pointer to the internal CustomActor.
139    */
140   TETButton( Dali::Internal::CustomActor* internal );
141 };
142
143 namespace Internal
144 {
145
146 /**
147  * Internal implementation
148  */
149 class TETButton : public PushButton
150 {
151 public:
152   /**
153    * Construct a new Button.
154    */
155   TETButton();
156
157   /**
158    * A reference counted object may only be deleted by calling Unreference()
159    */
160   virtual ~TETButton();
161
162   /**
163    * Creates an internal button.
164    */
165   static Toolkit::TETButton New();
166
167   /**
168    * @return the pressed signal.
169    */
170   Toolkit::TETButton::ButtonSignalType& PressedSignal();
171
172   /**
173    * Callback called when an interrupt events is received.
174    */
175   void OnTouchPointInterrupted();
176
177   /**
178    * Callback received when a down event is received.
179    */
180   void OnButtonDown();
181
182   Toolkit::TETButton::ButtonSignalType mPressedSignal;   ///< Signal emitted when the button is pressed.
183 };
184
185 } // namespace Internal
186
187 TETButton::TETButton()
188 {
189 }
190
191 TETButton::TETButton( const PushButton& button )
192 : PushButton( button )
193 {
194 }
195
196 TETButton& TETButton::operator=( const TETButton& button )
197 {
198   if( &button != this )
199   {
200     PushButton::operator=( button );
201   }
202   return *this;
203 }
204
205 TETButton TETButton::New()
206 {
207   return Internal::TETButton::New();
208 }
209
210 TETButton TETButton::DownCast( BaseHandle handle )
211 {
212   return Control::DownCast<TETButton, Internal::TETButton>(handle);
213 }
214
215 TETButton::ButtonSignalType& TETButton::PressedSignal()
216 {
217   TETButton button( *this );
218   DALI_ASSERT_ALWAYS( button );
219
220   Dali::RefObject& handle = button.GetImplementation();
221
222   return static_cast<Toolkit::Internal::TETButton&>( handle ).PressedSignal();
223 }
224
225 TETButton::TETButton( Internal::TETButton& implementation )
226 : PushButton( implementation )
227 {}
228
229 TETButton::TETButton( Dali::Internal::CustomActor* internal )
230 : PushButton( internal )
231 {
232   VerifyCustomActorPointer<Internal::TETButton>(internal);
233 }
234
235 namespace Internal
236 {
237
238 TETButton::TETButton()
239 : PushButton(),
240   mPressedSignal()
241 {
242 }
243
244 TETButton::~TETButton()
245 {
246 }
247
248 Toolkit::TETButton TETButton::New()
249 {
250   // Create the implementation, temporarily owned on stack
251   IntrusivePtr< TETButton > internalTETButton = new TETButton();
252
253   // Pass ownership to CustomActor
254   Dali::Toolkit::TETButton tetButton( *internalTETButton );
255
256   // Second-phase init of the implementation
257   // This can only be done after the CustomActor connection has been made...
258   internalTETButton->Initialize();
259
260   return tetButton;
261 }
262
263 Toolkit::TETButton::ButtonSignalType& TETButton::PressedSignal()
264 {
265   return mPressedSignal;
266 }
267
268 void TETButton::OnButtonDown()
269 {
270   Toolkit::TETButton handle( GetOwner() );
271
272   //Emit signal.
273   mPressedSignal.Emit( handle );
274 }
275
276 void TETButton::OnTouchPointInterrupted()
277 {
278   gOnTouchPointInterrupted = true;
279 }
280
281 } // namespace Internal
282
283 } // namespace Toolkit
284
285 } // namespace Dali
286
287 namespace
288 {
289
290 class TETButtonPressed : public Dali::ConnectionTracker
291 {
292 public:
293   enum Test
294   {
295     SENSITIVENESS,
296     VISIBILITY
297   };
298
299   TETButtonPressed( Actor actor, Test test )
300   : mActor( actor ),
301     mTest( test )
302   {
303   }
304
305   bool Callback( PushButton button )
306   {
307     switch( mTest )
308     {
309       case SENSITIVENESS:
310       {
311         mActor.SetSensitive( false );
312         break;
313       }
314       case VISIBILITY:
315       {
316         std::cout <<"VISIBILITY false" << std::endl;
317         mActor.SetVisible( false );
318         break;
319       }
320       default:
321       {
322         break;
323       }
324     }
325     return true;
326   }
327
328   Actor mActor;
329   Test mTest;
330 };
331
332 static bool TestCallback(Actor actor, const TouchEvent& event)
333 {
334   return true;
335 }
336
337 } // namespace
338
339 //////////////////////////////////////////////////////////
340
341 int UtcDaliPushButtonDownCast(void)
342 {
343   ToolkitTestApplication application;
344   tet_infoline(" UtcDaliPushButtonDownCast");
345
346   TETButton tetButton= Toolkit::TETButton::New();
347
348   BaseHandle object(tetButton);
349
350   TETButton tetButton2 = TETButton::DownCast( object );
351   DALI_TEST_CHECK(tetButton2);
352
353   TETButton tetButton3 = DownCast< TETButton >(object);
354   DALI_TEST_CHECK(tetButton3);
355   END_TEST;
356 }
357
358 int UtcDaliPushButtonInterruptEventWhenInsensitive(void)
359 {
360   ToolkitTestApplication application;
361   tet_infoline(" UtcDaliPushButtonInterruptEventWhenInsensitive");
362
363   // * Creates an actor which contains a button.
364   // * The size of the actor is bigger than the button.
365   // * The button's boundary is contained in the actor's one.
366   Actor actor = Actor::New();
367   TETButton tetButton= Toolkit::TETButton::New();
368
369   actor.SetName( "Actor" );
370   tetButton.SetName( "TETButton" );
371
372   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
373   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
374   actor.SetPosition( 0, 0 );
375   actor.SetSize( 400, 800 );
376
377   tetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
378   tetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
379   tetButton.SetPosition( 240, 400 );
380   tetButton.SetSize( 100, 100 );
381
382   actor.Add( tetButton );
383   Stage::GetCurrent().Add( actor );
384
385   // * Actor's touch event is connected to a callback function
386   //   and this callback function consumes the event.
387   actor.TouchedSignal().Connect( &TestCallback );
388
389   // * Button's pressed signal is connected to a callback function
390   //   which also consumes the event.
391   // * Changes the sensitiveness of the button to false.
392   TETButtonPressed tetButtonPressed( actor, TETButtonPressed::SENSITIVENESS );
393   tetButton.PressedSignal().Connect( &tetButtonPressed, &TETButtonPressed::Callback );
394
395   // Initializes TET state.
396   gOnTouchPointInterrupted = false;
397   tetButton.SetSensitive( true );
398
399   Dali::Integration::TouchEvent event;
400
401   // TET starts.
402
403   // Test a down point inside the button which is also consumed by the actor, and an up point
404   // consumed only by the actor.  gOnTouchPointInterrupted should be true (Button receives an
405   // interrupt event.
406
407   application.SendNotification();
408   application.Render();
409
410   // A down event is sent inside the button's boundary.
411
412   event = Dali::Integration::TouchEvent();
413   event.AddPoint( GetPointDownInside() );
414
415   // flush the queue and render once
416   application.SendNotification();
417   application.Render();
418   application.ProcessEvent( event );
419
420   // An up event is sent outside the button's boundary but inside the actor's one.
421
422   event = Dali::Integration::TouchEvent();
423   event.AddPoint( GetPointUpOutside() );
424
425   // flush the queue and render once
426   application.SendNotification();
427   application.Render();
428   application.ProcessEvent( event );
429
430   DALI_TEST_CHECK( gOnTouchPointInterrupted );
431
432   // Test a down point inside the button which is also consumed by the actor, and a motion point
433   // consumed only by the actor.  gOnTouchPointInterrupted should be true (Button receives an
434   // interrupt event.
435
436   // Initializes TET state.
437   gOnTouchPointInterrupted = false;
438   actor.SetSensitive( true );
439   tetButton.SetSensitive( true );
440
441   application.SendNotification();
442   application.Render();
443
444   // A down event is sent inside the button's boundary.
445
446   event = Dali::Integration::TouchEvent();
447   event.AddPoint( GetPointDownInside() );
448
449   // flush the queue and render once
450   application.SendNotification();
451   application.Render();
452   application.ProcessEvent( event );
453
454   // A motion event is sent outside the button's boundary but inside the actor's one.
455
456   event = Dali::Integration::TouchEvent();
457   event.AddPoint( GetPointMotionOut() );
458
459   // flush the queue and render once
460   application.SendNotification();
461   application.Render();
462   application.ProcessEvent( event );
463
464   DALI_TEST_CHECK( gOnTouchPointInterrupted );
465
466   // Test a down point inside the button which is also consumed by the actor, and an up point
467   // also inside the button and consumed by the actor.  gOnTouchPointInterrupted should be false.
468
469   // Initializes TET state.
470   gOnTouchPointInterrupted = false;
471   actor.SetSensitive( true );
472   tetButton.SetSensitive( true );
473
474   // A down event is sent inside the button's boundary.
475
476   event = Dali::Integration::TouchEvent();
477   event.AddPoint( GetPointDownInside() );
478
479   // flush the queue and render once
480   application.SendNotification();
481   application.Render();
482   application.ProcessEvent( event );
483
484   actor.SetSensitive( true );
485   // An up event is sent inside the button's boundary.
486
487   event = Dali::Integration::TouchEvent();
488   event.AddPoint( GetPointUpInside() );
489
490   // flush the queue and render once
491   application.SendNotification();
492   application.Render();
493   application.ProcessEvent( event );
494
495   DALI_TEST_CHECK( !gOnTouchPointInterrupted );
496   END_TEST;
497 }
498
499 int UtcDaliPushButtonInterruptEventWhenNonVisible(void)
500 {
501   ToolkitTestApplication application;
502   tet_infoline(" UtcDaliPushButtonInterruptEventWhenNonVisible");
503
504   // Does same test as above but changing the visibility instead the sensitiveness.
505
506   // * Creates an actor which contains a button.
507   // * The size of the actor is bigger than the button.
508   // * The button's boundary is contained in the actor's one.
509   Actor actor = Actor::New();
510   TETButton tetButton = Toolkit::TETButton::New();
511
512   actor.SetName( "Actor" );
513   tetButton.SetName( "TETButton" );
514
515   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
516   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
517   actor.SetPosition( 0, 0 );
518   actor.SetSize( 400, 800 );
519
520   tetButton.SetAnchorPoint( AnchorPoint::TOP_LEFT );
521   tetButton.SetParentOrigin( ParentOrigin::TOP_LEFT );
522   tetButton.SetPosition( 240, 400 );
523   tetButton.SetSize( 100, 100 );
524
525   actor.Add( tetButton );
526   Stage::GetCurrent().Add( actor );
527
528   // * Actor's touch event is connected to a callback function
529   //   and this callback function consumes the event.
530   actor.TouchedSignal().Connect( &TestCallback );
531
532   // * Button's pressed signal is connected to a callback function
533   //   which also consumes the event.
534   // * Changes the visibility of the button to false.
535   TETButtonPressed tetButtonPressed( tetButton, TETButtonPressed::VISIBILITY );
536   tetButton.PressedSignal().Connect( &tetButtonPressed, &TETButtonPressed::Callback );
537
538   // Initializes TET state.
539   gOnTouchPointInterrupted = false;
540   tetButton.SetVisible( true );
541
542   Dali::Integration::TouchEvent event;
543
544   // TET starts.
545
546   // Test a down point inside the button which is also consumed by the actor, and an up point
547   // consumed only by the actor.  gOnTouchPointInterrupted should be true (Button receives an
548   // interrupt event.
549
550   application.SendNotification();
551   application.Render();
552
553   // A down event is sent inside the button's boundary.
554
555   event = Dali::Integration::TouchEvent();
556   event.AddPoint( GetPointDownInside() );
557
558   // flush the queue and render once
559   application.SendNotification();
560   application.Render();
561   application.ProcessEvent( event );
562
563   // More renders are needed in order to allow the node of the actor to become invisible.
564   application.SendNotification();
565   application.Render();
566   application.SendNotification();
567   application.Render();
568   application.SendNotification();
569   application.Render();
570
571   // An up event is sent outside the button's boundary but inside the actor's one.
572
573   event = Dali::Integration::TouchEvent();
574   event.AddPoint( GetPointUpOutside() );
575
576   // flush the queue and render once
577   application.SendNotification();
578   application.Render();
579   application.ProcessEvent( event );
580
581   DALI_TEST_CHECK( gOnTouchPointInterrupted );
582
583   // Test a down point inside the button which is also consumed by the actor, and a motion point
584   // consumed only by the actor.  gOnTouchPointInterrupted should be true (Button receives an
585   // interrupt event.
586
587   // Initializes TET state.
588   gOnTouchPointInterrupted = false;
589   tetButton.SetVisible( true );
590
591   application.SendNotification();
592   application.Render();
593   application.SendNotification();
594   application.Render();
595   application.SendNotification();
596   application.Render();
597
598   // A down event is sent inside the button's boundary.
599
600   event = Dali::Integration::TouchEvent();
601   event.AddPoint( GetPointDownInside() );
602
603   // flush the queue and render once
604   application.SendNotification();
605   application.Render();
606   application.ProcessEvent( event );
607
608   // More renders are needed in order to allow the node of the actor to become invisible.
609   application.SendNotification();
610   application.Render();
611   application.SendNotification();
612   application.Render();
613   application.SendNotification();
614   application.Render();
615
616   // A motion event is sent outside the button's boundary but inside the actor's one.
617
618   event = Dali::Integration::TouchEvent();
619   event.AddPoint( GetPointMotionOut() );
620
621   // flush the queue and render once
622   application.SendNotification();
623   application.Render();
624   application.ProcessEvent( event );
625
626   DALI_TEST_CHECK( gOnTouchPointInterrupted );
627
628   // Test a down point inside the button which is also consumed by the actor, and an up point
629   // also inside the button and consumed by the actor.  gOnTouchPointInterrupted should be false.
630
631   // Initializes TET state.
632   gOnTouchPointInterrupted = false;
633   tetButton.SetVisible( true );
634
635   application.SendNotification();
636   application.Render();
637   application.SendNotification();
638   application.Render();
639   application.SendNotification();
640   application.Render();
641
642   // A down event is sent inside the button's boundary.
643
644   event = Dali::Integration::TouchEvent();
645   event.AddPoint( GetPointDownInside() );
646
647   // flush the queue and render once
648   application.SendNotification();
649   application.Render();
650   application.ProcessEvent( event );
651
652   tetButton.SetVisible( true );
653
654   application.SendNotification();
655   application.Render();
656   application.SendNotification();
657   application.Render();
658   application.SendNotification();
659   application.Render();
660
661   // An up event is sent inside the button's boundary.
662
663   event = Dali::Integration::TouchEvent();
664   event.AddPoint( GetPointUpInside() );
665
666   // flush the queue and render once
667   application.SendNotification();
668   application.Render();
669   application.ProcessEvent( event );
670
671   DALI_TEST_CHECK( !gOnTouchPointInterrupted );
672   END_TEST;
673 }
674
675 int UtcDaliPushButtonProperties(void)
676 {
677   ToolkitTestApplication application;
678
679   PushButton button = PushButton::New();
680   Stage::GetCurrent().Add( button );
681
682   // Button::PROPERTY_AUTO_REPEATING
683   button.SetAutoRepeating( false );
684   DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::AUTO_REPEATING ) );
685   button.SetProperty( Button::Property::AUTO_REPEATING, true );
686   DALI_TEST_CHECK( button.IsAutoRepeating() ) ;
687   DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::AUTO_REPEATING ) );
688
689   // Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY
690   button.SetInitialAutoRepeatingDelay( 10.0f );
691   DALI_TEST_EQUALS( 10.0f, button.GetProperty< float >( Button::Property::INITIAL_AUTO_REPEATING_DELAY ), TEST_LOCATION );
692   button.SetProperty( Button::Property::INITIAL_AUTO_REPEATING_DELAY, 25.0f );
693   DALI_TEST_EQUALS( 25.0f, button.GetInitialAutoRepeatingDelay(), TEST_LOCATION );
694   DALI_TEST_EQUALS( 25.0f, button.GetProperty< float >( Button::Property::INITIAL_AUTO_REPEATING_DELAY ), TEST_LOCATION );
695
696   //  Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY
697   button.SetNextAutoRepeatingDelay( 3.0f );
698   DALI_TEST_EQUALS( 3.0f, button.GetProperty< float >( Button::Property::NEXT_AUTO_REPEATING_DELAY ), TEST_LOCATION );
699   button.SetProperty( Button::Property::NEXT_AUTO_REPEATING_DELAY, 4.0f );
700   DALI_TEST_EQUALS( 4.0f, button.GetNextAutoRepeatingDelay(), TEST_LOCATION );
701   DALI_TEST_EQUALS( 4.0f, button.GetProperty< float >( Button::Property::NEXT_AUTO_REPEATING_DELAY ), TEST_LOCATION );
702
703   //  Button::PROPERTY_TOGGLABLE
704   button.SetTogglableButton( false );
705   DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::TOGGLABLE ) );
706   button.SetProperty( Button::Property::TOGGLABLE, true );
707   DALI_TEST_CHECK( button.IsTogglableButton() ) ;
708   DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::TOGGLABLE ) );
709
710   //  Button::PROPERTY_SELECTED
711   button.SetSelected( false );
712   DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::SELECTED ) );
713   button.SetProperty( Button::Property::SELECTED, true );
714   DALI_TEST_CHECK( button.IsSelected() ) ;
715   DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::SELECTED ) );
716
717   END_TEST;
718 }