test_clipboard.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # General test module for win32api - please add some :)
  2. import sys, os
  3. import unittest
  4. from win32clipboard import *
  5. import win32gui, win32con
  6. import pywintypes
  7. import array
  8. from pywin32_testutil import str2bytes
  9. custom_format_name = "PythonClipboardTestFormat"
  10. class CrashingTestCase(unittest.TestCase):
  11. def test_722082(self):
  12. class crasher(object):
  13. pass
  14. obj = crasher()
  15. OpenClipboard()
  16. try:
  17. EmptyClipboard()
  18. # This used to crash - now correctly raises type error.
  19. self.assertRaises(TypeError, SetClipboardData, 0, obj )
  20. finally:
  21. CloseClipboard()
  22. class TestBitmap(unittest.TestCase):
  23. def setUp(self):
  24. self.bmp_handle = None
  25. try:
  26. this_file = __file__
  27. except NameError:
  28. this_file = sys.argv[0]
  29. this_dir = os.path.dirname(this_file)
  30. self.bmp_name = os.path.join(os.path.abspath(this_dir),
  31. "..", "Demos", "images", "smiley.bmp")
  32. self.failUnless(os.path.isfile(self.bmp_name), self.bmp_name)
  33. flags = win32con.LR_DEFAULTSIZE | win32con.LR_LOADFROMFILE
  34. self.bmp_handle = win32gui.LoadImage(0, self.bmp_name,
  35. win32con.IMAGE_BITMAP,
  36. 0, 0, flags)
  37. self.failUnless(self.bmp_handle, "Failed to get a bitmap handle")
  38. def tearDown(self):
  39. if self.bmp_handle:
  40. win32gui.DeleteObject(self.bmp_handle)
  41. def test_bitmap_roundtrip(self):
  42. OpenClipboard()
  43. try:
  44. SetClipboardData(win32con.CF_BITMAP, self.bmp_handle)
  45. got_handle = GetClipboardDataHandle(win32con.CF_BITMAP)
  46. self.failUnlessEqual(got_handle, self.bmp_handle)
  47. finally:
  48. CloseClipboard()
  49. class TestStrings(unittest.TestCase):
  50. def setUp(self):
  51. OpenClipboard()
  52. def tearDown(self):
  53. CloseClipboard()
  54. def test_unicode(self):
  55. val = "test-\a9har"
  56. SetClipboardData(win32con.CF_UNICODETEXT, val)
  57. self.failUnlessEqual(GetClipboardData(win32con.CF_UNICODETEXT), val)
  58. def test_unicode_text(self):
  59. val = "test-val"
  60. SetClipboardText(val)
  61. # GetClipboardData doesn't to auto string conversions - so on py3k,
  62. # CF_TEXT returns bytes.
  63. expected = str2bytes(val)
  64. self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), expected)
  65. SetClipboardText(val, win32con.CF_UNICODETEXT)
  66. self.failUnlessEqual(GetClipboardData(win32con.CF_UNICODETEXT), val)
  67. def test_string(self):
  68. val = str2bytes("test")
  69. SetClipboardData(win32con.CF_TEXT, val)
  70. self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), val)
  71. class TestGlobalMemory(unittest.TestCase):
  72. def setUp(self):
  73. OpenClipboard()
  74. def tearDown(self):
  75. CloseClipboard()
  76. def test_mem(self):
  77. val = str2bytes("test")
  78. expected = str2bytes("test\0")
  79. SetClipboardData(win32con.CF_TEXT, val)
  80. # Get the raw data - this will include the '\0'
  81. raw_data = GetGlobalMemory(GetClipboardDataHandle(win32con.CF_TEXT))
  82. self.failUnlessEqual(expected, raw_data)
  83. def test_bad_mem(self):
  84. self.failUnlessRaises(pywintypes.error, GetGlobalMemory, 0)
  85. self.failUnlessRaises(pywintypes.error, GetGlobalMemory, -1)
  86. if sys.getwindowsversion()[0] <= 5:
  87. # For some reason, the value '1' dies from a 64bit process, but
  88. # "works" (ie, gives the correct exception) from a 32bit process.
  89. # just silently skip this value on Vista.
  90. self.failUnlessRaises(pywintypes.error, GetGlobalMemory, 1)
  91. def test_custom_mem(self):
  92. test_data = str2bytes("hello\x00\xff")
  93. test_buffer = array.array("b", test_data)
  94. cf = RegisterClipboardFormat(custom_format_name)
  95. self.failUnlessEqual(custom_format_name, GetClipboardFormatName(cf))
  96. SetClipboardData(cf, test_buffer)
  97. hglobal = GetClipboardDataHandle(cf)
  98. data = GetGlobalMemory(hglobal)
  99. self.failUnlessEqual(data, test_data)
  100. if __name__ == '__main__':
  101. unittest.main()