TestMachineManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. from unittest.mock import MagicMock, patch, PropertyMock
  2. import pytest
  3. from cura.Settings.MachineManager import MachineManager
  4. def createMockedStack(stack_id: str, name: str):
  5. stack = MagicMock(name=name)
  6. stack.getId = MagicMock(return_value=stack_id)
  7. return stack
  8. @pytest.fixture()
  9. def global_stack():
  10. return createMockedStack("GlobalStack", "Global Stack")
  11. @pytest.fixture()
  12. def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
  13. application.getExtruderManager = MagicMock(return_value = extruder_manager)
  14. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  15. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  16. manager = MachineManager(application)
  17. manager._onGlobalContainerChanged()
  18. return manager
  19. def test_getMachine():
  20. registry = MagicMock()
  21. mocked_global_stack = MagicMock()
  22. mocked_global_stack.getId = MagicMock(return_value="test_machine")
  23. mocked_global_stack.definition.getId = MagicMock(return_value = "test")
  24. registry.findContainerStacks = MagicMock(return_value=[mocked_global_stack])
  25. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  26. assert MachineManager.getMachine("test") == mocked_global_stack
  27. # Since only test is in the registry, this should be None
  28. assert MachineManager.getMachine("UnknownMachine") is None
  29. def test_addMachine(machine_manager):
  30. registry = MagicMock()
  31. mocked_stack = MagicMock()
  32. mocked_stack.getId = MagicMock(return_value="newlyCreatedStack")
  33. mocked_create_machine = MagicMock(name="createMachine", return_value = mocked_stack)
  34. machine_manager.setActiveMachine = MagicMock()
  35. with patch("cura.Settings.CuraStackBuilder.CuraStackBuilder.createMachine", mocked_create_machine):
  36. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  37. machine_manager.addMachine("derp")
  38. machine_manager.setActiveMachine.assert_called_with("newlyCreatedStack")
  39. def test_hasUserSettings(machine_manager, application):
  40. mocked_stack = application.getGlobalContainerStack()
  41. mocked_instance_container = MagicMock(name="UserSettingContainer")
  42. mocked_instance_container.getNumInstances = MagicMock(return_value = 12)
  43. mocked_stack.getTop = MagicMock(return_value = mocked_instance_container)
  44. machine_manager._reCalculateNumUserSettings()
  45. assert machine_manager.numUserSettings == 12
  46. assert machine_manager.hasUserSettings
  47. def test_hasUserSettingsExtruder(machine_manager, application):
  48. mocked_stack = application.getGlobalContainerStack()
  49. extruder = createMockedExtruder("extruder")
  50. mocked_instance_container_global = MagicMock(name="UserSettingContainerGlobal")
  51. mocked_instance_container_global.getNumInstances = MagicMock(return_value=0)
  52. mocked_stack.getTop = MagicMock(return_value=mocked_instance_container_global)
  53. mocked_stack.extruderList = [extruder]
  54. mocked_instance_container = MagicMock(name="UserSettingContainer")
  55. mocked_instance_container.getNumInstances = MagicMock(return_value=200)
  56. extruder.getTop = MagicMock(return_value = mocked_instance_container)
  57. machine_manager._reCalculateNumUserSettings()
  58. assert machine_manager.hasUserSettings
  59. assert machine_manager.numUserSettings == 200
  60. def test_hasUserSettingsEmptyUserChanges(machine_manager, application):
  61. mocked_stack = application.getGlobalContainerStack()
  62. extruder = createMockedExtruder("extruder")
  63. mocked_instance_container_global = MagicMock(name="UserSettingContainerGlobal")
  64. mocked_instance_container_global.getNumInstances = MagicMock(return_value=0)
  65. mocked_stack.getTop = MagicMock(return_value=mocked_instance_container_global)
  66. mocked_stack.extruderList = [extruder]
  67. mocked_instance_container = MagicMock(name="UserSettingContainer")
  68. mocked_instance_container.getNumInstances = MagicMock(return_value=0)
  69. extruder.getTop = MagicMock(return_value = mocked_instance_container)
  70. machine_manager._reCalculateNumUserSettings()
  71. assert not machine_manager.hasUserSettings
  72. def test_totalNumberOfSettings(machine_manager):
  73. registry = MagicMock()
  74. mocked_definition = MagicMock()
  75. mocked_definition.getAllKeys = MagicMock(return_value = ["omg", "zomg", "foo"])
  76. registry.findDefinitionContainers = MagicMock(return_value = [mocked_definition])
  77. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  78. assert machine_manager.totalNumberOfSettings == 3
  79. def createMockedExtruder(extruder_id):
  80. extruder = MagicMock()
  81. extruder.getId = MagicMock(return_value = extruder_id)
  82. return extruder
  83. def createMockedInstanceContainer(instance_id, name = ""):
  84. instance = MagicMock()
  85. instance.getName = MagicMock(return_value = name)
  86. instance.getId = MagicMock(return_value=instance_id)
  87. return instance
  88. def test_globalVariantName(machine_manager, application):
  89. global_stack = application.getGlobalContainerStack()
  90. global_stack.variant = createMockedInstanceContainer("beep", "zomg")
  91. assert machine_manager.globalVariantName == "zomg"
  92. def test_resetSettingForAllExtruders(machine_manager):
  93. global_stack = machine_manager.activeMachine
  94. extruder_1 = createMockedExtruder("extruder_1")
  95. extruder_2 = createMockedExtruder("extruder_2")
  96. extruder_1.userChanges = createMockedInstanceContainer("settings_1")
  97. extruder_2.userChanges = createMockedInstanceContainer("settings_2")
  98. global_stack.extruderList = [extruder_1, extruder_2]
  99. machine_manager.resetSettingForAllExtruders("whatever")
  100. extruder_1.userChanges.removeInstance.assert_called_once_with("whatever")
  101. extruder_2.userChanges.removeInstance.assert_called_once_with("whatever")
  102. def test_setUnknownActiveMachine(machine_manager):
  103. machine_action_manager = MagicMock()
  104. machine_manager.getMachineActionManager = MagicMock(return_value = machine_action_manager)
  105. machine_manager.setActiveMachine("UnknownMachine")
  106. # The machine isn't known to us, so this should not happen!
  107. machine_action_manager.addDefaultMachineActions.assert_not_called()
  108. def test_clearActiveMachine(machine_manager):
  109. machine_manager.setActiveMachine(None)
  110. machine_manager._application.setGlobalContainerStack.assert_called_once_with(None)
  111. def test_setActiveMachine(machine_manager):
  112. registry = MagicMock()
  113. machine_action_manager = MagicMock()
  114. machine_manager._validateVariantsAndMaterials = MagicMock() # Not testing that function, so whatever.
  115. machine_manager._application.getMachineActionManager = MagicMock(return_value=machine_action_manager)
  116. global_stack = createMockedStack("NewMachine", "Newly created Machine")
  117. # Ensure that the container stack will be found
  118. registry.findContainerStacks = MagicMock(return_value = [global_stack])
  119. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  120. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=registry)):
  121. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"): # Prevent the FixSingleExtruder from being called
  122. machine_manager.setActiveMachine("NewMachine")
  123. machine_action_manager.addDefaultMachineActions.assert_called_once_with(global_stack)
  124. # Yeah sure. It's technically an implementation detail. But if this function wasn't called, it exited early somehow
  125. machine_manager._validateVariantsAndMaterials.assert_called_once_with(global_stack)
  126. def test_setInvalidActiveMachine(machine_manager):
  127. registry = MagicMock()
  128. global_stack = createMockedStack("InvalidMachine", "Newly created Machine")
  129. # This machine is just plain wrong!
  130. global_stack.isValid = MagicMock(return_value = False)
  131. # Ensure that the container stack will be found
  132. registry.findContainerStacks = MagicMock(return_value=[global_stack])
  133. configuration_error_message = MagicMock()
  134. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  135. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=registry)):
  136. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"): # Prevent the FixSingleExtruder from being called
  137. with patch("UM.ConfigurationErrorMessage.ConfigurationErrorMessage.getInstance", MagicMock(return_value = configuration_error_message)):
  138. machine_manager.setActiveMachine("InvalidMachine")
  139. # Notification stuff should happen now!
  140. configuration_error_message.addFaultyContainers.assert_called_once_with("InvalidMachine")
  141. def test_clearUserSettingsAllCurrentStacks(machine_manager, application):
  142. global_stack = application.getGlobalContainerStack()
  143. extruder_1 = createMockedExtruder("extruder_1")
  144. instance_container = createMockedInstanceContainer("user", "UserContainer")
  145. extruder_1.getTop = MagicMock(return_value = instance_container)
  146. global_stack.extruderList = [extruder_1]
  147. machine_manager.clearUserSettingAllCurrentStacks("some_setting")
  148. instance_container.removeInstance.assert_called_once_with("some_setting", postpone_emit=True)
  149. def test_clearUserSettingsAllCurrentStacksLinkedSetting(machine_manager, application):
  150. global_stack = application.getGlobalContainerStack()
  151. extruder_1 = createMockedExtruder("extruder_1")
  152. instance_container = createMockedInstanceContainer("user", "UserContainer")
  153. instance_container_global = createMockedInstanceContainer("global_user", "GlobalUserContainer")
  154. global_stack.getTop = MagicMock(return_value = instance_container_global)
  155. extruder_1.getTop = MagicMock(return_value=instance_container)
  156. global_stack.extruderList = [extruder_1]
  157. global_stack.getProperty = MagicMock(side_effect = lambda key, prop: True if prop == "settable_per_extruder" else "-1" )
  158. machine_manager.clearUserSettingAllCurrentStacks("some_setting")
  159. instance_container.removeInstance.assert_not_called()
  160. instance_container_global.removeInstance.assert_called_once_with("some_setting", postpone_emit = True)
  161. def test_isActiveQualityExperimental(machine_manager):
  162. quality_group = MagicMock(is_experimental = True)
  163. machine_manager.activeQualityGroup = MagicMock(return_value = quality_group)
  164. assert machine_manager.isActiveQualityExperimental
  165. def test_isActiveQualityNotExperimental(machine_manager):
  166. quality_group = MagicMock(is_experimental = False)
  167. machine_manager.activeQualityGroup = MagicMock(return_value = quality_group)
  168. assert not machine_manager.isActiveQualityExperimental
  169. def test_isActiveQualityNotExperimental_noQualityGroup(machine_manager):
  170. machine_manager.activeQualityGroup = MagicMock(return_value=None)
  171. assert not machine_manager.isActiveQualityExperimental
  172. def test_isActiveQualitySupported(machine_manager):
  173. quality_group = MagicMock(is_available=True)
  174. machine_manager.activeQualityGroup = MagicMock(return_value=quality_group)
  175. assert machine_manager.isActiveQualitySupported
  176. def test_isActiveQualityNotSupported(machine_manager):
  177. quality_group = MagicMock(is_available=False)
  178. machine_manager.activeQualityGroup = MagicMock(return_value=quality_group)
  179. assert not machine_manager.isActiveQualitySupported
  180. def test_isActiveQualityNotSupported_noQualityGroup(machine_manager):
  181. machine_manager.activeQualityGroup = MagicMock(return_value=None)
  182. assert not machine_manager.isActiveQualitySupported