TestObjectsModel.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 == [{'name': 'Group #1', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': group_scene_node}]
  99. def test_updateWithNonGroup(self, objects_model, application_with_mocked_scene, slicable_scene_node):
  100. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  101. slicable_scene_node.setName("YAY(1)")
  102. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=slicable_scene_node)
  103. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  104. objects_model._update()
  105. assert objects_model.items == [{'name': 'YAY(1)', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': slicable_scene_node}]
  106. def test_updateWithNonTwoNodes(self, objects_model, application_with_mocked_scene, slicable_scene_node):
  107. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  108. slicable_scene_node.setName("YAY")
  109. copied_node = copy.deepcopy(slicable_scene_node)
  110. copied_node.setParent(slicable_scene_node)
  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 == [{'name': 'YAY', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': slicable_scene_node}, {'name': 'YAY(1)', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': copied_node}]
  115. def test_updateWithNonTwoGroups(self, objects_model, application_with_mocked_scene, group_scene_node):
  116. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  117. group_scene_node.setName("Group #1")
  118. copied_node = copy.deepcopy(group_scene_node)
  119. copied_node.setParent(group_scene_node)
  120. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=group_scene_node)
  121. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  122. objects_model._update()
  123. assert objects_model.items == [{'name': 'Group #1', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': group_scene_node}, {'name': 'Group #2', 'selected': False, 'outside_build_area': False, 'buildplate_number': None, 'node': copied_node}]
  124. def test_updateOutsideBuildplate(self, objects_model, application_with_mocked_scene, group_scene_node):
  125. objects_model._shouldNodeBeHandled = MagicMock(return_value=True)
  126. group_scene_node.setName("Group")
  127. group_scene_node.isOutsideBuildArea = MagicMock(return_value = True)
  128. application_with_mocked_scene.getController().getScene().getRoot = MagicMock(return_value=group_scene_node)
  129. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application_with_mocked_scene)):
  130. objects_model._update()
  131. assert objects_model.items == [{'name': 'Group #1', 'selected': False, 'outside_build_area': True, 'buildplate_number': None, 'node': group_scene_node}]