test_timer.py 3.5 KB

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