| 1 | import pytest |
| 2 | |
| 3 | from tests.base.interactive import InteractiveTestCase |
| 4 | |
| 5 | from pyglet import app |
| 6 | from pyglet import gl |
| 7 | from pyglet import graphics |
| 8 | from pyglet import text |
| 9 | from pyglet.text import caret |
| 10 | from pyglet.text import layout |
| 11 | from pyglet import window |
| 12 | from pyglet.window import key, mouse |
| 13 | |
| 14 | nonewline_nowrap = """{font_size 24}Multiline=False\n |
| 15 | {font_size 12}This paragraph contains a lots of newlines, however\n |
| 16 | the parameter multiline=False makes pyglet {font_size 16}ignore{font_size 12} it.\n |
| 17 | For example this line should not be in a new line.\n |
| 18 | And because the parameter multiline=False (ignoring wrap_lines) the {font_size 16}long lines are not broken{font_size 12}, as you can see in this line.""" |
| 19 | |
| 20 | newline_nowrap = """{font_size 24}Multiline=True -- Wrap_line=False\n |
| 21 | {font_size 12}This paragraph contains a lots of newlines, which\n |
| 22 | pyglet {font_size 16}accepts{font_size 12} due to the parameter multiline=True.\n |
| 23 | For example this line should be in a new line.\n |
| 24 | And, because the parameter wrap_lines=False, the {font_size 16}long lines are not broken{font_size 12}, as you can see in this line.""" |
| 25 | |
| 26 | newline_wrap = """{font_size 24}Multiline=True -- Wrap_line=True\n |
| 27 | {font_size 12}This paragraph contains a lots of newlines, which\n |
| 28 | pyglet {font_size 16}accepts{font_size 12} due to the parameter multiline=True.\n |
| 29 | For example this line should be in a new line.\n |
| 30 | And, because the parameter wrap_lines=True the {font_size 16}long lines are broken{font_size 12}, as you can see in this line.""" |
| 31 | |
| 32 | |
| 33 | class BaseTestWindow(window.Window): |
| 34 | def __init__(self, multiline, wrap_lines, msg, *args, **kwargs): |
| 35 | super().__init__(*args, width=640, height=480, **kwargs) |
| 36 | |
| 37 | self.batch = graphics.Batch() |
| 38 | self.document = text.decode_attributed(msg) |
| 39 | self.margin = 2 |
| 40 | self.layout = layout.IncrementalTextLayout(self.document, |
| 41 | width=(self.width - self.margin * 2), |
| 42 | height=self.height - self.margin * 2, |
| 43 | multiline=multiline, |
| 44 | wrap_lines=wrap_lines, |
| 45 | batch=self.batch) |
| 46 | self.caret = caret.Caret(self.layout) |
| 47 | self.push_handlers(self.caret) |
| 48 | |
| 49 | self.wrap_lines = wrap_lines |
| 50 | |
| 51 | self.set_mouse_cursor(self.get_system_mouse_cursor('text')) |
| 52 | |
| 53 | def on_resize(self, width, height): |
| 54 | super().on_resize(width, height) |
| 55 | self.layout.begin_update() |
| 56 | self.layout.x = self.margin |
| 57 | self.layout.y = self.margin |
| 58 | if self.wrap_lines: |
| 59 | self.layout.width = width - self.margin * 2 |
| 60 | self.layout.height = height - self.margin * 2 |
| 61 | self.layout.end_update() |
| 62 | |
| 63 | def on_mouse_scroll(self, x, y, scroll_x, scroll_y): |
| 64 | self.layout.view_x -= scroll_x |
| 65 | self.layout.view_y += scroll_y * 16 |
| 66 | |
| 67 | def on_draw(self): |
| 68 | gl.glClearColor(1, 1, 1, 1) |
| 69 | self.clear() |
| 70 | self.batch.draw() |
| 71 | |
| 72 | def on_key_press(self, symbol, modifiers): |
| 73 | super().on_key_press(symbol, modifiers) |
| 74 | if symbol == key.TAB: |
| 75 | self.caret.on_text('\t') |
| 76 | |
| 77 | |
| 78 | @pytest.mark.requires_user_action |
| 79 | class MultilineWrapTestCase(InteractiveTestCase): |
| 80 | """Test that a paragraph is broken or not according the settings in an |
| 81 | incremental layout. |
| 82 | |
| 83 | Three windows will be open (one per test) showing: |
| 84 | - A paragraph in a single line, ignoring newlines and not wrapping the line. |
| 85 | - A paragraph in multiple lines, but the long lines will not be wrapped. |
| 86 | - Last, a paragraph in multiple lines with wrapped lines. |
| 87 | |
| 88 | You can edit the text in each window, and you must press ESC to close the window |
| 89 | and continue with the next window until finish the test. |
| 90 | |
| 91 | Press ESC to exit the test. |
| 92 | """ |
| 93 | |
| 94 | def testMultilineFalse(self): |
| 95 | self.window = BaseTestWindow( |
| 96 | multiline=False, wrap_lines=False, |
| 97 | msg=nonewline_nowrap, resizable=True, visible=False) |
| 98 | self.window.set_visible() |
| 99 | app.run() |
| 100 | self.user_verify('Pass test?', take_screenshot=False) |
| 101 | |
| 102 | def testMultilineTrueNoLimited(self): |
| 103 | self.window = BaseTestWindow( |
| 104 | multiline=True, wrap_lines=False, |
| 105 | msg=newline_nowrap, resizable=True, visible=False) |
| 106 | self.window.set_visible() |
| 107 | app.run() |
| 108 | self.user_verify('Pass test?', take_screenshot=False) |
| 109 | |
| 110 | def testMultilineTrueLimited(self): |
| 111 | self.window = BaseTestWindow( |
| 112 | multiline=True, wrap_lines=True, |
| 113 | msg=newline_wrap, resizable=True, visible=False) |
| 114 | self.window.set_visible() |
| 115 | app.run() |
| 116 | self.user_verify('Pass test?', take_screenshot=False) |
| 117 |