Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestMachineAction.py « tests - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2bc9659e42dc3161fa5e95122d593ef4d7752a04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright (c) 2018 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

import pytest

from cura.MachineAction import MachineAction
from cura.UI.MachineActionManager import NotUniqueMachineActionError, UnknownMachineActionError
from cura.Settings.GlobalStack import GlobalStack


@pytest.fixture()
def global_stack():
    gs = GlobalStack("test_global_stack")
    gs._metadata = {
        "supported_actions": ["supported_action_1", "supported_action_2"],
        "required_actions": ["required_action_1", "required_action_2"],
        "first_start_actions": ["first_start_actions_1", "first_start_actions_2"]
    }
    return gs


class Machine:
    def __init__(self, key = ""):
        self._key = key

    def getKey(self):
        return self._key


def test_addDefaultMachineActions(machine_action_manager, global_stack):
    # The actions need to be registered first as "available actions" in the manager,
    # same as the "machine_action" type does when registering a plugin.
    all_actions = []
    for action_key_list in global_stack._metadata.values():
        for key in action_key_list:
            all_actions.append(MachineAction(key = key))
    for action in all_actions:
        machine_action_manager.addMachineAction(action)

    # Only the actions in the definition that were registered first will be added to the machine.
    # For the sake of this test, all the actions were previously added.
    machine_action_manager.addDefaultMachineActions(global_stack)
    definition_id = global_stack.getDefinition().getId()

    support_action_keys = [a.getKey() for a in machine_action_manager.getSupportedActions(definition_id)]
    assert support_action_keys == global_stack.getMetaDataEntry("supported_actions")

    required_action_keys = [a.getKey() for a in machine_action_manager.getRequiredActions(definition_id)]
    assert required_action_keys == global_stack.getMetaDataEntry("required_actions")

    first_start_action_keys = [a.getKey() for a in machine_action_manager.getFirstStartActions(definition_id)]
    assert first_start_action_keys == global_stack.getMetaDataEntry("first_start_actions")


def test_addMachineAction(machine_action_manager):

    test_action = MachineAction(key = "test_action")
    test_action_2 = MachineAction(key = "test_action_2")
    test_machine = Machine("test_machine")
    machine_action_manager.addMachineAction(test_action)
    machine_action_manager.addMachineAction(test_action_2)

    assert machine_action_manager.getMachineAction("test_action") == test_action
    assert machine_action_manager.getMachineAction("key_that_doesnt_exist") is None

    # Adding the same machine action is not allowed.
    with pytest.raises(NotUniqueMachineActionError):
        machine_action_manager.addMachineAction(test_action)

    # Check that the machine has no supported actions yet.
    assert machine_action_manager.getSupportedActions(test_machine) == list()

    # Check if adding a supported action works.
    machine_action_manager.addSupportedAction(test_machine, "test_action")
    assert machine_action_manager.getSupportedActions(test_machine) == [test_action, ]

    # Check that adding a unknown action doesn't change anything.
    machine_action_manager.addSupportedAction(test_machine, "key_that_doesnt_exist")
    assert machine_action_manager.getSupportedActions(test_machine) == [test_action, ]

    # Check if adding multiple supported actions works.
    machine_action_manager.addSupportedAction(test_machine, "test_action_2")
    assert machine_action_manager.getSupportedActions(test_machine) == [test_action, test_action_2]

    # Check that the machine has no required actions yet.
    assert machine_action_manager.getRequiredActions(test_machine) == list()

    ## Ensure that only known actions can be added.
    with pytest.raises(UnknownMachineActionError):
        machine_action_manager.addRequiredAction(test_machine, "key_that_doesnt_exist")

    ## Check if adding single required action works
    machine_action_manager.addRequiredAction(test_machine, "test_action")
    assert machine_action_manager.getRequiredActions(test_machine) == [test_action, ]

    # Check if adding multiple required actions works.
    machine_action_manager.addRequiredAction(test_machine, "test_action_2")
    assert machine_action_manager.getRequiredActions(test_machine) == [test_action, test_action_2]

    # Ensure that firstStart actions are empty by default.
    assert machine_action_manager.getFirstStartActions(test_machine) == []

    # Check if adding multiple (the same) actions to first start actions work.
    machine_action_manager.addFirstStartAction(test_machine, "test_action")
    machine_action_manager.addFirstStartAction(test_machine, "test_action")
    assert machine_action_manager.getFirstStartActions(test_machine) == [test_action, test_action]

    # Adding unknown action should not crash.
    machine_action_manager.addFirstStartAction(test_machine, "key_that_doesnt_exists")


def test_removeMachineAction(machine_action_manager):
    test_action = MachineAction(key="test_action")
    machine_action_manager.addMachineAction(test_action)

    # Remove the machine action
    machine_action_manager.removeMachineAction(test_action)
    assert machine_action_manager.getMachineAction("test_action") is None

    # Attempting to remove it again should not crash.
    machine_action_manager.removeMachineAction(test_action)