test_timer.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import asyncio
  2. import warnings
  3. import pytest
  4. from nicegui import ui
  5. from .screen import Screen
  6. class Counter:
  7. value = 0
  8. def increment(self):
  9. self.value += 1
  10. def test_timer(screen: Screen):
  11. counter = Counter()
  12. t = ui.timer(0.1, counter.increment)
  13. assert counter.value == 0, 'count is initially zero'
  14. screen.wait(0.5)
  15. assert counter.value == 0, 'timer is not running'
  16. screen.start_server()
  17. screen.wait(0.5)
  18. assert counter.value > 0, 'timer is running after starting the server'
  19. t.deactivate()
  20. screen.wait(0.5)
  21. c = counter.value
  22. screen.wait(0.5)
  23. assert counter.value == c, 'timer is not running anymore after deactivating it'
  24. t.activate()
  25. screen.wait(0.5)
  26. assert counter.value > c, 'timer is running again after activating it'
  27. t.cancel()
  28. screen.wait(0.5)
  29. c = counter.value
  30. screen.wait(0.5)
  31. assert counter.value == c, 'timer is not running anymore after canceling it'
  32. def test_timer_on_private_page(screen: Screen):
  33. counter = Counter()
  34. @ui.page('/', reconnect_timeout=0)
  35. def page():
  36. ui.timer(0.1, counter.increment)
  37. assert counter.value == 0, 'count is initially zero'
  38. screen.start_server()
  39. screen.wait(0.5)
  40. assert counter.value == 0, 'timer is not running even after starting the server'
  41. screen.open('/')
  42. screen.wait(0.5)
  43. assert counter.value > 0, 'timer is running after opening the page'
  44. screen.close()
  45. count = counter.value
  46. screen.wait(0.5)
  47. assert counter.value == count, 'timer is not running anymore after closing the page'
  48. @pytest.mark.parametrize('once', [True, False])
  49. def test_setting_visibility(screen: Screen, once: bool):
  50. """reproduction of https://github.com/zauberzeug/nicegui/issues/206"""
  51. @ui.page('/')
  52. def page():
  53. label = ui.label('Some Label')
  54. ui.timer(0.1, lambda: label.set_visibility(False), once=once)
  55. screen.open('/')
  56. screen.wait(0.5)
  57. screen.should_not_contain('Some Label')
  58. def test_awaiting_coroutine(screen: Screen):
  59. warnings.simplefilter('error')
  60. async def update_user():
  61. await asyncio.sleep(0.1)
  62. ui.timer(1, update_user)
  63. screen.open('/')
  64. screen.wait(1)
  65. def test_timer_on_deleted_container(screen: Screen):
  66. state = {'count': 0}
  67. with ui.row() as outer_container:
  68. with ui.row():
  69. ui.timer(0.1, lambda: state.update(count=state['count'] + 1))
  70. ui.button('delete', on_click=outer_container.clear)
  71. screen.open('/')
  72. screen.click('delete')
  73. screen.wait(0.5)
  74. count = state['count']
  75. screen.wait(0.5)
  76. assert state['count'] == count, 'timer is not running anymore after deleting the container'