test_scene.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 .screen 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() -> None:
  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 Exception('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. @ui.page('/')
  36. def page():
  37. global scene
  38. with ui.scene() as scene:
  39. sphere = scene.sphere().move(0, -4, 0)
  40. ui.timer(0.1, lambda: sphere.move(0, sphere.y + 0.5, 0))
  41. screen.open('/')
  42. screen.wait(0.4)
  43. screen.switch_to(1)
  44. screen.open('/')
  45. screen.switch_to(0)
  46. screen.wait(0.2)
  47. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
  48. screen.switch_to(1)
  49. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children.length') == 5
  50. def test_deleting_group(screen: Screen):
  51. with ui.scene() as scene:
  52. with scene.group() as group:
  53. scene.sphere()
  54. ui.button('Delete group', on_click=group.delete)
  55. screen.open('/')
  56. screen.wait(0.5)
  57. assert len(scene.objects) == 2
  58. screen.click('Delete group')
  59. screen.wait(0.5)
  60. assert len(scene.objects) == 0
  61. def test_replace_scene(screen: Screen):
  62. with ui.row() as container:
  63. with ui.scene() as scene:
  64. scene.sphere().with_name('sphere')
  65. def replace():
  66. container.clear()
  67. with container:
  68. nonlocal scene
  69. with ui.scene() as scene:
  70. scene.box().with_name('box')
  71. ui.button('Replace scene', on_click=replace)
  72. screen.open('/')
  73. screen.wait(0.5)
  74. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'sphere'
  75. screen.click('Replace scene')
  76. screen.wait(0.5)
  77. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  78. def test_create_dynamically(screen: Screen):
  79. ui.button('Create', on_click=lambda: ui.scene())
  80. screen.open('/')
  81. screen.click('Create')
  82. assert screen.find_by_tag('canvas')
  83. def test_rotation_matrix_from_euler():
  84. omega, phi, kappa = 0.1, 0.2, 0.3
  85. Rx = np.array([[1, 0, 0], [0, np.cos(omega), -np.sin(omega)], [0, np.sin(omega), np.cos(omega)]])
  86. Ry = np.array([[np.cos(phi), 0, np.sin(phi)], [0, 1, 0], [-np.sin(phi), 0, np.cos(phi)]])
  87. Rz = np.array([[np.cos(kappa), -np.sin(kappa), 0], [np.sin(kappa), np.cos(kappa), 0], [0, 0, 1]])
  88. R = Rz @ Ry @ Rx
  89. assert np.allclose(Object3D.rotation_matrix_from_euler(omega, phi, kappa), R)
  90. def test_object_creation_via_context(screen: Screen):
  91. with ui.scene() as scene:
  92. scene.box().with_name('box')
  93. screen.open('/')
  94. screen.wait(0.5)
  95. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  96. def test_object_creation_via_attribute(screen: Screen):
  97. scene = ui.scene()
  98. scene.box().with_name('box')
  99. screen.open('/')
  100. screen.wait(0.5)
  101. assert screen.selenium.execute_script(f'return scene_c{scene.id}.children[4].name') == 'box'
  102. def test_clearing_scene(screen: Screen):
  103. with ui.scene() as scene:
  104. scene.box().with_name('box')
  105. scene.box().with_name('box2')
  106. ui.button('Clear', on_click=scene.clear)
  107. screen.open('/')
  108. screen.wait(0.5)
  109. assert len(scene.objects) == 2
  110. screen.click('Clear')
  111. screen.wait(0.5)
  112. assert len(scene.objects) == 0