virtual-device: Use python's with idiom to check GLib expected error messages

And we can properly provide a real traceback as well
This commit is contained in:
Marco Trevisan (Treviño) 2021-01-28 00:47:24 +01:00
parent d90ee96df8
commit c495b82000

View file

@ -13,6 +13,7 @@ try:
import struct import struct
import subprocess import subprocess
import shutil import shutil
import traceback
import glob import glob
import tempfile import tempfile
except Exception as e: except Exception as e:
@ -46,6 +47,21 @@ class Connection:
self.con.close() self.con.close()
del self.con del self.con
class GLibErrorMessage:
def __init__(self, component, level, expected_message):
self.level = level
self.component = component
self.expected_message = expected_message
def __enter__(self):
GLib.test_expect_message(self.component, self.level, self.expected_message)
def __exit__(self, exc_type, exc_val, exc_tb):
(filename, line, func_name, text) = traceback.extract_stack()[-2]
GLib.test_assert_expected_messages_internal(self.component,
filename, line, func_name)
class VirtualDevice(unittest.TestCase): class VirtualDevice(unittest.TestCase):
@classmethod @classmethod
@ -416,14 +432,12 @@ class VirtualDevice(unittest.TestCase):
self.assertEqual(self.dev.get_nr_enroll_stages(), 1) self.assertEqual(self.dev.get_nr_enroll_stages(), 1)
self.assertEqual(notified_spec.name, 'nr-enroll-stages') self.assertEqual(notified_spec.name, 'nr-enroll-stages')
GLib.test_expect_message('libfprint-device', with GLibErrorMessage('libfprint-device',
GLib.LogLevelFlags.LEVEL_CRITICAL, '*enroll_stages > 0*') GLib.LogLevelFlags.LEVEL_CRITICAL, '*enroll_stages > 0*'):
notified_spec = None notified_spec = None
self.send_command('SET_ENROLL_STAGES', 0) self.send_command('SET_ENROLL_STAGES', 0)
self.assertEqual(self.dev.get_nr_enroll_stages(), 1) self.assertEqual(self.dev.get_nr_enroll_stages(), 1)
self.assertIsNone(notified_spec) self.assertIsNone(notified_spec)
GLib.test_assert_expected_messages_internal('libfprint-device',
__file__, 0, 'test_change_enroll_stages')
def test_quick_enroll(self): def test_quick_enroll(self):
self.send_command('SET_ENROLL_STAGES', 1) self.send_command('SET_ENROLL_STAGES', 1)
@ -446,14 +460,12 @@ class VirtualDevice(unittest.TestCase):
self.assertEqual(self.dev.get_scan_type(), scan_type) self.assertEqual(self.dev.get_scan_type(), scan_type)
self.assertEqual(notified_spec.name, 'scan-type') self.assertEqual(notified_spec.name, 'scan-type')
GLib.test_expect_message('libfprint-virtual_device', with GLibErrorMessage('libfprint-virtual_device',
GLib.LogLevelFlags.LEVEL_WARNING, '*Scan type*not found') GLib.LogLevelFlags.LEVEL_WARNING, '*Scan type*not found'):
notified_spec = None notified_spec = None
self.send_command('SET_SCAN_TYPE', 'eye-contact') self.send_command('SET_SCAN_TYPE', 'eye-contact')
self.assertEqual(self.dev.get_scan_type(), FPrint.ScanType.SWIPE) self.assertEqual(self.dev.get_scan_type(), FPrint.ScanType.SWIPE)
self.assertIsNone(notified_spec) self.assertIsNone(notified_spec)
GLib.test_assert_expected_messages_internal('libfprint-device',
__file__, 0, 'test_change_scan_type')
def test_device_unplug(self): def test_device_unplug(self):
self._close_on_teardown = False self._close_on_teardown = False