Dali 3D User Interface Engine
utc-Dali-PushButton.cpp
Go to the documentation of this file.
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>
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 
42 const Dali::TouchPoint pointUpInside( 0, TouchPoint::Up, 240, 400 );
43 const Dali::TouchPoint pointLeave( 0, TouchPoint::Leave, 240, 400 );
44 const Dali::TouchPoint pointEnter( 0, TouchPoint::Motion, 240, 400 );
48 } // namespace
49 
50 
52 
53 namespace
54 {
55 static bool gOnTouchPointInterrupted = false;
56 } //namespace
57 
58 namespace Dali
59 {
60 
61 namespace Toolkit
62 {
63 
64 namespace Internal
65 {
66 class TETButton;
67 }
68 
72 class TETButton : public PushButton
73 {
74 public:
75  // PushButton Pressed
77 
78  ButtonSignalType& PressedSignal();
79 
83  TETButton();
84 
88  TETButton( const PushButton& button );
89 
93  TETButton& operator=( const TETButton& button );
94 
98  static TETButton New();
99 
103  static TETButton DownCast( BaseHandle handle );
104 
109  TETButton( Internal::TETButton& implementation );
110 
116 };
117 
118 namespace Internal
119 {
120 
124 class TETButton : public PushButton
125 {
126 public:
130  TETButton();
131 
135  virtual ~TETButton();
136 
140  static Toolkit::TETButton New();
141 
145  Toolkit::TETButton::ButtonSignalType& PressedSignal();
146 
150  void OnTouchPointInterrupted();
151 
155  void OnButtonDown();
156 
158 };
159 
160 } // namespace Internal
161 
163 {
164 }
165 
167 : PushButton( button )
168 {
169 }
170 
172 {
173  if( &button != this )
174  {
175  PushButton::operator=( button );
176  }
177  return *this;
178 }
179 
181 {
182  return Internal::TETButton::New();
183 }
184 
186 {
187  return Control::DownCast<TETButton, Internal::TETButton>(handle);
188 }
189 
191 {
192  TETButton button( *this );
193  DALI_ASSERT_ALWAYS( button );
194 
195  Dali::RefObject& handle = button.GetImplementation();
196 
197  return static_cast<Toolkit::Internal::TETButton&>( handle ).PressedSignal();
198 }
199 
201 : PushButton( implementation )
202 {}
203 
205 : PushButton( internal )
206 {
207  VerifyCustomActorPointer<Internal::TETButton>(internal);
208 }
209 
210 namespace Internal
211 {
212 
214 : PushButton(),
215  mPressedSignal()
216 {
217 }
218 
220 {
221 }
222 
224 {
225  // Create the implementation, temporarily owned on stack
226  IntrusivePtr< TETButton > internalTETButton = new TETButton();
227 
228  // Pass ownership to CustomActor
229  Dali::Toolkit::TETButton tetButton( *internalTETButton );
230 
231  // Second-phase init of the implementation
232  // This can only be done after the CustomActor connection has been made...
233  internalTETButton->Initialize();
234 
235  return tetButton;
236 }
237 
239 {
240  return mPressedSignal;
241 }
242 
244 {
245  Toolkit::TETButton handle( GetOwner() );
246 
247  //Emit signal.
248  mPressedSignal.Emit( handle );
249 }
250 
252 {
254 }
255 
256 } // namespace Internal
257 
258 } // namespace Toolkit
259 
260 } // namespace Dali
261 
262 namespace
263 {
264 
266 {
267 public:
268  enum Test
269  {
271  VISIBILITY
272  };
273 
274  TETButtonPressed( Actor actor, Test test )
275  : mActor( actor ),
276  mTest( test )
277  {
278  }
279 
280  bool Callback( PushButton button )
281  {
282  switch( mTest )
283  {
284  case SENSITIVENESS:
285  {
286  mActor.SetSensitive( false );
287  break;
288  }
289  case VISIBILITY:
290  {
291  std::cout <<"VISIBILITY false" << std::endl;
292  mActor.SetVisible( false );
293  break;
294  }
295  default:
296  {
297  break;
298  }
299  }
300  return true;
301  }
302 
305 };
306 
307 static bool TestCallback(Actor actor, const TouchEvent& event)
308 {
309  return true;
310 }
311 
312 } // namespace
313 
315 
317 {
318  ToolkitTestApplication application;
319  tet_infoline(" UtcDaliPushButtonDownCast");
320 
321  TETButton tetButton= Toolkit::TETButton::New();
322 
323  BaseHandle object(tetButton);
324 
325  TETButton tetButton2 = TETButton::DownCast( object );
326  DALI_TEST_CHECK(tetButton2);
327 
328  TETButton tetButton3 = DownCast< TETButton >(object);
329  DALI_TEST_CHECK(tetButton3);
330  END_TEST;
331 }
332 
334 {
335  ToolkitTestApplication application;
336  tet_infoline(" UtcDaliPushButtonInterruptEventWhenInsensitive");
337 
338  // * Creates an actor which contains a button.
339  // * The size of the actor is bigger than the button.
340  // * The button's boundary is contained in the actor's one.
341  Actor actor = Actor::New();
342  TETButton tetButton= Toolkit::TETButton::New();
343 
344  actor.SetName( "Actor" );
345  tetButton.SetName( "TETButton" );
346 
349  actor.SetPosition( 0, 0 );
350  actor.SetSize( 400, 800 );
351 
354  tetButton.SetPosition( 240, 400 );
355  tetButton.SetSize( 100, 100 );
356 
357  actor.Add( tetButton );
358  Stage::GetCurrent().Add( actor );
359 
360  // * Actor's touch event is connected to a callback function
361  // and this callback function consumes the event.
362  actor.TouchedSignal().Connect( &TestCallback );
363 
364  // * Button's pressed signal is connected to a callback function
365  // which also consumes the event.
366  // * Changes the sensitiveness of the button to false.
367  TETButtonPressed tetButtonPressed( actor, TETButtonPressed::SENSITIVENESS );
368  tetButton.PressedSignal().Connect( &tetButtonPressed, &TETButtonPressed::Callback );
369 
370  // Initializes TET state.
371  gOnTouchPointInterrupted = false;
372  tetButton.SetSensitive( true );
373 
374  Dali::Integration::TouchEvent event;
375 
376  // TET starts.
377 
378  // Test a down point inside the button which is also consumed by the actor, and an up point
379  // consumed only by the actor. gOnTouchPointInterrupted should be true (Button receives an
380  // interrupt event.
381 
382  application.SendNotification();
383  application.Render();
384 
385  // A down event is sent inside the button's boundary.
386 
387  event = Dali::Integration::TouchEvent();
388  event.AddPoint( pointDownInside );
389 
390  // flush the queue and render once
391  application.SendNotification();
392  application.Render();
393  application.ProcessEvent( event );
394 
395  // An up event is sent outside the button's boundary but inside the actor's one.
396 
397  event = Dali::Integration::TouchEvent();
398  event.AddPoint( pointUpOutside );
399 
400  // flush the queue and render once
401  application.SendNotification();
402  application.Render();
403  application.ProcessEvent( event );
404 
406 
407  // Test a down point inside the button which is also consumed by the actor, and a motion point
408  // consumed only by the actor. gOnTouchPointInterrupted should be true (Button receives an
409  // interrupt event.
410 
411  // Initializes TET state.
412  gOnTouchPointInterrupted = false;
413  actor.SetSensitive( true );
414  tetButton.SetSensitive( true );
415 
416  application.SendNotification();
417  application.Render();
418 
419  // A down event is sent inside the button's boundary.
420 
421  event = Dali::Integration::TouchEvent();
422  event.AddPoint( pointDownInside );
423 
424  // flush the queue and render once
425  application.SendNotification();
426  application.Render();
427  application.ProcessEvent( event );
428 
429  // A motion event is sent outside the button's boundary but inside the actor's one.
430 
431  event = Dali::Integration::TouchEvent();
432  event.AddPoint( pointMotionOut );
433 
434  // flush the queue and render once
435  application.SendNotification();
436  application.Render();
437  application.ProcessEvent( event );
438 
440 
441  // Test a down point inside the button which is also consumed by the actor, and an up point
442  // also inside the button and consumed by the actor. gOnTouchPointInterrupted should be false.
443 
444  // Initializes TET state.
445  gOnTouchPointInterrupted = false;
446  actor.SetSensitive( true );
447  tetButton.SetSensitive( true );
448 
449  // A down event is sent inside the button's boundary.
450 
451  event = Dali::Integration::TouchEvent();
452  event.AddPoint( pointDownInside );
453 
454  // flush the queue and render once
455  application.SendNotification();
456  application.Render();
457  application.ProcessEvent( event );
458 
459  actor.SetSensitive( true );
460  // An up event is sent inside the button's boundary.
461 
462  event = Dali::Integration::TouchEvent();
463  event.AddPoint( pointUpInside );
464 
465  // flush the queue and render once
466  application.SendNotification();
467  application.Render();
468  application.ProcessEvent( event );
469 
471  END_TEST;
472 }
473 
475 {
476  ToolkitTestApplication application;
477  tet_infoline(" UtcDaliPushButtonInterruptEventWhenNonVisible");
478 
479  // Does same test as above but changing the visibility instead the sensitiveness.
480 
481  // * Creates an actor which contains a button.
482  // * The size of the actor is bigger than the button.
483  // * The button's boundary is contained in the actor's one.
484  Actor actor = Actor::New();
485  TETButton tetButton = Toolkit::TETButton::New();
486 
487  actor.SetName( "Actor" );
488  tetButton.SetName( "TETButton" );
489 
492  actor.SetPosition( 0, 0 );
493  actor.SetSize( 400, 800 );
494 
497  tetButton.SetPosition( 240, 400 );
498  tetButton.SetSize( 100, 100 );
499 
500  actor.Add( tetButton );
501  Stage::GetCurrent().Add( actor );
502 
503  // * Actor's touch event is connected to a callback function
504  // and this callback function consumes the event.
505  actor.TouchedSignal().Connect( &TestCallback );
506 
507  // * Button's pressed signal is connected to a callback function
508  // which also consumes the event.
509  // * Changes the visibility of the button to false.
510  TETButtonPressed tetButtonPressed( tetButton, TETButtonPressed::VISIBILITY );
511  tetButton.PressedSignal().Connect( &tetButtonPressed, &TETButtonPressed::Callback );
512 
513  // Initializes TET state.
514  gOnTouchPointInterrupted = false;
515  tetButton.SetVisible( true );
516 
517  Dali::Integration::TouchEvent event;
518 
519  // TET starts.
520 
521  // Test a down point inside the button which is also consumed by the actor, and an up point
522  // consumed only by the actor. gOnTouchPointInterrupted should be true (Button receives an
523  // interrupt event.
524 
525  application.SendNotification();
526  application.Render();
527 
528  // A down event is sent inside the button's boundary.
529 
530  event = Dali::Integration::TouchEvent();
531  event.AddPoint( pointDownInside );
532 
533  // flush the queue and render once
534  application.SendNotification();
535  application.Render();
536  application.ProcessEvent( event );
537 
538  // More renders are needed in order to allow the node of the actor to become invisible.
539  application.SendNotification();
540  application.Render();
541  application.SendNotification();
542  application.Render();
543  application.SendNotification();
544  application.Render();
545 
546  // An up event is sent outside the button's boundary but inside the actor's one.
547 
548  event = Dali::Integration::TouchEvent();
549  event.AddPoint( pointUpOutside );
550 
551  // flush the queue and render once
552  application.SendNotification();
553  application.Render();
554  application.ProcessEvent( event );
555 
557 
558  // Test a down point inside the button which is also consumed by the actor, and a motion point
559  // consumed only by the actor. gOnTouchPointInterrupted should be true (Button receives an
560  // interrupt event.
561 
562  // Initializes TET state.
563  gOnTouchPointInterrupted = false;
564  tetButton.SetVisible( true );
565 
566  application.SendNotification();
567  application.Render();
568  application.SendNotification();
569  application.Render();
570  application.SendNotification();
571  application.Render();
572 
573  // A down event is sent inside the button's boundary.
574 
575  event = Dali::Integration::TouchEvent();
576  event.AddPoint( pointDownInside );
577 
578  // flush the queue and render once
579  application.SendNotification();
580  application.Render();
581  application.ProcessEvent( event );
582 
583  // More renders are needed in order to allow the node of the actor to become invisible.
584  application.SendNotification();
585  application.Render();
586  application.SendNotification();
587  application.Render();
588  application.SendNotification();
589  application.Render();
590 
591  // A motion event is sent outside the button's boundary but inside the actor's one.
592 
593  event = Dali::Integration::TouchEvent();
594  event.AddPoint( pointMotionOut );
595 
596  // flush the queue and render once
597  application.SendNotification();
598  application.Render();
599  application.ProcessEvent( event );
600 
602 
603  // Test a down point inside the button which is also consumed by the actor, and an up point
604  // also inside the button and consumed by the actor. gOnTouchPointInterrupted should be false.
605 
606  // Initializes TET state.
607  gOnTouchPointInterrupted = false;
608  tetButton.SetVisible( true );
609 
610  application.SendNotification();
611  application.Render();
612  application.SendNotification();
613  application.Render();
614  application.SendNotification();
615  application.Render();
616 
617  // A down event is sent inside the button's boundary.
618 
619  event = Dali::Integration::TouchEvent();
620  event.AddPoint( pointDownInside );
621 
622  // flush the queue and render once
623  application.SendNotification();
624  application.Render();
625  application.ProcessEvent( event );
626 
627  tetButton.SetVisible( true );
628 
629  application.SendNotification();
630  application.Render();
631  application.SendNotification();
632  application.Render();
633  application.SendNotification();
634  application.Render();
635 
636  // An up event is sent inside the button's boundary.
637 
638  event = Dali::Integration::TouchEvent();
639  event.AddPoint( pointUpInside );
640 
641  // flush the queue and render once
642  application.SendNotification();
643  application.Render();
644  application.ProcessEvent( event );
645 
647  END_TEST;
648 }
649 
651 {
652  ToolkitTestApplication application;
653 
654  PushButton button = PushButton::New();
655  Stage::GetCurrent().Add( button );
656 
657  // Button::PROPERTY_AUTO_REPEATING
658  button.SetAutoRepeating( false );
659  DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::AUTO_REPEATING ) );
660  button.SetProperty( Button::Property::AUTO_REPEATING, true );
661  DALI_TEST_CHECK( button.IsAutoRepeating() ) ;
662  DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::AUTO_REPEATING ) );
663 
664  // Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY
665  button.SetInitialAutoRepeatingDelay( 10.0f );
666  DALI_TEST_EQUALS( 10.0f, button.GetProperty< float >( Button::Property::INITIAL_AUTO_REPEATING_DELAY ), TEST_LOCATION );
667  button.SetProperty( Button::Property::INITIAL_AUTO_REPEATING_DELAY, 25.0f );
669  DALI_TEST_EQUALS( 25.0f, button.GetProperty< float >( Button::Property::INITIAL_AUTO_REPEATING_DELAY ), TEST_LOCATION );
670 
671  // Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY
672  button.SetNextAutoRepeatingDelay( 3.0f );
673  DALI_TEST_EQUALS( 3.0f, button.GetProperty< float >( Button::Property::NEXT_AUTO_REPEATING_DELAY ), TEST_LOCATION );
674  button.SetProperty( Button::Property::NEXT_AUTO_REPEATING_DELAY, 4.0f );
676  DALI_TEST_EQUALS( 4.0f, button.GetProperty< float >( Button::Property::NEXT_AUTO_REPEATING_DELAY ), TEST_LOCATION );
677 
678  // Button::PROPERTY_TOGGLABLE
679  button.SetTogglableButton( false );
680  DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::TOGGLABLE ) );
681  button.SetProperty( Button::Property::TOGGLABLE, true );
682  DALI_TEST_CHECK( button.IsTogglableButton() ) ;
683  DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::TOGGLABLE ) );
684 
685  // Button::PROPERTY_SELECTED
686  button.SetSelected( false );
687  DALI_TEST_CHECK( ! button.GetProperty< bool >( Button::Property::SELECTED ) );
688  button.SetProperty( Button::Property::SELECTED, true );
689  DALI_TEST_CHECK( button.IsSelected() ) ;
690  DALI_TEST_CHECK( button.GetProperty< bool >( Button::Property::SELECTED ) );
691 
692  END_TEST;
693 }
Dali Docs Home
Read more about Dali