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.

test_clipboard.py 4.0KB

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