TestMachineManager.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from cura.Settings.MachineManager import MachineManager
  4. @pytest.fixture()
  5. def global_stack():
  6. stack = MagicMock(name = "Global Stack")
  7. stack.getId = MagicMock(return_value = "GlobalStack")
  8. return stack
  9. @pytest.fixture()
  10. def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
  11. application.getExtruderManager = MagicMock(return_value = extruder_manager)
  12. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  13. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  14. manager = MachineManager(application)
  15. manager._onGlobalContainerChanged()
  16. return manager
  17. @pytest.mark.skip(reason = "Outdated test")
  18. def test_setActiveMachine(machine_manager):
  19. registry = MagicMock()
  20. mocked_global_stack = MagicMock()
  21. mocked_global_stack.getId = MagicMock(return_value = "test_machine")
  22. registry.findContainerStacks = MagicMock(return_value = [mocked_global_stack])
  23. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  24. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=registry)):
  25. machine_manager.setActiveMachine("test_machine")
  26. # Although we mocked the application away, we still want to know if it was notified about the attempted change.
  27. machine_manager._application.setGlobalContainerStack.assert_called_with(mocked_global_stack)
  28. def test_getMachine():
  29. registry = MagicMock()
  30. mocked_global_stack = MagicMock()
  31. mocked_global_stack.getId = MagicMock(return_value="test_machine")
  32. mocked_global_stack.definition.getId = MagicMock(return_value = "test")
  33. registry.findContainerStacks = MagicMock(return_value=[mocked_global_stack])
  34. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  35. assert MachineManager.getMachine("test") == mocked_global_stack
  36. def test_addMachine(machine_manager):
  37. registry = MagicMock()
  38. mocked_stack = MagicMock()
  39. mocked_stack.getId = MagicMock(return_value="newlyCreatedStack")
  40. mocked_create_machine = MagicMock(name="createMachine", return_value = mocked_stack)
  41. machine_manager.setActiveMachine = MagicMock()
  42. with patch("cura.Settings.CuraStackBuilder.CuraStackBuilder.createMachine", mocked_create_machine):
  43. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  44. machine_manager.addMachine("derp")
  45. machine_manager.setActiveMachine.assert_called_with("newlyCreatedStack")
  46. def test_hasUserSettings(machine_manager, application):
  47. mocked_stack = application.getGlobalContainerStack()
  48. mocked_instance_container = MagicMock(name="UserSettingContainer")
  49. mocked_instance_container.getNumInstances = MagicMock(return_value = 12)
  50. mocked_stack.getTop = MagicMock(return_value = mocked_instance_container)
  51. assert machine_manager.numUserSettings == 12
  52. assert machine_manager.hasUserSettings
  53. def test_totalNumberOfSettings(machine_manager):
  54. registry = MagicMock()
  55. mocked_definition = MagicMock()
  56. mocked_definition.getAllKeys = MagicMock(return_value = ["omg", "zomg", "foo"])
  57. registry.findDefinitionContainers = MagicMock(return_value = [mocked_definition])
  58. with patch("cura.Settings.CuraContainerRegistry.CuraContainerRegistry.getInstance", MagicMock(return_value=registry)):
  59. assert machine_manager.totalNumberOfSettings == 3
  60. def createMockedExtruder(extruder_id):
  61. extruder = MagicMock()
  62. extruder.getId = MagicMock(return_value = extruder_id)
  63. return extruder
  64. def createMockedInstanceContainer(instance_id, name = ""):
  65. instance = MagicMock()
  66. instance.getName = MagicMock(return_value = name)
  67. instance.getId = MagicMock(return_value=instance_id)
  68. return instance
  69. def test_globalVariantName(machine_manager, application):
  70. global_stack = application.getGlobalContainerStack()
  71. global_stack.variant = createMockedInstanceContainer("beep", "zomg")
  72. assert machine_manager.globalVariantName == "zomg"
  73. def test_resetSettingForAllExtruders(machine_manager):
  74. global_stack = machine_manager.activeMachine
  75. extruder_1 = createMockedExtruder("extruder_1")
  76. extruder_2 = createMockedExtruder("extruder_2")
  77. extruder_1.userChanges = createMockedInstanceContainer("settings_1")
  78. extruder_2.userChanges = createMockedInstanceContainer("settings_2")
  79. global_stack.extruderList = [extruder_1, extruder_2]
  80. machine_manager.resetSettingForAllExtruders("whatever")
  81. extruder_1.userChanges.removeInstance.assert_called_once_with("whatever")
  82. extruder_2.userChanges.removeInstance.assert_called_once_with("whatever")