Skip to content

Commit

Permalink
Fix bug in bdDetect device registration affecting Albatross (#17585)
Browse files Browse the repository at this point in the history
Fixes #17537 (comment)

Summary of the issue:
In #17537, I introduced aa new addUsbDevice method on DriverRegistrar, but it had parameter ordering wrong.

Description of user facing changes
Albatross detection should work again.

Description of development approach
Fixed order. I added a bunch of unit tests to avoid this in the future.
  • Loading branch information
LeonarddeR authored Jan 7, 2025
1 parent 8cc88ea commit 1edb075
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
11 changes: 9 additions & 2 deletions source/bdDetect.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,9 @@ def addUsbDevice(
)
devs = self._getDriverDict()
driverUsb = devs[CommunicationType.USB]
driverUsb.add(_UsbDeviceRegistryEntry(type, id, useAsFallback, matchFunc))
driverUsb.add(
_UsbDeviceRegistryEntry(id=id, type=type, useAsFallback=useAsFallback, matchFunc=matchFunc),
)

def addUsbDevices(
self,
Expand Down Expand Up @@ -774,7 +776,12 @@ def addUsbDevices(
)
devs = self._getDriverDict()
driverUsb = devs[CommunicationType.USB]
driverUsb.update((_UsbDeviceRegistryEntry(id, type, useAsFallback, matchFunc) for id in ids))
driverUsb.update(
(
_UsbDeviceRegistryEntry(id=id, type=type, useAsFallback=useAsFallback, matchFunc=matchFunc)
for id in ids
)
)

def addBluetoothDevices(self, matchFunc: MatchFuncT):
"""Associate Bluetooth HID or COM ports with the driver on this instance.
Expand Down
2 changes: 1 addition & 1 deletion source/brailleDisplayDrivers/albatross/driver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A part of NonVisual Desktop Access (NVDA)
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
# Copyright (C) 2023 NV Access Limited, Burman's Computer and Education Ltd.
# Copyright (C) 2023-25 NV Access Limited, Burman's Computer and Education Ltd., Leonard de Ruijter

"""Main code for Tivomatic Caiku Albatross braille display driver.
Communication with display is done here. See class L{BrailleDisplayDriver}
Expand Down
70 changes: 69 additions & 1 deletion tests/unit/test_bdDetect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A part of NonVisual Desktop Access (NVDA)
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
# Copyright (C) 2023 NV Access Limited, Babbage B.V., Leonard de Ruijter
# Copyright (C) 2023-25 NV Access Limited, Babbage B.V., Leonard de Ruijter

"""Unit tests for the bdDetect module."""

Expand Down Expand Up @@ -31,3 +31,71 @@ def test_scanForDevices(self):
shouldStopEvaluator=lambda detector: detector is None,
)
self.assertTrue(success)


class TestDriverRegistration(unittest.TestCase):
"""A test for driver device registration."""

def tearDown(self):
bdDetect._driverDevices.clear()

def test_addUsbDevice(self):
"""Test adding a USB device."""
from brailleDisplayDrivers import albatross

registrar = bdDetect.DriverRegistrar(albatross.BrailleDisplayDriver.name)

def matchFunc(match: bdDetect.DeviceMatch) -> bool:
return match.deviceInfo.get("busReportedDeviceDescription") == albatross.driver.BUS_DEVICE_DESC

registrar.addUsbDevice(
bdDetect.ProtocolType.SERIAL,
albatross.driver.VID_AND_PID,
matchFunc=matchFunc,
)
expected = bdDetect._UsbDeviceRegistryEntry(
albatross.driver.VID_AND_PID,
bdDetect.ProtocolType.SERIAL,
matchFunc=matchFunc,
)
self.assertIn(expected, registrar._getDriverDict().get(bdDetect.CommunicationType.USB))

def test_addUsbDevices(self):
"""Test adding multiple USB devices."""
from brailleDisplayDrivers import albatross

registrar = bdDetect.DriverRegistrar(albatross.BrailleDisplayDriver.name)

def matchFunc(match: bdDetect.DeviceMatch) -> bool:
return match.deviceInfo.get("busReportedDeviceDescription") == albatross.driver.BUS_DEVICE_DESC

fakeVidAndPid = "VID_0403&PID_6002"
registrar.addUsbDevices(
bdDetect.ProtocolType.SERIAL,
{albatross.driver.VID_AND_PID, fakeVidAndPid},
matchFunc=matchFunc,
)
expected = bdDetect._UsbDeviceRegistryEntry(
albatross.driver.VID_AND_PID,
bdDetect.ProtocolType.SERIAL,
matchFunc=matchFunc,
)
self.assertIn(expected, registrar._getDriverDict().get(bdDetect.CommunicationType.USB))
expected2 = bdDetect._UsbDeviceRegistryEntry(
fakeVidAndPid,
bdDetect.ProtocolType.SERIAL,
matchFunc=matchFunc,
)
self.assertIn(expected2, registrar._getDriverDict().get(bdDetect.CommunicationType.USB))

def test_addBluetoothDevices(self):
"""Test adding a fake Bluetooth match func."""
from brailleDisplayDrivers import albatross

registrar = bdDetect.DriverRegistrar(albatross.BrailleDisplayDriver.name)

def matchFunc(match: bdDetect.DeviceMatch) -> bool:
return True

registrar.addBluetoothDevices(matchFunc)
self.assertEqual(registrar._getDriverDict().get(bdDetect.CommunicationType.BLUETOOTH), matchFunc)

0 comments on commit 1edb075

Please sign in to comment.