TestPrintInformation.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import functools
  2. from UM.Qt.Duration import Duration
  3. from cura.UI import PrintInformation
  4. from cura.Settings.MachineManager import MachineManager
  5. from unittest.mock import MagicMock, patch
  6. from UM.MimeTypeDatabase import MimeTypeDatabase, MimeType
  7. def preferencesGetValue(key: str):
  8. if key == "cura/job_name_template":
  9. return "{machine_name_short}_{project_name}"
  10. return '{"omgzomg": {"spool_weight": 10, "spool_cost": 9}}'
  11. def getPrintInformation(printer_name) -> PrintInformation:
  12. mock_application = MagicMock(name = "mock_application")
  13. mocked_preferences = MagicMock(name="mocked_preferences")
  14. mocked_extruder_stack = MagicMock()
  15. mocked_extruder_stack.getProperty = MagicMock(return_value = 3)
  16. mocked_material = MagicMock(name= "mocked material")
  17. mocked_material.getMetaDataEntry = MagicMock(return_value = "omgzomg")
  18. mocked_extruder_stack.material = mocked_material
  19. mock_application.getInstance = MagicMock(return_value = mock_application)
  20. mocked_preferences.getValue = MagicMock(side_effect = preferencesGetValue)
  21. global_container_stack = MagicMock()
  22. global_container_stack.definition.getName = MagicMock(return_value = printer_name)
  23. mock_application.getGlobalContainerStack = MagicMock(return_value = global_container_stack)
  24. mock_application.getPreferences = MagicMock(return_value = mocked_preferences)
  25. multi_build_plate_model = MagicMock()
  26. multi_build_plate_model.maxBuildPlate = 0
  27. mock_application.getMultiBuildPlateModel = MagicMock(return_value = multi_build_plate_model)
  28. # Mock-up the entire machine manager except the function that needs to be tested: getAbbreviatedMachineName
  29. original_get_abbreviated_name = MachineManager.getAbbreviatedMachineName
  30. mock_machine_manager = MagicMock()
  31. mock_machine_manager.getAbbreviatedMachineName = functools.partial(original_get_abbreviated_name, mock_machine_manager)
  32. mock_application.getMachineManager = MagicMock(return_value = mock_machine_manager)
  33. with patch("UM.Application.Application.getInstance", MagicMock(return_value = mock_application)):
  34. with patch("json.loads", lambda x: {}):
  35. print_information = PrintInformation.PrintInformation(mock_application)
  36. return print_information
  37. def setup_module():
  38. MimeTypeDatabase.addMimeType(
  39. MimeType(
  40. name = "application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  41. comment = "3MF",
  42. suffixes = ["3mf"]
  43. )
  44. )
  45. MimeTypeDatabase.addMimeType(
  46. MimeType(
  47. name = "application/x-cura-gcode-file",
  48. comment = "Cura GCode File",
  49. suffixes = ["gcode"]
  50. )
  51. )
  52. def test_duration():
  53. print_information = getPrintInformation("ultimaker")
  54. feature_print_times = print_information.getFeaturePrintTimes()
  55. assert int(feature_print_times["Travel"]) == int(Duration(None))
  56. # Ensure that all print times are zero-ed
  57. print_information.setToZeroPrintInformation()
  58. assert int(feature_print_times["Travel"]) == 0
  59. # Fake a print duration message
  60. print_information._onPrintDurationMessage(0, {"travel": 20}, [10])
  61. # We only set a single time, so the total time must be of the same value.
  62. assert int(print_information.currentPrintTime) == 20
  63. feature_print_times = print_information.getFeaturePrintTimes()
  64. assert int(feature_print_times["Travel"]) == 20
  65. print_information.setToZeroPrintInformation()
  66. assert int(feature_print_times["Travel"]) == 0
  67. def test_setProjectName():
  68. print_information = getPrintInformation("ultimaker")
  69. # Test simple name
  70. project_name = ["HelloWorld", ".3mf"]
  71. print_information.setProjectName(project_name[0] + project_name[1])
  72. assert "UM_" + project_name[0] == print_information._job_name
  73. # Test the name with one dot
  74. project_name = ["Hello.World", ".3mf"]
  75. print_information.setProjectName(project_name[0] + project_name[1])
  76. assert "UM_" + project_name[0] == print_information._job_name
  77. # Test the name with two dot
  78. project_name = ["Hello.World.World", ".3mf"]
  79. print_information.setProjectName(project_name[0] + project_name[1])
  80. assert "UM_" + project_name[0] == print_information._job_name
  81. # Test the name with dot at the beginning
  82. project_name = [".Hello.World", ".3mf"]
  83. print_information.setProjectName(project_name[0] + project_name[1])
  84. assert "UM_" + project_name[0] == print_information._job_name
  85. # Test the name with underline
  86. project_name = ["Hello_World", ".3mf"]
  87. print_information.setProjectName(project_name[0] + project_name[1])
  88. assert "UM_" + project_name[0] == print_information._job_name
  89. # Test gcode extension
  90. project_name = ["Hello_World", ".gcode"]
  91. print_information.setProjectName(project_name[0] + project_name[1])
  92. assert "UM_" + project_name[0] == print_information._job_name
  93. # Test empty project name
  94. project_name = ["", ""]
  95. print_information.setProjectName(project_name[0] + project_name[1])
  96. assert print_information.UNTITLED_JOB_NAME == print_information._job_name
  97. # Test wrong file extension
  98. project_name = ["Hello_World", ".test"]
  99. print_information.setProjectName(project_name[0] + project_name[1])
  100. assert "UM_" + project_name[0] != print_information._job_name
  101. def test_setJobName():
  102. print_information = getPrintInformation("ultimaker")
  103. print_information._abbr_machine = "UM"
  104. print_information.setJobName("UM_HelloWorld", is_user_specified_job_name = False)
  105. def test_defineAbbreviatedMachineName():
  106. printer_name = "Test"
  107. print_information = getPrintInformation(printer_name)
  108. # Test not ultimaker printer, name suffix should have first letter from the printer name
  109. project_name = ["HelloWorld", ".3mf"]
  110. print_information.setProjectName(project_name[0] + project_name[1])
  111. assert printer_name[0] + "_" + project_name[0] == print_information._job_name