test_scene.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import numpy as np
  2. from selenium.common.exceptions import JavascriptException
  3. from nicegui import ui
  4. from nicegui.elements.scene_object3d import Object3D
  5. from nicegui.testing import Screen
  6. def test_moving_sphere_with_timer(screen: Screen):
  7. with ui.scene() as scene:
  8. sphere = scene.sphere().with_name('sphere')
  9. ui.timer(0.1, lambda: sphere.move(0, 0, sphere.z + 0.01))
  10. screen.open('/')
  11. def position() -> float:
  12. for _ in range(3):
  13. try:
  14. pos = screen.selenium.execute_script(f'return scene_c{scene.id}.getObjectByName("sphere").position.z')
  15. if pos is not None:
  16. return pos
  17. except JavascriptException as e:
  18. print(e.msg, flush=True)
  19. screen.wait(1.0)
  20. raise RuntimeError('Could not get position')
  21. screen.wait(0.2)
  22. assert position() > 0
  23. def test_no_object_duplication_on_index_client(screen: Screen):
  24. with ui.scene() as scene:
  25. sphere = scene.sphere().move(0, -4, 0)
  26. ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
  27. screen.open('/')
  28. screen.wait(0.4)
  29. screen.switch_to(1)
  30. screen.open('/')
  31. screen.switch_to(0)
  32. screen.wait(0.2)
  33. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
  34. def test_no_object_duplication_with_page_builder(screen: Screen):
  35. scene: ui.scene
  36. @ui.page('/')
  37. def page():
  38. nonlocal scene
  39. with ui.scene() as scene:
  40. sphere = scene.sphere().move(0, -4, 0)
  41. ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
  42. screen.open('/')
  43. screen.wait(0.4)
  44. screen.switch_to(1)
  45. screen.open('/')
  46. screen.switch_to(0)
  47. screen.wait(0.2)
  48. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
  49. screen.switch_to(1)
  50. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
  51. def test_deleting_group(screen: Screen):
  52. with ui.scene() as scene:
  53. with scene.group() as group:
  54. scene.sphere()
  55. ui.button('Delete group', on_click=group.delete)
  56. screen.open('/')
  57. screen.wait(0.5)
  58. assert len(scene.objects) == 2
  59. screen.click('Delete group')
  60. screen.wait(0.5)
  61. assert len(scene.objects) == 0
  62. def test_replace_scene(screen: Screen):
  63. with ui.row() as container:
  64. with ui.scene() as scene:
  65. scene.sphere().with_name('sphere')
  66. def replace():
  67. container.clear()
  68. with container:
  69. nonlocal scene
  70. with ui.scene() as scene:
  71. scene.box().with_name('box')
  72. ui.button('Replace scene', on_click=replace)
  73. screen.open('/')
  74. screen.wait(0.5)
  75. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'sphere'
  76. screen.click('Replace scene')
  77. screen.wait(0.5)
  78. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  79. def test_create_dynamically(screen: Screen):
  80. ui.button('Create', on_click=ui.scene)
  81. screen.open('/')
  82. screen.click('Create')
  83. assert screen.find_by_tag('canvas')
  84. def test_rotation_matrix_from_euler():
  85. omega, phi, kappa = 0.1, 0.2, 0.3
  86. Rx = np.array([[1, 0, 0], [0, np.cos(omega), -np.sin(omega)], [0, np.sin(omega), np.cos(omega)]])
  87. Ry = np.array([[np.cos(phi), 0, np.sin(phi)], [0, 1, 0], [-np.sin(phi), 0, np.cos(phi)]])
  88. Rz = np.array([[np.cos(kappa), -np.sin(kappa), 0], [np.sin(kappa), np.cos(kappa), 0], [0, 0, 1]])
  89. R = Rz @ Ry @ Rx
  90. assert np.allclose(Object3D.rotation_matrix_from_euler(omega, phi, kappa), R)
  91. def test_object_creation_via_context(screen: Screen):
  92. with ui.scene() as scene:
  93. scene.box().with_name('box')
  94. screen.open('/')
  95. screen.wait(0.5)
  96. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  97. def test_object_creation_via_attribute(screen: Screen):
  98. scene = ui.scene()
  99. scene.box().with_name('box')
  100. screen.open('/')
  101. screen.wait(0.5)
  102. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  103. def test_clearing_scene(screen: Screen):
  104. with ui.scene() as scene:
  105. scene.box().with_name('box')
  106. scene.box().with_name('box2')
  107. ui.button('Clear', on_click=scene.clear)
  108. screen.open('/')
  109. screen.wait(0.5)
  110. assert len(scene.objects) == 2
  111. screen.click('Clear')
  112. screen.wait(0.5)
  113. assert len(scene.objects) == 0