| 1 |
""" |
| 2 |
Tests for events on windows. |
| 3 |
""" |
| 4 |
import pytest |
| 5 |
import random |
| 6 |
|
| 7 |
from pyglet import font |
| 8 |
from pyglet import gl |
| 9 |
from pyglet.window import key, Window |
| 10 |
from pyglet.window.event import WindowEventLogger |
| 11 |
|
| 12 |
from tests.base.interactive import InteractiveTestCase |
| 13 |
from tests.interactive.window import window_util |
| 14 |
|
| 15 |
|
| 16 |
class WindowEventsTestCase(InteractiveTestCase): |
| 17 |
""" |
| 18 |
Base class that shows a window displaying instructions for the test. Then it waits for events |
| 19 |
to continue. Optionally the user can fail the test by pressing Escape. |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
window_size = 400, 200 |
| 24 |
window = None |
| 25 |
question = None |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
self.finished = False |
| 29 |
self.failure = None |
| 30 |
self.label = None |
| 31 |
|
| 32 |
def fail_test(self, failure): |
| 33 |
self.failure = failure |
| 34 |
self.finished = True |
| 35 |
|
| 36 |
def pass_test(self): |
| 37 |
self.finished = True |
| 38 |
|
| 39 |
def _render_question(self): |
| 40 |
fnt = font.load('Courier') |
| 41 |
self.label = font.Text(fnt, text=self.question, x=10, y=self.window_size[1]-20) |
| 42 |
|
| 43 |
def _draw(self): |
| 44 |
gl.glClearColor(0.5, 0, 0, 1) |
| 45 |
gl.glClear(gl.GL_COLOR_BUFFER_BIT) |
| 46 |
gl.glLoadIdentity() |
| 47 |
self.label.draw() |
| 48 |
self.window.flip() |
| 49 |
|
| 50 |
def _test_main(self): |
| 51 |
assert self.question |
| 52 |
|
| 53 |
width, height = self.window_size |
| 54 |
self.window = w = Window(width, height, visible=False, resizable=False) |
| 55 |
try: |
| 56 |
w.push_handlers(self) |
| 57 |
self._render_question() |
| 58 |
w.set_visible() |
| 59 |
|
| 60 |
while not self.finished and not w.has_exit: |
| 61 |
self._draw() |
| 62 |
w.dispatch_events() |
| 63 |
|
| 64 |
finally: |
| 65 |
w.close() |
| 66 |
|
| 67 |
|
| 68 |
self.assertTrue(self.finished, msg="Test aborted") |
| 69 |
self.assertIsNone(self.failure, msg=self.failure) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.requires_user_action |
| 73 |
class KeyPressWindowEventTestCase(WindowEventsTestCase): |
| 74 |
number_of_checks = 10 |
| 75 |
keys = (key.A, key.B, key.C, key.D, key.E, key.F, key.G, key.H, key.I, key.J, key.K, key.L, |
| 76 |
key.M, key.N, key.O, key.P, key.Q, key.R, key.S, key.T, key.U, key.V, key.W, key.X, |
| 77 |
key.Y, key.Z) |
| 78 |
mod_shift_keys = (key.LSHIFT, key.RSHIFT) |
| 79 |
mod_ctrl_keys = (key.LCTRL, key.RCTRL) |
| 80 |
mod_alt_keys = (key.LALT, key.RALT) |
| 81 |
mod_option_keys = (key.LOPTION, key.ROPTION) |
| 82 |
mod_meta_keys = (key.LMETA, key.RMETA) |
| 83 |
mod_meta = key.MOD_SHIFT | key.MOD_ALT |
| 84 |
|
| 85 |
def setUp(self): |
| 86 |
super().setUp() |
| 87 |
self.chosen_symbol = None |
| 88 |
self.chosen_modifiers = None |
| 89 |
self.completely_pressed = False |
| 90 |
self.active_keys = [] |
| 91 |
self.checks_passed = 0 |
| 92 |
|
| 93 |
def on_key_press(self, symbol, modifiers): |
| 94 |
print('Press: ', key.symbol_string(symbol)) |
| 95 |
self.active_keys.append(symbol) |
| 96 |
|
| 97 |
if self.completely_pressed: |
| 98 |
self.fail_test('Key already pressed, no release received.') |
| 99 |
|
| 100 |
elif self._is_correct_modifier_key(symbol): |
| 101 |
|
| 102 |
|
| 103 |
pass |
| 104 |
|
| 105 |
elif self._is_correct_key(symbol): |
| 106 |
self._check_modifiers_against_pressed_keys(modifiers) |
| 107 |
self.completely_pressed = True |
| 108 |
|
| 109 |
def on_key_release(self, symbol, modifiers): |
| 110 |
print('Release: ', key.symbol_string(symbol)) |
| 111 |
symbol = self._handle_meta_release(symbol) |
| 112 |
if symbol not in self.active_keys: |
| 113 |
self.fail_test('Released key "{}" was not pressed before.'.format(key.symbol_string(symbol))) |
| 114 |
else: |
| 115 |
self.active_keys.remove(symbol) |
| 116 |
|
| 117 |
if len(self.active_keys) == 0 and self.completely_pressed: |
| 118 |
self.completely_pressed = False |
| 119 |
self.checks_passed += 1 |
| 120 |
if self.checks_passed == self.number_of_checks: |
| 121 |
self.pass_test() |
| 122 |
else: |
| 123 |
self._select_next_key() |
| 124 |
|
| 125 |
def _select_next_key(self): |
| 126 |
self.chosen_symbol = random.choice(self.keys) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
max_modifiers = key.MOD_SHIFT | key.MOD_ALT | key.MOD_CTRL |
| 131 |
|
| 132 |
self.chosen_modifiers = max(0, random.randint(-2, max_modifiers)) |
| 133 |
|
| 134 |
self._update_question() |
| 135 |
|
| 136 |
def _update_question(self): |
| 137 |
modifiers = [] |
| 138 |
if self.chosen_modifiers & key.MOD_SHIFT: |
| 139 |
modifiers.append('<Shift>') |
| 140 |
if self.chosen_modifiers & key.MOD_ALT: |
| 141 |
modifiers.append('<Alt/Option>') |
| 142 |
if self.chosen_modifiers & key.MOD_CTRL: |
| 143 |
modifiers.append('<Ctrl>') |
| 144 |
|
| 145 |
self.question = """Please press and release the following combination of keys. |
| 146 |
Only use <Shift> if explicitly asked to do so. |
| 147 |
|
| 148 |
{} {} |
| 149 |
|
| 150 |
|
| 151 |
Press Esc if test does not pass.""".format(' '.join(modifiers), key.symbol_string(self.chosen_symbol)) |
| 152 |
self._render_question() |
| 153 |
|
| 154 |
def _is_correct_modifier_key(self, symbol): |
| 155 |
modifier = self._get_modifier_for_key(symbol) |
| 156 |
if modifier == 0: |
| 157 |
return False |
| 158 |
|
| 159 |
if modifier == key.MOD_OPTION: |
| 160 |
|
| 161 |
return True |
| 162 |
|
| 163 |
if not self.chosen_modifiers & modifier: |
| 164 |
self.fail_test('Unexpected modifier key "{}"'.format(key.symbol_string(symbol))) |
| 165 |
|
| 166 |
return True |
| 167 |
|
| 168 |
def _get_modifier_for_key(self, symbol): |
| 169 |
if symbol in self.mod_shift_keys: |
| 170 |
return key.MOD_SHIFT |
| 171 |
elif symbol in self.mod_alt_keys: |
| 172 |
return key.MOD_ALT |
| 173 |
elif symbol in self.mod_ctrl_keys: |
| 174 |
return key.MOD_CTRL |
| 175 |
elif symbol in self.mod_option_keys: |
| 176 |
return key.MOD_OPTION |
| 177 |
elif symbol in self.mod_meta_keys: |
| 178 |
return self.mod_meta |
| 179 |
else: |
| 180 |
return 0 |
| 181 |
|
| 182 |
def _get_modifiers_from_pressed_keys(self): |
| 183 |
modifiers = 0 |
| 184 |
for symbol in self.active_keys: |
| 185 |
modifiers |= self._get_modifier_for_key(symbol) |
| 186 |
return modifiers |
| 187 |
|
| 188 |
def _translate_option_modifier(self, modifiers): |
| 189 |
""" |
| 190 |
On macOS the Alt key is also called the Option key. Key presses for both Alt and Option |
| 191 |
are emitted, but the modifier list contains only one of them. Unify modifiers to |
| 192 |
contain only Alt if Alt and/or Option is active. |
| 193 |
""" |
| 194 |
if modifiers & key.MOD_OPTION or modifiers & key.MOD_ALT: |
| 195 |
return (modifiers & ~key.MOD_OPTION) | key.MOD_ALT |
| 196 |
else: |
| 197 |
return modifiers |
| 198 |
|
| 199 |
def _check_modifiers_against_pressed_keys(self, modifiers): |
| 200 |
modifiers_from_keys = self._translate_option_modifier(self._get_modifiers_from_pressed_keys()) |
| 201 |
modifiers = self._translate_option_modifier(modifiers) |
| 202 |
if modifiers != modifiers_from_keys: |
| 203 |
self.fail_test('Received modifiers "{}" do not match pressed keys "{}"'.format( |
| 204 |
key.modifiers_string(modifiers), |
| 205 |
key.modifiers_string(modifiers_from_keys))) |
| 206 |
|
| 207 |
def _is_correct_key(self, symbol): |
| 208 |
if not self.chosen_symbol: |
| 209 |
self.fail_test('No more key presses/releases expected.') |
| 210 |
return False |
| 211 |
|
| 212 |
if self.chosen_symbol != symbol: |
| 213 |
self.fail_test('Received key "{}", but expected "{}"'.format(key.symbol_string(symbol), |
| 214 |
key.symbol_string(self.chosen_symbol))) |
| 215 |
return False |
| 216 |
|
| 217 |
return True |
| 218 |
|
| 219 |
def _handle_meta_release(self, symbol): |
| 220 |
"""The meta key can be either released as meta or as alt shift or vv""" |
| 221 |
if symbol in (key.LMETA, key.LALT, key.RMETA, key.RALT): |
| 222 |
if symbol not in self.active_keys: |
| 223 |
if symbol == key.LMETA and key.LALT in self.active_keys: |
| 224 |
return key.LALT |
| 225 |
if symbol == key.RMETA and key.RALT in self.active_keys: |
| 226 |
return key.RALT |
| 227 |
if symbol == key.LALT and key.LMETA in self.active_keys: |
| 228 |
return key.LMETA |
| 229 |
if symbol == key.RALT and key.RMETA in self.active_keys: |
| 230 |
return key.RMETA |
| 231 |
return symbol |
| 232 |
|
| 233 |
def test_key_press_release(self): |
| 234 |
"""Show several keys to press. Check that the event is triggered for the correct key.""" |
| 235 |
self._select_next_key() |
| 236 |
self._test_main() |
| 237 |
|
| 238 |
|
| 239 |
@pytest.mark.requires_user_action |
| 240 |
class TextWindowEventsTest(WindowEventsTestCase): |
| 241 |
number_of_checks = 10 |
| 242 |
text = '`1234567890-=~!@#$%^&*()_+qwertyuiop[]\\QWERTYUIOP{}|asdfghjkl;\'ASDFGHJKL:"zxcvbnm,./ZXCVBNM<>?' |
| 243 |
|
| 244 |
def setUp(self): |
| 245 |
super().setUp() |
| 246 |
self.chosen_text = None |
| 247 |
self.checks_passed = 0 |
| 248 |
|
| 249 |
def on_text(self, text): |
| 250 |
if text != self.chosen_text: |
| 251 |
self.fail_test('Expected "{}", received "{}"'.format(self.chosen_text, text)) |
| 252 |
else: |
| 253 |
self.checks_passed += 1 |
| 254 |
if self.checks_passed >= self.number_of_checks: |
| 255 |
self.pass_test() |
| 256 |
else: |
| 257 |
self._select_next_text() |
| 258 |
|
| 259 |
def _select_next_text(self): |
| 260 |
self.chosen_text = random.choice(self.text) |
| 261 |
self._update_question() |
| 262 |
|
| 263 |
def _update_question(self): |
| 264 |
self.question = """Please type the following character exactly. |
| 265 |
Use <Shift> if needed. |
| 266 |
|
| 267 |
{} |
| 268 |
|
| 269 |
|
| 270 |
Press Esc if test does not pass.""".format(self.chosen_text) |
| 271 |
self._render_question() |
| 272 |
|
| 273 |
def test_key_text(self): |
| 274 |
"""Show several keys to press. Check that the text events are triggered correctly.""" |
| 275 |
self._select_next_text() |
| 276 |
self._test_main() |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.requires_user_action |
| 280 |
class TextMotionWindowEventsTest(WindowEventsTestCase): |
| 281 |
number_of_checks = 10 |
| 282 |
motion_keys = (key.MOTION_UP, key.MOTION_RIGHT, key.MOTION_DOWN, key.MOTION_LEFT, |
| 283 |
key.MOTION_NEXT_PAGE, key.MOTION_PREVIOUS_PAGE, key.MOTION_BACKSPACE, |
| 284 |
key.MOTION_DELETE) |
| 285 |
|
| 286 |
def setUp(self): |
| 287 |
super().setUp() |
| 288 |
self.chosen_key = None |
| 289 |
self.checks_passed = 0 |
| 290 |
|
| 291 |
def on_key_press(self, symbol, modifiers): |
| 292 |
if symbol == key.X: |
| 293 |
self._select_next_key() |
| 294 |
|
| 295 |
def on_text_motion(self, motion): |
| 296 |
if motion != self.chosen_key: |
| 297 |
self.fail_test('Expected "{}", received "{}"'.format( |
| 298 |
key.motion_string(self.chosen_key), key.motion_string(motion))) |
| 299 |
else: |
| 300 |
self.checks_passed += 1 |
| 301 |
if self.checks_passed >= self.number_of_checks: |
| 302 |
self.pass_test() |
| 303 |
else: |
| 304 |
self._select_next_key() |
| 305 |
|
| 306 |
def _select_next_key(self): |
| 307 |
self.chosen_key = random.choice(self.motion_keys) |
| 308 |
self._update_question() |
| 309 |
|
| 310 |
def _update_question(self): |
| 311 |
self.question = """Please press: |
| 312 |
|
| 313 |
{} ({}) |
| 314 |
|
| 315 |
|
| 316 |
Press the X key if you do not have this motion key. |
| 317 |
Press Esc if test does not pass.""".format(key.motion_string(self.chosen_key), |
| 318 |
key.symbol_string(self.chosen_key)) |
| 319 |
self._render_question() |
| 320 |
|
| 321 |
def test_key_text_motion(self): |
| 322 |
"""Show several motion keys to press. Check that the on_text_motion events are triggered |
| 323 |
correctly.""" |
| 324 |
self._select_next_key() |
| 325 |
self._test_main() |
| 326 |
|
| 327 |
@pytest.mark.requires_user_action |
| 328 |
class TextMotionSelectWindowEventsTest(WindowEventsTestCase): |
| 329 |
number_of_checks = 10 |
| 330 |
motion_keys = (key.MOTION_UP, key.MOTION_RIGHT, key.MOTION_DOWN, key.MOTION_LEFT, |
| 331 |
key.MOTION_NEXT_PAGE, key.MOTION_PREVIOUS_PAGE, key.MOTION_BACKSPACE, |
| 332 |
key.MOTION_DELETE) |
| 333 |
|
| 334 |
def setUp(self): |
| 335 |
super().setUp() |
| 336 |
self.chosen_key = None |
| 337 |
self.checks_passed = 0 |
| 338 |
|
| 339 |
def on_key_press(self, symbol, modifiers): |
| 340 |
if symbol == key.X: |
| 341 |
self._select_next_key() |
| 342 |
|
| 343 |
def on_text_motion_select(self, motion): |
| 344 |
if motion != self.chosen_key: |
| 345 |
self.fail_test('Expected "{}", received "{}"'.format( |
| 346 |
key.motion_string(self.chosen_key), key.motion_string(motion))) |
| 347 |
else: |
| 348 |
self.checks_passed += 1 |
| 349 |
if self.checks_passed >= self.number_of_checks: |
| 350 |
self.pass_test() |
| 351 |
else: |
| 352 |
self._select_next_key() |
| 353 |
|
| 354 |
def _select_next_key(self): |
| 355 |
self.chosen_key = random.choice(self.motion_keys) |
| 356 |
self._update_question() |
| 357 |
|
| 358 |
def _update_question(self): |
| 359 |
self.question = """Please hold <Shift> and press: |
| 360 |
|
| 361 |
{} ({}) |
| 362 |
|
| 363 |
|
| 364 |
Press the X key if you do not have this motion key. |
| 365 |
Press Esc if test does not pass.""".format(key.motion_string(self.chosen_key), |
| 366 |
key.symbol_string(self.chosen_key)) |
| 367 |
self._render_question() |
| 368 |
|
| 369 |
def test_key_text_motion_select(self): |
| 370 |
"""Show several motion keys to press. Check that the on_text_motion_select events are |
| 371 |
triggered correctly combined with shift.""" |
| 372 |
self._select_next_key() |
| 373 |
self._test_main() |
| 374 |
|
| 375 |
|
| 376 |
@pytest.mark.requires_user_action |
| 377 |
class CloseWindowEventsTest(WindowEventsTestCase): |
| 378 |
def on_close(self): |
| 379 |
self.pass_test() |
| 380 |
|
| 381 |
def test_on_close_event(self): |
| 382 |
"""Test the on_close event triggered when closing the window.""" |
| 383 |
self.question = "Please close this window by\nclicking the close button." |
| 384 |
self._test_main() |
| 385 |
|
| 386 |
|
| 387 |
@pytest.mark.requires_user_action |
| 388 |
class ActivateDeactivateWindowEventsTest(WindowEventsTestCase): |
| 389 |
number_of_checks = 3 |
| 390 |
|
| 391 |
def setUp(self): |
| 392 |
super().setUp() |
| 393 |
self.window_active = None |
| 394 |
self.checks_passed = 0 |
| 395 |
|
| 396 |
def on_expose(self): |
| 397 |
self.window_active = True |
| 398 |
self._update_question() |
| 399 |
|
| 400 |
def on_activate(self): |
| 401 |
if self.window_active: |
| 402 |
self.fail_test('Got double on_activate') |
| 403 |
else: |
| 404 |
self.window_active = True |
| 405 |
self.checks_passed += 1 |
| 406 |
if self.checks_passed >= self.number_of_checks: |
| 407 |
self.pass_test() |
| 408 |
else: |
| 409 |
self._update_question() |
| 410 |
|
| 411 |
def on_deactivate(self): |
| 412 |
if not self.window_active: |
| 413 |
self.fail_test('Got double on_deactivate') |
| 414 |
else: |
| 415 |
self.window_active = False |
| 416 |
self._update_question() |
| 417 |
|
| 418 |
def _update_question(self): |
| 419 |
if self.window_active: |
| 420 |
self.question = "Please activate another window." |
| 421 |
else: |
| 422 |
self.question = "Please activate this window." |
| 423 |
self._render_question() |
| 424 |
|
| 425 |
def test_activate_deactivate(self): |
| 426 |
"""Test the on_activate and on_deactivate events triggered when the window gets activated |
| 427 |
or deactivated.""" |
| 428 |
self._update_question() |
| 429 |
self._test_main() |
| 430 |
|
| 431 |
|
| 432 |
@pytest.mark.requires_user_action |
| 433 |
class ExposeWindowEventsTest(WindowEventsTestCase): |
| 434 |
number_of_checks = 5 |
| 435 |
|
| 436 |
def setUp(self): |
| 437 |
super().setUp() |
| 438 |
self.checks_passed = 0 |
| 439 |
|
| 440 |
def on_expose(self): |
| 441 |
self.checks_passed += 1 |
| 442 |
if self.checks_passed >= self.number_of_checks: |
| 443 |
self.pass_test() |
| 444 |
|
| 445 |
def test_expose(self): |
| 446 |
"""Test the on_expose event triggered when a redraw of the window is required.""" |
| 447 |
self.question = ("Please trigger a redraw of this window.\n\n" |
| 448 |
"Depending on your OS and window manager you might need to:\n" |
| 449 |
"- Cover the window with another window and uncover again\n" |
| 450 |
"- Minimize and restore the window\n\n" |
| 451 |
"Repeat up to 5 times (less might be accepted due to initial drawing)") |
| 452 |
self.window_size = 700, 200 |
| 453 |
self._test_main() |
| 454 |
|
| 455 |
|
| 456 |
@pytest.mark.requires_user_action |
| 457 |
class ShowHideWindowEventsTest(WindowEventsTestCase): |
| 458 |
number_of_checks = 5 |
| 459 |
|
| 460 |
def setUp(self): |
| 461 |
super().setUp() |
| 462 |
self.checks_passed = 0 |
| 463 |
self.visible = False |
| 464 |
|
| 465 |
def on_show(self): |
| 466 |
if self.visible: |
| 467 |
self.fail_test('Received on_show twice without on_hide') |
| 468 |
else: |
| 469 |
self.checks_passed += 1 |
| 470 |
self.visible = True |
| 471 |
if self.checks_passed >= self.number_of_checks: |
| 472 |
self.pass_test() |
| 473 |
|
| 474 |
def on_hide(self): |
| 475 |
if not self.visible: |
| 476 |
self.fail_test('Received on_hide twice without on_show') |
| 477 |
else: |
| 478 |
self.visible = False |
| 479 |
|
| 480 |
def test_show_hide(self): |
| 481 |
"""Test the on_show and on_hide events.""" |
| 482 |
self.question = ('Please trigger hide and show this window again.\n' |
| 483 |
'You can do this by:\n' |
| 484 |
'- Minimize and restore the window\n' |
| 485 |
'- On OS X show and hide using Command+H or the dock context menu\n' |
| 486 |
'\n' |
| 487 |
'Test passes after doing this 4 times.') |
| 488 |
self.window_size = 700, 200 |
| 489 |
self._test_main() |
| 490 |
|
| 491 |
|
| 492 |
@pytest.mark.requires_user_action |
| 493 |
class EVENT_BUTTON(InteractiveTestCase): |
| 494 |
"""Test that mouse button events work correctly. |
| 495 |
|
| 496 |
Expected behaviour: |
| 497 |
One window will be opened. Click within this window and check the console |
| 498 |
output for mouse events. |
| 499 |
- Buttons 1, 2, 4 correspond to left, middle, right, respectively. |
| 500 |
- No events for scroll wheel |
| 501 |
- Modifiers are correct |
| 502 |
|
| 503 |
Close the window or press ESC to end the test. |
| 504 |
""" |
| 505 |
def on_mouse_press(self, x, y, button, modifiers): |
| 506 |
print('Mouse button %d pressed at %f,%f with %s' % \ |
| 507 |
(button, x, y, key.modifiers_string(modifiers))) |
| 508 |
|
| 509 |
def on_mouse_release(self, x, y, button, modifiers): |
| 510 |
print('Mouse button %d released at %f,%f with %s' % \ |
| 511 |
(button, x, y, key.modifiers_string(modifiers))) |
| 512 |
|
| 513 |
def test_button(self): |
| 514 |
w = Window(200, 200) |
| 515 |
try: |
| 516 |
w.push_handlers(self) |
| 517 |
while not w.has_exit: |
| 518 |
w.dispatch_events() |
| 519 |
finally: |
| 520 |
w.close() |
| 521 |
self.user_verify('Pass test?', take_screenshot=False) |
| 522 |
|
| 523 |
|
| 524 |
@pytest.mark.requires_user_action |
| 525 |
class EVENT_MOVE(InteractiveTestCase): |
| 526 |
"""Test that window move event works correctly. |
| 527 |
|
| 528 |
Expected behaviour: |
| 529 |
One window will be opened. Move the window and ensure that the |
| 530 |
location printed to the terminal is correct. |
| 531 |
|
| 532 |
Close the window or press ESC to end the test. |
| 533 |
""" |
| 534 |
def on_move(self, x, y): |
| 535 |
print('Window moved to %dx%d.' % (x, y)) |
| 536 |
|
| 537 |
def test_move(self): |
| 538 |
w = Window(200, 200) |
| 539 |
try: |
| 540 |
w.push_handlers(self) |
| 541 |
while not w.has_exit: |
| 542 |
w.dispatch_events() |
| 543 |
finally: |
| 544 |
w.close() |
| 545 |
self.user_verify('Pass test?', take_screenshot=False) |
| 546 |
|
| 547 |
|
| 548 |
@pytest.mark.requires_user_action |
| 549 |
class EVENT_RESIZE(InteractiveTestCase): |
| 550 |
"""Test that resize event works correctly. |
| 551 |
|
| 552 |
Expected behaviour: |
| 553 |
One window will be opened. Resize the window and ensure that the |
| 554 |
dimensions printed to the terminal are correct. You should see |
| 555 |
a green border inside the window but no red. |
| 556 |
|
| 557 |
Close the window or press ESC to end the test. |
| 558 |
""" |
| 559 |
def on_resize(self, width, height): |
| 560 |
print('Window resized to %dx%d.' % (width, height)) |
| 561 |
|
| 562 |
def test_resize(self): |
| 563 |
w = Window(200, 200, resizable=True) |
| 564 |
try: |
| 565 |
w.push_handlers(self) |
| 566 |
while not w.has_exit: |
| 567 |
window_util.draw_client_border(w) |
| 568 |
w.flip() |
| 569 |
w.dispatch_events() |
| 570 |
finally: |
| 571 |
w.close() |
| 572 |
self.user_verify('Pass test?', take_screenshot=False) |
| 573 |
|
| 574 |
|
| 575 |
@pytest.mark.requires_user_action |
| 576 |
class EVENT_MOUSE_DRAG(InteractiveTestCase): |
| 577 |
"""Test that mouse drag event works correctly. |
| 578 |
|
| 579 |
Expected behaviour: |
| 580 |
One window will be opened. Click and drag with the mouse and ensure |
| 581 |
that buttons, coordinates and modifiers are reported correctly. Events |
| 582 |
should be generated even when the drag leaves the window. |
| 583 |
|
| 584 |
Close the window or press ESC to end the test. |
| 585 |
""" |
| 586 |
def test_mouse_drag(self): |
| 587 |
w = Window(200, 200) |
| 588 |
try: |
| 589 |
w.push_handlers(WindowEventLogger()) |
| 590 |
while not w.has_exit: |
| 591 |
w.dispatch_events() |
| 592 |
finally: |
| 593 |
w.close() |
| 594 |
self.user_verify('Pass test?', take_screenshot=False) |
| 595 |
|
| 596 |
|
| 597 |
@pytest.mark.requires_user_action |
| 598 |
class EVENT_MOUSEMOTION(InteractiveTestCase): |
| 599 |
"""Test that mouse motion event works correctly. |
| 600 |
|
| 601 |
Expected behaviour: |
| 602 |
One window will be opened. Move the mouse in and out of this window |
| 603 |
and ensure the absolute and relative coordinates are correct. |
| 604 |
- Absolute coordinates should have (0,0) at bottom-left of client area |
| 605 |
of window with positive y-axis pointing up and positive x-axis right. |
| 606 |
- Relative coordinates should be positive when moving up and right. |
| 607 |
|
| 608 |
Close the window or press ESC to end the test. |
| 609 |
""" |
| 610 |
def on_mouse_motion(self, x, y, dx, dy): |
| 611 |
print('Mouse at (%f, %f); relative (%f, %f).' % \ |
| 612 |
(x, y, dx, dy)) |
| 613 |
|
| 614 |
def test_motion(self): |
| 615 |
w = Window(200, 200) |
| 616 |
try: |
| 617 |
w.push_handlers(self) |
| 618 |
while not w.has_exit: |
| 619 |
w.dispatch_events() |
| 620 |
finally: |
| 621 |
w.close() |
| 622 |
self.user_verify('Pass test?', take_screenshot=False) |
| 623 |
|
| 624 |
|
| 625 |
@pytest.mark.requires_user_action |
| 626 |
class EVENT_MOUSEMOTION_EXCLUSIVE(InteractiveTestCase): |
| 627 |
"""Test that mouse motion event works correctly in exclusive mode. |
| 628 |
|
| 629 |
Expected behaviour: |
| 630 |
One window will be opened. The mouse cursor will disappear. Move the |
| 631 |
mouse and ensure the relative coordinates are correct. |
| 632 |
- Absolute coordinates should be 0, 0. |
| 633 |
- Relative coordinates should be positive when moving up and right. |
| 634 |
|
| 635 |
Try also to use the mouse buttons and mouse wheel. |
| 636 |
- Buttons 1, 2, 4 correspond to left, middle, right, respectively. |
| 637 |
- Buttons press and release will display the button and key modifiers. |
| 638 |
- Drag motions show relative movement, buttons pressed and key |
| 639 |
modifier. |
| 640 |
- Mouse wheel show positive value for forward movements. |
| 641 |
|
| 642 |
Close the window or press ESC to end the test. |
| 643 |
""" |
| 644 |
def on_mouse_motion(self, x, y, dx, dy): |
| 645 |
print('Mouse at (%f, %f); relative (%f, %f).' % \ |
| 646 |
(x, y, dx, dy)) |
| 647 |
|
| 648 |
def on_mouse_press(self, x, y, button, modifiers): |
| 649 |
print('Mouse button %d pressed at %f,%f with %s' % \ |
| 650 |
(button, x, y, key.modifiers_string(modifiers))) |
| 651 |
|
| 652 |
def on_mouse_release(self, x, y, button, modifiers): |
| 653 |
print('Mouse button %d released at %f,%f with %s' % \ |
| 654 |
(button, x, y, key.modifiers_string(modifiers))) |
| 655 |
|
| 656 |
def on_mouse_drag(self, x, y, dx, dy, button, modifiers): |
| 657 |
print('Mouse dragged. Button %d pressed. dx, dy %f,%f with %s' % \ |
| 658 |
(button, dx, dy, key.modifiers_string(modifiers))) |
| 659 |
|
| 660 |
def on_mouse_scroll(self, x, y, scrollx, scrolly): |
| 661 |
print('Mouse scroll %f' % (scrolly)) |
| 662 |
|
| 663 |
|
| 664 |
def test_motion(self): |
| 665 |
w = Window(200, 200) |
| 666 |
try: |
| 667 |
w.set_exclusive_mouse(True) |
| 668 |
w.push_handlers(self) |
| 669 |
while not w.has_exit: |
| 670 |
w.dispatch_events() |
| 671 |
finally: |
| 672 |
w.set_exclusive_mouse(False) |
| 673 |
w.close() |
| 674 |
|
| 675 |
self.user_verify('Pass test?', take_screenshot=False) |
| 676 |
|
| 677 |
|
| 678 |
@pytest.mark.requires_user_action |
| 679 |
class EVENT_MOUSE_SCROLL(InteractiveTestCase): |
| 680 |
"""Test that mouse scroll event works correctly. |
| 681 |
|
| 682 |
Expected behaviour: |
| 683 |
One window will be opened. Move the scroll wheel and check that events |
| 684 |
are printed to console. Positive values are associated with scrolling |
| 685 |
up. |
| 686 |
|
| 687 |
Scrolling can also be side-to-side, for example with an Apple Mighty |
| 688 |
Mouse. |
| 689 |
|
| 690 |
The actual scroll value is dependent on your operating system |
| 691 |
user preferences. |
| 692 |
|
| 693 |
Close the window or press ESC to end the test. |
| 694 |
""" |
| 695 |
def on_mouse_scroll(self, x, y, dx, dy): |
| 696 |
print('Mouse scrolled (%f, %f) (x=%f, y=%f)' % (dx, dy, x, y)) |
| 697 |
|
| 698 |
def test_mouse_scroll(self): |
| 699 |
w = Window(200, 200) |
| 700 |
try: |
| 701 |
w.push_handlers(self) |
| 702 |
while not w.has_exit: |
| 703 |
w.dispatch_events() |
| 704 |
finally: |
| 705 |
w.close() |
| 706 |
self.user_verify('Pass test?', take_screenshot=False) |
| 707 |
|
| 708 |
|
| 709 |
@pytest.mark.requires_user_action |
| 710 |
class EVENT_MOUSE_ENTER_LEAVE(InteractiveTestCase): |
| 711 |
"""Test that mouse enter and leave events work correctly. |
| 712 |
|
| 713 |
Expected behaviour: |
| 714 |
One window will be opened. Move the mouse in and out of this window |
| 715 |
and ensure the events displayed are correct. |
| 716 |
|
| 717 |
Close the window or press ESC to end the test. |
| 718 |
""" |
| 719 |
def on_mouse_enter(self, x, y): |
| 720 |
print('Entered at %f, %f' % (x, y)) |
| 721 |
|
| 722 |
def on_mouse_leave(self, x, y): |
| 723 |
print('Left at %f, %f' % (x, y)) |
| 724 |
|
| 725 |
def test_motion(self): |
| 726 |
w = Window(200, 200) |
| 727 |
try: |
| 728 |
w.push_handlers(self) |
| 729 |
while not w.has_exit: |
| 730 |
w.dispatch_events() |
| 731 |
finally: |
| 732 |
w.close() |
| 733 |
self.user_verify('Pass test?', take_screenshot=False) |