virtual-device: Implement UNPLUG operation

This commit is contained in:
Marco Trevisan (Treviño) 2021-01-25 14:16:12 +01:00
parent 67cb61cc18
commit c3ece8621d
2 changed files with 37 additions and 5 deletions

View file

@ -44,6 +44,7 @@ G_DEFINE_TYPE (FpDeviceVirtualDevice, fpi_device_virtual_device, FP_TYPE_DEVICE)
#define SET_SCAN_TYPE_PREFIX "SET_SCAN_TYPE " #define SET_SCAN_TYPE_PREFIX "SET_SCAN_TYPE "
#define LIST_CMD "LIST" #define LIST_CMD "LIST"
#define UNPLUG_CMD "UNPLUG"
static void static void
maybe_continue_current_action (FpDeviceVirtualDevice *self) maybe_continue_current_action (FpDeviceVirtualDevice *self)
@ -198,6 +199,10 @@ recv_instruction_cb (GObject *source_object,
if (self->prints_storage) if (self->prints_storage)
g_hash_table_foreach (self->prints_storage, write_key_to_listener, listener); g_hash_table_foreach (self->prints_storage, write_key_to_listener, listener);
} }
else if (g_str_has_prefix (cmd, UNPLUG_CMD))
{
fpi_device_remove (FP_DEVICE (self));
}
else if (g_str_has_prefix (cmd, SET_ENROLL_STAGES_PREFIX)) else if (g_str_has_prefix (cmd, SET_ENROLL_STAGES_PREFIX))
{ {
guint stages; guint stages;

View file

@ -82,11 +82,13 @@ class VirtualDevice(unittest.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self._close_on_teardown = True
self.assertFalse(self.dev.is_open()) self.assertFalse(self.dev.is_open())
self.dev.open_sync() self.dev.open_sync()
self.assertTrue(self.dev.is_open()) self.assertTrue(self.dev.is_open())
def tearDown(self): def tearDown(self):
if self._close_on_teardown:
self.assertTrue(self.dev.is_open()) self.assertTrue(self.dev.is_open())
self.dev.close_sync() self.dev.close_sync()
self.assertFalse(self.dev.is_open()) self.assertFalse(self.dev.is_open())
@ -94,7 +96,7 @@ class VirtualDevice(unittest.TestCase):
def send_command(self, command, *args): def send_command(self, command, *args):
self.assertIn(command, ['INSERT', 'REMOVE', 'SCAN', 'ERROR', 'RETRY', self.assertIn(command, ['INSERT', 'REMOVE', 'SCAN', 'ERROR', 'RETRY',
'FINGER', 'SET_ENROLL_STAGES', 'SET_SCAN_TYPE']) 'FINGER', 'UNPLUG', 'SET_ENROLL_STAGES', 'SET_SCAN_TYPE'])
with Connection(self.sockaddr) as con: with Connection(self.sockaddr) as con:
params = ' '.join(str(p) for p in args) params = ' '.join(str(p) for p in args)
@ -373,6 +375,30 @@ class VirtualDevice(unittest.TestCase):
GLib.test_assert_expected_messages_internal('libfprint-device', GLib.test_assert_expected_messages_internal('libfprint-device',
__file__, 0, 'test_change_scan_type') __file__, 0, 'test_change_scan_type')
def test_device_unplug(self):
self._close_on_teardown = False
notified_spec = None
def on_removed_notify(dev, spec):
nonlocal notified_spec
notified_spec = spec
removed = False
def on_removed(dev):
nonlocal removed
removed = True
self.assertFalse(self.dev.props.removed)
self.dev.connect('notify::removed', on_removed_notify)
self.dev.connect('removed', on_removed)
self.send_command('UNPLUG')
self.assertEqual(notified_spec.name, 'removed')
self.assertTrue(self.dev.props.removed)
self.assertTrue(removed)
with self.assertRaisesRegex(GLib.GError, 'device has been removed from the system'):
self.dev.close_sync()
class VirtualDeviceStorage(VirtualDevice): class VirtualDeviceStorage(VirtualDevice):
def tearDown(self): def tearDown(self):
@ -380,6 +406,7 @@ class VirtualDeviceStorage(VirtualDevice):
super().tearDown() super().tearDown()
def cleanup_device_storage(self): def cleanup_device_storage(self):
if self.dev.is_open() and not self.dev.props.removed:
for print in self.dev.list_prints_sync(): for print in self.dev.list_prints_sync():
self.assertTrue(self.dev.delete_print_sync(print, None)) self.assertTrue(self.dev.delete_print_sync(print, None))