Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_monikers. #606

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions comtypes/test/test_monikers.py
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)