TestNetworkedPrinterOutputDevice.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import time
  4. from unittest.mock import MagicMock, patch
  5. from PyQt6.QtNetwork import QNetworkAccessManager
  6. from PyQt6.QtCore import QUrl
  7. from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
  8. from cura.PrinterOutput.PrinterOutputDevice import ConnectionState
  9. def test_properties():
  10. properties = { b"firmware_version": b"12", b"printer_type": b"BHDHAHHADAD", b"address": b"ZOMG", b"name": b":(", b"testProp": b"zomg"}
  11. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  12. output_device = NetworkedPrinterOutputDevice(device_id = "test", address = "127.0.0.1", properties = properties)
  13. assert output_device.address == "ZOMG"
  14. assert output_device.firmwareVersion == "12"
  15. assert output_device.printerType == "BHDHAHHADAD"
  16. assert output_device.ipAddress == "127.0.0.1"
  17. assert output_device.name == ":("
  18. assert output_device.key == "test"
  19. assert output_device.getProperties() == properties
  20. assert output_device.getProperty("testProp") == "zomg"
  21. assert output_device.getProperty("whateverr") == ""
  22. def test_authenticationState():
  23. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  24. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  25. output_device.setAuthenticationState(AuthState.Authenticated)
  26. assert output_device.authenticationState == AuthState.Authenticated
  27. def test_post():
  28. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  29. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  30. mocked_network_manager = MagicMock()
  31. output_device._manager = mocked_network_manager
  32. # Create a fake reply (we can't use a QReply, since those are abstract C++)
  33. reply = MagicMock()
  34. reply.operation = MagicMock(return_value=QNetworkAccessManager.Operation.PostOperation)
  35. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  36. mocked_network_manager.post = MagicMock(return_value = reply)
  37. mocked_callback_handler = MagicMock()
  38. output_device.post("whatever", "omgzomg", on_finished = mocked_callback_handler.onFinished)
  39. # So we now fake that the request was successful.
  40. output_device._handleOnFinished(reply)
  41. # We expect to get a callback regarding this.
  42. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  43. def test_get():
  44. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  45. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  46. mocked_network_manager = MagicMock()
  47. output_device._manager = mocked_network_manager
  48. # Create a fake reply (we can't use a QReply, since those are abstract C++)
  49. reply = MagicMock()
  50. reply.operation = MagicMock(return_value=QNetworkAccessManager.Operation.PostOperation)
  51. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  52. mocked_network_manager.get = MagicMock(return_value=reply)
  53. mocked_callback_handler = MagicMock()
  54. output_device.get("whatever", on_finished=mocked_callback_handler.onFinished)
  55. # So we now fake that the request was successful.
  56. output_device._handleOnFinished(reply)
  57. # We expect to get a callback regarding this.
  58. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  59. def test_delete():
  60. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  61. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  62. mocked_network_manager = MagicMock()
  63. output_device._manager = mocked_network_manager
  64. # Create a fake reply (we can't use a QReply, since those are abstract C++)
  65. reply = MagicMock()
  66. reply.operation = MagicMock(return_value=QNetworkAccessManager.Operation.PostOperation)
  67. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  68. mocked_network_manager.deleteResource = MagicMock(return_value=reply)
  69. mocked_callback_handler = MagicMock()
  70. output_device.delete("whatever", on_finished=mocked_callback_handler.onFinished)
  71. # So we now fake that the request was successful.
  72. output_device._handleOnFinished(reply)
  73. # We expect to get a callback regarding this.
  74. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  75. def test_put():
  76. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  77. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  78. mocked_network_manager = MagicMock()
  79. output_device._manager = mocked_network_manager
  80. # Create a fake reply (we can't use a QReply, since those are abstract C++)
  81. reply = MagicMock()
  82. reply.operation = MagicMock(return_value=QNetworkAccessManager.Operation.PostOperation)
  83. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  84. mocked_network_manager.put = MagicMock(return_value = reply)
  85. mocked_callback_handler = MagicMock()
  86. output_device.put("whatever", "omgzomg", on_finished = mocked_callback_handler.onFinished)
  87. # So we now fake that the request was successful.
  88. output_device._handleOnFinished(reply)
  89. # We expect to get a callback regarding this.
  90. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  91. def test_timeout():
  92. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  93. output_device = NetworkedPrinterOutputDevice(device_id = "test", address = "127.0.0.1", properties = {})
  94. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  95. output_device.setConnectionState(ConnectionState.Connected)
  96. assert output_device.connectionState == ConnectionState.Connected
  97. output_device._update()
  98. # Pretend we didn't get any response for 15 seconds
  99. output_device._last_response_time = time.time() - 15
  100. # But we did recently ask for a response!
  101. output_device._last_request_time = time.time() - 5
  102. with patch("cura.CuraApplication.CuraApplication.getInstance"):
  103. output_device._update()
  104. # The connection should now be closed, since it went into timeout.
  105. assert output_device.connectionState == ConnectionState.Closed