test_scene.py 4.8 KB

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