TestIntentManager.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest.mock import MagicMock, patch
  4. import pytest
  5. from typing import Any, Dict, List
  6. from cura.Settings.IntentManager import IntentManager
  7. from cura.Machines.QualityGroup import QualityGroup
  8. from tests.Settings.MockContainer import MockContainer
  9. mocked_intent_metadata = [
  10. {"id": "um3_aa4_pla_smooth_normal", "GUID": "abcxyz", "definition": "ultimaker3", "variant": "AA 0.4",
  11. "material_id": "generic_pla", "intent_category": "smooth", "quality_type": "normal"},
  12. {"id": "um3_aa4_pla_strong_abnorm", "GUID": "defqrs", "definition": "ultimaker3", "variant": "AA 0.4",
  13. "material_id": "generic_pla", "intent_category": "strong", "quality_type": "abnorm"}] # type:List[Dict[str, str]]
  14. mocked_qualitygroup_metadata = {
  15. "normal": QualityGroup("um3_aa4_pla_normal", "normal"),
  16. "abnorm": QualityGroup("um3_aa4_pla_abnorm", "abnorm")} # type: Dict[str, QualityGroup]
  17. @pytest.fixture
  18. def mock_container_tree() -> MagicMock:
  19. container_tree = MagicMock()
  20. container_tree.getCurrentQualityGroups = MagicMock(return_value = mocked_qualitygroup_metadata)
  21. container_tree.machines = {
  22. "ultimaker3": MagicMock(
  23. variants = {
  24. "AA 0.4": MagicMock(
  25. materials = {
  26. "generic_pla": MagicMock(
  27. qualities = {
  28. "um3_aa4_pla_normal": MagicMock(
  29. quality_type = "normal",
  30. intents = {
  31. "um3_aa4_pla_smooth_normal": MagicMock(
  32. intent_category = "smooth",
  33. getMetadata = MagicMock(return_value = mocked_intent_metadata[0])
  34. )
  35. }
  36. ),
  37. "um3_aa4_pla_abnorm": MagicMock(
  38. quality_type = "abnorm",
  39. intents = {
  40. "um3_aa4_pla_strong_abnorm": MagicMock(
  41. intent_category = "strong",
  42. getMetadata = MagicMock(return_value = mocked_intent_metadata[1])
  43. )
  44. }
  45. )
  46. }
  47. )
  48. }
  49. )
  50. }
  51. )
  52. }
  53. return container_tree
  54. @pytest.fixture
  55. def intent_manager(application, extruder_manager, machine_manager, container_registry, global_stack) -> IntentManager:
  56. application.getExtruderManager = MagicMock(return_value = extruder_manager)
  57. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  58. application.getMachineManager = MagicMock(return_value = machine_manager)
  59. machine_manager.setIntentByCategory = MagicMock()
  60. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  61. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  62. manager = IntentManager()
  63. return manager
  64. def mockFindMetadata(**kwargs) -> List[Dict[str, Any]]:
  65. if "id" in kwargs:
  66. return [x for x in mocked_intent_metadata if x["id"] == kwargs["id"]]
  67. else:
  68. result = []
  69. for data in mocked_intent_metadata:
  70. should_add = True
  71. for key, value in kwargs.items():
  72. if key in data.keys():
  73. should_add &= (data[key] == value)
  74. if should_add:
  75. result.append(data)
  76. return result
  77. def mockFindContainers(**kwargs) -> List[MockContainer]:
  78. result = []
  79. metadatas = mockFindMetadata(**kwargs)
  80. for metadata in metadatas:
  81. result.append(MockContainer(metadata))
  82. return result
  83. def doSetup(application, extruder_manager, container_registry, global_stack) -> None:
  84. container_registry.findContainersMetadata = MagicMock(side_effect = mockFindMetadata)
  85. container_registry.findContainers = MagicMock(side_effect = mockFindContainers)
  86. for qualitygroup in mocked_qualitygroup_metadata.values():
  87. qualitygroup.node_for_global = MagicMock(name = "Node for global")
  88. global_stack.definition = MockContainer({"id": "ultimaker3"})
  89. extruder_stack_a = MockContainer({"id": "Extruder The First"})
  90. extruder_stack_a.variant = MockContainer({"name": "AA 0.4"})
  91. extruder_stack_a.quality = MockContainer({"id": "um3_aa4_pla_normal"})
  92. extruder_stack_a.material = MockContainer({"base_file": "generic_pla"})
  93. extruder_stack_a.intent = MockContainer({"id": "empty_intent", "intent_category": "default"})
  94. extruder_stack_a.qualityChanges = MockContainer({"id": "empty_quality_changes", "intent_category": "default"})
  95. extruder_stack_b = MockContainer({"id": "Extruder II: Plastic Boogaloo"})
  96. extruder_stack_b.variant = MockContainer({"name": "AA 0.4"})
  97. extruder_stack_b.quality = MockContainer({"id": "um3_aa4_pla_normal"})
  98. extruder_stack_b.material = MockContainer({"base_file": "generic_pla"})
  99. extruder_stack_b.intent = MockContainer({"id": "empty_intent", "intent_category": "default"})
  100. extruder_stack_b.qualityChanges = MockContainer({"id": "empty_quality_changes", "intent_category": "default"})
  101. global_stack.extruderList = [extruder_stack_a, extruder_stack_b]
  102. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  103. extruder_manager.getUsedExtruderStacks = MagicMock(return_value = [extruder_stack_a, extruder_stack_b])
  104. def test_intentCategories(intent_manager, mock_container_tree):
  105. with patch("cura.Machines.ContainerTree.ContainerTree.getInstance", MagicMock(return_value = mock_container_tree)):
  106. categories = intent_manager.intentCategories("ultimaker3", "AA 0.4", "generic_pla") # type:List[str]
  107. assert "default" in categories, "default should always be in categories"
  108. assert "strong" in categories, "strong should be in categories"
  109. assert "smooth" in categories, "smooth should be in categories"
  110. def test_getCurrentAvailableIntents(application, extruder_manager, intent_manager, container_registry, global_stack, mock_container_tree):
  111. doSetup(application, extruder_manager, container_registry, global_stack)
  112. with patch("cura.Machines.ContainerTree.ContainerTree.getInstance", MagicMock(return_value = mock_container_tree)):
  113. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  114. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  115. intents = intent_manager.getCurrentAvailableIntents()
  116. assert ("smooth", "normal") in intents
  117. assert ("strong", "abnorm") in intents
  118. #assert ("default", "normal") in intents # Pending to-do in 'IntentManager'.
  119. #assert ("default", "abnorm") in intents # Pending to-do in 'IntentManager'.
  120. assert len(intents) == 2 # Or 4? pending to-do in 'IntentManager'.
  121. def test_currentAvailableIntentCategories(application, extruder_manager, intent_manager, container_registry, global_stack, mock_container_tree):
  122. doSetup(application, extruder_manager, container_registry, global_stack)
  123. with patch("cura.Machines.ContainerTree.ContainerTree.getInstance", MagicMock(return_value = mock_container_tree)):
  124. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  125. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  126. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value = extruder_manager)):
  127. categories = intent_manager.currentAvailableIntentCategories()
  128. assert "default" in categories # Currently inconsistent with 'currentAvailableIntents'!
  129. assert "smooth" in categories
  130. assert "strong" in categories
  131. assert len(categories) == 3
  132. def test_selectIntent(application, extruder_manager, intent_manager, container_registry, global_stack, mock_container_tree):
  133. doSetup(application, extruder_manager, container_registry, global_stack)
  134. with patch("cura.Machines.ContainerTree.ContainerTree.getInstance", MagicMock(return_value = mock_container_tree)):
  135. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value = application)):
  136. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value = container_registry)):
  137. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value = extruder_manager)):
  138. intents = intent_manager.getCurrentAvailableIntents()
  139. for intent, quality in intents:
  140. intent_manager.selectIntent(intent, quality)
  141. extruder_stacks = extruder_manager.getUsedExtruderStacks()
  142. assert len(extruder_stacks) == 2
  143. assert extruder_stacks[0].intent.getMetaDataEntry("intent_category") == intent
  144. assert extruder_stacks[1].intent.getMetaDataEntry("intent_category") == intent