TestPrintInformation.py 5.7 KB

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