| 1 |
from typing import List |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
import pyglet.text.layout |
| 6 |
from tests.base.interactive import InteractiveTestCase |
| 7 |
|
| 8 |
import pyglet |
| 9 |
from pyglet.text import caret, document |
| 10 |
from pyglet.text.layout import IncrementalTextLayout |
| 11 |
|
| 12 |
|
| 13 |
doctext = """ELEMENT.py test document. |
| 14 |
|
| 15 |
PLACE CURSOR AT THE END OF THE ABOVE LINE, AND DELETE ALL ITS TEXT, |
| 16 |
BY PRESSING THE DELETE KEY REPEATEDLY. |
| 17 |
|
| 18 |
IF THIS WORKS OK, AND THE ELEMENT (GRAY RECTANGLE) WITHIN THIS LINE |
| 19 |
[element here] |
| 20 |
REMAINS VISIBLE BETWEEN THE SAME CHARACTERS, WITH NO ASSERTIONS PRINTED TO |
| 21 |
THE CONSOLE, THE TEST PASSES. |
| 22 |
|
| 23 |
(In code with bug 538, the element sometimes moves within the text, and |
| 24 |
eventually there is an assertion failure. Note that there is another bug, |
| 25 |
unrelated to this one, which sometimes causes the first press of the delete |
| 26 |
key to be ignored.) |
| 27 |
|
| 28 |
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce venenatis |
| 29 |
pharetra libero. Phasellus lacinia nisi feugiat felis. Sed id magna in nisl |
| 30 |
cursus consectetuer. Aliquam aliquam lectus eu magna. Praesent sit amet ipsum |
| 31 |
vitae nisl mattis commodo. Aenean pulvinar facilisis lectus. Phasellus sodales |
| 32 |
risus sit amet lectus. Suspendisse in turpis. Vestibulum ac mi accumsan eros |
| 33 |
commodo tincidunt. Nullam velit. In pulvinar, dui sit amet ullamcorper dictum, |
| 34 |
dui risus ultricies nisl, a dignissim sapien enim sit amet tortor. |
| 35 |
Pellentesque fringilla, massa sit amet bibendum blandit, pede leo commodo mi, |
| 36 |
eleifend feugiat neque tortor dapibus mauris. Morbi nunc arcu, tincidunt vel, |
| 37 |
blandit non, iaculis vel, libero. Vestibulum sed metus vel velit scelerisque |
| 38 |
varius. Vivamus a tellus. Proin nec orci vel elit molestie venenatis. Aenean |
| 39 |
fringilla, lorem vel fringilla bibendum, nibh mi varius mi, eget semper ipsum |
| 40 |
ligula ut urna. Nullam tempor convallis augue. Sed at dui. |
| 41 |
""" |
| 42 |
|
| 43 |
element_index = doctext.index('[element here]') |
| 44 |
doctext = doctext.replace('[element here]', '') |
| 45 |
|
| 46 |
class TestElement(document.InlineElement): |
| 47 |
|
| 48 |
def __init__(self, ascent, descent, advance): |
| 49 |
self.vertex_list = None |
| 50 |
super().__init__(ascent, descent, advance) |
| 51 |
|
| 52 |
def place(self, layout, x, y, z, line_x, line_y, rotation, visible, anchor_x, anchor_y): |
| 53 |
group = layout.foreground_decoration_group |
| 54 |
program = pyglet.text.layout.get_default_decoration_shader() |
| 55 |
|
| 56 |
x1 = line_x |
| 57 |
y1 = line_y + self.descent |
| 58 |
x2 = line_x + self.advance |
| 59 |
y2 = line_y + self.ascent - self.descent |
| 60 |
|
| 61 |
self.vertex_list = program.vertex_list_indexed(4, pyglet.gl.GL_TRIANGLES, [0, 1, 2, 0, 2, 3], |
| 62 |
layout.batch, group, |
| 63 |
position=('f', (x1, y1, z, x2, y1, z, x2, y2, z, x1, y2, z)), |
| 64 |
colors=('Bn', (200, 200, 200, 255) * 4), |
| 65 |
translation=('f', (x, y, z) * 4), |
| 66 |
visible=('f', (visible,) * 4), |
| 67 |
rotation=('f', (rotation,) * 4), |
| 68 |
anchor=('f', (anchor_x, anchor_y) * 4) |
| 69 |
) |
| 70 |
def update_translation(self, x: float, y: float, z: float): |
| 71 |
self.vertex_list.translation[:] = (x, y, z) * self.vertex_list.count |
| 72 |
|
| 73 |
def update_color(self, color: List[int]): |
| 74 |
pass |
| 75 |
|
| 76 |
def update_view_translation(self, translate_x: float, translate_y: float): |
| 77 |
self.vertex_list.view_translation[:] = (-translate_x, -translate_y, 0) * self.vertex_list.count |
| 78 |
|
| 79 |
def update_rotation(self, rotation: float): |
| 80 |
pass |
| 81 |
|
| 82 |
def update_visibility(self, visible: bool): |
| 83 |
pass |
| 84 |
|
| 85 |
def update_anchor(self, anchor_x: float, anchor_y: float): |
| 86 |
pass |
| 87 |
|
| 88 |
def remove(self, layout): |
| 89 |
self.vertex_list.delete() |
| 90 |
del self.vertex_list |
| 91 |
|
| 92 |
class TestWindow(pyglet.window.Window): |
| 93 |
def __init__(self, *args, **kwargs): |
| 94 |
super().__init__(*args, **kwargs) |
| 95 |
|
| 96 |
self.batch = pyglet.graphics.Batch() |
| 97 |
self.document = pyglet.text.decode_attributed(doctext) |
| 98 |
for i in [element_index]: |
| 99 |
self.document.insert_element(i, TestElement(60, -10, 70)) |
| 100 |
self.margin = 2 |
| 101 |
self.layout = IncrementalTextLayout( |
| 102 |
self.document, |
| 103 |
width=self.width - self.margin * 2, |
| 104 |
height=self.height - self.margin * 2, |
| 105 |
multiline=True, |
| 106 |
batch=self.batch) |
| 107 |
self.caret = caret.Caret(self.layout) |
| 108 |
self.push_handlers(self.caret) |
| 109 |
|
| 110 |
self.set_mouse_cursor(self.get_system_mouse_cursor('text')) |
| 111 |
|
| 112 |
def on_draw(self): |
| 113 |
pyglet.gl.glClearColor(1, 1, 1, 1) |
| 114 |
self.clear() |
| 115 |
self.batch.draw() |
| 116 |
|
| 117 |
def on_key_press(self, symbol, modifiers): |
| 118 |
super().on_key_press(symbol, modifiers) |
| 119 |
if symbol == pyglet.window.key.TAB: |
| 120 |
self.caret.on_text('\t') |
| 121 |
|
| 122 |
self.document.set_style(0, len(self.document.text), dict(bold = None)) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.requires_user_action |
| 126 |
class InlineElementStyleChangeTestCase(InteractiveTestCase): |
| 127 |
"""Test that inline elements can have their style changed, even after text |
| 128 |
has been deleted before them. [This triggers bug 538 if it has not yet been fixed.] |
| 129 |
|
| 130 |
To run the test, delete the first line, one character at a time, |
| 131 |
verifying that the element remains visible and no tracebacks are |
| 132 |
printed to the console. |
| 133 |
|
| 134 |
Press ESC to end the test. |
| 135 |
""" |
| 136 |
|
| 137 |
def test_inline_elements_style_change(self): |
| 138 |
self.window = TestWindow(visible=False) |
| 139 |
self.window.set_visible() |
| 140 |
pyglet.app.run() |
| 141 |
self.user_verify('Pass test?', take_screenshot=False) |