TestSettingOverrideDecorator.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from unittest.mock import patch, MagicMock
  2. import pytest
  3. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  4. extruder_manager = MagicMock(name= "ExtruderManager")
  5. application = MagicMock(name="application")
  6. container_registry = MagicMock(name="container_registry")
  7. @pytest.fixture()
  8. def setting_override_decorator():
  9. # Ensure that all the call counts and the like are reset.
  10. container_registry.reset_mock()
  11. application.reset_mock()
  12. extruder_manager.reset_mock()
  13. # Actually create the decorator.
  14. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  15. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  16. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value=extruder_manager)):
  17. return SettingOverrideDecorator()
  18. def test_onSettingValueChanged(setting_override_decorator):
  19. def mock_getRawProperty(key, property_name, *args, **kwargs):
  20. if property_name == "limit_to_extruder":
  21. return "-1"
  22. return MagicMock(name="rawProperty")
  23. container_registry.findContainerStacks().__getitem__().getRawProperty = mock_getRawProperty
  24. # On creation the needs slicing should be called once (as it being added should trigger a reslice)
  25. assert application.getBackend().needsSlicing.call_count == 1
  26. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  27. setting_override_decorator._onSettingChanged("blarg", "value")
  28. # Once we set a setting value, it should trigger again.
  29. assert application.getBackend().needsSlicing.call_count == 2
  30. def test_onSettingEnableChanged(setting_override_decorator):
  31. # On creation the needs slicing should be called once (as it being added should trigger a reslice)
  32. assert application.getBackend().needsSlicing.call_count == 1
  33. with patch("UM.Application.Application.getInstance", MagicMock(return_value=application)):
  34. setting_override_decorator._onSettingChanged("blarg", "enabled")
  35. # Once we set a property that is not a value, no re-slice should happen.
  36. assert application.getBackend().needsSlicing.call_count == 1
  37. def test_setActiveExtruder(setting_override_decorator):
  38. setting_override_decorator.activeExtruderChanged.emit = MagicMock()
  39. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value=extruder_manager)):
  40. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  41. setting_override_decorator.setActiveExtruder("ZOMG")
  42. setting_override_decorator.activeExtruderChanged.emit.assert_called_once_with()
  43. assert setting_override_decorator.getActiveExtruder() == "ZOMG"