TestConvexHullDecorator.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import copy
  2. from unittest.mock import patch, MagicMock
  3. import pytest
  4. from UM.Math.Polygon import Polygon
  5. from UM.Mesh.MeshBuilder import MeshBuilder
  6. from UM.Scene.GroupDecorator import GroupDecorator
  7. from UM.Scene.SceneNode import SceneNode
  8. from UM.Scene.SceneNodeDecorator import SceneNodeDecorator
  9. from cura.Scene.ConvexHullDecorator import ConvexHullDecorator
  10. mocked_application = MagicMock()
  11. mocked_controller = MagicMock()
  12. # We need to mock out this function, otherwise we get a recursion
  13. mocked_controller.isToolOperationActive = MagicMock(return_value = False)
  14. mocked_application.getController = MagicMock(return_value = mocked_controller)
  15. class NonPrintingDecorator(SceneNodeDecorator):
  16. def isNonPrintingMesh(self):
  17. return True
  18. class PrintingDecorator(SceneNodeDecorator):
  19. def isNonPrintingMesh(self):
  20. return False
  21. @pytest.fixture
  22. def convex_hull_decorator():
  23. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = mocked_application)):
  24. with patch("UM.Application.Application.getInstance", MagicMock(return_value = mocked_application)):
  25. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  26. return ConvexHullDecorator()
  27. def test_getSetNode(convex_hull_decorator):
  28. node = SceneNode()
  29. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  30. convex_hull_decorator.setNode(node)
  31. assert convex_hull_decorator.getNode() == node
  32. def test_getConvexHullBoundaryNoNode(convex_hull_decorator):
  33. assert convex_hull_decorator.getConvexHullBoundary() is None
  34. def test_getConvexHullHeadNoNode(convex_hull_decorator):
  35. assert convex_hull_decorator.getConvexHullHead() is None
  36. def test_getConvexHullHeadNotPrintingMesh(convex_hull_decorator):
  37. node = SceneNode()
  38. node.addDecorator(NonPrintingDecorator())
  39. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  40. convex_hull_decorator.setNode(node)
  41. assert convex_hull_decorator.getConvexHullHead() is None
  42. def test_getConvexHullNoNode(convex_hull_decorator):
  43. assert convex_hull_decorator.getConvexHull() is None
  44. def test_getConvexHeadFullNoNode(convex_hull_decorator):
  45. assert convex_hull_decorator.getConvexHullHeadFull() is None
  46. def test_getConvexHullNotPrintingMesh(convex_hull_decorator):
  47. node = SceneNode()
  48. node.addDecorator(NonPrintingDecorator())
  49. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  50. convex_hull_decorator.setNode(node)
  51. assert convex_hull_decorator.getConvexHull() is None
  52. def test_getConvexHullPrintingMesh(convex_hull_decorator):
  53. node = SceneNode()
  54. node.addDecorator(PrintingDecorator())
  55. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  56. convex_hull_decorator.setNode(node)
  57. convex_hull_decorator._compute2DConvexHull = MagicMock(return_value = Polygon.approximatedCircle(10))
  58. assert convex_hull_decorator.getConvexHull() == Polygon.approximatedCircle(10)
  59. def test_getConvexHullBoundaryNotPrintingMesh(convex_hull_decorator):
  60. node = SceneNode()
  61. node.addDecorator(NonPrintingDecorator())
  62. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  63. convex_hull_decorator.setNode(node)
  64. assert convex_hull_decorator.getConvexHullBoundary() is None
  65. def test_getConvexHulLBoundaryPrintingMesh(convex_hull_decorator):
  66. node = SceneNode()
  67. node.addDecorator(PrintingDecorator())
  68. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  69. convex_hull_decorator.setNode(node)
  70. # Should still be None, since print sequence is not one at a time
  71. assert convex_hull_decorator.getConvexHullBoundary() is None
  72. def test_getConvexHulLBoundaryPrintingMeshOneAtATime(convex_hull_decorator):
  73. node = SceneNode()
  74. node.addDecorator(PrintingDecorator())
  75. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  76. convex_hull_decorator.setNode(node)
  77. convex_hull_decorator._global_stack = MagicMock()
  78. convex_hull_decorator._global_stack.getProperty = MagicMock(return_value = "one_at_a_time")
  79. # In this test we don't care for the result of the function, just that the convex hull computation is called.
  80. convex_hull_decorator._compute2DConvexHull = MagicMock()
  81. convex_hull_decorator.getConvexHullBoundary()
  82. convex_hull_decorator._compute2DConvexHull.assert_called_once_with()
  83. def value_changed(convex_hull_decorator, key):
  84. convex_hull_decorator._onChanged = MagicMock()
  85. convex_hull_decorator._onSettingValueChanged(key, "value")
  86. convex_hull_decorator._onChanged.assert_called_once_with()
  87. # This should have no effect at all
  88. convex_hull_decorator._onSettingValueChanged(key, "not value")
  89. convex_hull_decorator._onChanged.assert_called_once_with()
  90. @pytest.mark.parametrize("key", ConvexHullDecorator._affected_settings)
  91. def test_onSettingValueChangedAffectedSettings(convex_hull_decorator, key):
  92. value_changed(convex_hull_decorator, key)
  93. @pytest.mark.parametrize("key", ConvexHullDecorator._influencing_settings)
  94. def test_onSettingValueChangedInfluencingSettings(convex_hull_decorator, key):
  95. convex_hull_decorator._init2DConvexHullCache = MagicMock()
  96. value_changed(convex_hull_decorator, key)
  97. convex_hull_decorator._init2DConvexHullCache.assert_called_once_with()
  98. def test_compute2DConvexHullNoNode(convex_hull_decorator):
  99. assert convex_hull_decorator._compute2DConvexHull() is None
  100. def test_compute2DConvexHullNoMeshData(convex_hull_decorator):
  101. node = SceneNode()
  102. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  103. convex_hull_decorator.setNode(node)
  104. assert convex_hull_decorator._compute2DConvexHull() == Polygon([])
  105. def test_compute2DConvexHullMeshData(convex_hull_decorator):
  106. node = SceneNode()
  107. mb = MeshBuilder()
  108. mb.addCube(10, 10, 10)
  109. node.setMeshData(mb.build())
  110. convex_hull_decorator._getSettingProperty = MagicMock(return_value = 0)
  111. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  112. convex_hull_decorator.setNode(node)
  113. mocked_stack = MagicMock()
  114. mocked_stack.getProperty = MagicMock(return_value=1)
  115. convex_hull_decorator._global_stack = mocked_stack
  116. assert convex_hull_decorator._compute2DConvexHull() == Polygon([[5.0, -5.0], [-5.0, -5.0], [-5.0, 5.0], [5.0, 5.0]])
  117. def test_compute2DConvexHullMeshDataGrouped(convex_hull_decorator):
  118. parent_node = SceneNode()
  119. parent_node.addDecorator(GroupDecorator())
  120. node = SceneNode()
  121. parent_node.addChild(node)
  122. mb = MeshBuilder()
  123. mb.addCube(10, 10, 10)
  124. node.setMeshData(mb.build())
  125. mocked_stack = MagicMock()
  126. mocked_stack.getProperty = MagicMock(return_value=1)
  127. convex_hull_decorator._global_stack = mocked_stack
  128. convex_hull_decorator._getSettingProperty = MagicMock(return_value=0)
  129. with patch("UM.Application.Application.getInstance", MagicMock(return_value=mocked_application)):
  130. convex_hull_decorator.setNode(parent_node)
  131. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  132. copied_decorator = copy.deepcopy(convex_hull_decorator)
  133. mocked_stack = MagicMock()
  134. mocked_stack.getProperty = MagicMock(return_value=1)
  135. copied_decorator._global_stack = mocked_stack
  136. copied_decorator._getSettingProperty = MagicMock(return_value=0)
  137. node.addDecorator(copied_decorator)
  138. assert convex_hull_decorator._compute2DConvexHull() == Polygon([[-5.0, 5.0], [5.0, 5.0], [5.0, -5.0], [-5.0, -5.0]])