PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testClipboard.py



0001 # testClipboard.py
0002 import unittest
0003 import pythoncom
0004 import win32con
0005 import winerror
0006 import win32clipboard
0007 
0008 from win32com.server.util import NewEnum, wrap
0009 from win32com.server.exception import COMException
0010 
0011 IDataObject_Methods = """GetData GetDataHere QueryGetData
0012                          GetCanonicalFormatEtc SetData EnumFormatEtc
0013                          DAdvise DUnadvise EnumDAdvise""".split()
0014 
0015 # A COM object implementing IDataObject used for basic testing.
0016 num_do_objects = 0
0017 
0018 def WrapCOMObject(ob, iid=None):
0019     return wrap(ob, iid=iid, useDispatcher = 0)
0020 
0021 class TestDataObject:
0022     _com_interfaces_ = [pythoncom.IID_IDataObject]
0023     _public_methods_ = IDataObject_Methods
0024     def __init__(self, strval):
0025         global num_do_objects
0026         num_do_objects += 1
0027         self.strval = strval
0028         self.supported_fe = []
0029         for cf in (win32con.CF_TEXT, win32con.CF_UNICODETEXT):
0030             fe = cf, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL
0031             self.supported_fe.append(fe)
0032 
0033     def __del__(self):
0034         global num_do_objects
0035         num_do_objects -= 1
0036 
0037     def _query_interface_(self, iid):
0038         if iid==pythoncom.IID_IEnumFORMATETC:
0039             return NewEnum(self.supported_fe, iid=iid)
0040 
0041     def GetData(self, fe):
0042         ret_stg = None
0043         cf, target, aspect, index, tymed  = fe
0044         if aspect & pythoncom.DVASPECT_CONTENT and \
0045            tymed==pythoncom.TYMED_HGLOBAL:
0046             if cf == win32con.CF_TEXT:
0047                 ret_stg = pythoncom.STGMEDIUM()
0048                 ret_stg.set(pythoncom.TYMED_HGLOBAL, self.strval)
0049             elif cf == win32con.CF_UNICODETEXT:
0050                 ret_stg = pythoncom.STGMEDIUM()
0051                 ret_stg.set(pythoncom.TYMED_HGLOBAL, unicode(self.strval))
0052 
0053         if ret_stg is None:
0054             raise COMException(hresult=winerror.E_NOTIMPL)
0055         return ret_stg
0056 
0057     def GetDataHere(self, fe):
0058         raise COMException(hresult=winerror.E_NOTIMPL)
0059 
0060     def QueryGetData(self, fe):
0061         cf, target, aspect, index, tymed  = fe
0062         if aspect & pythoncom.DVASPECT_CONTENT == 0:
0063             raise COMException(hresult=winerror.DV_E_DVASPECT)
0064         if tymed!=pythoncom.TYMED_HGLOBAL:
0065             raise COMException(hresult=winerror.DV_E_TYMED)
0066         return None # should check better
0067 
0068     def GetCanonicalFormatEtc(self, fe):
0069         RaiseCOMException(winerror.DATA_S_SAMEFORMATETC)
0070         # return fe
0071 
0072     def SetData(self, fe, medium):
0073         raise COMException(hresult=winerror.E_NOTIMPL)
0074 
0075     def EnumFormatEtc(self, direction):
0076         if direction != pythoncom.DATADIR_GET:
0077             raise COMException(hresult=winerror.E_NOTIMPL)
0078         return NewEnum(self.supported_fe, iid=pythoncom.IID_IEnumFORMATETC)
0079 
0080     def DAdvise(self, fe, flags, sink):
0081         raise COMException(hresult=winerror.E_NOTIMPL)
0082 
0083     def DUnadvise(self, connection):
0084         raise COMException(hresult=winerror.E_NOTIMPL)
0085 
0086     def EnumDAdvise(self):
0087         raise COMException(hresult=winerror.E_NOTIMPL)
0088 
0089 class ClipboardTester(unittest.TestCase):
0090     def setUp(self):
0091         pythoncom.OleInitialize()
0092     def tearDown(self):
0093         try:
0094             pythoncom.OleFlushClipboard()
0095         except pythoncom.com_error:
0096             # We never set anything!
0097             pass
0098     def testIsCurrentClipboard(self):
0099         do = TestDataObject("Hello from Python")
0100         do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
0101         pythoncom.OleSetClipboard(do)
0102         self.failUnless(pythoncom.OleIsCurrentClipboard(do))
0103 
0104     def testComToWin32(self):
0105         # Set the data via our DataObject
0106         do = TestDataObject("Hello from Python")
0107         do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
0108         pythoncom.OleSetClipboard(do)
0109         # Then get it back via the standard win32 clipboard functions.
0110         win32clipboard.OpenClipboard()
0111         got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
0112         self.assertEqual(got, "Hello from Python")
0113         # Now check unicode
0114         got = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
0115         self.assertEqual(got, u"Hello from Python")
0116         win32clipboard.CloseClipboard()
0117 
0118     def testWin32ToCom(self):
0119         # Set the data via the std win32 clipboard functions.
0120         val = "Hello again!"
0121         win32clipboard.OpenClipboard()
0122         win32clipboard.SetClipboardData(win32con.CF_TEXT, val)
0123         win32clipboard.CloseClipboard()
0124         # and get it via an IDataObject provided by COM
0125         do = pythoncom.OleGetClipboard()
0126         cf = win32con.CF_TEXT, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL
0127         stg = do.GetData(cf)
0128         got = stg.data
0129         # The data we get back has the \0, as our STGMEDIUM has no way of 
0130         # knowing if it meant to be a string, or a binary buffer, so
0131         # it must return it too.
0132         self.failUnlessEqual(got, val+"\0")
0133         
0134     def testDataObjectFlush(self):
0135         do = TestDataObject("Hello from Python")
0136         do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
0137         pythoncom.OleSetClipboard(do)
0138         self.assertEqual(num_do_objects, 1)
0139 
0140         do = None # clear my ref!
0141         pythoncom.OleFlushClipboard()
0142         self.assertEqual(num_do_objects, 0)
0143 
0144     def testDataObjectReset(self):
0145         do = TestDataObject("Hello from Python")
0146         do = WrapCOMObject(do)
0147         pythoncom.OleSetClipboard(do)
0148         do = None # clear my ref!
0149         self.assertEqual(num_do_objects, 1)
0150         pythoncom.OleSetClipboard(None)
0151         self.assertEqual(num_do_objects, 0)
0152 
0153 if __name__=='__main__':
0154     import util
0155     util.testmain()
0156 

Generated by PyXR 0.9.4
SourceForge.net Logo