TestSettingVisibilityPresets.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from unittest.mock import MagicMock, patch
  2. import os.path
  3. from UM.Preferences import Preferences
  4. from UM.Resources import Resources
  5. from cura.CuraApplication import CuraApplication
  6. from cura.Machines.Models.SettingVisibilityPresetsModel import SettingVisibilityPresetsModel
  7. from cura.Settings.SettingVisibilityPreset import SettingVisibilityPreset
  8. setting_visibility_preset_test_settings = {"test", "zomg", "derp", "yay", "whoo"}
  9. Resources.addSearchPath(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "resources")))
  10. Resources.addStorageType(CuraApplication.ResourceTypes.SettingVisibilityPreset, "setting_visibility")
  11. def test_createVisibilityPresetFromLocalFile():
  12. # Simple creation test. This is seperated from the visibilityFromPrevious, since we can't check for the contents
  13. # of the other profiles, since they might change over time.
  14. visibility_preset = SettingVisibilityPreset()
  15. visibility_preset.loadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "setting_visiblity_preset_test.cfg"))
  16. assert setting_visibility_preset_test_settings == set(visibility_preset.settings)
  17. assert visibility_preset.name == "test"
  18. assert visibility_preset.weight == 1
  19. assert visibility_preset.settings.count("yay") == 1 # It's in the file twice but we should load it once.
  20. def test_visibilityFromPrevious():
  21. # This test checks that all settings in basic are in advanced and all settings in advanced are in expert.
  22. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  23. visibility_model = SettingVisibilityPresetsModel(Preferences())
  24. basic_visibility = visibility_model.getVisibilityPresetById("basic")
  25. advanced_visibility = visibility_model.getVisibilityPresetById("advanced")
  26. expert_visibility = visibility_model.getVisibilityPresetById("expert")
  27. # Check if there are settings that are in basic, but not in advanced.
  28. settings_not_in_advanced = set(basic_visibility.settings) - set(advanced_visibility.settings)
  29. assert len(settings_not_in_advanced) == 0 # All settings in basic should be in advanced
  30. # Check if there are settings that are in advanced, but not in expert.
  31. settings_not_in_expert = set(advanced_visibility.settings) - set(expert_visibility.settings)
  32. assert len(settings_not_in_expert) == 0 # All settings in advanced should be in expert.
  33. def test_setActivePreset():
  34. preferences = Preferences()
  35. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  36. visibility_model = SettingVisibilityPresetsModel(preferences)
  37. visibility_model.activePresetChanged = MagicMock()
  38. # Ensure that we start off with basic (since we didn't change anyting just yet!)
  39. assert visibility_model.activePreset == "basic"
  40. # Everything should be the same.
  41. visibility_model.setActivePreset("basic")
  42. assert visibility_model.activePreset == "basic"
  43. assert visibility_model.activePresetChanged.emit.call_count == 0 # No events should be sent.
  44. # Change it to existing type (should work...)
  45. visibility_model.setActivePreset("advanced")
  46. assert visibility_model.activePreset == "advanced"
  47. assert visibility_model.activePresetChanged.emit.call_count == 1
  48. # Change to unknown preset. Shouldn't do anything.
  49. visibility_model.setActivePreset("OMGZOMGNOPE")
  50. assert visibility_model.activePreset == "advanced"
  51. assert visibility_model.activePresetChanged.emit.call_count == 1
  52. def test_preferenceChanged():
  53. preferences = Preferences()
  54. # Set the visible_settings to something silly
  55. preferences.addPreference("general/visible_settings", "omgzomg")
  56. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  57. visibility_model = SettingVisibilityPresetsModel(preferences)
  58. visibility_model.activePresetChanged = MagicMock()
  59. assert visibility_model.activePreset == "custom" # This should make the model start at "custom
  60. assert visibility_model.activePresetChanged.emit.call_count == 0
  61. basic_visibility = visibility_model.getVisibilityPresetById("basic")
  62. new_visibility_string = ";".join(basic_visibility.settings)
  63. preferences.setValue("general/visible_settings", new_visibility_string)
  64. # Fake a signal emit (since we didn't create the application, our own signals are not fired)
  65. visibility_model._onPreferencesChanged("general/visible_settings")
  66. # Set the visibility settings to basic
  67. assert visibility_model.activePreset == "basic"
  68. assert visibility_model.activePresetChanged.emit.call_count == 1