Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testClipboard.py 5.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # testClipboard.py
  2. import unittest
  3. import pythoncom
  4. import win32clipboard
  5. import win32con
  6. import winerror
  7. from win32com.server.exception import COMException
  8. from win32com.server.util import NewEnum, wrap
  9. IDataObject_Methods = """GetData GetDataHere QueryGetData
  10. GetCanonicalFormatEtc SetData EnumFormatEtc
  11. DAdvise DUnadvise EnumDAdvise""".split()
  12. # A COM object implementing IDataObject used for basic testing.
  13. num_do_objects = 0
  14. def WrapCOMObject(ob, iid=None):
  15. return wrap(ob, iid=iid, useDispatcher=0)
  16. class TestDataObject:
  17. _com_interfaces_ = [pythoncom.IID_IDataObject]
  18. _public_methods_ = IDataObject_Methods
  19. def __init__(self, bytesval):
  20. global num_do_objects
  21. num_do_objects += 1
  22. self.bytesval = bytesval
  23. self.supported_fe = []
  24. for cf in (win32con.CF_TEXT, win32con.CF_UNICODETEXT):
  25. fe = cf, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL
  26. self.supported_fe.append(fe)
  27. def __del__(self):
  28. global num_do_objects
  29. num_do_objects -= 1
  30. def _query_interface_(self, iid):
  31. if iid == pythoncom.IID_IEnumFORMATETC:
  32. return NewEnum(self.supported_fe, iid=iid)
  33. def GetData(self, fe):
  34. ret_stg = None
  35. cf, target, aspect, index, tymed = fe
  36. if aspect & pythoncom.DVASPECT_CONTENT and tymed == pythoncom.TYMED_HGLOBAL:
  37. if cf == win32con.CF_TEXT:
  38. ret_stg = pythoncom.STGMEDIUM()
  39. ret_stg.set(pythoncom.TYMED_HGLOBAL, self.bytesval)
  40. elif cf == win32con.CF_UNICODETEXT:
  41. ret_stg = pythoncom.STGMEDIUM()
  42. ret_stg.set(pythoncom.TYMED_HGLOBAL, self.bytesval.decode("latin1"))
  43. if ret_stg is None:
  44. raise COMException(hresult=winerror.E_NOTIMPL)
  45. return ret_stg
  46. def GetDataHere(self, fe):
  47. raise COMException(hresult=winerror.E_NOTIMPL)
  48. def QueryGetData(self, fe):
  49. cf, target, aspect, index, tymed = fe
  50. if aspect & pythoncom.DVASPECT_CONTENT == 0:
  51. raise COMException(hresult=winerror.DV_E_DVASPECT)
  52. if tymed != pythoncom.TYMED_HGLOBAL:
  53. raise COMException(hresult=winerror.DV_E_TYMED)
  54. return None # should check better
  55. def GetCanonicalFormatEtc(self, fe):
  56. RaiseCOMException(winerror.DATA_S_SAMEFORMATETC)
  57. # return fe
  58. def SetData(self, fe, medium):
  59. raise COMException(hresult=winerror.E_NOTIMPL)
  60. def EnumFormatEtc(self, direction):
  61. if direction != pythoncom.DATADIR_GET:
  62. raise COMException(hresult=winerror.E_NOTIMPL)
  63. return NewEnum(self.supported_fe, iid=pythoncom.IID_IEnumFORMATETC)
  64. def DAdvise(self, fe, flags, sink):
  65. raise COMException(hresult=winerror.E_NOTIMPL)
  66. def DUnadvise(self, connection):
  67. raise COMException(hresult=winerror.E_NOTIMPL)
  68. def EnumDAdvise(self):
  69. raise COMException(hresult=winerror.E_NOTIMPL)
  70. class ClipboardTester(unittest.TestCase):
  71. def setUp(self):
  72. pythoncom.OleInitialize()
  73. def tearDown(self):
  74. try:
  75. pythoncom.OleFlushClipboard()
  76. except pythoncom.com_error:
  77. # We never set anything!
  78. pass
  79. def testIsCurrentClipboard(self):
  80. do = TestDataObject(b"Hello from Python")
  81. do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
  82. pythoncom.OleSetClipboard(do)
  83. self.assertTrue(pythoncom.OleIsCurrentClipboard(do))
  84. def testComToWin32(self):
  85. # Set the data via our DataObject
  86. do = TestDataObject(b"Hello from Python")
  87. do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
  88. pythoncom.OleSetClipboard(do)
  89. # Then get it back via the standard win32 clipboard functions.
  90. win32clipboard.OpenClipboard()
  91. got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
  92. # CF_TEXT gives bytes.
  93. expected = b"Hello from Python"
  94. self.assertEqual(got, expected)
  95. # Now check unicode
  96. got = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
  97. self.assertEqual(got, "Hello from Python")
  98. win32clipboard.CloseClipboard()
  99. def testWin32ToCom(self):
  100. # Set the data via the std win32 clipboard functions.
  101. val = b"Hello again!" # always bytes
  102. win32clipboard.OpenClipboard()
  103. win32clipboard.SetClipboardData(win32con.CF_TEXT, val)
  104. win32clipboard.CloseClipboard()
  105. # and get it via an IDataObject provided by COM
  106. do = pythoncom.OleGetClipboard()
  107. cf = (
  108. win32con.CF_TEXT,
  109. None,
  110. pythoncom.DVASPECT_CONTENT,
  111. -1,
  112. pythoncom.TYMED_HGLOBAL,
  113. )
  114. stg = do.GetData(cf)
  115. got = stg.data
  116. # The data we get back has the \0, as our STGMEDIUM has no way of
  117. # knowing if it meant to be a string, or a binary buffer, so
  118. # it must return it too.
  119. self.assertTrue(got, b"Hello again!\0")
  120. def testDataObjectFlush(self):
  121. do = TestDataObject(b"Hello from Python")
  122. do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
  123. pythoncom.OleSetClipboard(do)
  124. self.assertEqual(num_do_objects, 1)
  125. do = None # clear my ref!
  126. pythoncom.OleFlushClipboard()
  127. self.assertEqual(num_do_objects, 0)
  128. def testDataObjectReset(self):
  129. do = TestDataObject(b"Hello from Python")
  130. do = WrapCOMObject(do)
  131. pythoncom.OleSetClipboard(do)
  132. do = None # clear my ref!
  133. self.assertEqual(num_do_objects, 1)
  134. pythoncom.OleSetClipboard(None)
  135. self.assertEqual(num_do_objects, 0)
  136. if __name__ == "__main__":
  137. from win32com.test import util
  138. util.testmain()