forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphical_test_base.py
More file actions
237 lines (192 loc) · 7.06 KB
/
graphical_test_base.py
File metadata and controls
237 lines (192 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Base class for graphical tests in MicroPythonOS.
This class provides common setup/teardown patterns for tests that require
LVGL/UI initialization. It handles:
- Screen creation and cleanup
- Screenshot directory configuration
- Common UI testing utilities
Usage:
from base import GraphicalTestBase
class TestMyApp(GraphicalTestBase):
def test_something(self):
# self.screen is already set up (320x240)
# self.screenshot_dir is configured
label = lv.label(self.screen)
label.set_text("Hello")
self.wait_for_render()
self.capture_screenshot("my_test")
"""
import unittest
import lvgl as lv
import sys
import os
class GraphicalTestBase(unittest.TestCase):
"""
Base class for all graphical tests.
Provides:
- Automatic screen creation and cleanup
- Screenshot directory configuration
- Common UI testing utilities
Class Attributes:
SCREEN_WIDTH: Default screen width (320)
SCREEN_HEIGHT: Default screen height (240)
DEFAULT_RENDER_ITERATIONS: Default iterations for wait_for_render (5)
Instance Attributes:
screen: The LVGL screen object for the test
screenshot_dir: Path to the screenshots directory
"""
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 240
DEFAULT_RENDER_ITERATIONS = 5
@classmethod
def setUpClass(cls):
"""
Set up class-level fixtures.
Configures the screenshot directory based on platform.
"""
# Determine screenshot directory based on platform
if sys.platform == "esp32":
cls.screenshot_dir = "tests/screenshots"
else:
# On desktop, tests directory is in parent
cls.screenshot_dir = "../tests/screenshots"
# Ensure screenshots directory exists
try:
os.mkdir(cls.screenshot_dir)
except OSError:
pass # Directory already exists
def setUp(self):
"""
Set up test fixtures before each test method.
Creates a new screen and loads it.
"""
# Create and load a new screen
self.screen = lv.obj()
self.screen.set_size(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
lv.screen_load(self.screen)
self.wait_for_render()
def tearDown(self):
"""
Clean up after each test method.
Loads an empty screen to clean up.
"""
# Load an empty screen to clean up
lv.screen_load(lv.obj())
self.wait_for_render()
def wait_for_render(self, iterations=None):
"""
Wait for LVGL to render.
Args:
iterations: Number of render iterations (default: DEFAULT_RENDER_ITERATIONS)
"""
from mpos import wait_for_render
if iterations is None:
iterations = self.DEFAULT_RENDER_ITERATIONS
wait_for_render(iterations)
def capture_screenshot(self, name, width=None, height=None):
"""
Capture a screenshot with standardized naming.
Args:
name: Name for the screenshot (without extension)
width: Screenshot width (default: SCREEN_WIDTH)
height: Screenshot height (default: SCREEN_HEIGHT)
Returns:
bytes: The screenshot buffer
"""
from mpos import capture_screenshot
if width is None:
width = self.SCREEN_WIDTH
if height is None:
height = self.SCREEN_HEIGHT
path = f"{self.screenshot_dir}/{name}.raw"
return capture_screenshot(path, width=width, height=height)
def find_label_with_text(self, text, parent=None):
"""
Find a label containing the specified text.
Args:
text: Text to search for
parent: Parent widget to search in (default: current screen)
Returns:
The label widget if found, None otherwise
"""
from mpos import find_label_with_text
if parent is None:
parent = lv.screen_active()
return find_label_with_text(parent, text)
def verify_text_present(self, text, parent=None):
"""
Verify that text is present on screen.
Args:
text: Text to search for
parent: Parent widget to search in (default: current screen)
Returns:
bool: True if text is found
"""
from mpos import verify_text_present
if parent is None:
parent = lv.screen_active()
return verify_text_present(parent, text)
def print_screen_labels(self, parent=None):
"""
Print all labels on screen (for debugging).
Args:
parent: Parent widget to search in (default: current screen)
"""
from mpos import print_screen_labels
if parent is None:
parent = lv.screen_active()
print_screen_labels(parent)
def click_button(self, text, use_send_event=True):
"""
Click a button by its text.
Args:
text: Button text to find and click
use_send_event: If True, use send_event (more reliable)
Returns:
bool: True if button was found and clicked
"""
from mpos import click_button
return click_button(text, use_send_event=use_send_event)
def click_label(self, text, use_send_event=True):
"""
Click a label by its text.
Args:
text: Label text to find and click
use_send_event: If True, use send_event (more reliable)
Returns:
bool: True if label was found and clicked
"""
from mpos import click_label
return click_label(text, use_send_event=use_send_event)
def simulate_click(self, x, y):
"""
Simulate a click at specific coordinates.
Note: For most UI testing, prefer click_button() or click_label()
which are more reliable. Use this only when testing touch behavior.
Args:
x: X coordinate
y: Y coordinate
"""
from mpos import simulate_click
simulate_click(x, y)
self.wait_for_render()
def assertTextPresent(self, text, msg=None):
"""
Assert that text is present on screen.
Args:
text: Text to search for
msg: Optional failure message
"""
if msg is None:
msg = f"Text '{text}' not found on screen"
self.assertTrue(self.verify_text_present(text), msg)
def assertTextNotPresent(self, text, msg=None):
"""
Assert that text is NOT present on screen.
Args:
text: Text to search for
msg: Optional failure message
"""
if msg is None:
msg = f"Text '{text}' should not be on screen"
self.assertFalse(self.verify_text_present(text), msg)