TestMachineAction.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import pytest
  4. from cura.MachineAction import MachineAction
  5. from cura.MachineActionManager import NotUniqueMachineActionError, UnknownMachineActionError
  6. from cura.Settings.GlobalStack import GlobalStack
  7. @pytest.fixture()
  8. def global_stack():
  9. gs = GlobalStack("test_global_stack")
  10. gs._metadata = {
  11. "supported_actions": ["supported_action_1", "supported_action_2"],
  12. "required_actions": ["required_action_1", "required_action_2"],
  13. "first_start_actions": ["first_start_actions_1", "first_start_actions_2"]
  14. }
  15. return gs
  16. class Machine:
  17. def __init__(self, key = ""):
  18. self._key = key
  19. def getKey(self):
  20. return self._key
  21. def test_addDefaultMachineActions(machine_action_manager, global_stack):
  22. # The actions need to be registered first as "available actions" in the manager,
  23. # same as the "machine_action" type does when registering a plugin.
  24. all_actions = []
  25. for action_key_list in global_stack._metadata.values():
  26. for key in action_key_list:
  27. all_actions.append(MachineAction(key = key))
  28. for action in all_actions:
  29. machine_action_manager.addMachineAction(action)
  30. # Only the actions in the definition that were registered first will be added to the machine.
  31. # For the sake of this test, all the actions were previouly added.
  32. machine_action_manager.addDefaultMachineActions(global_stack)
  33. definition_id = global_stack.getDefinition().getId()
  34. support_action_keys = [a.getKey() for a in machine_action_manager.getSupportedActions(definition_id)]
  35. assert support_action_keys == global_stack.getMetaDataEntry("supported_actions")
  36. required_action_keys = [a.getKey() for a in machine_action_manager.getRequiredActions(definition_id)]
  37. assert required_action_keys == global_stack.getMetaDataEntry("required_actions")
  38. first_start_action_keys = [a.getKey() for a in machine_action_manager.getFirstStartActions(definition_id)]
  39. assert first_start_action_keys == global_stack.getMetaDataEntry("first_start_actions")
  40. def test_addMachineAction(machine_action_manager):
  41. test_action = MachineAction(key = "test_action")
  42. test_action_2 = MachineAction(key = "test_action_2")
  43. test_machine = Machine("test_machine")
  44. machine_action_manager.addMachineAction(test_action)
  45. machine_action_manager.addMachineAction(test_action_2)
  46. assert machine_action_manager.getMachineAction("test_action") == test_action
  47. assert machine_action_manager.getMachineAction("key_that_doesnt_exist") is None
  48. # Adding the same machine action is not allowed.
  49. with pytest.raises(NotUniqueMachineActionError):
  50. machine_action_manager.addMachineAction(test_action)
  51. # Check that the machine has no supported actions yet.
  52. assert machine_action_manager.getSupportedActions(test_machine) == list()
  53. # Check if adding a supported action works.
  54. machine_action_manager.addSupportedAction(test_machine, "test_action")
  55. assert machine_action_manager.getSupportedActions(test_machine) == [test_action, ]
  56. # Check that adding a unknown action doesn't change anything.
  57. machine_action_manager.addSupportedAction(test_machine, "key_that_doesnt_exist")
  58. assert machine_action_manager.getSupportedActions(test_machine) == [test_action, ]
  59. # Check if adding multiple supported actions works.
  60. machine_action_manager.addSupportedAction(test_machine, "test_action_2")
  61. assert machine_action_manager.getSupportedActions(test_machine) == [test_action, test_action_2]
  62. # Check that the machine has no required actions yet.
  63. assert machine_action_manager.getRequiredActions(test_machine) == list()
  64. ## Ensure that only known actions can be added.
  65. with pytest.raises(UnknownMachineActionError):
  66. machine_action_manager.addRequiredAction(test_machine, "key_that_doesnt_exist")
  67. ## Check if adding single required action works
  68. machine_action_manager.addRequiredAction(test_machine, "test_action")
  69. assert machine_action_manager.getRequiredActions(test_machine) == [test_action, ]
  70. # Check if adding multiple required actions works.
  71. machine_action_manager.addRequiredAction(test_machine, "test_action_2")
  72. assert machine_action_manager.getRequiredActions(test_machine) == [test_action, test_action_2]
  73. # Ensure that firstStart actions are empty by default.
  74. assert machine_action_manager.getFirstStartActions(test_machine) == []
  75. # Check if adding multiple (the same) actions to first start actions work.
  76. machine_action_manager.addFirstStartAction(test_machine, "test_action")
  77. machine_action_manager.addFirstStartAction(test_machine, "test_action")
  78. assert machine_action_manager.getFirstStartActions(test_machine) == [test_action, test_action]
  79. # Adding unknown action should not crash.
  80. machine_action_manager.addFirstStartAction(test_machine, "key_that_doesnt_exists")
  81. def test_removeMachineAction(machine_action_manager):
  82. test_action = MachineAction(key="test_action")
  83. test_machine = Machine("test_machine")
  84. machine_action_manager.addMachineAction(test_action)
  85. # Remove the machine action
  86. machine_action_manager.removeMachineAction(test_action)
  87. assert machine_action_manager.getMachineAction("test_action") is None
  88. # Attempting to remove it again should not crash.
  89. machine_action_manager.removeMachineAction(test_action)