From 6d136d1c04ac5b6479d8762ce89237351ceb66a4 Mon Sep 17 00:00:00 2001 From: junkmd Date: Thu, 12 Jan 2023 23:48:36 +0900 Subject: [PATCH] add `portabledeviceapi` test --- comtypes/test/test_portabledevice.py | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 comtypes/test/test_portabledevice.py diff --git a/comtypes/test/test_portabledevice.py b/comtypes/test/test_portabledevice.py new file mode 100644 index 00000000..caeb400d --- /dev/null +++ b/comtypes/test/test_portabledevice.py @@ -0,0 +1,60 @@ +import ctypes +import unittest as ut + +import comtypes +import comtypes.client + +comtypes.client.GetModule("portabledeviceapi.dll") +import comtypes.gen.PortableDeviceApiLib as port_api + +comtypes.client.GetModule("portabledevicetypes.dll") +import comtypes.gen.PortableDeviceTypesLib as port_types + + +################################################################ +# +# TODO: +# +# It seems bad that only external test like this can verify +# the behavior of `comtypes` implementation. +# Find a safe way to do this, like having a virtual portable +# device and using the COM API for it. +# +################################################################ + + +class Test_IPortableDevice(ut.TestCase): + # To avoid damaging or changing the environment, do not CREATE, DELETE or UPDATE! + # Do READ only! + def setUp(self): + info = comtypes.client.CreateObject( + port_types.PortableDeviceValues().IPersist_GetClassID(), + clsctx=comtypes.CLSCTX_INPROC_SERVER, + interface=port_types.IPortableDeviceValues, + ) + mng = comtypes.client.CreateObject( + port_api.PortableDeviceManager().IPersist_GetClassID(), + clsctx=comtypes.CLSCTX_INPROC_SERVER, + interface=port_api.IPortableDeviceManager, + ) + p_id_cnt = ctypes.pointer(ctypes.c_ulong()) + mng.GetDevices(ctypes.POINTER(ctypes.c_wchar_p)(), p_id_cnt) + if p_id_cnt.contents.value == 0: + self.skipTest("There is no portable device in the environment.") + dev_ids = (ctypes.c_wchar_p * p_id_cnt.contents.value)() + mng.GetDevices(ctypes.cast(dev_ids, ctypes.POINTER(ctypes.c_wchar_p)), p_id_cnt) + self.device = comtypes.client.CreateObject( + port_api.PortableDevice().IPersist_GetClassID(), + clsctx=comtypes.CLSCTX_INPROC_SERVER, + interface=port_api.IPortableDevice, + ) + self.device.Open(list(dev_ids)[0], info) + + def test_EnumObjects(self): + enumobj = self.device.Content().EnumObjects(ctypes.c_ulong(0), "DEVICE", None) + self.assertIsInstance(enumobj, port_api.IEnumPortableDeviceObjectIDs) + [_ for _ in enumobj] # is iterable? + + +if __name__ == "__main__": + ut.main()