| 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 |
| 13 |
|
| 14 |
|
| 15 |
class TestWindow(window.Window): |
| 16 |
|
| 17 |
INSTRUCTIONS =\ |
| 18 |
"Please observe whether the caret color matches" \ |
| 19 |
"{0}, then close the window." |
| 20 |
|
| 21 |
def __init__( |
| 22 |
self, |
| 23 |
*args, |
| 24 |
caret_color=(0, 0, 0, 255), |
| 25 |
**kwargs |
| 26 |
): |
| 27 |
super().__init__(*args, **kwargs) |
| 28 |
|
| 29 |
self.batch = graphics.Batch() |
| 30 |
self.document = text.decode_text( |
| 31 |
self.INSTRUCTIONS.format(caret_color) |
| 32 |
) |
| 33 |
|
| 34 |
self.margin = 2 |
| 35 |
self.layout = layout.IncrementalTextLayout( |
| 36 |
self.document, |
| 37 |
width=self.width - self.margin * 2, |
| 38 |
height=self.height - self.margin * 2, |
| 39 |
multiline=True, |
| 40 |
batch=self.batch) |
| 41 |
self.caret = caret.Caret(self.layout, color=caret_color) |
| 42 |
self.push_handlers(self.caret) |
| 43 |
|
| 44 |
self.set_mouse_cursor(self.get_system_mouse_cursor('text')) |
| 45 |
|
| 46 |
def on_resize(self, width, height): |
| 47 |
super().on_resize(width, height) |
| 48 |
self.layout.begin_update() |
| 49 |
self.layout.x = self.margin |
| 50 |
self.layout.y = self.margin |
| 51 |
self.layout.width = width - self.margin * 2 |
| 52 |
self.layout.height = height - self.margin * 2 |
| 53 |
self.layout.end_update() |
| 54 |
|
| 55 |
def on_mouse_scroll(self, x, y, scroll_x, scroll_y): |
| 56 |
self.layout.view_x -= scroll_x |
| 57 |
self.layout.view_y += scroll_y * 16 |
| 58 |
|
| 59 |
def on_draw(self): |
| 60 |
gl.glClearColor(1, 1, 1, 1) |
| 61 |
self.clear() |
| 62 |
self.batch.draw() |
| 63 |
|
| 64 |
def on_key_press(self, symbol, modifiers): |
| 65 |
super().on_key_press(symbol, modifiers) |
| 66 |
if symbol == key.TAB: |
| 67 |
self.caret.on_text('\t') |
| 68 |
|
| 69 |
|
| 70 |
class _DRYHelperMixin: |
| 71 |
|
| 72 |
def build_window(self, color=(0, 0, 0, 255), window_class=TestWindow): |
| 73 |
self.window = window_class(caret_color=color, resizable=False, visible=False) |
| 74 |
self.window.set_visible() |
| 75 |
|
| 76 |
def build_window_via_setter(self, color, window_class=TestWindow): |
| 77 |
self.build_window(window_class=window_class) |
| 78 |
self.window.caret.color = color |
| 79 |
|
| 80 |
def ask_color(self, color): |
| 81 |
self.user_verify(f"Was the caret color {color}?", take_screenshot=False) |
| 82 |
|
| 83 |
|
| 84 |
@pytest.mark.requires_user_action |
| 85 |
class CaretColorInitTestCase(InteractiveTestCase, _DRYHelperMixin): |
| 86 |
"""Test if the text area Caret color is set correctly by init |
| 87 |
|
| 88 |
Examine & remember the color of the caret, and type Y + enter if the |
| 89 |
caret color matched the numbers in the label. |
| 90 |
|
| 91 |
Press ESC to exit the test. |
| 92 |
""" |
| 93 |
|
| 94 |
def test_caret_color_init_rgb(self): |
| 95 |
color = (255, 0, 0) |
| 96 |
self.build_window(color) |
| 97 |
app.run() |
| 98 |
self.ask_color(color) |
| 99 |
|
| 100 |
def test_caret_color_init_rgba(self): |
| 101 |
|
| 102 |
|
| 103 |
color = (255, 0, 0, 80) |
| 104 |
self.build_window(color) |
| 105 |
app.run() |
| 106 |
self.ask_color(color) |
| 107 |
|
| 108 |
|
| 109 |
class SetterTestWindow(TestWindow): |
| 110 |
|
| 111 |
INSTRUCTIONS = \ |
| 112 |
"Press the tab key & observe whether the caret color" \ |
| 113 |
"changed to {0}, then close the window." |
| 114 |
|
| 115 |
def on_key_press(self, symbol, modifiers): |
| 116 |
super().on_key_press(symbol, modifiers) |
| 117 |
if symbol == key.TAB: |
| 118 |
self.caret.color = (0, 255, 0, 10) |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.requires_user_action |
| 122 |
class CaretColorSetterTestCase(InteractiveTestCase, _DRYHelperMixin): |
| 123 |
"""Test if the Caret's color setter works correctly |
| 124 |
|
| 125 |
Examine & remember the color of the caret, and type Y + enter if the |
| 126 |
caret color matched the numbers in the label. |
| 127 |
|
| 128 |
Press ESC to exit the test. |
| 129 |
""" |
| 130 |
|
| 131 |
def test_caret_color_setter_rgb(self): |
| 132 |
color = (0, 0, 255) |
| 133 |
self.build_window_via_setter(color, window_class=SetterTestWindow) |
| 134 |
app.run() |
| 135 |
self.ask_color(color) |
| 136 |
|
| 137 |
def test_caret_color_setter_rgba(self): |
| 138 |
|
| 139 |
|
| 140 |
color = (0, 0, 255, 0) |
| 141 |
self.build_window_via_setter(color, window_class=SetterTestWindow) |
| 142 |
app.run() |
| 143 |
self.ask_color(color) |
| 144 |
|