Skip to content

Commit

Permalink
Merge pull request #291 from MyreMylar/elements-coverage-imps-2
Browse files Browse the repository at this point in the history
Further improvements to UI Elements testing coverage
  • Loading branch information
MyreMylar authored Aug 5, 2022
2 parents 5892ebd + 9268228 commit 2c9c718
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 110 deletions.
18 changes: 9 additions & 9 deletions pygame_gui/core/interfaces/text_owner_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IUITextOwnerInterface(metaclass=ABCMeta):
"""

@abstractmethod
def set_text_alpha(self, alpha: int, sub_chunk: Optional['TextLineChunkFTFont']):
def set_text_alpha(self, alpha: int, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Set the global alpha value for the text
Expand All @@ -24,7 +24,7 @@ def set_text_alpha(self, alpha: int, sub_chunk: Optional['TextLineChunkFTFont'])

@abstractmethod
def set_text_offset_pos(self, offset: Tuple[int, int],
sub_chunk: Optional['TextLineChunkFTFont']):
sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Move the text around by this offset.
Expand All @@ -35,7 +35,7 @@ def set_text_offset_pos(self, offset: Tuple[int, int],

@abstractmethod
def set_text_rotation(self, rotation: int,
sub_chunk: Optional['TextLineChunkFTFont']):
sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
rotate the text by this int in degrees
Expand All @@ -45,7 +45,7 @@ def set_text_rotation(self, rotation: int,
"""

@abstractmethod
def set_text_scale(self, scale: float, sub_chunk: Optional['TextLineChunkFTFont']):
def set_text_scale(self, scale: float, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Scale the text by this float
Expand All @@ -55,15 +55,15 @@ def set_text_scale(self, scale: float, sub_chunk: Optional['TextLineChunkFTFont
"""

@abstractmethod
def clear_text_surface(self, sub_chunk: Optional['TextLineChunkFTFont']):
def clear_text_surface(self, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Clear the text surface
:param sub_chunk: An optional chunk so we only clear the surface for this chunk.
"""

@abstractmethod
def get_text_letter_count(self, sub_chunk: Optional['TextLineChunkFTFont']) -> int:
def get_text_letter_count(self, sub_chunk: Optional['TextLineChunkFTFont'] = None) -> int:
"""
The amount of letters in the text
Expand All @@ -72,7 +72,7 @@ def get_text_letter_count(self, sub_chunk: Optional['TextLineChunkFTFont']) -> i
:return: number of letters as an int
"""
@abstractmethod
def update_text_end_position(self, end_pos: int, sub_chunk: Optional['TextLineChunkFTFont']):
def update_text_end_position(self, end_pos: int, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
The position in the text to render up to.
Expand Down Expand Up @@ -120,7 +120,7 @@ def get_object_id(self) -> str:
"""

@abstractmethod
def stop_finished_effect(self, sub_chunk: Optional['TextLineChunkFTFont']):
def stop_finished_effect(self, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Stops a finished effect. Will leave effected text in the state it was in when effect
ended. Used when an effect reaches a natural end where we might want to keep it in
Expand All @@ -130,7 +130,7 @@ def stop_finished_effect(self, sub_chunk: Optional['TextLineChunkFTFont']):
"""

@abstractmethod
def clear_all_active_effects(self, sub_chunk: Optional['TextLineChunkFTFont']):
def clear_all_active_effects(self, sub_chunk: Optional['TextLineChunkFTFont'] = None):
"""
Clears any active effects and redraws the text. A full reset, usually called before
firing off a new effect if one is already in progress.
Expand Down
6 changes: 3 additions & 3 deletions pygame_gui/elements/ui_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,15 @@ def set_text_alpha(self, alpha: int, sub_chunk: Optional[TextLineChunkFTFont] =
self.drawable_shape.set_text_alpha(alpha)

def set_text_offset_pos(self, offset: Tuple[int, int],
sub_chunk: Optional[TextLineChunkFTFont]):
sub_chunk: Optional[TextLineChunkFTFont] = None):
pass

def set_text_rotation(self, rotation: int,
sub_chunk: Optional[TextLineChunkFTFont]):
sub_chunk: Optional[TextLineChunkFTFont] = None):
pass

def set_text_scale(self, scale: int,
sub_chunk: Optional[TextLineChunkFTFont]):
sub_chunk: Optional[TextLineChunkFTFont] = None):
pass

def clear_text_surface(self, sub_chunk: Optional[TextLineChunkFTFont] = None):
Expand Down
1 change: 0 additions & 1 deletion pygame_gui/elements/ui_text_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,4 +1045,3 @@ def set_text(self, html_text: str):
self.html_text = html_text
self.appended_text = "" # clear appended text as it feels odd to set the text and still have appended text
self._reparse_and_rebuild()

9 changes: 9 additions & 0 deletions tests/data/themes/ui_drop_down_menu_bad_values.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@
"border_width": "0.6",
"shadow_width": "french"
}
},
"drop_down_menu.#drop_down_options_list":
{
"misc":
{
"shadow_width": "goose",
"border_width": "goose",
"list_item_height": "goose"
}
}
}
3 changes: 2 additions & 1 deletion tests/data/themes/ui_drop_down_menu_non_default.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"shape_corner_radius": "6",
"expand_direction": "up",
"border_width": "0",
"shadow_width": "3"
"shadow_width": "3",
"open_button_width": "25"

}
},
Expand Down
16 changes: 10 additions & 6 deletions tests/test_core/test_text/test_text_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ def test_update(self, _init_pygame, default_ui_manager: UIManager):

assert typing_effect.text_progress == 2

label = UILabel(pygame.Rect((10, 10), (200, 100)), 'Hello world',
label = UILabel(pygame.Rect((10, 10), (200, 100)), 'Hell',
default_ui_manager)
label_typing_effect = TypingAppearEffect(text_owner=label)

assert label_typing_effect.text_progress == 0

label_typing_effect.update(time_delta=0.06)
label_typing_effect.update(time_delta=0.06)
label_typing_effect.update(time_delta=0.06)
label_typing_effect.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)
label.update(time_delta=0.06)

assert label_typing_effect.text_progress == 2
assert label_typing_effect.text_progress == 0

def test_has_text_changed(self, _init_pygame, default_ui_manager: UIManager):
text_box = UITextBox('hello <font color=#FF0000>this is a</font> test',
Expand Down
81 changes: 81 additions & 0 deletions tests/test_elements/test_ui_drop_down_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ def test_creation(self, _init_pygame, default_ui_manager, _display_surface_retur
manager=default_ui_manager)
assert menu.image is not None

def test_focus(self, _init_pygame, default_ui_manager, _display_surface_return_none):
menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar'],
starting_option='eggs',
relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)

menu.focus()

assert menu.is_focused

menu.unfocus()

assert not menu.is_focused

# test expanded too
menu.current_state.should_transition = True
menu.update(0.01)

menu.focus()

assert menu.is_focused

menu.unfocus()

assert not menu.is_focused

def test_addition(self, _init_pygame, default_ui_manager,
_display_surface_return_none):
menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar'],
Expand Down Expand Up @@ -201,6 +227,26 @@ def test_rebuild_from_theme_data_bad_values(self, _init_pygame,
manager=manager)
assert menu.image is not None

@pytest.mark.filterwarnings("ignore:Invalid value")
@pytest.mark.filterwarnings("ignore:Colour hex code")
def test_rebuild_from_theme_data_bad_values_live(self, _init_pygame,
_display_surface_return_none):
manager = UIManager((800, 600))

menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar'],
starting_option='eggs',
relative_rect=pygame.Rect(100, 100, 200, 30),
manager=manager)

# load new theme
manager.ui_theme.load_theme(os.path.join("tests", "data", "themes", "ui_drop_down_menu_bad_values.json"))

# switch to expanded state
menu.current_state.should_transition = True
menu.update(0.01)

assert menu.image is not None

def test_set_position(self, _init_pygame, default_ui_manager,
_display_surface_return_none):
test_container = UIContainer(relative_rect=pygame.Rect(10, 10, 300, 300),
Expand Down Expand Up @@ -295,6 +341,41 @@ def test_set_dimensions(self, _init_pygame, default_ui_manager, _display_surface
# if we successfully clicked on the moved menu then this button should be True
assert menu.current_state.close_button.held is True

# now with non default theming values
manager = UIManager((800, 600), os.path.join("tests", "data",
"themes", "ui_drop_down_menu_non_default.json"))

non_default_menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar'],
starting_option='eggs',
relative_rect=pygame.Rect(100, 100, 200, 30),
manager=manager)
non_default_menu.set_dimensions((300, 50))

assert (non_default_menu.current_state.open_button.relative_rect.right ==
300 - (non_default_menu.border_width + non_default_menu.shadow_width))

assert (non_default_menu.current_state.open_button.relative_rect.bottom ==
50 - (non_default_menu.border_width + non_default_menu.shadow_width))

# try to click on the menu
manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': pygame.BUTTON_LEFT,
'pos': (390, 125)}))
# if we successfully clicked on the moved menu then this button should be True
assert non_default_menu.current_state.open_button.held is True

non_default_menu.current_state.should_transition = True
non_default_menu.update(0.01)

non_default_menu.set_dimensions((200, 30))

# try to click on the menu
manager.process_events(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': 1,
'pos': (290, 115)}))
# if we successfully clicked on the moved menu then this button should be True
assert non_default_menu.current_state.close_button.held is True

def test_on_fresh_drawable_shape_ready(self, _init_pygame, default_ui_manager):
menu = UIDropDownMenu(options_list=['eggs', 'flour', 'sugar'],
starting_option='eggs',
Expand Down
18 changes: 18 additions & 0 deletions tests/test_elements/test_ui_horizontal_scroll_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ def test_rebuild(self, _init_pygame, default_ui_manager,
scroll_bar.rebuild()
assert scroll_bar.image is not None

scroll_bar.enable_arrow_buttons = False

scroll_bar.rebuild()

assert scroll_bar.left_button is None and scroll_bar.right_button is None

def test_check_has_moved_recently(self, _init_pygame, default_ui_manager,
_display_surface_return_none):
scroll_bar = UIHorizontalScrollBar(relative_rect=pygame.Rect(100, 100, 150, 30),
Expand Down Expand Up @@ -84,6 +90,12 @@ def test_redraw_scroll_bar(self, _init_pygame, default_ui_manager,
scroll_bar.redraw_scrollbar()
assert scroll_bar.sliding_button is not None

scroll_bar.sliding_button.kill()
scroll_bar.sliding_button = None

scroll_bar.redraw_scrollbar()
assert scroll_bar.sliding_button is not None

def test_reset_scroll_position(self, _init_pygame, default_ui_manager,
_display_surface_return_none):
scroll_bar = UIHorizontalScrollBar(relative_rect=pygame.Rect(100, 100, 150, 30),
Expand Down Expand Up @@ -242,8 +254,14 @@ def test_disable(self, _init_pygame: None, default_ui_manager: UIManager,
scroll_bar = UIHorizontalScrollBar(relative_rect=pygame.Rect(0, 0, 200, 30),
visible_percentage=0.25, manager=default_ui_manager)

scroll_bar.sliding_button.hovered = True

assert scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'x': 1.0}))

scroll_bar.disable()

assert not scroll_bar.process_event(pygame.event.Event(pygame.MOUSEWHEEL, {'x': 1.0}))

# process a mouse button down event
scroll_bar.right_button.process_event(
pygame.event.Event(pygame.MOUSEBUTTONDOWN,
Expand Down
Loading

0 comments on commit 2c9c718

Please sign in to comment.