|
| 1 | +import ctypes |
| 2 | +import gc |
| 3 | +import weakref |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import windows |
| 8 | +import windows.com |
| 9 | + |
| 10 | +import windows.generated_def as gdef |
| 11 | + |
| 12 | +# ICallFrameEvents is a good candidate for testing : |
| 13 | +# - only 1 fonction (not counting IUnkown()) |
| 14 | +# - Only 1 param for the function |
| 15 | + |
| 16 | +# class ICallFrameEventsImplem(windows.com.COMImplementation): |
| 17 | +# IMPLEMENT = ICallFrameEvents |
| 18 | +# |
| 19 | +# def OnCall(self, This, pFrame): |
| 20 | +# print('ICallFrameEvents.OnCall') |
| 21 | +# return E_NOTIMPL |
| 22 | + |
| 23 | +class ICallFrameEventsImplemIncomplete(windows.com.COMImplementation): |
| 24 | + IMPLEMENT = gdef.ICallFrameEvents |
| 25 | + |
| 26 | + |
| 27 | +def test_com_implementation_incomplete(): |
| 28 | + with pytest.raises(ValueError): |
| 29 | + obj = ICallFrameEventsImplemIncomplete() |
| 30 | + |
| 31 | + |
| 32 | +class ICallFrameEventsImplemSimple(windows.com.COMImplementation): |
| 33 | + IMPLEMENT = gdef.ICallFrameEvents |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + super(ICallFrameEventsImplemSimple, self).__init__() |
| 37 | + self.called = 0 |
| 38 | + |
| 39 | + def OnCall(self, This, pFrame): |
| 40 | + self.called += 1 |
| 41 | + return self.called |
| 42 | + |
| 43 | +def test_com_implementation_simple(): |
| 44 | + obj = ICallFrameEventsImplemSimple() |
| 45 | + assert obj.com_refcount == 1 |
| 46 | + assert obj.AddRef() == 2 |
| 47 | + assert obj.Release() == 1 |
| 48 | + assert id(obj) in obj._get_keepalive_registry() |
| 49 | + assert obj.Release() == 0 |
| 50 | + assert id(obj) not in obj._get_keepalive_registry() |
| 51 | + |
| 52 | +def test_com_implementation_query_interface(): |
| 53 | + obj = ICallFrameEventsImplemSimple() |
| 54 | + iunk = gdef.IUnknown() |
| 55 | + # Emulate a call from C code |
| 56 | + obj.QueryInterface(obj._as_parameter_, ctypes.pointer(gdef.IUnknown.IID), ctypes.pointer(iunk)) |
| 57 | + assert obj._as_parameter_ == iunk.value |
| 58 | + assert obj.com_refcount == 2 # +1 on QueryInterface |
| 59 | + assert iunk.Release() == 1 # We are now working via ctypes/vtable call |
| 60 | + |
| 61 | +def test_com_implementation_keep_alive(): |
| 62 | + obj = ICallFrameEventsImplemSimple() |
| 63 | + # Create a C-like pointer from the interface |
| 64 | + objcom = gdef.ICallFrameEvents(obj._as_parameter_) |
| 65 | + assert objcom.AddRef() == 2 |
| 66 | + wref = weakref.ref(obj) |
| 67 | + assert wref() is obj # Check object is still alive in python world |
| 68 | + del obj # No more python-side direct reference |
| 69 | + gc.collect() # Force gc : code below would crash without keep-alive logic |
| 70 | + assert objcom.Release() == 1 |
| 71 | + assert objcom.OnCall(None) == 1 # Count the number of call : proof of correct python-side execution |
| 72 | + assert objcom.OnCall(None) == 2 |
| 73 | + gc.collect() |
| 74 | + assert objcom.OnCall(None) == 3 |
| 75 | + assert objcom.Release() == 0 # trigger Revoke / free ! |
| 76 | + gc.collect() |
| 77 | + assert wref() is None # Check object is dead in python world |
0 commit comments