Skip to content

Commit

Permalink
Merge pull request #3663 from TEParsons/_textWrapTesting
Browse files Browse the repository at this point in the history
TEST: Add tests for uax14 textbox layout, modularise existing tests
  • Loading branch information
peircej committed Mar 17, 2021
2 parents ae519ab + f35377d commit 4d352be
Show file tree
Hide file tree
Showing 33 changed files with 63 additions and 52 deletions.
Binary file removed psychopy/tests/data/textbox_colors_WOB.png
Binary file not shown.
Binary file removed psychopy/tests/data/textbox_colors_exemplar1.png
Binary file not shown.
Binary file removed psychopy/tests/data/textbox_colors_exemplar2.png
Binary file not shown.
Binary file removed psychopy/tests/data/textbox_colors_exemplar3.png
Binary file not shown.
Binary file removed psychopy/tests/data/textbox_colors_tyke2.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file removed psychopy/tests/data/textbox_exemplar_1.png
Binary file not shown.
Binary file added psychopy/tests/data/textbox_uax14_colors_WOB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added psychopy/tests/data/textbox_uax14_colors_tyke3.png
Binary file added psychopy/tests/data/textbox_uax14_exemplar_1.png
Binary file added psychopy/tests/data/textbox_uax14_exemplar_2.png
Binary file added psychopy/tests/data/textbox_uax14_exemplar_3.png
Binary file added psychopy/tests/data/textbox_uax14_exemplar_4.png
Binary file added psychopy/tests/data/textbox_uax14_tyke_1.png
Binary file added psychopy/tests/data/textbox_uax14_tyke_2.png
115 changes: 63 additions & 52 deletions psychopy/tests/test_all_visual/test_textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,117 +11,119 @@
# cd psychopy/psychopy
# py.test -k textbox --cov-report term-missing --cov visual/textbox


@pytest.mark.textbox
class Test_textbox(object):
def setup_class(self):
self.win = Window([128,128], pos=[50,50], allowGUI=False, autoLog=False)
self.win = Window([128, 128], pos=[50, 50], allowGUI=False, autoLog=False)
self.textbox = TextBox2(self.win, "", "Noto Sans",
pos=(0, 0), size=(1, 1), units='height',
letterHeight=0.1, colorSpace="rgb")

def teardown_class(self):
self.win.close()

def test_glyph_rendering(self):
textbox = TextBox2(self.win, "", "Arial", pos=(0,0), size=(1,1), letterHeight=0.1, units='height')

# Add all Noto Sans fonts to cover widest possible base of handles characters
for font in ["Noto Sans", "Noto Sans HK", "Noto Sans JP", "Noto Sans KR", "Noto Sans SC", "Noto Sans TC", "Niramit", "Indie Flower"]:
textbox.fontMGR.addGoogleFont(font)
self.textbox.fontMGR.addGoogleFont(font)
# Some exemplar text to test basic TextBox rendering
exemplars = [
# An English pangram
{"text": "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question.",
"font": "Noto Sans",
"screenshot": "textbox_exemplar_1.png"},
"screenshot": "exemplar_1.png"},
# The same pangram in IPA
{"text": "ə saɪkəʊpaɪ zɛlət nəʊz ə smidge ɒv wx, bʌt ˈʤɑːvəskrɪpt ɪz ðə ˈkwɛsʧən",
"font": "Noto Sans",
"screenshot": "textbox_exemplar_2.png"},
"screenshot": "exemplar_2.png"},
# The same pangram in Hangul
{"text": "아 프시초피 제알롣 크노W스 아 s믿게 오f wx, 붇 자v앗c립t 잇 테 q왯디온",
"font": "Noto Sans KR",
"screenshot": "textbox_exemplar_3.png"},
"screenshot": "exemplar_3.png"},
# A noticeably non-standard font
{"text": "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question.",
"font": "Indie Flower",
"screenshot": "textbox_exemplar_4.png",
"screenshot": "exemplar_4.png",
}
]
# Some text which is likely to cause problems if something isn't working
tykes = [
# Text which doesn't render properly on Mac (Issue #3203)
{"text": "कोशिकायें",
"font": "Noto Sans",
"screenshot": "textbox_tyke_1.png"},
"screenshot": "tyke_1.png"},
# Thai text which old Text component couldn't handle due to Pyglet
{"text": "ขาว แดง เขียว เหลือง ชมพู ม่วง เทา",
"font": "Niramit",
"screenshot": "textbox_tyke_2.png"
"screenshot": "tyke_2.png"
}
]
# Test each case and compare against screenshot
for case in exemplars + tykes:
textbox.reset()
textbox.fontMGR.addGoogleFont(case['font'])
textbox.font = case['font']
textbox.text = case['text']
self.textbox.reset()
self.textbox.fontMGR.addGoogleFont(case['font'])
self.textbox.font = case['font']
self.textbox.text = case['text']
self.win.flip()
textbox.draw()
self.textbox.draw()
if case['screenshot']:
# Uncomment to save current configuration as desired
#self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / case['screenshot'])
utils.compareScreenshot(Path(utils.TESTS_DATA_PATH) / case['screenshot'], self.win, crit=20)
filename = "textbox_{}_{}".format(self.textbox._lineBreaking, case['screenshot'])
#self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / filename)
utils.compareScreenshot(Path(utils.TESTS_DATA_PATH) / filename, self.win, crit=20)

def test_colors(self):
textbox = TextBox2(self.win, "",
"Consolas", pos=(0, 0), size=(1, 1), letterHeight=0.1, units='height', colorSpace="rgb")
textbox.fontMGR.addGoogleFont("Noto Sans")
textbox.font = "Noto Sans"
textbox.text = "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question."
self.textbox.text = "A PsychoPy zealot knows a smidge of wx, but JavaScript is the question."
# Some exemplar text to test basic colors
exemplars = [
# White on black in rgb
{"color": (1, 1, 1), "fillColor": (-1,-1,-1), "borderColor": (-1,-1,-1), "space": "rgb",
"screenshot": "textbox_colors_WOB.png"},
"screenshot": "colors_WOB.png"},
# White on black in named
{"color": "white", "fillColor": "black", "borderColor": "black", "space": "rgb",
"screenshot": "textbox_colors_WOB.png"},
"screenshot": "colors_WOB.png"},
# White on black in hex
{"color": "#ffffff", "fillColor": "#000000", "borderColor": "#000000", "space": "hex",
"screenshot": "textbox_colors_WOB.png"},
"screenshot": "colors_WOB.png"},
{"color": "red", "fillColor": "yellow", "borderColor": "blue", "space": "rgb",
"screenshot": "textbox_colors_exemplar1.png"},
"screenshot": "colors_exemplar1.png"},
{"color": "yellow", "fillColor": "blue", "borderColor": "red", "space": "rgb",
"screenshot": "textbox_colors_exemplar2.png"},
"screenshot": "colors_exemplar2.png"},
{"color": "blue", "fillColor": "red", "borderColor": "yellow", "space": "rgb",
"screenshot": "textbox_colors_exemplar3.png"},
"screenshot": "colors_exemplar3.png"},
]
# Some colors which are likely to cause problems if something isn't working
tykes = [
# Text only
{"color": "white", "fillColor": None, "borderColor": None, "space": "rgb",
"screenshot": "textbox_colors_tyke1.png"},
"screenshot": "colors_tyke1.png"},
# Fill only
{"color": None, "fillColor": "white", "borderColor": None, "space": "rgb",
"screenshot": "textbox_colors_tyke2.png"},
"screenshot": "colors_tyke2.png"},
# Border only
{"color": None, "fillColor": None, "borderColor": "white", "space": "rgb",
"screenshot": "textbox_colors_tyke3.png"},
"screenshot": "colors_tyke3.png"},
]
# Test each case and compare against screenshot
for case in exemplars + tykes:
# Raise error if case spec does not contain all necessary keys
if not all(key in case for key in ["color", "fillColor", "borderColor", "space", "screenshot"]):
raise KeyError(f"Case spec for test_colors in class {self.__class__.__name__} ({__file__}) invalid, test cannot be run.")
# Apply params from case spec
textbox.colorSpace = case['space']
textbox.color = case['color']
textbox.fillColor = case['fillColor']
textbox.borderColor = case['borderColor']
self.win.flip()
textbox.draw()
if case['screenshot']:
# Uncomment to save current configuration as desired
# self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / case['screenshot'])
utils.compareScreenshot(Path(utils.TESTS_DATA_PATH) / case['screenshot'], self.win, crit=20)

self.textbox.colorSpace = case['space']
self.textbox.color = case['color']
self.textbox.fillColor = case['fillColor']
self.textbox.borderColor = case['borderColor']
for lineBreaking in ('default', 'uax14'):
self.win.flip()
self.textbox.draw()
if case['screenshot']:
# Uncomment to save current configuration as desired
filename = "textbox_{}_{}".format(self.textbox._lineBreaking, case['screenshot'])
# self.win.getMovieFrame(buffer='back').save(Path(utils.TESTS_DATA_PATH) / filename)
utils.compareScreenshot(Path(utils.TESTS_DATA_PATH) / filename, self.win, crit=20)

def test_basic(self):
pass
Expand All @@ -130,14 +132,23 @@ def test_something(self):
# to-do: test visual display, char position, etc
pass

def test_font_manager(self):
# Create a font manager
mgr = FontManager()
# Check that it finds fonts which should be pre-packaged with PsychoPy in the resources folder
assert bool(mgr.getFontNamesSimilar("Open Sans"))
# Check that it doesn't find fonts which aren't installed as default
assert not bool(mgr.getFontNamesSimilar("Dancing Script"))
# Check that it can install fonts from Google
mgr.addGoogleFont("Hanalei")
# Check that these fonts are found once installed
assert bool(mgr.getFontNamesSimilar("Hanalei"))

def test_font_manager():
# Create a font manager
mgr = FontManager()
# Check that it finds fonts which should be pre-packaged with PsychoPy in the resources folder
assert bool(mgr.getFontNamesSimilar("Open Sans"))
# Check that it doesn't find fonts which aren't installed as default
assert not bool(mgr.getFontNamesSimilar("Dancing Script"))
# Check that it can install fonts from Google
mgr.addGoogleFont("Hanalei")
# Check that these fonts are found once installed
assert bool(mgr.getFontNamesSimilar("Hanalei"))


@pytest.mark.uax14
class Test_uax14_textbox(Test_textbox):
"""Runs the same tests as for Test_textbox, but with the textbox set to uax14 line breaking"""
def setup_class(self):
Test_textbox.setup_class(self)
self.textbox._lineBreaking = 'uax14'

0 comments on commit 4d352be

Please sign in to comment.