def test_addRemoveOutputDevice(): manager = OutputDeviceManager() manager.outputDevicesChanged.emit = MagicMock() manager.activeDeviceChanged.emit = MagicMock() output_device = OutputDevice("test_device_one") output_device.setPriority(2) output_device_2 = OutputDevice("test_device_two") output_device_2.setPriority(9001) manager.addOutputDevice(output_device) assert manager.outputDevicesChanged.emit.call_count == 1 assert manager.getOutputDevice("test_device_one") == output_device # Our new device is also the one with the highest priority (as it's the only one). So it should be active. assert manager.getActiveDevice() == output_device manager.addOutputDevice(output_device) assert manager.outputDevicesChanged.emit.call_count == 1 manager.addOutputDevice(output_device_2) assert manager.outputDevicesChanged.emit.call_count == 2 # We added a new device with a higher priority, so that one should be the active one assert manager.getActiveDevice() == output_device_2 assert set([output_device, output_device_2]) == set(manager.getOutputDevices()) assert set(["test_device_one", "test_device_two"]) == set(manager.getOutputDeviceIds()) # Try to manually change the active device a few times manager.setActiveDevice("test_device_one") assert manager.activeDeviceChanged.emit.call_count == 3 assert manager.getActiveDevice() == output_device manager.setActiveDevice("test_device_two") assert manager.activeDeviceChanged.emit.call_count == 4 assert manager.getActiveDevice() == output_device_2 # As usual, doing it twice shouldn't cause more updates manager.setActiveDevice("test_device_two") assert manager.activeDeviceChanged.emit.call_count == 4 manager.setActiveDevice("Whatever") # Simply shouldn't cause issues. # Time to remove the device again assert manager.removeOutputDevice("test_device_two") assert manager.getActiveDevice() == output_device assert manager.outputDevicesChanged.emit.call_count == 3 # Trying to remove it again should return false assert not manager.removeOutputDevice("test_device_two") assert manager.outputDevicesChanged.emit.call_count == 3
def test_addRemoveOutputDevice(): manager = OutputDeviceManager() manager.outputDevicesChanged.emit = MagicMock() manager.activeDeviceChanged.emit = MagicMock() output_device = OutputDevice("test_device_one") output_device.setPriority(2) output_device_2 = OutputDevice("test_device_two") output_device_2.setPriority(9001) manager.addOutputDevice(output_device) assert manager.outputDevicesChanged.emit.call_count == 1 assert manager.getOutputDevice("test_device_one") == output_device # Our new device is also the one with the highest priority (as it's the only one). So it should be active. assert manager.getActiveDevice() == output_device manager.addOutputDevice(output_device) assert manager.outputDevicesChanged.emit.call_count == 1 manager.addOutputDevice(output_device_2) assert manager.outputDevicesChanged.emit.call_count == 2 # We added a new device with a higher priority, so that one should be the active one assert manager.getActiveDevice() == output_device_2 assert set([output_device, output_device_2]) == set(manager.getOutputDevices()) assert set(["test_device_one", "test_device_two"]) == set(manager.getOutputDeviceIds()) # Try to manually change the active device a few times manager.setActiveDevice("test_device_one") assert manager.activeDeviceChanged.emit.call_count == 3 assert manager.getActiveDevice() == output_device manager.setActiveDevice("test_device_two") assert manager.activeDeviceChanged.emit.call_count == 4 assert manager.getActiveDevice() == output_device_2 # As usual, doing it twice shouldn't cause more updates manager.setActiveDevice("test_device_two") assert manager.activeDeviceChanged.emit.call_count == 4 manager.setActiveDevice("Whatever") # Simply shouldn't cause issues. # Time to remove the device again assert manager.removeOutputDevice("test_device_two") assert manager.getActiveDevice() == output_device assert manager.outputDevicesChanged.emit.call_count == 3 # Trying to remove it again should return false assert not manager.removeOutputDevice("test_device_two") assert manager.outputDevicesChanged.emit.call_count == 3
class TestCloudOutputDeviceManager(TestCase): maxDiff = None URL = CuraCloudAPIRoot + "/connect/v1/clusters" def setUp(self): super().setUp() self.app = MagicMock() self.device_manager = OutputDeviceManager() self.app.getOutputDeviceManager.return_value = self.device_manager self.patches = [patch("UM.Qt.QtApplication.QtApplication.getInstance", return_value=self.app), patch("UM.Application.Application.getInstance", return_value=self.app)] for patched_method in self.patches: patched_method.start() self.network = NetworkManagerMock() self.timer = MagicMock(timeout = FakeSignal()) with patch.object(CloudApiClient, "QNetworkAccessManager", return_value = self.network), \ patch.object(CloudOutputDeviceManager, "QTimer", return_value = self.timer): self.manager = CloudOutputDeviceManager.CloudOutputDeviceManager() self.clusters_response = parseFixture("getClusters") self.network.prepareReply("GET", self.URL, 200, readFixture("getClusters")) def tearDown(self): try: self._beforeTearDown() self.network.flushReplies() self.manager.stop() for patched_method in self.patches: patched_method.stop() finally: super().tearDown() ## Before tear down method we check whether the state of the output device manager is what we expect based on the # mocked API response. def _beforeTearDown(self): # let the network send replies self.network.flushReplies() # get the created devices devices = self.device_manager.getOutputDevices() # TODO: Check active device response_clusters = self.clusters_response.get("data", []) manager_clusters = sorted([device.clusterData.toDict() for device in self.manager._remote_clusters.values()], key=lambda cluster: cluster['cluster_id'], reverse=True) self.assertEqual(response_clusters, manager_clusters) ## Runs the initial request to retrieve the clusters. def _loadData(self): self.manager.start() self.network.flushReplies() def test_device_is_created(self): # just create the cluster, it is checked at tearDown self._loadData() def test_device_is_updated(self): self._loadData() # update the cluster from member variable, which is checked at tearDown self.clusters_response["data"][0]["host_name"] = "New host name" self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self.manager._update_timer.timeout.emit() def test_device_is_removed(self): self._loadData() # delete the cluster from member variable, which is checked at tearDown del self.clusters_response["data"][1] self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self.manager._update_timer.timeout.emit() def test_device_connects_by_cluster_id(self): active_machine_mock = self.app.getGlobalContainerStack.return_value cluster1, cluster2 = self.clusters_response["data"] cluster_id = cluster1["cluster_id"] active_machine_mock.getMetaDataEntry.side_effect = {"um_cloud_cluster_id": cluster_id}.get self._loadData() self.assertTrue(self.device_manager.getOutputDevice(cluster1["cluster_id"]).isConnected()) self.assertIsNone(self.device_manager.getOutputDevice(cluster2["cluster_id"])) self.assertEquals([], active_machine_mock.setMetaDataEntry.mock_calls) def test_device_connects_by_network_key(self): active_machine_mock = self.app.getGlobalContainerStack.return_value cluster1, cluster2 = self.clusters_response["data"] network_key = cluster2["host_name"] + ".ultimaker.local" active_machine_mock.getMetaDataEntry.side_effect = {"um_network_key": network_key}.get self._loadData() self.assertIsNone(self.device_manager.getOutputDevice(cluster1["cluster_id"])) self.assertTrue(self.device_manager.getOutputDevice(cluster2["cluster_id"]).isConnected()) active_machine_mock.setMetaDataEntry.assert_called_with("um_cloud_cluster_id", cluster2["cluster_id"]) @patch.object(CloudOutputDeviceManager, "Message") def test_api_error(self, message_mock): self.clusters_response = { "errors": [{"id": "notFound", "title": "Not found!", "http_status": "404", "code": "notFound"}] } self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self._loadData() message_mock.return_value.show.assert_called_once_with()
class TestCloudOutputDeviceManager(TestCase): maxDiff = None URL = CuraCloudAPIRoot + "/connect/v1/clusters" def setUp(self): super().setUp() self.app = MagicMock() self.device_manager = OutputDeviceManager() self.app.getOutputDeviceManager.return_value = self.device_manager self.patches = [ patch("UM.Qt.QtApplication.QtApplication.getInstance", return_value=self.app), patch("UM.Application.Application.getInstance", return_value=self.app) ] for patched_method in self.patches: patched_method.start() self.network = NetworkManagerMock() self.timer = MagicMock(timeout=FakeSignal()) with patch.object(CloudApiClient, "QNetworkAccessManager", return_value = self.network), \ patch.object(CloudOutputDeviceManager, "QTimer", return_value = self.timer): self.manager = CloudOutputDeviceManager.CloudOutputDeviceManager() self.clusters_response = parseFixture("getClusters") self.network.prepareReply("GET", self.URL, 200, readFixture("getClusters")) def tearDown(self): try: self._beforeTearDown() self.network.flushReplies() self.manager.stop() for patched_method in self.patches: patched_method.stop() finally: super().tearDown() ## Before tear down method we check whether the state of the output device manager is what we expect based on the # mocked API response. def _beforeTearDown(self): # let the network send replies self.network.flushReplies() # get the created devices devices = self.device_manager.getOutputDevices() # TODO: Check active device response_clusters = self.clusters_response.get("data", []) manager_clusters = sorted([ device.clusterData.toDict() for device in self.manager._remote_clusters.values() ], key=lambda cluster: cluster['cluster_id'], reverse=True) self.assertEqual(response_clusters, manager_clusters) ## Runs the initial request to retrieve the clusters. def _loadData(self): self.manager.start() self.network.flushReplies() def test_device_is_created(self): # just create the cluster, it is checked at tearDown self._loadData() def test_device_is_updated(self): self._loadData() # update the cluster from member variable, which is checked at tearDown self.clusters_response["data"][0]["host_name"] = "New host name" self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self.manager._update_timer.timeout.emit() def test_device_is_removed(self): self._loadData() # delete the cluster from member variable, which is checked at tearDown del self.clusters_response["data"][1] self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self.manager._update_timer.timeout.emit() def test_device_connects_by_cluster_id(self): active_machine_mock = self.app.getGlobalContainerStack.return_value cluster1, cluster2 = self.clusters_response["data"] cluster_id = cluster1["cluster_id"] active_machine_mock.getMetaDataEntry.side_effect = { "um_cloud_cluster_id": cluster_id }.get self._loadData() self.assertTrue( self.device_manager.getOutputDevice( cluster1["cluster_id"]).isConnected()) self.assertIsNone( self.device_manager.getOutputDevice(cluster2["cluster_id"])) self.assertEquals([], active_machine_mock.setMetaDataEntry.mock_calls) def test_device_connects_by_network_key(self): active_machine_mock = self.app.getGlobalContainerStack.return_value cluster1, cluster2 = self.clusters_response["data"] network_key = cluster2["host_name"] + ".ultimaker.local" active_machine_mock.getMetaDataEntry.side_effect = { "um_network_key": network_key }.get self._loadData() self.assertIsNone( self.device_manager.getOutputDevice(cluster1["cluster_id"])) self.assertTrue( self.device_manager.getOutputDevice( cluster2["cluster_id"]).isConnected()) active_machine_mock.setMetaDataEntry.assert_called_with( "um_cloud_cluster_id", cluster2["cluster_id"]) @patch.object(CloudOutputDeviceManager, "Message") def test_api_error(self, message_mock): self.clusters_response = { "errors": [{ "id": "notFound", "title": "Not found!", "http_status": "404", "code": "notFound" }] } self.network.prepareReply("GET", self.URL, 200, self.clusters_response) self._loadData() message_mock.return_value.show.assert_called_once_with()