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