TestPrinterOutputDevice.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import pytest
  4. from unittest.mock import MagicMock, patch
  5. from cura.PrinterOutput.Models.ExtruderConfigurationModel import ExtruderConfigurationModel
  6. from cura.PrinterOutput.Models.MaterialOutputModel import MaterialOutputModel
  7. from cura.PrinterOutput.Models.PrinterConfigurationModel import PrinterConfigurationModel
  8. from cura.PrinterOutput.Models.PrinterOutputModel import PrinterOutputModel
  9. from cura.PrinterOutput.PrinterOutputDevice import PrinterOutputDevice
  10. test_validate_data_get_set = [
  11. {"attribute": "connectionText", "value": "yay"},
  12. {"attribute": "connectionState", "value": 1},
  13. ]
  14. @pytest.fixture()
  15. def printer_output_device():
  16. with patch("UM.Application.Application.getInstance"):
  17. return PrinterOutputDevice("whatever")
  18. @pytest.mark.parametrize("data", test_validate_data_get_set)
  19. def test_getAndSet(data, printer_output_device):
  20. model = printer_output_device
  21. # Convert the first letter into a capital
  22. attribute = list(data["attribute"])
  23. attribute[0] = attribute[0].capitalize()
  24. attribute = "".join(attribute)
  25. # mock the correct emit
  26. setattr(model, data["attribute"] + "Changed", MagicMock())
  27. # Attempt to set the value
  28. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  29. getattr(model, "set" + attribute)(data["value"])
  30. # Check if signal fired.
  31. signal = getattr(model, data["attribute"] + "Changed")
  32. assert signal.emit.call_count == 1
  33. # Ensure that the value got set
  34. assert getattr(model, data["attribute"]) == data["value"]
  35. # Attempt to set the value again
  36. getattr(model, "set" + attribute)(data["value"])
  37. # The signal should not fire again
  38. assert signal.emit.call_count == 1
  39. def test_uniqueConfigurations(printer_output_device):
  40. printer = PrinterOutputModel(MagicMock())
  41. # Add a printer and fire the signal that ensures they get hooked up correctly.
  42. printer_output_device._printers = [printer]
  43. printer_output_device._onPrintersChanged()
  44. assert printer_output_device.uniqueConfigurations == []
  45. configuration = PrinterConfigurationModel()
  46. printer.addAvailableConfiguration(configuration)
  47. assert printer_output_device.uniqueConfigurations == [configuration]
  48. # Once the type of printer is set, it's active configuration counts as being set.
  49. # In that case, that should also be added to the list of available configurations
  50. printer.updateType("blarg!")
  51. loaded_material = MaterialOutputModel(guid = "", type = "PLA", color = "Blue", brand = "Generic", name = "Blue PLA")
  52. loaded_left_extruder = ExtruderConfigurationModel(0)
  53. loaded_left_extruder.setMaterial(loaded_material)
  54. loaded_right_extruder = ExtruderConfigurationModel(1)
  55. loaded_right_extruder.setMaterial(loaded_material)
  56. printer.printerConfiguration.setExtruderConfigurations([loaded_left_extruder, loaded_right_extruder])
  57. assert set(printer_output_device.uniqueConfigurations) == set([configuration, printer.printerConfiguration])
  58. def test_uniqueConfigurations_empty_is_filtered_out(printer_output_device):
  59. printer = PrinterOutputModel(MagicMock())
  60. # Add a printer and fire the signal that ensures they get hooked up correctly.
  61. printer_output_device._printers = [printer]
  62. printer_output_device._onPrintersChanged()
  63. printer.updateType("blarg!")
  64. empty_material = MaterialOutputModel(guid = "", type = "empty", color = "empty", brand = "Generic", name = "Empty")
  65. empty_left_extruder = ExtruderConfigurationModel(0)
  66. empty_left_extruder.setMaterial(empty_material)
  67. empty_right_extruder = ExtruderConfigurationModel(1)
  68. empty_right_extruder.setMaterial(empty_material)
  69. printer.printerConfiguration.setExtruderConfigurations([empty_left_extruder, empty_right_extruder])
  70. assert printer_output_device.uniqueConfigurations == []