test_timer.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. 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. def test_timer_on_private_page(screen: Screen):
  20. counter = Counter()
  21. @ui.page('/')
  22. def page():
  23. ui.timer(0.1, counter.increment)
  24. assert counter.value == 0, 'count is initially zero'
  25. screen.start_server()
  26. screen.wait(0.5)
  27. assert counter.value == 0, 'timer is not running even after starting the server'
  28. screen.open('/')
  29. screen.wait(0.5)
  30. assert counter.value > 0, 'timer is running after opening the page'
  31. screen.close()
  32. count = counter.value
  33. screen.wait(0.5)
  34. assert counter.value == count, 'timer is not running anymore after closing the page'
  35. @pytest.mark.parametrize('once', [True, False])
  36. def test_setting_visibility(screen: Screen, once: bool):
  37. '''reproduction of https://github.com/zauberzeug/nicegui/issues/206'''
  38. @ui.page('/')
  39. def page():
  40. label = ui.label('Some Label')
  41. ui.timer(0.1, lambda: label.set_visibility(False), once=once)
  42. screen.open('/')
  43. screen.wait(0.5)
  44. screen.should_not_contain('Some Label')
  45. def test_awaiting_coroutine(screen: Screen):
  46. warnings.simplefilter('error')
  47. async def update_user():
  48. await asyncio.sleep(0.1)
  49. ui.timer(1, lambda: update_user())
  50. screen.open('/')
  51. screen.wait(1)