-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |