Skip to content

Commit

Permalink
UI Button test coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MyreMylar committed Aug 3, 2022
1 parent b23cb22 commit 62cdfa4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tests/data/themes/ui_button_bad_values_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"button":
{
"misc":
{
"state_transitions":
{
"normal_hovered": "dog"
}
}
}
}
2 changes: 2 additions & 0 deletions tests/data/themes/ui_button_non_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"text_vert_alignment": "top",
"text_horiz_alignment_padding": "6",
"text_vert_alignment_padding": "7",
"text_shadow_size": "3",
"text_shadow_offset": "0,2",

"state_transitions":
{
Expand Down
41 changes: 40 additions & 1 deletion tests/test_elements/test_ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def test_process_event_double_click(self, _init_pygame: None, default_ui_manager
consumed_event_1 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': pygame.BUTTON_LEFT,
'pos': button.rect.center}))
button.update(0.001)

consumed_event_2 = button.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': pygame.BUTTON_LEFT,
Expand Down Expand Up @@ -505,6 +506,14 @@ def test_set_text(self, _init_pygame: None, default_ui_manager: UIManager,
assert (empty_queue == 0 and full_queue != 0 and
button.drawable_shape.theming['text'] == 'Ipsum' and button.text == 'Ipsum')

dynamic_width_button = UIButton(relative_rect=pygame.Rect(10, 10, -1, 30),
text="Test Button",
tool_tip_text="This is a test of the button's tool tip functionality.",
manager=default_ui_manager)

dynamic_width_button.set_text('Ipsum')
assert dynamic_width_button.text == "Ipsum"

def test_set_text_same(self, _init_pygame: None, default_ui_manager: UIManager,
_display_surface_return_none):
button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30),
Expand Down Expand Up @@ -606,6 +615,18 @@ def test_rebuild_from_changed_theme_data_bad_values(self, _init_pygame,

assert button.image is not None

def test_rebuild_from_changed_theme_data_bad_values_2(self, _init_pygame,
_display_surface_return_none):
manager = UIManager((800, 600),
os.path.join("tests", "data", "themes", "ui_button_bad_values_2.json"))

button = UIButton(relative_rect=pygame.Rect(10, 10, 150, 30),
text="Test Button",
tool_tip_text="This is a test of the button's tool tip functionality.",
manager=manager)

assert button.state_transitions[("normal", "hovered")] == 0.0

def test_rebuild_shape(self, _init_pygame, _display_surface_return_none):
manager = UIManager((800, 600),
os.path.join("tests", "data", "themes", "ui_button_non_default.json"))
Expand All @@ -628,6 +649,20 @@ def test_rebuild_shape_ellipse(self, _init_pygame, _display_surface_return_none)

assert button.image is not None

def test_rebuild_anchors_dynamic_dimensions(self, _init_pygame, default_ui_manager, _display_surface_return_none):

button = UIButton(relative_rect=pygame.Rect(10, 10, 150, -1),
text="Test Button",
tool_tip_text="This is a test of the button's tool tip functionality.",
manager=default_ui_manager,
anchors={'top': 'bottom', 'bottom': 'bottom'})

assert button.dynamic_height

button.rebuild()

assert button.image is not None

def test_show(self, _init_pygame, default_ui_manager, _display_surface_return_none):
button = UIButton(relative_rect=pygame.Rect(100, 100, 150, 30),
text="Test Button",
Expand Down Expand Up @@ -737,15 +772,19 @@ def test_change_locale(self, _init_pygame, default_ui_manager, _display_surface_

assert button.text == "Test Button"

default_ui_manager.set_locale('en')

dynamic_width_button = UIButton(relative_rect=pygame.Rect(100, 100, -1, 30),
text="Test Button",
tool_tip_text="This is a test of the button's tool tip functionality.",
manager=default_ui_manager)

assert dynamic_width_button.dynamic_width

default_ui_manager.set_locale('fr')
default_ui_manager.set_locale('ja')

assert dynamic_width_button.text == "Test Button"


if __name__ == '__main__':
pytest.console_main()

0 comments on commit 62cdfa4

Please sign in to comment.