Skip to content

Commit

Permalink
add portabledeviceapi test
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd committed Jan 12, 2023
1 parent d470b62 commit 6d136d1
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions comtypes/test/test_portabledevice.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6d136d1

Please sign in to comment.