TestPrintInformation.py 5.8 KB

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