| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
import io |
| 12 |
import textwrap |
| 13 |
import unittest |
| 14 |
from unittest import mock |
| 15 |
|
| 16 |
from pyface.api import GUI |
| 17 |
|
| 18 |
from traits.api import HasTraits, Int |
| 19 |
from traits.testing.api import UnittestTools |
| 20 |
from traitsui.tests._tools import ( |
| 21 |
process_cascade_events, |
| 22 |
requires_toolkit, |
| 23 |
ToolkitName, |
| 24 |
) |
| 25 |
from traitsui.testing.tester._abstract_target_registry import ( |
| 26 |
AbstractTargetRegistry, |
| 27 |
) |
| 28 |
from traitsui.testing.tester.exceptions import ( |
| 29 |
InteractionNotSupported, |
| 30 |
LocationNotSupported, |
| 31 |
) |
| 32 |
from traitsui.testing.tester.target_registry import ( |
| 33 |
TargetRegistry, |
| 34 |
) |
| 35 |
from traitsui.testing.tester.ui_wrapper import ( |
| 36 |
UIWrapper, |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
def example_ui_wrapper(**kwargs): |
| 41 |
"""Return an instance of UIWrapper for testing purposes. |
| 42 |
|
| 43 |
Parameters |
| 44 |
---------- |
| 45 |
**kwargs |
| 46 |
Values to use instead of the default values. |
| 47 |
|
| 48 |
Returns |
| 49 |
------- |
| 50 |
wrapper: UIWrapper |
| 51 |
""" |
| 52 |
values = dict( |
| 53 |
target=None, |
| 54 |
registries=[], |
| 55 |
) |
| 56 |
values.update(kwargs) |
| 57 |
return UIWrapper(**values) |
| 58 |
|
| 59 |
|
| 60 |
class StubRegistry(AbstractTargetRegistry): |
| 61 |
"""A stub implementation of the AbstractTargetRegistry for testing""" |
| 62 |
|
| 63 |
def __init__( |
| 64 |
self, |
| 65 |
handler=None, |
| 66 |
solver=None, |
| 67 |
supported_interaction_classes=(), |
| 68 |
supported_locator_classes=(), |
| 69 |
interaction_doc="", |
| 70 |
location_doc="", |
| 71 |
): |
| 72 |
self.handler = handler |
| 73 |
self.solver = solver |
| 74 |
self.supported_interaction_classes = supported_interaction_classes |
| 75 |
self.supported_locator_classes = supported_locator_classes |
| 76 |
self.interaction_doc = interaction_doc |
| 77 |
self.location_doc = location_doc |
| 78 |
|
| 79 |
def _get_handler(self, target, interaction): |
| 80 |
if interaction.__class__ not in self.supported_interaction_classes: |
| 81 |
raise InteractionNotSupported( |
| 82 |
target_class=target.__class__, |
| 83 |
interaction_class=interaction.__class__, |
| 84 |
supported=list(self.supported_interaction_classes), |
| 85 |
) |
| 86 |
return self.handler |
| 87 |
|
| 88 |
def _get_interactions(self, target): |
| 89 |
return set(self.supported_interaction_classes) |
| 90 |
|
| 91 |
def _get_interaction_doc(self, target, interaction_class): |
| 92 |
return self.interaction_doc |
| 93 |
|
| 94 |
def _get_solver(self, target, location): |
| 95 |
if location.__class__ not in self.supported_locator_classes: |
| 96 |
raise LocationNotSupported( |
| 97 |
target_class=target.__class__, |
| 98 |
locator_class=location.__class__, |
| 99 |
supported=list(self.supported_locator_classes), |
| 100 |
) |
| 101 |
return self.solver |
| 102 |
|
| 103 |
def _get_locations(self, target): |
| 104 |
return set(self.supported_locator_classes) |
| 105 |
|
| 106 |
def _get_location_doc(self, target, locator_class): |
| 107 |
return self.location_doc |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
@requires_toolkit([ToolkitName.qt, ToolkitName.wx]) |
| 112 |
class TestUIWrapperInteractionRegistries(unittest.TestCase): |
| 113 |
"""Test the logic regarding the order of (interaction) registries.""" |
| 114 |
|
| 115 |
def test_registry_priority(self): |
| 116 |
|
| 117 |
|
| 118 |
registry1 = StubRegistry( |
| 119 |
handler=lambda w, l: 1, |
| 120 |
supported_interaction_classes=[str], |
| 121 |
) |
| 122 |
registry2 = StubRegistry( |
| 123 |
handler=lambda w, l: 2, |
| 124 |
supported_interaction_classes=[str], |
| 125 |
) |
| 126 |
|
| 127 |
wrapper = example_ui_wrapper( |
| 128 |
registries=[registry2, registry1], |
| 129 |
) |
| 130 |
value = wrapper.inspect("some string") |
| 131 |
|
| 132 |
self.assertEqual(value, 2) |
| 133 |
|
| 134 |
|
| 135 |
wrapper = example_ui_wrapper(registries=[registry1, registry2]) |
| 136 |
value = wrapper.inspect("some other string") |
| 137 |
|
| 138 |
self.assertEqual(value, 1) |
| 139 |
|
| 140 |
def test_registry_selection(self): |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
registry1 = StubRegistry() |
| 145 |
registry2_handler = mock.Mock() |
| 146 |
registry2 = StubRegistry( |
| 147 |
handler=registry2_handler, |
| 148 |
supported_interaction_classes=[int], |
| 149 |
) |
| 150 |
|
| 151 |
wrapper = example_ui_wrapper( |
| 152 |
registries=[registry1, registry2], |
| 153 |
) |
| 154 |
wrapper.perform(123) |
| 155 |
|
| 156 |
self.assertEqual(registry2_handler.call_count, 1) |
| 157 |
|
| 158 |
def test_registry_all_declined(self): |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
wrapper = example_ui_wrapper( |
| 164 |
registries=[ |
| 165 |
StubRegistry(supported_interaction_classes=[int]), |
| 166 |
StubRegistry(supported_interaction_classes=[float]), |
| 167 |
], |
| 168 |
) |
| 169 |
with self.assertRaises(InteractionNotSupported) as exception_context: |
| 170 |
wrapper.perform(None) |
| 171 |
|
| 172 |
self.assertCountEqual( |
| 173 |
exception_context.exception.supported, |
| 174 |
[int, float], |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@requires_toolkit([ToolkitName.qt, ToolkitName.wx]) |
| 180 |
class TestUIWrapperLocationRegistry(unittest.TestCase): |
| 181 |
"""Test the use of registries with locate.""" |
| 182 |
|
| 183 |
def test_location_registry_priority(self): |
| 184 |
registry1 = StubRegistry( |
| 185 |
solver=lambda w, l: 1, |
| 186 |
supported_locator_classes=[str], |
| 187 |
) |
| 188 |
registry2 = StubRegistry( |
| 189 |
solver=lambda w, l: 2, |
| 190 |
supported_locator_classes=[str], |
| 191 |
) |
| 192 |
|
| 193 |
wrapper = example_ui_wrapper( |
| 194 |
registries=[registry2, registry1], |
| 195 |
) |
| 196 |
new_wrapper = wrapper.locate("some string") |
| 197 |
|
| 198 |
self.assertEqual(new_wrapper._target, 2) |
| 199 |
|
| 200 |
|
| 201 |
wrapper = example_ui_wrapper( |
| 202 |
registries=[registry1, registry2], |
| 203 |
) |
| 204 |
new_wrapper = wrapper.locate("some other string") |
| 205 |
|
| 206 |
self.assertEqual(new_wrapper._target, 1) |
| 207 |
|
| 208 |
def test_location_registry_selection(self): |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
def solver2(wrapper, location): |
| 213 |
return 2 |
| 214 |
|
| 215 |
registry1 = StubRegistry() |
| 216 |
registry2 = StubRegistry( |
| 217 |
solver=solver2, |
| 218 |
supported_locator_classes=[str], |
| 219 |
) |
| 220 |
|
| 221 |
wrapper = example_ui_wrapper( |
| 222 |
registries=[registry1, registry2], |
| 223 |
) |
| 224 |
new_wrapper = wrapper.locate("some string") |
| 225 |
|
| 226 |
self.assertEqual(new_wrapper._target, 2) |
| 227 |
self.assertEqual( |
| 228 |
new_wrapper._registries, |
| 229 |
wrapper._registries, |
| 230 |
) |
| 231 |
|
| 232 |
def test_registry_all_declined(self): |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
wrapper = example_ui_wrapper( |
| 238 |
registries=[ |
| 239 |
StubRegistry(supported_locator_classes=[int]), |
| 240 |
StubRegistry(supported_locator_classes=[float]), |
| 241 |
], |
| 242 |
) |
| 243 |
with self.assertRaises(LocationNotSupported) as exception_context: |
| 244 |
wrapper.locate(None) |
| 245 |
|
| 246 |
self.assertCountEqual( |
| 247 |
exception_context.exception.supported, |
| 248 |
[int, float], |
| 249 |
) |
| 250 |
|
| 251 |
|
| 252 |
class TestUIWrapperHelp(unittest.TestCase): |
| 253 |
"""Test calling UIWrapper.help""" |
| 254 |
|
| 255 |
def test_help_message(self): |
| 256 |
class Action: |
| 257 |
"""Say hello. |
| 258 |
Say bye. |
| 259 |
""" |
| 260 |
|
| 261 |
pass |
| 262 |
|
| 263 |
class Locator: |
| 264 |
"""Return anything you like. |
| 265 |
Good day! |
| 266 |
""" |
| 267 |
|
| 268 |
pass |
| 269 |
|
| 270 |
registry1 = TargetRegistry() |
| 271 |
registry1.register_interaction( |
| 272 |
target_class=str, |
| 273 |
interaction_class=Action, |
| 274 |
handler=mock.Mock(), |
| 275 |
) |
| 276 |
registry2 = TargetRegistry() |
| 277 |
registry2.register_location( |
| 278 |
target_class=str, |
| 279 |
locator_class=Locator, |
| 280 |
solver=mock.Mock(), |
| 281 |
) |
| 282 |
|
| 283 |
wrapper = example_ui_wrapper( |
| 284 |
target="dummy", registries=[registry1, registry2] |
| 285 |
) |
| 286 |
|
| 287 |
|
| 288 |
stream = io.StringIO() |
| 289 |
with mock.patch("sys.stdout", stream): |
| 290 |
wrapper.help() |
| 291 |
|
| 292 |
|
| 293 |
self.assertEqual( |
| 294 |
stream.getvalue(), |
| 295 |
textwrap.dedent( |
| 296 |
f"""\ |
| 297 |
Interactions |
| 298 |
------------ |
| 299 |
{Action!r} |
| 300 |
Say hello. |
| 301 |
Say bye. |
| 302 |
|
| 303 |
Locations |
| 304 |
--------- |
| 305 |
{Locator!r} |
| 306 |
Return anything you like. |
| 307 |
Good day! |
| 308 |
|
| 309 |
""" |
| 310 |
), |
| 311 |
) |
| 312 |
|
| 313 |
def test_help_message_priority_interactions(self): |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
high_priority_registry = StubRegistry( |
| 318 |
supported_locator_classes=[str], |
| 319 |
supported_interaction_classes=[float], |
| 320 |
interaction_doc="Interaction: I get a higher priority.", |
| 321 |
location_doc="Location: I get a higher priority.", |
| 322 |
) |
| 323 |
|
| 324 |
low_priority_registry = StubRegistry( |
| 325 |
supported_locator_classes=[str], |
| 326 |
supported_interaction_classes=[float], |
| 327 |
interaction_doc="Interaction: I get a lower priority.", |
| 328 |
location_doc="Location: I get a lower priority.", |
| 329 |
) |
| 330 |
|
| 331 |
|
| 332 |
wrapper = example_ui_wrapper( |
| 333 |
target="dummy", |
| 334 |
registries=[high_priority_registry, low_priority_registry], |
| 335 |
) |
| 336 |
|
| 337 |
|
| 338 |
stream = io.StringIO() |
| 339 |
with mock.patch("sys.stdout", stream): |
| 340 |
wrapper.help() |
| 341 |
|
| 342 |
|
| 343 |
self.assertEqual( |
| 344 |
stream.getvalue(), |
| 345 |
textwrap.dedent( |
| 346 |
f"""\ |
| 347 |
Interactions |
| 348 |
------------ |
| 349 |
{float!r} |
| 350 |
Interaction: I get a higher priority. |
| 351 |
|
| 352 |
Locations |
| 353 |
--------- |
| 354 |
{str!r} |
| 355 |
Location: I get a higher priority. |
| 356 |
|
| 357 |
""" |
| 358 |
), |
| 359 |
) |
| 360 |
|
| 361 |
def test_help_message_nothing_is_supported(self): |
| 362 |
registry = TargetRegistry() |
| 363 |
wrapper = example_ui_wrapper(registries=[registry]) |
| 364 |
|
| 365 |
|
| 366 |
stream = io.StringIO() |
| 367 |
with mock.patch("sys.stdout", stream): |
| 368 |
wrapper.help() |
| 369 |
|
| 370 |
|
| 371 |
self.assertEqual( |
| 372 |
stream.getvalue(), |
| 373 |
textwrap.dedent( |
| 374 |
"""\ |
| 375 |
Interactions |
| 376 |
------------ |
| 377 |
No interactions are supported. |
| 378 |
|
| 379 |
Locations |
| 380 |
--------- |
| 381 |
No locations are supported. |
| 382 |
|
| 383 |
""" |
| 384 |
), |
| 385 |
) |
| 386 |
|
| 387 |
|
| 388 |
class NumberHasTraits(HasTraits): |
| 389 |
number = Int() |
| 390 |
|
| 391 |
|
| 392 |
@requires_toolkit([ToolkitName.qt, ToolkitName.wx]) |
| 393 |
class TestUIWrapperEventProcessed(unittest.TestCase, UnittestTools): |
| 394 |
"""Test GUI events are processed and exceptions from the GUI event |
| 395 |
loop are handled. |
| 396 |
""" |
| 397 |
|
| 398 |
def test_event_processed(self): |
| 399 |
|
| 400 |
gui = GUI() |
| 401 |
model = NumberHasTraits() |
| 402 |
|
| 403 |
def handler(wrapper, action): |
| 404 |
gui.set_trait_later(model, "number", 2) |
| 405 |
|
| 406 |
wrapper = example_ui_wrapper( |
| 407 |
registries=[ |
| 408 |
StubRegistry( |
| 409 |
handler=handler, |
| 410 |
supported_interaction_classes=[float], |
| 411 |
), |
| 412 |
], |
| 413 |
) |
| 414 |
|
| 415 |
with self.assertTraitChanges(model, "number"): |
| 416 |
wrapper.perform(2.123) |
| 417 |
|
| 418 |
def test_event_processed_prior_to_resolving_location(self): |
| 419 |
|
| 420 |
gui = GUI() |
| 421 |
model = NumberHasTraits() |
| 422 |
gui.set_trait_later(model, "number", 2) |
| 423 |
|
| 424 |
def solver(wrapper, location): |
| 425 |
return model.number |
| 426 |
|
| 427 |
wrapper = example_ui_wrapper( |
| 428 |
registries=[ |
| 429 |
StubRegistry( |
| 430 |
solver=solver, |
| 431 |
supported_locator_classes=[float], |
| 432 |
) |
| 433 |
], |
| 434 |
) |
| 435 |
|
| 436 |
new_wrapper = wrapper.locate(4.567) |
| 437 |
self.assertEqual(new_wrapper._target, 2) |
| 438 |
|
| 439 |
def test_event_processed_with_exception_captured(self): |
| 440 |
|
| 441 |
|
| 442 |
gui = GUI() |
| 443 |
|
| 444 |
def raise_error(): |
| 445 |
raise ZeroDivisionError() |
| 446 |
|
| 447 |
def handler(wrapper, action): |
| 448 |
gui.invoke_later(raise_error) |
| 449 |
|
| 450 |
wrapper = example_ui_wrapper( |
| 451 |
registries=[ |
| 452 |
StubRegistry( |
| 453 |
handler=handler, |
| 454 |
supported_interaction_classes=[float], |
| 455 |
), |
| 456 |
], |
| 457 |
) |
| 458 |
with self.assertRaises(RuntimeError), self.assertLogs("traitsui"): |
| 459 |
wrapper.perform(1.234) |
| 460 |
|
| 461 |
def test_exception_not_in_gui(self): |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
def handler(wrapper, action): |
| 466 |
raise ZeroDivisionError() |
| 467 |
|
| 468 |
wrapper = example_ui_wrapper( |
| 469 |
registries=[ |
| 470 |
StubRegistry( |
| 471 |
handler=handler, |
| 472 |
supported_interaction_classes=[float], |
| 473 |
), |
| 474 |
], |
| 475 |
) |
| 476 |
|
| 477 |
with self.assertRaises(ZeroDivisionError): |
| 478 |
wrapper.perform(9.99) |
| 479 |
|
| 480 |
def test_perform_event_processed_optional(self): |
| 481 |
|
| 482 |
gui = GUI() |
| 483 |
side_effect = mock.Mock() |
| 484 |
|
| 485 |
def handler(wrapper, action): |
| 486 |
gui.invoke_later(side_effect) |
| 487 |
|
| 488 |
wrapper = example_ui_wrapper( |
| 489 |
registries=[ |
| 490 |
StubRegistry( |
| 491 |
handler=handler, |
| 492 |
supported_interaction_classes=[float], |
| 493 |
), |
| 494 |
], |
| 495 |
auto_process_events=False, |
| 496 |
) |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
wrapper.perform(12.345) |
| 501 |
self.addCleanup(process_cascade_events) |
| 502 |
|
| 503 |
self.assertEqual(side_effect.call_count, 0) |
| 504 |
|
| 505 |
def test_locate_event_processed_optional(self): |
| 506 |
|
| 507 |
gui = GUI() |
| 508 |
side_effect = mock.Mock() |
| 509 |
gui.invoke_later(side_effect) |
| 510 |
self.addCleanup(process_cascade_events) |
| 511 |
|
| 512 |
def solver(wrapper, location): |
| 513 |
return 1 |
| 514 |
|
| 515 |
wrapper = example_ui_wrapper( |
| 516 |
registries=[ |
| 517 |
StubRegistry( |
| 518 |
solver=solver, |
| 519 |
supported_locator_classes=[float], |
| 520 |
), |
| 521 |
], |
| 522 |
auto_process_events=False, |
| 523 |
) |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
new_wrapper = wrapper.locate(1.234) |
| 528 |
|
| 529 |
self.assertEqual(side_effect.call_count, 0) |
| 530 |
self.assertFalse(new_wrapper._auto_process_events) |