Dali 3D User Interface Engine
utc-Dali-Button.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 #include <stdlib.h>
20 
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
24 
25 #include <dali.h>
28 
29 using namespace Dali;
30 using namespace Toolkit;
31 
32 
34 {
36 }
37 
39 {
41 }
42 
43 namespace
44 {
45 static bool gIsCalledButtonCallback = false;
46 
47 static bool ButtonCallback( Button button )
48 {
49  gIsCalledButtonCallback = true;
50  return false;
51 }
52 
53 Image CreateSolidColorImage( const Vector4& color, unsigned int width, unsigned int height )
54 {
55  BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
56 
57  // Create the image
58  PixelBuffer* pixbuf = imageData.GetBuffer();
59  unsigned int size = width * height;
60 
61  for( size_t i = 0; i < size; i++ )
62  {
63  pixbuf[i*4+0] = 0xFF * color.r;
64  pixbuf[i*4+1] = 0xFF * color.g;
65  pixbuf[i*4+2] = 0xFF * color.b;
66  pixbuf[i*4+3] = 0xFF * color.a;
67  }
68 
69  imageData.Update();
70 
71  return imageData;
72 }
73 
75 const Dali::TouchPoint pointUpInside( 0, TouchPoint::Up, 240, 400 );
76 const Dali::TouchPoint pointLeave( 0, TouchPoint::Leave, 240, 400 );
77 const Dali::TouchPoint pointEnter( 0, TouchPoint::Motion, 240, 400 );
80 
81 static float ANIMATION_TIME( 0.5f );
82 } // namespace
83 
85 {
86  TestApplication application;
87 
88  Button button;
89 
90  DALI_TEST_CHECK( !button );
91  END_TEST;
92 }
93 
95 {
96  TestApplication application;
97 
98  // Initialize an object, ref count == 1
99  Button button = PushButton::New();
100 
101  Button copy( button );
102  DALI_TEST_CHECK( copy );
103  END_TEST;
104 }
105 
107 {
108  TestApplication application;
109 
110  Button button = PushButton::New();
111 
112  Button copy( button );
113  DALI_TEST_CHECK( copy );
114 
115  DALI_TEST_CHECK( button == copy );
116  END_TEST;
117 }
118 
120 {
121  TestApplication application;
122 
123  Button button = PushButton::New();
124 
125  BaseHandle object(button);
126 
127  Button button2 = Button::DownCast( object );
128  DALI_TEST_CHECK(button2);
129 
130  Button button3 = DownCast< Button >(object);
131  DALI_TEST_CHECK(button3);
132  END_TEST;
133 }
134 
136 {
137  TestApplication application;
138 
139  BaseHandle unInitializedObject;
140 
141  Button button1 = Button::DownCast( unInitializedObject );
142  DALI_TEST_CHECK( !button1 );
143 
144  Button button2 = DownCast< Button >( unInitializedObject );
145  DALI_TEST_CHECK( !button2 );
146  END_TEST;
147 }
148 
150 {
151  ToolkitTestApplication application;
152 
153  Button button = PushButton::New();
154 
155  button.SetDisabled( true );
156 
157  DALI_TEST_CHECK( button.IsDisabled() );
158 
159  button.SetDisabled( false );
160 
161  DALI_TEST_CHECK( !button.IsDisabled() );
162 
163  button.SetDisabled( true );
164 
165  DALI_TEST_CHECK( button.IsDisabled() );
166 
167  button.SetDisabled( false );
168 
169  DALI_TEST_CHECK( !button.IsDisabled() );
170  END_TEST;
171 }
172 
174 {
175  ToolkitTestApplication application;
176 
177  Button button = PushButton::New();
178 
179  button.SetDisabled( true );
180 
181  DALI_TEST_CHECK( button.IsDisabled() );
182 
183  button.SetDisabled( false );
184 
185  DALI_TEST_CHECK( !button.IsDisabled() );
186  END_TEST;
187 }
188 
190 {
191  ToolkitTestApplication application;
192 
193  Button button = PushButton::New();
194 
195  button.SetAutoRepeating( true );
196 
197  DALI_TEST_CHECK( button.IsAutoRepeating() );
198 
199  button.SetAutoRepeating( false );
200 
201  DALI_TEST_CHECK( !button.IsAutoRepeating() );
202 
203  button.SetAutoRepeating( true );
204 
205  DALI_TEST_CHECK( button.IsAutoRepeating() );
206 
207  button.SetAutoRepeating( false );
208 
209  DALI_TEST_CHECK( !button.IsAutoRepeating() );
210  END_TEST;
211 }
212 
214 {
215  ToolkitTestApplication application;
216 
217  Button button = PushButton::New();
218 
219  button.SetAutoRepeating( true );
220 
221  DALI_TEST_CHECK( button.IsAutoRepeating() );
222 
223  button.SetAutoRepeating( false );
224 
225  DALI_TEST_CHECK( !button.IsAutoRepeating() );
226  END_TEST;
227 }
228 
230 {
231  ToolkitTestApplication application;
232 
233  Button button = PushButton::New();
234 
235  button.SetInitialAutoRepeatingDelay( 0.5f );
236 
238 
239  button.SetInitialAutoRepeatingDelay( 0.2f );
240 
242  END_TEST;
243 }
244 
246 {
247  ToolkitTestApplication application;
248 
249  Button button = PushButton::New();
250 
251  button.SetNextAutoRepeatingDelay( 0.5f );
252 
254 
255  button.SetNextAutoRepeatingDelay( 0.2f );
256 
258  END_TEST;
259 }
260 
262 {
263  ToolkitTestApplication application;
264 
265  Button button = PushButton::New();
266 
267  button.SetTogglableButton( true );
268 
270 
271  button.SetTogglableButton( false );
272 
273  DALI_TEST_CHECK( !button.IsTogglableButton() );
274  END_TEST;
275 }
276 
278 {
279  ToolkitTestApplication application;
280 
281  Button button = PushButton::New();
282  button.SetTogglableButton( true );
283 
284  button.SetSelected( true );
285 
286  DALI_TEST_CHECK( button.IsSelected() );
287 
288  button.SetSelected( false );
289 
290  DALI_TEST_CHECK( !button.IsSelected() );
291  END_TEST;
292 }
293 
295 {
296  ToolkitTestApplication application;
297  tet_infoline(" UtcDaliButtonSetAnimationTimeP");
298 
299  Button button = PushButton::New();
300 
302 
304  END_TEST;
305 }
306 
308 {
309  ToolkitTestApplication application;
310 
311  Button button = PushButton::New();
312 
313  button.SetLabelText( "Button Label" );
314 
315  DALI_TEST_EQUALS( button.GetLabelText(), "Button Label", TEST_LOCATION );
316  END_TEST;
317 }
318 
320 {
321  ToolkitTestApplication application;
322 
323  Button button = PushButton::New();
324 
325  button.SetLabelText( "Button Label" );
326 
327  DALI_TEST_EQUALS( button.GetLabelText(), "Button Label", TEST_LOCATION );
328  END_TEST;
329 }
330 
332 {
333  ToolkitTestApplication application;
334  tet_infoline(" UtcDaliButtonSetUnselectedImageP");
335 
336  PushButton pushButton = PushButton::New();
337  Stage::GetCurrent().Add( pushButton );
338 
339  application.SendNotification();
340  application.Render();
341 
342  pushButton.SetSize( Vector2( 20.0f, 20.0f ) );
343  pushButton.SetUnselectedImage( "Image.jpg" );
344 
345  application.SendNotification();
346  application.Render();
347 
348  Vector3 size = pushButton.GetCurrentSize();
349 
350  DALI_TEST_EQUALS( size.width, 20.f, TEST_LOCATION );
351  DALI_TEST_EQUALS( size.height, 20.f, TEST_LOCATION );
352 
353  END_TEST;
354 }
355 
357 {
358  ToolkitTestApplication application;
359  tet_infoline(" UtcDaliButtonSetButtonImage");
360 
361  PushButton pushButton = PushButton::New();
362  Stage::GetCurrent().Add( pushButton );
363 
364  application.SendNotification();
365  application.Render();
366 
367  pushButton.SetSize( Vector2( 20.0f, 20.0f ) );
368  pushButton.SetSelectedImage( "Image.jpg" );
369 
370  application.SendNotification();
371  application.Render();
372 
373  Vector3 size = pushButton.GetCurrentSize();
374 
375  DALI_TEST_EQUALS( size.width, 20.f, TEST_LOCATION );
376  DALI_TEST_EQUALS( size.height, 20.f, TEST_LOCATION );
377 
378  END_TEST;
379 }
380 
382 {
383  ToolkitTestApplication application;
384  tet_infoline(" UtcDaliButtonPressedSignalP");
385 
386  Button button = PushButton::New();
389  button.SetPosition( 240, 400 );
390  button.SetSize( 100, 100 );
391 
392  Stage::GetCurrent().Add( button );
393 
394  application.SendNotification();
395  application.Render();
396 
397  // connect to its touch signal
398  button.PressedSignal().Connect( &ButtonCallback );
399  button.ReleasedSignal().Connect( &ButtonCallback );
400 
401  Dali::Integration::TouchEvent event;
402 
403  // Test1. Touch point down and up inside the button.
404 
405  gIsCalledButtonCallback = false;
406  event = Dali::Integration::TouchEvent();
407  event.AddPoint( pointDownInside );
408  application.ProcessEvent( event );
409 
411 
412  gIsCalledButtonCallback = false;
413  event = Dali::Integration::TouchEvent();
414  event.AddPoint( pointUpInside );
415  application.ProcessEvent( event );
416 
418 
419  // Test2. Touch point down and up outside the button.
420 
421  gIsCalledButtonCallback = false;
422  event = Dali::Integration::TouchEvent();
423  event.AddPoint( pointDownOutside );
424  application.ProcessEvent( event );
425 
427 
428  gIsCalledButtonCallback = false;
429  event = Dali::Integration::TouchEvent();
430  event.AddPoint( pointUpOutside );
431  application.ProcessEvent( event );
432 
434 
435  // Test3. Touch point down inside and up outside the button.
436 
437  gIsCalledButtonCallback = false;
438  event = Dali::Integration::TouchEvent();
439  event.AddPoint( pointDownInside );
440  application.ProcessEvent( event );
441 
443 
444  gIsCalledButtonCallback = false;
445  event = Dali::Integration::TouchEvent();
446  event.AddPoint( pointLeave );
447  application.ProcessEvent( event );
448 
449  event = Dali::Integration::TouchEvent();
450  event.AddPoint( pointUpOutside );
451  application.ProcessEvent( event );
452 
454 
455  // Test4. Touch point down outside and up inside the button.
456 
457  gIsCalledButtonCallback = false;
458  event = Dali::Integration::TouchEvent();
459  event.AddPoint( pointDownOutside );
460  application.ProcessEvent( event );
461 
463 
464  gIsCalledButtonCallback = false;
465  event = Dali::Integration::TouchEvent();
466  event.AddPoint( pointEnter );
467  application.ProcessEvent( event );
468 
469  event = Dali::Integration::TouchEvent();
470  event.AddPoint( pointUpInside );
471  application.ProcessEvent( event );
472 
474  END_TEST;
475 }
476 
478 {
479  ToolkitTestApplication application;
480  tet_infoline(" UtcDaliButtonClickedSignalP");
481 
482  Button button = PushButton::New();
485  button.SetPosition( 240, 400 );
486  button.SetSize( 100, 100 );
487 
488  Stage::GetCurrent().Add( button );
489 
490  application.SendNotification();
491  application.Render();
492 
493  // connect to its touch signal
494  button.ClickedSignal().Connect( &ButtonCallback );
495 
496  Dali::Integration::TouchEvent event;
497 
498  // Test1. Touch point down and up inside the button.
499 
500  gIsCalledButtonCallback = false;
501  event = Dali::Integration::TouchEvent();
502  event.AddPoint( pointDownInside );
503  application.ProcessEvent( event );
504 
505  event = Dali::Integration::TouchEvent();
506  event.AddPoint( pointUpInside );
507  application.ProcessEvent( event );
508 
510 
511  // Test2. Touch point down and up outside the button.
512 
513  gIsCalledButtonCallback = false;
514  event = Dali::Integration::TouchEvent();
515  event.AddPoint( pointDownOutside );
516  application.ProcessEvent( event );
517 
518  event = Dali::Integration::TouchEvent();
519  event.AddPoint( pointUpOutside );
520  application.ProcessEvent( event );
521 
523 
524  // Test3. Touch point down inside and up outside the button.
525 
526  gIsCalledButtonCallback = false;
527  event = Dali::Integration::TouchEvent();
528  event.AddPoint( pointDownInside );
529  application.ProcessEvent( event );
530 
531  event = Dali::Integration::TouchEvent();
532  event.AddPoint( pointLeave );
533  application.ProcessEvent( event );
534 
535  event = Dali::Integration::TouchEvent();
536  event.AddPoint( pointUpOutside );
537  application.ProcessEvent( event );
538 
540 
541  // Test4. Touch point down outside and up inside the button.
542 
543  gIsCalledButtonCallback = false;
544  event = Dali::Integration::TouchEvent();
545  event.AddPoint( pointDownOutside );
546  application.ProcessEvent( event );
547 
548  event = Dali::Integration::TouchEvent();
549  event.AddPoint( pointEnter );
550  application.ProcessEvent( event );
551 
552  event = Dali::Integration::TouchEvent();
553  event.AddPoint( pointUpInside );
554  application.ProcessEvent( event );
555 
557  END_TEST;
558 }
559 
561 {
562  ToolkitTestApplication application;
563  tet_infoline(" UtcDaliButtonStateChangedSignalP");
564 
565  Button button = PushButton::New();
566  button.SetTogglableButton( true );
567 
568  Stage::GetCurrent().Add( button );
569 
570  application.SendNotification();
571  application.Render();
572 
573  // connect to its signal
574  button.StateChangedSignal().Connect( &ButtonCallback );
575 
576  gIsCalledButtonCallback = false;
577  button.SetSelected( true );
578 
580 
581  gIsCalledButtonCallback = false;
582  button.SetSelected( false );
583 
585  END_TEST;
586 }
587 
589 {
590  tet_infoline("UtcDaliButtonSetProperty: ");
591  ToolkitTestApplication application;
592 
593  PushButton pushButton = PushButton::New();
594 
595  pushButton.SetProperty(pushButton.GetPropertyIndex("disabled"), false);
596  DALI_TEST_CHECK( false == pushButton.IsDisabled() );
597 
598  pushButton.SetProperty(pushButton.GetPropertyIndex("disabled"), true);
599  DALI_TEST_CHECK( true == pushButton.IsDisabled() );
600 
601  END_TEST;
602 }
603 
605 {
606  ToolkitTestApplication application;
607  tet_infoline(" UtcDaliButtonSize");
608 
609  // First an image is set, then SetSize is called.
610  PushButton pushButton = PushButton::New();
611  Stage::GetCurrent().Add( pushButton );
612 
613  pushButton.SetBackgroundImage( "Image.jpg" );
614  pushButton.SetSize( 10.f, 10.f );
615 
616  application.SendNotification();
617  application.Render();
618 
619  Vector3 size = pushButton.GetCurrentSize();
620 
621  DALI_TEST_EQUALS( size.width, 10.f, TEST_LOCATION );
622  DALI_TEST_EQUALS( size.height, 10.f, TEST_LOCATION );
623  END_TEST;
624 }
Dali Docs Home
Read more about Dali