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