test_timer.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_timer_on_deleted_container(screen: Screen):
  58. state = {'count': 0}
  59. with ui.row() as outer_container:
  60. with ui.row():
  61. ui.timer(0.1, lambda: state.update(count=state['count'] + 1))
  62. ui.button('delete', on_click=outer_container.clear)
  63. screen.open('/')
  64. screen.click('delete')
  65. screen.wait(0.5)
  66. count = state['count']
  67. screen.wait(0.5)
  68. assert state['count'] == count, 'timer is not running anymore after deleting the container'
  69. def test_different_callbacks(screen: Screen):
  70. def sync_function():
  71. ui.label('a synchronous function')
  72. async def async_function():
  73. await asyncio.sleep(0.1)
  74. ui.label('an asynchronous function')
  75. async def async_lambda(msg: str):
  76. await asyncio.sleep(0.1)
  77. ui.label(f'an asynchronous lambda: {msg}')
  78. ui.timer(0.1, sync_function, once=True)
  79. ui.timer(0.1, async_function, once=True)
  80. ui.timer(0.1, lambda: ui.label('a synchronous lambda'), once=True)
  81. ui.timer(0.1, lambda: async_lambda('Hi!'), once=True)
  82. screen.open('/')
  83. screen.should_contain('a synchronous function')
  84. screen.should_contain('an asynchronous function')
  85. screen.should_contain('a synchronous lambda')
  86. screen.should_contain('an asynchronous lambda: Hi!')