| 1 |
import pytest |
| 2 |
|
| 3 |
import pyglet |
| 4 |
from tests.base.interactive import InteractiveTestCase |
| 5 |
|
| 6 |
from pyglet import app |
| 7 |
from pyglet import gl |
| 8 |
from pyglet import graphics |
| 9 |
from pyglet import text |
| 10 |
from pyglet.text import caret |
| 11 |
from pyglet.text import layout |
| 12 |
from pyglet import window |
| 13 |
from pyglet.window import key, mouse |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class TestWindow(window.Window): |
| 18 |
def __init__(self, layout_text, anchor_x, anchor_y, formatted = False, *args, **kwargs): |
| 19 |
super().__init__(*args, **kwargs) |
| 20 |
self.layout_text = layout_text |
| 21 |
self.formatted = formatted |
| 22 |
|
| 23 |
self.batch = graphics.Batch() |
| 24 |
self.line = pyglet.shapes.Line(x=self.width // 2, y=0, |
| 25 |
x2=self.width // 2, y2=self.height, color=(255, 0, 0, 255), batch=self.batch) |
| 26 |
self.line2 = pyglet.shapes.Line(x=0, y=self.height // 2, |
| 27 |
x2=self.width, y2=self.height // 2, color=(255, 0, 0, 255), batch=self.batch) |
| 28 |
if formatted: |
| 29 |
self.document = pyglet.text.document.FormattedDocument(layout_text) |
| 30 |
self._set_doc_format() |
| 31 |
else: |
| 32 |
self.document = text.decode_text(layout_text) |
| 33 |
self.margin = 2 |
| 34 |
self.anchor_x = anchor_x |
| 35 |
self.anchor_y = anchor_y |
| 36 |
self.layout = layout.TextLayout(self.document, |
| 37 |
x=0, |
| 38 |
anchor_x=anchor_x, |
| 39 |
anchor_y=anchor_y, |
| 40 |
width=300, |
| 41 |
height=500, |
| 42 |
multiline=True, |
| 43 |
batch=self.batch) |
| 44 |
|
| 45 |
def _set_doc_format(self): |
| 46 |
self.document.set_style(0, 20, {'color': (255, 0, 0, 255), 'font_size': 15}) |
| 47 |
self.document.set_style(0, 3, {'italic': True}) |
| 48 |
|
| 49 |
def on_key_press(self, symbol, modifiers): |
| 50 |
if symbol == key.SPACE: |
| 51 |
self.layout.anchor_x = self.anchor_x |
| 52 |
self.layout.anchor_y = self.anchor_y |
| 53 |
self.document.text = self.layout_text + " (Spacebar Pressed)" |
| 54 |
if self.formatted: |
| 55 |
self._set_doc_format() |
| 56 |
|
| 57 |
def on_resize(self, width, height): |
| 58 |
super().on_resize(width, height) |
| 59 |
self.layout.begin_update() |
| 60 |
self.layout.x = (self.width // 2) |
| 61 |
|
| 62 |
self.layout.end_update() |
| 63 |
|
| 64 |
self.line.x = self.width // 2 |
| 65 |
self.line.x2 = self.width // 2 |
| 66 |
self.line.y2 = self.height |
| 67 |
|
| 68 |
self.line2.y = self.height // 2 |
| 69 |
self.line2.x2 = self.width |
| 70 |
self.line2.y2 = self.height // 2 |
| 71 |
|
| 72 |
def on_draw(self): |
| 73 |
gl.glClearColor(1, 1, 1, 1) |
| 74 |
self.clear() |
| 75 |
self.batch.draw() |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.requires_user_action |
| 79 |
class AnchorTestCase(InteractiveTestCase): |
| 80 |
def test_content_anchor_left(self): |
| 81 |
"""test_content_anchor_left = 'left' property of TextLayout. |
| 82 |
|
| 83 |
Ensure that the text is anchored the way the text describes. |
| 84 |
|
| 85 |
Press ESC to exit the test. |
| 86 |
""" |
| 87 |
desc = """This should be aligned against the left side of the Y axis red line. (Top right quadrant) |
| 88 |
|
| 89 |
Press Spacebar to verify the position remains the same.""" |
| 90 |
self.window = TestWindow(desc, anchor_x="left", anchor_y="bottom", resizable=True, visible=False) |
| 91 |
self.window.set_visible() |
| 92 |
app.run() |
| 93 |
self.user_verify('Test passed?', take_screenshot=False) |
| 94 |
|
| 95 |
def test_content_anchor_center(self): |
| 96 |
"""test_content_anchor_center = 'center' property of TextLayout. |
| 97 |
|
| 98 |
Ensure that the text is anchored the way the text describes. |
| 99 |
|
| 100 |
Press ESC to exit the test. |
| 101 |
""" |
| 102 |
desc = """This should be aligned on the Y axis red line. |
| 103 |
|
| 104 |
Press Spacebar to verify the position remains the same.""" |
| 105 |
|
| 106 |
self.window = TestWindow(desc, anchor_x="center", anchor_y="bottom", resizable=True, visible=False) |
| 107 |
self.window.set_visible() |
| 108 |
app.run() |
| 109 |
self.user_verify('Test passed?', take_screenshot=False) |
| 110 |
|
| 111 |
def test_content_anchor_right(self): |
| 112 |
"""test_content_anchor_right = 'right' property of TextLayout. |
| 113 |
|
| 114 |
Ensure that the text is anchored the way the text describes. |
| 115 |
|
| 116 |
Press ESC to exit the test. |
| 117 |
""" |
| 118 |
desc = """This should be aligned against the right side of the Y axis red line. (Top left quadrant) |
| 119 |
|
| 120 |
Press Spacebar to verify the position remains the same.""" |
| 121 |
|
| 122 |
self.window = TestWindow(desc, anchor_x="right", anchor_y="bottom", resizable=True, visible=False) |
| 123 |
self.window.set_visible() |
| 124 |
app.run() |
| 125 |
self.user_verify('Test passed?', take_screenshot=False) |
| 126 |
|
| 127 |
def test_content_anchor_left_formatted(self): |
| 128 |
"""test_content_anchor_left = 'left' property of TextLayout. |
| 129 |
|
| 130 |
Ensure that the text is anchored the way the text describes. |
| 131 |
|
| 132 |
Press ESC to exit the test. |
| 133 |
""" |
| 134 |
desc = """This should be aligned against the left side of the Y axis red line. (Top right quadrant) |
| 135 |
|
| 136 |
Press Spacebar to verify the position remains the same.""" |
| 137 |
self.window = TestWindow(desc, anchor_x="left", anchor_y="bottom", formatted=True,resizable=True, visible=False) |
| 138 |
self.window.set_visible() |
| 139 |
app.run() |
| 140 |
self.user_verify('Test passed?', take_screenshot=False) |
| 141 |
|
| 142 |
def test_content_anchor_center_formatted(self): |
| 143 |
"""test_content_anchor_center = 'center' property of TextLayout. |
| 144 |
|
| 145 |
Ensure that the text is anchored the way the text describes. |
| 146 |
|
| 147 |
Press ESC to exit the test. |
| 148 |
""" |
| 149 |
desc = """This should be aligned on the Y axis red line. |
| 150 |
|
| 151 |
Press Spacebar to verify the position remains the same.""" |
| 152 |
|
| 153 |
self.window = TestWindow(desc, anchor_x="center", anchor_y="bottom", formatted=True, resizable=True, visible=False) |
| 154 |
self.window.set_visible() |
| 155 |
app.run() |
| 156 |
self.user_verify('Test passed?', take_screenshot=False) |
| 157 |
|
| 158 |
def test_content_anchor_right_formatted(self): |
| 159 |
"""test_content_anchor_right = 'right' property of TextLayout. |
| 160 |
|
| 161 |
Ensure that the text is anchored the way the text describes. |
| 162 |
|
| 163 |
Press ESC to exit the test. |
| 164 |
""" |
| 165 |
desc = """This should be aligned against the right side of the Y axis red line. (Top left quadrant) |
| 166 |
|
| 167 |
Press Spacebar to verify the position remains the same.""" |
| 168 |
|
| 169 |
self.window = TestWindow(desc, anchor_x="right", anchor_y="bottom", formatted=True, resizable=True, visible=False) |
| 170 |
self.window.set_visible() |
| 171 |
app.run() |
| 172 |
self.user_verify('Test passed?', take_screenshot=False) |
| 173 |
|