test_win32gui.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # tests for win32gui
  2. import unittest
  3. import win32gui
  4. import pywin32_testutil
  5. import operator
  6. import array
  7. import sys
  8. class TestPyGetString(unittest.TestCase):
  9. def test_get_string(self):
  10. # test invalid addresses cause a ValueError rather than crash!
  11. self.assertRaises(ValueError, win32gui.PyGetString, 0)
  12. self.assertRaises(ValueError, win32gui.PyGetString, 1)
  13. self.assertRaises(ValueError, win32gui.PyGetString, 1,1)
  14. class TestPyGetMemory(unittest.TestCase):
  15. def test_ob(self):
  16. # Check the PyGetMemory result and a bytes string can be compared
  17. test_data = b"\0\1\2\3\4\5\6"
  18. c = array.array("b", test_data)
  19. addr, buflen = c.buffer_info()
  20. got = win32gui.PyGetMemory(addr, buflen)
  21. self.assertEqual(len(got), len(test_data))
  22. self.assertEqual(bytes(got), test_data)
  23. def test_memory_index(self):
  24. # Check we can index into the buffer object returned by PyGetMemory
  25. test_data = b"\0\1\2\3\4\5\6"
  26. c = array.array("b", test_data)
  27. addr, buflen = c.buffer_info()
  28. got = win32gui.PyGetMemory(addr, buflen)
  29. self.assertEqual(got[0], 0)
  30. def test_memory_slice(self):
  31. # Check we can slice the buffer object returned by PyGetMemory
  32. test_data = b"\0\1\2\3\4\5\6"
  33. c = array.array("b", test_data)
  34. addr, buflen = c.buffer_info()
  35. got = win32gui.PyGetMemory(addr, buflen)
  36. self.assertEqual(list(got[0:3]), [0, 1, 2])
  37. def test_real_view(self):
  38. # Do the PyGetMemory, then change the original memory, then ensure
  39. # the initial object we fetched sees the new value.
  40. test_data = b"\0\1\2\3\4\5\6"
  41. c = array.array("b", test_data)
  42. addr, buflen = c.buffer_info()
  43. got = win32gui.PyGetMemory(addr, buflen)
  44. self.assertEqual(got[0], 0)
  45. c[0] = 1
  46. self.assertEqual(got[0], 1)
  47. def test_memory_not_writable(self):
  48. # Check the buffer object fetched by PyGetMemory isn't writable.
  49. test_data = b"\0\1\2\3\4\5\6"
  50. c = array.array("b", test_data)
  51. addr, buflen = c.buffer_info()
  52. got = win32gui.PyGetMemory(addr, buflen)
  53. self.assertRaises(TypeError, operator.setitem, got, 0, 1)
  54. if __name__=='__main__':
  55. unittest.main()