You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from contextlib import contextmanager
  2. import time
  3. from threading import Thread
  4. from typing import Tuple
  5. import wx
  6. from gooey.gui import bootstrap
  7. from gooey.python_bindings.config_generator import create_from_parser
  8. from gooey.python_bindings.parameters import gooey_params
  9. from gooey.util.functional import merge
  10. from gooey.gui.application.application import RGooey
  11. @contextmanager
  12. def instrumentGooey(parser, **kwargs) -> Tuple[wx.App, wx.Frame, RGooey]:
  13. """
  14. Context manager used during testing for setup/tear down of the
  15. WX infrastructure during subTests.
  16. Weirdness warning: this uses a globally reused wx.App instance.
  17. """
  18. from gooey.tests import app
  19. if app == None:
  20. raise Exception("App instance has not been created! This is likely due to "
  21. "you forgetting to add the magical import which makes all these "
  22. "tests work. See the module doc in gooey.tests.__init__ for guidance")
  23. buildspec = create_from_parser(parser, "", **gooey_params(**kwargs))
  24. app, frame = bootstrap._build_app(buildspec, app)
  25. app.SetTopWindow(frame)
  26. try:
  27. # we need to run the main loop temporarily to get it to
  28. # apply any pending updates from the initial creation.
  29. # The UI state will be stale otherwise
  30. # this works because CallLater just enqueues the message to
  31. # be processed. The MainLoop starts running, picks it up, and
  32. # then exists
  33. wx.CallLater(1, app.ExitMainLoop)
  34. app.MainLoop()
  35. yield (app, frame, frame._instance)
  36. finally:
  37. frame.Destroy()
  38. del frame