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.

77 lines
2.5 KiB

3 years ago
3 years ago
  1. """
  2. This weirdness exists to work around a very specific problem
  3. with testing WX: you can only ever have one App() instance per
  4. process. I've spent hours and hours trying to work around this and
  5. figure out how to gracefully destroy and recreate them, but... no dice.
  6. This is echo'd in the docs: https://wxpython.org/Phoenix/docs/html/wx.App.html
  7. Destroying/recreating causes instability in the tests. We can work around that
  8. by reusing the same App instance across tests and only destroying the top level
  9. frame (which is fine). However, this causes a new problem: the last test which
  10. runs will now always fail, cause we're not Destroying the App instance.
  11. Ideal world: UnitTest would expose a global "done" hook regardless of test
  12. discovery type. That doesn't exist, so the best we can do is use the Module cleanup
  13. methods. These aren't perfect, but destroying / recreating at the module boundary
  14. gives slightly more reliable tests. These are picked up by the test runner
  15. by their existence in the module's globals(). There's no other way to hook
  16. things together. We need it in every test, and thus... that's the background
  17. for why this weirdness is going on.
  18. It's a hack around a hack around a problem in Wx.
  19. Usage:
  20. In any tests which use WX, you must import this module's definitions
  21. into the test's global scope
  22. ```
  23. from gooey.tests import *
  24. ```
  25. """
  26. import wx
  27. import locale
  28. import platform
  29. class TestApp(wx.App):
  30. """
  31. Stolen from the mailing list here:
  32. https://groups.google.com/g/wxpython-users/c/q5DSyyuKluA
  33. Wx started randomly exploding with locale issues while running
  34. the tests. For whatever reason, manually setting it in InitLocale
  35. seems to solve the problem.
  36. """
  37. def __init__(self, with_c_locale=None, **kws):
  38. if with_c_locale is None:
  39. with_c_locale = (platform.system() == 'Windows')
  40. self.with_c_locale = with_c_locale
  41. wx.App.__init__(self, **kws)
  42. def InitLocale(self):
  43. """over-ride wxPython default initial locale"""
  44. if self.with_c_locale:
  45. self._initial_locale = None
  46. locale.setlocale(locale.LC_ALL, 'C')
  47. else:
  48. lang, enc = locale.getdefaultlocale()
  49. self._initial_locale = wx.Locale(lang, lang[:2], lang)
  50. locale.setlocale(locale.LC_ALL, lang)
  51. def OnInit(self):
  52. self.createApp()
  53. return True
  54. def createApp(self):
  55. return True
  56. app = None
  57. def setUpModule():
  58. global app
  59. app = TestApp()
  60. def tearDownModule():
  61. global app
  62. app.Destroy()