|
| 1 | +import env |
| 2 | +import unittest |
| 3 | + |
| 4 | +from hardwarelibrary.physicaldevice import PhysicalDevice |
| 5 | +from hardwarelibrary.communication.serialport import SerialPort |
| 6 | +from hardwarelibrary.powermeters import ( |
| 7 | + FieldMasterDevice, DebugFieldMasterDevice, IntegraDevice, |
| 8 | +) |
| 9 | +from hardwarelibrary.devicemanager import DeviceManager |
| 10 | +import hardwarelibrary.utils as utils |
| 11 | + |
| 12 | + |
| 13 | +class TestGenericConverterFlag(unittest.TestCase): |
| 14 | + def testFieldMasterIsGeneric(self): |
| 15 | + self.assertTrue(FieldMasterDevice.usesGenericSerialConverter) |
| 16 | + |
| 17 | + def testDebugFieldMasterIsNotGeneric(self): |
| 18 | + self.assertFalse(DebugFieldMasterDevice.usesGenericSerialConverter) |
| 19 | + |
| 20 | + def testIntegraIsNotGeneric(self): |
| 21 | + self.assertFalse(IntegraDevice.usesGenericSerialConverter) |
| 22 | + |
| 23 | + def testBaseDefaultIsNotGeneric(self): |
| 24 | + self.assertFalse(PhysicalDevice.usesGenericSerialConverter) |
| 25 | + |
| 26 | + |
| 27 | +class TestGenericConverterVidpids(unittest.TestCase): |
| 28 | + def testGenericExpandsToAllConverterVendors(self): |
| 29 | + expected = [(idVendor, None) for idVendor in SerialPort.genericSerialConverterVendors] |
| 30 | + self.assertEqual(FieldMasterDevice.vidpids(), expected) |
| 31 | + |
| 32 | + def testNonGenericKeepsSinglePair(self): |
| 33 | + self.assertEqual(IntegraDevice.vidpids(), [(0x1ad5, 0x0300)]) |
| 34 | + |
| 35 | + def testDebugKeepsItsOwnPair(self): |
| 36 | + self.assertEqual(DebugFieldMasterDevice.vidpids(), [(0xFFFF, 0xFFF1)]) |
| 37 | + |
| 38 | + |
| 39 | +class TestGenericConverterCompatibility(unittest.TestCase): |
| 40 | + def testMatchesAnyGenericConverterVendorWithAnyProduct(self): |
| 41 | + # Same instrument behind FTDI, Prolific, or CP210x cables (different PIDs) |
| 42 | + self.assertTrue(FieldMasterDevice.isCompatibleWith("*", 0x6001, 0x0403)) |
| 43 | + self.assertTrue(FieldMasterDevice.isCompatibleWith("*", 0x2303, 0x067b)) |
| 44 | + self.assertTrue(FieldMasterDevice.isCompatibleWith("*", 0xEA60, 0x10c4)) |
| 45 | + |
| 46 | + def testRejectsUnknownVendor(self): |
| 47 | + self.assertFalse(FieldMasterDevice.isCompatibleWith("*", 0x0001, 0x9999)) |
| 48 | + |
| 49 | + def testNonGenericStillRequiresExactProduct(self): |
| 50 | + self.assertTrue(IntegraDevice.isCompatibleWith("*", 0x0300, 0x1ad5)) |
| 51 | + self.assertFalse(IntegraDevice.isCompatibleWith("*", 0x9999, 0x1ad5)) |
| 52 | + # No wildcard leak: a None product must not match a concrete pair. |
| 53 | + self.assertFalse(IntegraDevice.isCompatibleWith("*", None, 0x1ad5)) |
| 54 | + |
| 55 | + def testDebugFieldMasterStillConstructs(self): |
| 56 | + device = DebugFieldMasterDevice() # would raise if the flag broke its vidpids |
| 57 | + self.assertEqual(device.idVendor, 0xFFFF) |
| 58 | + self.assertEqual(device.idProduct, 0xFFF1) |
| 59 | + |
| 60 | + |
| 61 | +class TestAutoDiscoveryGuard(unittest.TestCase): |
| 62 | + def testGenericClassesMatchButAreExcludedFromAutoDiscovery(self): |
| 63 | + candidates = utils.getCandidateDeviceClasses(PhysicalDevice, 0x0403, 0x6001) |
| 64 | + self.assertIn(FieldMasterDevice, candidates) # it does match the FTDI cable |
| 65 | + |
| 66 | + discoverable = DeviceManager.candidateClassesForAutoDiscovery(0x0403, 0x6001) |
| 67 | + self.assertNotIn(FieldMasterDevice, discoverable) # ...but is not auto-probed |
| 68 | + for aClass in discoverable: |
| 69 | + self.assertFalse(aClass.usesGenericSerialConverter) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments