diff --git a/tests/data/themes/ui_button_bad_values_2.json b/tests/data/themes/ui_button_bad_values_2.json new file mode 100644 index 00000000..39459677 --- /dev/null +++ b/tests/data/themes/ui_button_bad_values_2.json @@ -0,0 +1,12 @@ +{ + "button": + { + "misc": + { + "state_transitions": + { + "normal_hovered": "dog" + } + } + } + } \ No newline at end of file diff --git a/tests/data/themes/ui_button_non_default.json b/tests/data/themes/ui_button_non_default.json index 76fd182b..529a26a2 100644 --- a/tests/data/themes/ui_button_non_default.json +++ b/tests/data/themes/ui_button_non_default.json @@ -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": { diff --git a/tests/test_core/test_layered_gui_group.py b/tests/test_core/test_layered_gui_group.py index 658196ee..1aed6a08 100644 --- a/tests/test_core/test_layered_gui_group.py +++ b/tests/test_core/test_layered_gui_group.py @@ -14,40 +14,73 @@ def update(self, time_delta: float): pass -class MyDodgySprite1(pygame.sprite.Sprite): +class MyDodgySprite1: def __init__(self, *groups): - super().__init__(*groups) + self.blendmode = 0 + + self.__g = {} + if groups: + self.add(*groups) + + def add(self, *groups): + has = self.__g.__contains__ + for group in groups: + if hasattr(group, '_spritegroup'): + if not has(group): + group.add_internal(self) + else: + self.add(*group) def update(self, time_delta: float): pass -class MyDodgySprite2(pygame.sprite.Sprite): +class MyDodgySprite2: def __init__(self, *groups): - super().__init__(*groups) self.visible = 1 + self.__g = {} + if groups: + self.add(*groups) + + def add(self, *groups): + has = self.__g.__contains__ + for group in groups: + if hasattr(group, '_spritegroup'): + if not has(group): + group.add_internal(self) + else: + self.add(*group) + def update(self, time_delta: float): pass class MyDodgySprite3(pygame.sprite.Sprite): def __init__(self, *groups): - super().__init__(*groups) self.blendmode = 0 self.visible = 1 + super().__init__(*groups) class TestUIElement: def test_remove_sprite_from_group(self, _init_pygame, default_ui_manager): group = LayeredGUIGroup() + group_2 = LayeredGUIGroup() proper_sprite = MyProperSprite(group) + proper_sprite_2 = MyProperSprite([group]) + + proper_sprite_3 = MyProperSprite([[group], [group_2]]) + proper_sprite.remove(group) + proper_sprite_2.remove([group]) + proper_sprite_3.remove([[group], [group_2]]) assert len(group.sprites()) == 0 + assert len(group_2.sprites()) == 0 def test_add_dodgy_sprites(self, _init_pygame, default_ui_manager): group = LayeredGUIGroup() @@ -58,7 +91,7 @@ def test_add_dodgy_sprites(self, _init_pygame, default_ui_manager): with pytest.raises(AttributeError): MyDodgySprite2(group) - with pytest.raises(AttributeError): + with pytest.raises(TypeError): MyDodgySprite3(group) def test_sprite_set_layer_before_add(self, _init_pygame, default_ui_manager): @@ -88,4 +121,4 @@ def test_print_sprite(self, _init_pygame, default_ui_manager): if __name__ == '__main__': - pytest.console_main() \ No newline at end of file + pytest.console_main() diff --git a/tests/test_elements/test_ui_button.py b/tests/test_elements/test_ui_button.py index f5517728..b7830ee3 100644 --- a/tests/test_elements/test_ui_button.py +++ b/tests/test_elements/test_ui_button.py @@ -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, @@ -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), @@ -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")) @@ -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", @@ -726,6 +761,30 @@ def test_class_theming_id(self, _init_pygame, _display_surface_return_none): '@test_class', 'button'] + def test_change_locale(self, _init_pygame, default_ui_manager, _display_surface_return_none): + button = UIButton(relative_rect=pygame.Rect(100, 100, 150, 30), + text="Test Button", + tool_tip_text="This is a test of the button's tool tip functionality.", + manager=default_ui_manager) + + default_ui_manager.set_locale('fr') + default_ui_manager.set_locale('ja') + + 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') + + assert dynamic_width_button.text == "Test Button" + if __name__ == '__main__': pytest.console_main() diff --git a/tests/test_elements/test_ui_panel.py b/tests/test_elements/test_ui_panel.py index 5922b957..7f61d714 100644 --- a/tests/test_elements/test_ui_panel.py +++ b/tests/test_elements/test_ui_panel.py @@ -380,6 +380,24 @@ def test_set_dimensions(self, _init_pygame, default_ui_manager): 'top': 'top', 'bottom': 'bottom'}) + button = UIButton(relative_rect=pygame.Rect(0, 40, 10, 10), text="A", + manager=default_ui_manager, + container=element_5, + anchors={'left': 'left', + 'right': 'left', + 'top': 'bottom', + 'bottom': 'bottom'} + ) + + button_2 = UIButton(relative_rect=pygame.Rect(40, 0, 10, 10), text="A", + manager=default_ui_manager, + container=element_5, + anchors={'left': 'right', + 'right': 'right', + 'top': 'top', + 'bottom': 'top'} + ) + assert element_5.relative_right_margin == 240 assert element_5.relative_bottom_margin == 240 diff --git a/tests/test_elements/test_ui_screen_space_health_bar.py b/tests/test_elements/test_ui_screen_space_health_bar.py index 3f50d92a..63795809 100644 --- a/tests/test_elements/test_ui_screen_space_health_bar.py +++ b/tests/test_elements/test_ui_screen_space_health_bar.py @@ -91,6 +91,15 @@ def test_update(self, _init_pygame, default_ui_manager): health_bar.update(0.01) assert health_bar.image is not None + def test_health_percentage(self, _init_pygame, default_ui_manager): + healthy_sprite = HealthySprite() + health_bar = UIScreenSpaceHealthBar(relative_rect=pygame.Rect(100, 100, 150, 30), + sprite_to_monitor=healthy_sprite, + manager=default_ui_manager) + healthy_sprite.current_health = 50 + health_bar.update(0.01) + assert health_bar.health_percentage == 0.5 + def test_rebuild_from_theme_data_non_default(self, _init_pygame): manager = UIManager((800, 600), os.path.join("tests", "data", "themes", "ui_screen_health_bar_non_default.json"))