diff --git a/tests/virtual-device.py b/tests/virtual-device.py index 30d9705..1f9d76c 100644 --- a/tests/virtual-device.py +++ b/tests/virtual-device.py @@ -94,6 +94,16 @@ class VirtualDevice(unittest.TestCase): self.assertFalse(self.dev.is_open()) super().tearDown() + def wait_timeout(self, interval): + timeout_reached = False + def on_timeout(): + nonlocal timeout_reached + timeout_reached = True + + GLib.timeout_add(interval, on_timeout) + while not timeout_reached: + ctx.iteration(False) + def send_command(self, command, *args): self.assertIn(command, ['INSERT', 'REMOVE', 'SCAN', 'ERROR', 'RETRY', 'FINGER', 'UNPLUG', 'SLEEP', 'SET_ENROLL_STAGES', 'SET_SCAN_TYPE', @@ -458,55 +468,33 @@ class VirtualDevice(unittest.TestCase): self.dev.close_sync() def test_device_sleep(self): - timeout_reached = False - def on_timeout(): - nonlocal timeout_reached - timeout_reached = True - self.send_command('SLEEP', 1500) - GLib.timeout_add(300, on_timeout) self.start_verify(FPrint.Print.new(self.dev), identify=self.dev.supports_identify()) - while not timeout_reached: - ctx.iteration(False) + self.wait_timeout(300) self.assertFalse(self._verify_completed) - timeout_reached = False self._cancellable.cancel() - GLib.timeout_add(200, on_timeout) - - while not timeout_reached: - ctx.iteration(False) + self.wait_timeout(200) self.assertTrue(self._verify_completed) self.cancel_verify() def test_device_sleep_on_cancellation(self): - timeout_reached = False - def on_timeout(): - nonlocal timeout_reached - timeout_reached = True - self.send_command('SET_CANCELLATION_ENABLED', int(False)) self.send_command('SLEEP', 1500) self.send_command('SCAN', 'foo-print') - GLib.timeout_add(300, on_timeout) self.start_verify(FPrint.Print.new(self.dev), identify=self.dev.supports_identify()) - while not timeout_reached: - ctx.iteration(False) + self.wait_timeout(300) self.assertFalse(self._verify_completed) - timeout_reached = False self._cancellable.cancel() - GLib.timeout_add(300, on_timeout) - - while not timeout_reached: - ctx.iteration(False) + self.wait_timeout(300) self.assertFalse(self._verify_completed) self.cancel_verify()