TestPrinterOutputDevice.py 3.7 KB

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