/kivy/tests/test_uix_modal.py

https://github.com/akshayaurora/kivy · Python · 101 lines · 69 code · 16 blank · 16 comment · 6 complexity · c20f9561177096b32f055f7d05271939 MD5 · raw file

  1. """ modal view unit tests. """
  2. from kivy.tests import async_run, UnitKivyApp
  3. from math import isclose
  4. def modal_app():
  5. """ test app factory function. """
  6. from kivy.app import App
  7. from kivy.uix.button import Button
  8. from kivy.uix.modalview import ModalView
  9. # noinspection PyProtectedMember
  10. class ModalButton(Button):
  11. """ button used as root widget to test touch. """
  12. modal = None
  13. def on_touch_down(self, touch):
  14. """ touch down event handler. """
  15. assert self.modal._window is None
  16. assert not self.modal._is_open
  17. return super(ModalButton, self).on_touch_down(touch)
  18. def on_touch_move(self, touch):
  19. """ touch move event handler. """
  20. assert self.modal._window is None
  21. assert not self.modal._is_open
  22. return super(ModalButton, self).on_touch_move(touch)
  23. def on_touch_up(self, touch):
  24. """ touch up event handler. """
  25. assert self.modal._window is None
  26. assert not self.modal._is_open
  27. return super(ModalButton, self).on_touch_up(touch)
  28. class TestApp(UnitKivyApp, App):
  29. """ test app class. """
  30. def build(self):
  31. """ build root layout. """
  32. root = ModalButton()
  33. root.modal = ModalView(size_hint=(.2, .5))
  34. return root
  35. return TestApp()
  36. @async_run(app_cls_func=modal_app)
  37. async def test_modal_app(kivy_app):
  38. await kivy_app.wait_clock_frames(2)
  39. button = kivy_app.root
  40. modal = button.modal
  41. modal._anim_duration = 0
  42. assert modal._window is None
  43. assert not modal._is_open
  44. # just press button
  45. async for _ in kivy_app.do_touch_down_up(widget=button):
  46. assert modal._window is None
  47. assert not modal._is_open
  48. async for _ in kivy_app.do_touch_drag(widget=button, dx=button.width / 4):
  49. assert modal._window is None
  50. assert not modal._is_open
  51. # open modal
  52. modal.open()
  53. await kivy_app.wait_clock_frames(2)
  54. assert modal._window is not None
  55. assert modal._is_open
  56. assert isclose(modal.center_x, button.center_x, abs_tol=.1)
  57. assert isclose(modal.center_y, button.center_y, abs_tol=.1)
  58. # press within modal area - should stay open
  59. async for _ in kivy_app.do_touch_down_up(widget=button):
  60. pass
  61. assert modal._window is not None
  62. assert modal._is_open
  63. # start in modal but release outside - should stay open
  64. async for _ in kivy_app.do_touch_drag(widget=button, dx=button.width / 4):
  65. pass
  66. assert modal._window is not None
  67. assert modal._is_open
  68. # start outside but release in modal - should close
  69. async for _ in kivy_app.do_touch_drag(
  70. pos=(button.center_x + button.width / 4, button.center_y),
  71. target_widget=button):
  72. pass
  73. assert modal._window is None
  74. assert not modal._is_open
  75. # open modal again
  76. modal.open()
  77. await kivy_app.wait_clock_frames(2)
  78. assert modal._window is not None
  79. assert modal._is_open
  80. # press outside modal area - should close
  81. async for _ in kivy_app.do_touch_down_up(
  82. pos=(button.center_x + button.width / 4, button.center_y)):
  83. pass
  84. assert modal._window is None
  85. assert not modal._is_open