From cfde050220eff8e07c51573ddf39fe50c28a9da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Tue, 26 Jan 2021 02:24:16 +0100 Subject: [PATCH] virtual-device: Add ability to close a device with delay or with error --- libfprint/drivers/virtual-device.c | 16 ++++++++++++++++ tests/virtual-device.py | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/libfprint/drivers/virtual-device.c b/libfprint/drivers/virtual-device.c index b32610c..9da1f77 100644 --- a/libfprint/drivers/virtual-device.c +++ b/libfprint/drivers/virtual-device.c @@ -78,6 +78,10 @@ maybe_continue_current_action (FpDeviceVirtualDevice *self) FP_DEVICE_GET_CLASS (self)->delete (dev); break; + case FPI_DEVICE_ACTION_CLOSE: + FP_DEVICE_GET_CLASS (self)->close (dev); + break; + default: break; } @@ -635,8 +639,20 @@ dev_cancel (FpDevice *dev) static void dev_deinit (FpDevice *dev) { + g_autoptr(GError) error = NULL; FpDeviceVirtualDevice *self = FP_DEVICE_VIRTUAL_DEVICE (dev); + process_cmds (self, FALSE, &error); + if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) + { + fpi_device_close_complete (dev, g_steal_pointer (&error)); + return; + } + else if (self->sleep_timeout_id) + { + return; + } + g_clear_handle_id (&self->wait_command_id, g_source_remove); g_clear_handle_id (&self->sleep_timeout_id, g_source_remove); g_cancellable_cancel (self->cancellable); diff --git a/tests/virtual-device.py b/tests/virtual-device.py index bce202f..58eb02e 100644 --- a/tests/virtual-device.py +++ b/tests/virtual-device.py @@ -544,6 +544,27 @@ class VirtualDevice(unittest.TestCase): self.complete_verify() self.assertTrue(self._verify_reported) + def test_close_error(self): + self._close_on_teardown = False + close_res = None + + def on_closed(dev, res): + nonlocal close_res + try: + close_res = dev.close_finish(res) + except GLib.Error as e: + close_res = e + + self.send_command('SLEEP', 100) + self.send_error(FPrint.DeviceError.BUSY) + self.dev.close(callback=on_closed) + self.wait_timeout(2) + self.assertIsNone(close_res) + + while not close_res: + ctx.iteration(True) + + self.assertEqual(close_res.code, int(FPrint.DeviceError.BUSY)) class VirtualDeviceStorage(VirtualDevice):