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

TestPrintInformation.py « tests - github.com/Ultimaker/Cura.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bfebe4a52886b9cf42e7d5489c5e56ca0e0c63fc (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright (c) 2020 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.

import functools

from UM.Qt.Duration import Duration
from cura.UI import PrintInformation
from cura.Settings.MachineManager import MachineManager

from unittest.mock import MagicMock, patch
from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType


def getPrintInformation(printer_name) -> PrintInformation:

    mock_application = MagicMock(name = "mock_application")
    mocked_preferences = MagicMock(name="mocked_preferences")
    mocked_extruder_stack = MagicMock()
    mocked_extruder_stack.getProperty = MagicMock(return_value = 3)
    mocked_material = MagicMock(name= "mocked material")
    mocked_material.getMetaDataEntry = MagicMock(return_value = "omgzomg")
    mocked_extruder_stack.material = mocked_material

    mock_application.getInstance = MagicMock(return_value = mock_application)
    mocked_preferences.getValue = MagicMock(return_value = '{"omgzomg": {"spool_weight": 10, "spool_cost": 9}}')

    global_container_stack = MagicMock()
    global_container_stack.definition.getName = MagicMock(return_value = printer_name)
    mock_application.getGlobalContainerStack = MagicMock(return_value = global_container_stack)
    mock_application.getPreferences = MagicMock(return_value = mocked_preferences)

    multi_build_plate_model = MagicMock()
    multi_build_plate_model.maxBuildPlate = 0
    mock_application.getMultiBuildPlateModel = MagicMock(return_value = multi_build_plate_model)

    # Mock-up the entire machine manager except the function that needs to be tested: getAbbreviatedMachineName
    original_get_abbreviated_name = MachineManager.getAbbreviatedMachineName
    mock_machine_manager = MagicMock()
    mock_machine_manager.getAbbreviatedMachineName = functools.partial(original_get_abbreviated_name, mock_machine_manager)
    mock_application.getMachineManager = MagicMock(return_value = mock_machine_manager)
    with patch("UM.Application.Application.getInstance", MagicMock(return_value = mock_application)):

        with patch("json.loads", lambda x: {}):
            print_information = PrintInformation.PrintInformation(mock_application)

    return print_information


def setup_module():
     MimeTypeDatabase.addMimeType(
        MimeType(
            name = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
            comment = "3MF",
            suffixes = ["3mf"]
        )
     )

     MimeTypeDatabase.addMimeType(
         MimeType(
             name = "application/x-cura-gcode-file",
             comment = "Cura G-code File",
             suffixes = ["gcode"]
         )
     )


def test_duration():
    print_information = getPrintInformation("ultimaker")

    feature_print_times = print_information.getFeaturePrintTimes()
    assert int(feature_print_times["Travel"]) == int(Duration(None))

    # Ensure that all print times are zero-ed
    print_information.setToZeroPrintInformation()
    assert int(feature_print_times["Travel"]) == 0

    # Fake a print duration message
    print_information._onPrintDurationMessage(0, {"travel": 20}, [10])

    # We only set a single time, so the total time must be of the same value.
    assert int(print_information.currentPrintTime) == 20

    feature_print_times = print_information.getFeaturePrintTimes()
    assert int(feature_print_times["Travel"]) == 20

    print_information.setToZeroPrintInformation()
    assert int(feature_print_times["Travel"]) == 0


def test_setProjectName():

    print_information = getPrintInformation("ultimaker")

    # Test simple name
    project_name = ["HelloWorld", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test the name with one dot
    project_name = ["Hello.World", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test the name with two dot
    project_name = ["Hello.World.World", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test the name with dot at the beginning
    project_name = [".Hello.World", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test the name with underline
    project_name = ["Hello_World", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test gcode extension
    project_name = ["Hello_World", ".gcode"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] == print_information._job_name

    # Test empty project name
    project_name = ["", ""]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert print_information.UNTITLED_JOB_NAME == print_information._job_name

    # Test wrong file extension
    project_name = ["Hello_World", ".test"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert "UM_" + project_name[0] != print_information._job_name


def test_setJobName():

    print_information = getPrintInformation("ultimaker")

    print_information._abbr_machine = "UM"
    print_information.setJobName("UM_HelloWorld", is_user_specified_job_name = False)


def test_defineAbbreviatedMachineName():
    printer_name = "Test"

    print_information = getPrintInformation(printer_name)

    # Test not ultimaker printer, name suffix should have first letter from the printer name
    project_name = ["HelloWorld", ".3mf"]
    print_information.setProjectName(project_name[0] + project_name[1])
    assert printer_name[0] + "_" + project_name[0] == print_information._job_name