|
11 | 11 | import unittest |
12 | 12 | import lvgl as lv |
13 | 13 | from mpos.ui.keyboard import MposKeyboard |
| 14 | +from graphical_test_helper import simulate_click, wait_for_render |
14 | 15 |
|
15 | 16 |
|
16 | 17 | class TestMposKeyboard(unittest.TestCase): |
@@ -162,4 +163,93 @@ def test_api_compatibility(self): |
162 | 163 |
|
163 | 164 | print("API compatibility verified") |
164 | 165 |
|
| 166 | + def test_simulate_click_on_button(self): |
| 167 | + """Test clicking keyboard buttons using simulate_click().""" |
| 168 | + print("Testing simulate_click() on keyboard buttons...") |
| 169 | + |
| 170 | + # Create keyboard and load screen |
| 171 | + keyboard = MposKeyboard(self.screen) |
| 172 | + keyboard.set_textarea(self.textarea) |
| 173 | + keyboard.align(lv.ALIGN.BOTTOM_MID, 0, 0) |
| 174 | + lv.screen_load(self.screen) |
| 175 | + wait_for_render(10) |
| 176 | + |
| 177 | + # Get initial text |
| 178 | + initial_text = self.textarea.get_text() |
| 179 | + print(f"Initial textarea text: '{initial_text}'") |
| 180 | + |
| 181 | + # Get keyboard area and click on it |
| 182 | + # The keyboard is an lv.keyboard object (accessed via _keyboard or through __getattr__) |
| 183 | + obj_area = lv.area_t() |
| 184 | + keyboard.get_coords(obj_area) |
| 185 | + |
| 186 | + # Calculate a point to click - let's click in the lower part of keyboard |
| 187 | + # which should be around where letters are |
| 188 | + click_x = (obj_area.x1 + obj_area.x2) // 2 # Center horizontally |
| 189 | + click_y = obj_area.y1 + (obj_area.y2 - obj_area.y1) // 3 # Upper third |
| 190 | + |
| 191 | + print(f"Keyboard area: ({obj_area.x1}, {obj_area.y1}) to ({obj_area.x2}, {obj_area.y2})") |
| 192 | + print(f"Clicking keyboard at ({click_x}, {click_y})") |
| 193 | + |
| 194 | + # Click on the keyboard using simulate_click |
| 195 | + simulate_click(click_x, click_y, press_duration_ms=100) |
| 196 | + wait_for_render(5) |
| 197 | + |
| 198 | + final_text = self.textarea.get_text() |
| 199 | + print(f"Final textarea text: '{final_text}'") |
| 200 | + |
| 201 | + # The important thing is that simulate_click worked without crashing |
| 202 | + # The text might have changed if we hit a letter key |
| 203 | + print("simulate_click() completed successfully") |
| 204 | + |
| 205 | + def test_click_vs_send_event_comparison(self): |
| 206 | + """Compare simulate_click() vs send_event() for triggering button actions.""" |
| 207 | + print("Testing simulate_click() vs send_event() comparison...") |
| 208 | + |
| 209 | + # Create keyboard and load screen |
| 210 | + keyboard = MposKeyboard(self.screen) |
| 211 | + keyboard.set_textarea(self.textarea) |
| 212 | + keyboard.align(lv.ALIGN.BOTTOM_MID, 0, 0) |
| 213 | + lv.screen_load(self.screen) |
| 214 | + wait_for_render(10) |
| 215 | + |
| 216 | + # Test 1: Use send_event() to trigger READY event |
| 217 | + callback_from_send_event = [False] |
| 218 | + |
| 219 | + def callback_send_event(event): |
| 220 | + callback_from_send_event[0] = True |
| 221 | + print("send_event callback triggered") |
| 222 | + |
| 223 | + keyboard.add_event_cb(callback_send_event, lv.EVENT.READY, None) |
| 224 | + keyboard.send_event(lv.EVENT.READY, None) |
| 225 | + wait_for_render(3) |
| 226 | + |
| 227 | + self.assertTrue( |
| 228 | + callback_from_send_event[0], |
| 229 | + "send_event() should trigger callback" |
| 230 | + ) |
| 231 | + |
| 232 | + # Test 2: Use simulate_click() to click on keyboard |
| 233 | + # This demonstrates that simulate_click works with real UI interaction |
| 234 | + initial_text = self.textarea.get_text() |
| 235 | + |
| 236 | + # Get keyboard area to click within it |
| 237 | + obj_area = lv.area_t() |
| 238 | + keyboard.get_coords(obj_area) |
| 239 | + |
| 240 | + # Click somewhere in the middle of the keyboard |
| 241 | + click_x = (obj_area.x1 + obj_area.x2) // 2 |
| 242 | + click_y = (obj_area.y1 + obj_area.y2) // 2 |
| 243 | + |
| 244 | + print(f"Clicking keyboard at ({click_x}, {click_y})") |
| 245 | + simulate_click(click_x, click_y, press_duration_ms=100) |
| 246 | + wait_for_render(5) |
| 247 | + |
| 248 | + # Verify click completed without crashing |
| 249 | + final_text = self.textarea.get_text() |
| 250 | + print(f"Text before click: '{initial_text}'") |
| 251 | + print(f"Text after click: '{final_text}'") |
| 252 | + |
| 253 | + print("Both send_event() and simulate_click() work correctly") |
| 254 | + |
165 | 255 |
|
0 commit comments