Skip to content

Commit

Permalink
Merge pull request #289 from MyreMylar/coverage-again
Browse files Browse the repository at this point in the history
Improve UITextEntryLine test coverage
  • Loading branch information
MyreMylar authored Aug 3, 2022
2 parents 8fda7af + b8b2040 commit ce6f31d
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 26 deletions.
44 changes: 23 additions & 21 deletions pygame_gui/elements/ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def update(self, time_delta: float):

if not self.alive():
return
scaled_mouse_pos = self.ui_manager.calculate_scaled_mouse_position(pygame.mouse.get_pos())
scaled_mouse_pos = self.ui_manager.get_mouse_position()
if self.hover_point(scaled_mouse_pos[0], scaled_mouse_pos[1]):
self.ui_manager.set_text_input_hovered(True)
else:
Expand Down Expand Up @@ -735,31 +735,33 @@ def _calculate_double_click_word_selection(self):
if self.edit_position != self.select_range[0]:
return False
index = min(self.edit_position, len(self.text) - 1)
if index > 0:
if index >= 0:
char = self.text[index]
# Check we clicked in the same place on a our second click.
pattern = re.compile(r"[\w']+")
while not pattern.match(char):
index -= 1
if index > 0:
char = self.text[index]
else:
break
while pattern.match(char):
index -= 1
if index > 0:
char = self.text[index]
else:
break
if index > 0:
pattern = re.compile(r"[\w']+")
while not pattern.match(char):
index -= 1
if index > 0:
char = self.text[index]
else:
break
while pattern.match(char):
index -= 1
if index > 0:
char = self.text[index]
else:
break
start_select_index = index + 1 if index > 0 else index
index += 1
char = self.text[index]
while index < len(self.text) and pattern.match(char):
index += 1
if index < len(self.text):
char = self.text[index]
if index < len(self.text):
char = self.text[index]
while index < len(self.text) and pattern.match(char):
index += 1
if index < len(self.text):
char = self.text[index]
end_select_index = index

print([start_select_index, end_select_index])
self.select_range = [start_select_index, end_select_index]
self.edit_position = end_select_index
self.cursor_has_moved_recently = True
Expand Down
2 changes: 1 addition & 1 deletion pygame_gui/elements/ui_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def get_relative_mouse_pos(self):
:return: tuple of relative mouse co-ords or None
"""
abs_mouse_pos = pygame.mouse.get_pos()
abs_mouse_pos = self.ui_manager.get_mouse_position()
rel_mouse_pos = None
inside_window_rect = self.get_container().get_rect()
if inside_window_rect.contains(pygame.Rect(abs_mouse_pos, (1, 1))):
Expand Down
Binary file added tests/data/NotoSansSymbols-Regular.ttf
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/data/themes/ui_text_entry_line_symbol_font.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"text_entry_line":
{
"colours":
{
"dark_bg": "#25f92e,#FFFFFF,90",
"selected_bg": "#25f92e,#70A040,90",
"normal_text": "#cfcbd8,#70A040,90",
"selected_text": "#F0FFFF,#70A040,90",
"normal_border": "#AA0AAA"
},
"font":
{
"name": "noto_symbols_regular",
"size": "14",
"bold": "0",
"italic": "0",
"regular_path": "tests/data/NotoSansSymbols-Regular.ttf"
},
"misc":
{
"shape": "rounded_rectangle",
"shape_corner_radius": "10",
"border_width": "4",
"shadow_width": "4",
"padding": "6,3"
}
}
}
133 changes: 129 additions & 4 deletions tests/test_elements/test_ui_text_entry_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,74 @@ def test_set_text_forbidden_characters(self, _init_pygame, default_ui_manager,):
" characters on text entry element"):
text_entry.set_text("1,2,3,4,5")

def test_hidden_text(self, _init_pygame, default_ui_manager):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)

text_entry.set_text_hidden(False)
text_entry.set_text("Some basic text")
text_entry.set_text_hidden(True)
text_entry.set_text("Some other basic text")
text_entry.set_text_hidden(False)
text_entry.set_text_hidden(True)

assert text_entry.image is not None

text_entry.focus()

text_entry.process_event(pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_d, 'mod': 0,
'unicode': 'd'}))
text_entry.process_event(pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_a, 'mod': 0,
'unicode': 'a'}))
text_entry.process_event(pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_n, 'mod': 0,
'unicode': 'n'}))

text_entry.set_text_hidden(False)

text_entry.set_text('dan')
text_entry.focus()
text_entry.select_range = [1, 3]

text_entry.process_event(pygame.event.Event(pygame.KEYDOWN, {'key': pygame.K_c,
'mod': pygame.KMOD_CTRL,
'unicode': 'c'}))
text_entry.set_text_hidden(True)
text_entry.select_range = [0, 0]
text_entry.edit_position = 3
text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
{'key': pygame.K_v,
'mod': pygame.KMOD_CTRL,
'unicode': 'v'}))

text_entry.select_range = [1, 3]
text_entry.edit_position = 1
text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
{'key': pygame.K_v,
'mod': pygame.KMOD_CTRL,
'unicode': 'v'}))

assert text_entry.image is not None

manager = UIManager((800, 600), os.path.join("tests", "data",
"themes",
"ui_text_entry_line_non_default.json"))
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=manager)
text_entry.set_text('dan')
text_entry.set_text_hidden(True)

assert text_entry.image is not None

manager = UIManager((800, 600), os.path.join("tests", "data",
"themes",
"ui_text_entry_line_symbol_font.json"))

text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=manager)
text_entry.set_text('dan')
with pytest.raises(ValueError, match='Selected font for UITextEntryLine'):
text_entry.set_text_hidden(True)

def test_rebuild_select_area_1(self, _init_pygame, default_ui_manager,
_display_surface_return_none):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
Expand Down Expand Up @@ -138,7 +206,7 @@ def test_process_event_text_entered_success(self, _init_pygame: None,
manager=default_ui_manager)

text_entry.focus()
# process a mouse button down event

processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
{'key': pygame.K_d,
'mod': 0,
Expand Down Expand Up @@ -243,7 +311,7 @@ def test_process_event_text_ctrl_c(self, _init_pygame: None,
'mod': pygame.KMOD_CTRL,
'unicode': 'c'}))
text_entry.cursor_on = True
#text_entry.redraw_cursor()
# text_entry.redraw_cursor()

assert processed_key_event and clipboard_paste() == 'dan'

Expand Down Expand Up @@ -457,6 +525,16 @@ def test_process_event_mouse_button_double_click(self, _init_pygame: None,

assert (processed_down_event and processed_up_event and text_entry.select_range == [7, 14])

text_entry.set_text('')
processed_down_event = text_entry.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': 1,
'pos': (90, 15)}))
processed_up_event = text_entry.process_event(pygame.event.Event(pygame.MOUSEBUTTONDOWN,
{'button': 1,
'pos': (90, 15)}))

assert (processed_down_event and processed_up_event and text_entry.select_range == [0, 0])

def test_process_event_mouse_button_double_click_in_empty_space(
self, _init_pygame: None,
default_ui_manager: UIManager,
Expand Down Expand Up @@ -648,7 +726,7 @@ def test_process_event_backspace_select_range(self, _init_pygame: None,

assert processed_key_event

def test_set_allowed_characters_numbers(self, _init_pygame, default_ui_manager):
def test_set_allowed_characters_predefined(self, _init_pygame, default_ui_manager):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)

Expand All @@ -658,6 +736,23 @@ def test_set_allowed_characters_numbers(self, _init_pygame, default_ui_manager):
text_entry.set_text("one two three")
assert text_entry.get_text() == ""

text_entry.set_allowed_characters('letters')
with pytest.warns(UserWarning, match="Tried to set text string with invalid"
" characters on text entry element"):
text_entry.set_text("123")
assert text_entry.get_text() == ""

text_entry.set_allowed_characters('alpha_numeric')
with pytest.warns(UserWarning, match="Tried to set text string with invalid"
" characters on text entry element"):
text_entry.set_text("*&%")
assert text_entry.get_text() == ""

default_ui_manager.set_locale('jp')
text_entry.set_allowed_characters('numbers')
text_entry.set_allowed_characters('letters')
text_entry.set_allowed_characters('alpha_numeric')

def test_set_allowed_characters_anything(self, _init_pygame, default_ui_manager):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)
Expand All @@ -676,7 +771,7 @@ def test_set_allowed_characters_invalid_id(self, _init_pygame, default_ui_manage
"type string, but no match"):
text_entry.set_allowed_characters('dan')

def test_set_forbidden_characters_file_path(self, _init_pygame, default_ui_manager):
def test_set_forbidden_characters_predefined(self, _init_pygame, default_ui_manager):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)

Expand All @@ -686,6 +781,10 @@ def test_set_forbidden_characters_file_path(self, _init_pygame, default_ui_manag
text_entry.set_text(">:<")
assert text_entry.get_text() == ""

default_ui_manager.set_locale('jp')
text_entry.set_forbidden_characters('forbidden_file_path')
text_entry.set_forbidden_characters('numbers')

def test_set_forbidden_characters_anything(self, _init_pygame, default_ui_manager):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)
Expand Down Expand Up @@ -739,6 +838,19 @@ def test_update(self, _init_pygame):

text_entry.update(0.01)

assert text_entry.alive()
assert not manager.text_input_hovered

manager.mouse_position = (150, 115)
text_entry.update(0.01)
assert manager.text_input_hovered

text_entry.kill()

text_entry.update(0.01)

assert not text_entry.alive()

def test_update_after_click(self, _init_pygame, _display_surface_return_none: None, default_ui_manager):
manager = UIManager((800, 600), os.path.join("tests", "data",
"themes",
Expand Down Expand Up @@ -883,6 +995,8 @@ def test_enable(self, _init_pygame: None, default_ui_manager: UIManager,
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)

text_entry.disable()

text_entry.focus()
text_entry.enable()

Expand Down Expand Up @@ -944,6 +1058,17 @@ def test_show_hide_rendering(self, _init_pygame, default_ui_manager, _display_su
manager.draw_ui(surface)
assert compare_surfaces(empty_surface, surface)

def test_change_locale(self, _init_pygame, default_ui_manager, _display_surface_return_none):
text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
manager=default_ui_manager)
text_entry.set_text("Some basic text")

default_ui_manager.set_locale('fr')

default_ui_manager.set_locale('ja')

assert text_entry.get_text() == "Some basic text"


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

0 comments on commit ce6f31d

Please sign in to comment.