@@ -450,6 +450,108 @@ def test_enable(self, _init_pygame: None, default_ui_manager: UIManager,
450
450
button .update (0.01 )
451
451
452
452
assert button .check_pressed () is True and button .is_enabled is True
453
+
454
+ def test_command (self , _init_pygame : None , default_ui_manager : UIManager ,
455
+ _display_surface_return_none ):
456
+ button_clicked = False
457
+
458
+ def test_function (data ):
459
+ nonlocal button_clicked
460
+ button_clicked = True
461
+
462
+ button = UIButton (relative_rect = pygame .Rect (10 , 10 , 150 , 30 ),
463
+ text = "Test Button" ,
464
+ tool_tip_text = "This is a test of the button's tool tip functionality." ,
465
+ manager = default_ui_manager ,
466
+ command = test_function )
467
+
468
+ assert not button_clicked
469
+ assert button .handler [pygame_gui .UI_BUTTON_PRESSED ] == test_function
470
+
471
+ # process a mouse button down event
472
+ button .process_event (
473
+ pygame .event .Event (pygame .MOUSEBUTTONDOWN , {'button' : 1 , 'pos' : (50 , 25 )}))
474
+
475
+ # process a mouse button up event
476
+ button .process_event (
477
+ pygame .event .Event (pygame .MOUSEBUTTONUP , {'button' : 1 , 'pos' : (50 , 25 )}))
478
+
479
+ button .update (0.01 )
480
+ assert button_clicked
481
+
482
+ def test_command_bad_value (self , _init_pygame : None , default_ui_manager : UIManager ,
483
+ _display_surface_return_none ):
484
+ with pytest .raises (TypeError , match = "Function to bind must be callable" ):
485
+ button = UIButton (relative_rect = pygame .Rect (10 , 10 , 150 , 30 ),
486
+ text = "Test Button" ,
487
+ tool_tip_text = "This is a test of the button's tool tip functionality." ,
488
+ manager = default_ui_manager ,
489
+ command = {pygame_gui .UI_BUTTON_PRESSED :5 })
490
+
491
+ def test_bind (self , _init_pygame : None , default_ui_manager : UIManager ,
492
+ _display_surface_return_none ):
493
+ def test_function (data ):
494
+ pass
495
+
496
+ button = UIButton (relative_rect = pygame .Rect (10 , 10 , 150 , 30 ),
497
+ text = "Test Button" ,
498
+ tool_tip_text = "This is a test of the button's tool tip functionality." ,
499
+ manager = default_ui_manager )
500
+
501
+ assert pygame_gui .UI_BUTTON_PRESSED not in button .handler
502
+
503
+ button .bind (pygame_gui .UI_BUTTON_PRESSED , test_function )
504
+ assert button .handler [pygame_gui .UI_BUTTON_PRESSED ] == test_function
505
+
506
+ button .bind (pygame_gui .UI_BUTTON_PRESSED , None )
507
+ assert pygame_gui .UI_BUTTON_PRESSED not in button .handler
508
+
509
+ with pytest .raises (TypeError , match = "Function to bind must be callable" ):
510
+ button .bind (pygame_gui .UI_BUTTON_PRESSED , "non-callable" )
511
+
512
+ def test_on_event (self , _init_pygame : None , default_ui_manager : UIManager ,
513
+ _display_surface_return_none ):
514
+ button_start_press = False
515
+
516
+ def test_function (data ):
517
+ nonlocal button_start_press
518
+ button_start_press = True
519
+
520
+ pressed_button = 0
521
+ def test_function2 (data ):
522
+ nonlocal pressed_button
523
+ pressed_button = data ["mouse_button" ]
524
+
525
+ command_dict = {pygame_gui .UI_BUTTON_START_PRESS :test_function ,
526
+ pygame_gui .UI_BUTTON_PRESSED :test_function2 }
527
+
528
+ button = UIButton (relative_rect = pygame .Rect (10 , 10 , 150 , 30 ),
529
+ text = "Test Button" ,
530
+ tool_tip_text = "This is a test of the button's tool tip functionality." ,
531
+ manager = default_ui_manager ,
532
+ command = command_dict )
533
+
534
+ assert button .handler [pygame_gui .UI_BUTTON_START_PRESS ] == test_function
535
+ assert button .handler [pygame_gui .UI_BUTTON_PRESSED ] == test_function2
536
+ assert pygame_gui .UI_BUTTON_DOUBLE_CLICKED not in button .handler
537
+
538
+ assert not button_start_press
539
+ button .on_event (pygame_gui .UI_BUTTON_START_PRESS , {'mouse_button' :1 })
540
+ assert button_start_press
541
+
542
+ assert pressed_button == 0
543
+ button .on_event (pygame_gui .UI_BUTTON_PRESSED , {'mouse_button' :3 })
544
+ assert pressed_button == 3
545
+
546
+ button .on_event (pygame_gui .UI_BUTTON_DOUBLE_CLICKED , {'mouse_button' :1 })
547
+
548
+ confirm_double_click_event_fired = False
549
+ for event in pygame .event .get ():
550
+ if (event .type == pygame_gui .UI_BUTTON_DOUBLE_CLICKED and
551
+ event .ui_element == button ):
552
+ confirm_double_click_event_fired = True
553
+ assert confirm_double_click_event_fired
554
+
453
555
454
556
def test_set_active (self , _init_pygame : None , default_ui_manager : UIManager ,
455
557
_display_surface_return_none ):
0 commit comments