test_timer.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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('/')
  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, lambda: update_user())
  63. screen.open('/')
  64. screen.wait(1)