-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
70 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,70 @@ | ||
from _ctypes import COMError | ||
from ctypes import byref, POINTER | ||
import contextlib | ||
import unittest | ||
|
||
import comtypes | ||
from comtypes.client import GetModule, CreateObject | ||
from comtypes import hresult, GUID | ||
|
||
with contextlib.redirect_stdout(None): # supress warnings | ||
GetModule("msvidctl.dll") | ||
from comtypes.gen import MSVidCtlLib as msvidctl | ||
from comtypes.gen.MSVidCtlLib import IBindCtx, IMoniker, IRunningObjectTable | ||
|
||
|
||
MKSYS_ITEMMONIKER = 4 | ||
ROTFLAGS_ALLOWANYCLIENT = 1 | ||
|
||
|
||
def _create_item_moniker(delim: str, item: str) -> IMoniker: | ||
mon = POINTER(IMoniker)() | ||
comtypes._ole32.CreateItemMoniker(delim, item, byref(mon)) | ||
return mon # type: ignore | ||
|
||
|
||
def _create_bctx() -> IBindCtx: | ||
bctx = POINTER(IBindCtx)() | ||
# The first parameter is reserved and must be 0. | ||
comtypes._ole32.CreateBindCtx(0, byref(bctx)) | ||
return bctx # type: ignore | ||
|
||
|
||
def _create_rot() -> IRunningObjectTable: | ||
rot = POINTER(IRunningObjectTable)() | ||
# The first parameter is reserved and must be 0. | ||
comtypes._ole32.GetRunningObjectTable(0, byref(rot)) | ||
return rot # type: ignore | ||
|
||
|
||
class Test_IMoniker(unittest.TestCase): | ||
def test_IsSystemMoniker(self): | ||
item_id = str(GUID.create_new()) | ||
mon = _create_item_moniker("!", item_id) | ||
self.assertEqual(mon.IsSystemMoniker(), MKSYS_ITEMMONIKER) | ||
|
||
|
||
class Test_IBindCtx(unittest.TestCase): | ||
def test_EnumObjectParam(self): | ||
bctx = _create_bctx() | ||
with self.assertRaises(COMError) as err_ctx: | ||
# calling `EnumObjectParam` results in a return value of E_NOTIMPL. | ||
# https://learn.microsoft.com/en-us/windows/win32/api/objidl/nf-objidl-ibindctx-enumobjectparam#notes-to-callers | ||
bctx.EnumObjectParam() | ||
self.assertEqual(err_ctx.exception.hresult, hresult.E_NOTIMPL) | ||
|
||
|
||
class Test_IRunningObjectTable(unittest.TestCase): | ||
def test_register_and_revoke_item_moniker(self): | ||
vidctl = CreateObject(msvidctl.MSVidCtl, interface=msvidctl.IMSVidCtl) | ||
item_id = str(GUID.create_new()) | ||
mon = _create_item_moniker("!", item_id) | ||
rot = _create_rot() | ||
bctx = _create_bctx() | ||
self.assertEqual(mon.IsRunning(bctx, None, None), hresult.S_FALSE) | ||
dw_reg = rot.Register(ROTFLAGS_ALLOWANYCLIENT, vidctl, mon) | ||
self.assertEqual(mon.IsRunning(bctx, None, None), hresult.S_OK) | ||
self.assertEqual(f"!{item_id}", mon.GetDisplayName(bctx, None)) | ||
self.assertEqual(rot.GetObject(mon).QueryInterface(msvidctl.IMSVidCtl), vidctl) | ||
rot.Revoke(dw_reg) | ||
self.assertEqual(mon.IsRunning(bctx, None, None), hresult.S_FALSE) |