TestObjectsModel.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import pytest
  2. import copy
  3. from unittest.mock import patch, MagicMock
  4. from UM.Scene.GroupDecorator import GroupDecorator
  5. from UM.Scene.SceneNode import SceneNode
  6. from cura.Scene.BuildPlateDecorator import BuildPlateDecorator
  7. from cura.Scene.SliceableObjectDecorator import SliceableObjectDecorator
  8. from cura.UI.ObjectsModel import ObjectsModel, _NodeInfo
  9. @pytest.fixture()
  10. def objects_model(application):
  11. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  12. return ObjectsModel()
  13. @pytest.fixture()
  14. def group_scene_node():
  15. node = SceneNode()
  16. node.addDecorator(GroupDecorator())
  17. return node
  18. @pytest.fixture()
  19. def slicable_scene_node():
  20. node = SceneNode()
  21. node.addDecorator(SliceableObjectDecorator())
  22. return node
  23. @pytest.fixture()
  24. def application_with_mocked_scene(application):
  25. mocked_controller = MagicMock(name = "Controller")
  26. mocked_scene = MagicMock(name = "Scene")
  27. mocked_controller.getScene = MagicMock(return_value = mocked_scene)
  28. application.getController = MagicMock(return_value = mocked_controller)
  29. return application
  30. def test_setActiveBuildPlate(objects_model):
  31. objects_model._update = MagicMock()
  32. objects_model.setActiveBuildPlate(12)
  33. assert objects_model._update.call_count == 1
  34. objects_model.setActiveBuildPlate(12)
  35. assert objects_model._update.call_count == 1
  36. class Test_shouldNodeBeHandled:
  37. def test_nonSlicableSceneNode(self, objects_model):
  38. # An empty SceneNode should not be handled by this model
  39. assert not objects_model._shouldNodeBeHandled(SceneNode())
  40. def test_groupedNode(self, objects_model, slicable_scene_node, application):
  41. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  42. # A node without a build plate number should not be handled.
  43. assert not objects_model._shouldNodeBeHandled(slicable_scene_node)
  44. def test_childNode(self, objects_model, group_scene_node, slicable_scene_node, application):
  45. slicable_scene_node.setParent(group_scene_node)
  46. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  47. # A child node of a group node should not be handled.
  48. assert not objects_model._shouldNodeBeHandled(slicable_scene_node)
  49. def test_slicableNodeWithoutFiltering(self, objects_model, slicable_scene_node, application):
  50. mocked_preferences = MagicMock(name="preferences")
  51. mocked_preferences.getValue = MagicMock(return_value = False)
  52. application.getPreferences = MagicMock(return_value = mocked_preferences)
  53. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  54. # A slicable node should be handled by this model.
  55. assert objects_model._shouldNodeBeHandled(slicable_scene_node)
  56. def test_slicableNodeWithFiltering(self, objects_model, slicable_scene_node, application):
  57. mocked_preferences = MagicMock(name="preferences")
  58. mocked_preferences.getValue = MagicMock(return_value = True)
  59. application.getPreferences = MagicMock(return_value = mocked_preferences)
  60. buildplate_decorator = BuildPlateDecorator()
  61. buildplate_decorator.setBuildPlateNumber(-1)
  62. slicable_scene_node.addDecorator(buildplate_decorator)
  63. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  64. # A slicable node with the same buildplate number should be handled.
  65. assert objects_model._shouldNodeBeHandled(slicable_scene_node)
  66. class Test_renameNodes:
  67. def test_emptyDict(self, objects_model):
  68. assert objects_model._renameNodes({}) == []
  69. def test_singleItemNoRename(self, objects_model):
  70. node = SceneNode()
  71. assert objects_model._renameNodes({"zomg": _NodeInfo(index_to_node={1: node})}) == [node]
  72. def test_singleItemRename(self, objects_model):
  73. node = SceneNode()
  74. result = objects_model._renameNodes({"zomg": _NodeInfo(nodes_to_rename=[node])})
  75. assert result == [node]
  76. assert node.getName() == "zomg(1)"
  77. def test_singleItemRenameWithIndex(self, objects_model):
  78. node = SceneNode()
  79. objects_model._renameNodes({"zomg": _NodeInfo(index_to_node = {1: node}, nodes_to_rename=[node])})
  80. assert node.getName() == "zomg(2)"
  81. def test_multipleItemsRename(self, objects_model):
  82. node1 = SceneNode()
  83. node2 = SceneNode()
  84. result = objects_model._renameNodes({"zomg": _NodeInfo(nodes_to_rename=[node1, node2])})
  85. assert result == [node1, node2]
  86. assert node1.getName() == "zomg(1)"
  87. assert node2.getName() == "zomg(2)"
  88. def test_renameGroup(self, objects_model, group_scene_node):
  89. result = objects_model._renameNodes({"zomg": _NodeInfo(nodes_to_rename=[group_scene_node], is_group=True)})
  90. assert result == [group_scene_node]
  91. assert group_scene_node.getName() == "zomg#1"
  92. class Test_Update:
  93. def test_updateWithGroup(self, objects_model, application_with_mocked_scene, group_scene_node):
  94. objects_model._shouldNodeBeHandled = MagicMock(return_value = True)
  95. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value = group_scene_node)
  96. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  97. objects_model._update()
  98. assert objects_model.items == [{
  99. 'name': 'Group #1',
  100. 'selected': False,
  101. 'outside_build_area': False,
  102. 'buildplate_number': None,
  103. 'node': group_scene_node,
  104. "extruder_number": -1,
  105. "per_object_settings_count": 0,
  106. "mesh_type": ""
  107. }]
  108. def test_updateWithNonGroup(self, objects_model, application_with_mocked_scene, slicable_scene_node):
  109. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  110. slicable_scene_node.setName("YAY(1)")
  111. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=slicable_scene_node)
  112. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  113. objects_model._update()
  114. assert objects_model.items == [{
  115. 'name': 'YAY(1)',
  116. 'selected': False,
  117. 'outside_build_area': False,
  118. 'buildplate_number': None,
  119. 'node': slicable_scene_node,
  120. "extruder_number": -1,
  121. "per_object_settings_count": 0,
  122. "mesh_type": ""
  123. }]
  124. def test_updateWithNonTwoNodes(self, objects_model, application_with_mocked_scene, slicable_scene_node):
  125. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  126. slicable_scene_node.setName("YAY")
  127. copied_node = copy.deepcopy(slicable_scene_node)
  128. copied_node.setParent(slicable_scene_node)
  129. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=slicable_scene_node)
  130. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  131. objects_model._update()
  132. assert objects_model.items == [{
  133. 'name': 'YAY',
  134. 'selected': False,
  135. 'outside_build_area': False,
  136. 'buildplate_number': None,
  137. 'node': slicable_scene_node,
  138. "extruder_number": -1,
  139. "per_object_settings_count": 0,
  140. "mesh_type": ""
  141. }, {
  142. 'name': 'YAY(1)',
  143. 'selected': False,
  144. 'outside_build_area': False,
  145. 'buildplate_number': None,
  146. 'node': copied_node,
  147. "extruder_number": -1,
  148. "per_object_settings_count": 0,
  149. "mesh_type": ""
  150. }]
  151. def test_updateWithNonTwoGroups(self, objects_model, application_with_mocked_scene, group_scene_node):
  152. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  153. group_scene_node.setName("Group #1")
  154. copied_node = copy.deepcopy(group_scene_node)
  155. copied_node.setParent(group_scene_node)
  156. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=group_scene_node)
  157. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  158. objects_model._update()
  159. assert objects_model.items == [{
  160. 'name': 'Group #1',
  161. 'selected': False,
  162. 'outside_build_area': False,
  163. 'buildplate_number': None,
  164. 'node': group_scene_node,
  165. "extruder_number": -1,
  166. "per_object_settings_count": 0,
  167. "mesh_type": ""
  168. }, {
  169. 'name': 'Group #2',
  170. 'selected': False,
  171. 'outside_build_area': False,
  172. 'buildplate_number': None,
  173. 'node': copied_node,
  174. "extruder_number": -1,
  175. "per_object_settings_count": 0,
  176. "mesh_type": ""
  177. }]
  178. def test_updateOutsideBuildplate(self, objects_model, application_with_mocked_scene, group_scene_node):
  179. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  180. group_scene_node.setName("Group")
  181. group_scene_node.isOutsideBuildArea = MagicMock(return_value = True)
  182. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=group_scene_node)
  183. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  184. objects_model._update()
  185. assert objects_model.items == [{
  186. 'name': 'Group',
  187. 'selected': False,
  188. 'outside_build_area': True,
  189. 'buildplate_number': None,
  190. 'node': group_scene_node,
  191. "extruder_number": -1,
  192. "per_object_settings_count": 0,
  193. "mesh_type": ""
  194. }]