test_win32gui.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # tests for win32gui
  2. import unittest
  3. import win32gui
  4. import pywin32_testutil
  5. import operator
  6. import array
  7. import sys
  8. # theoretically should be in pywin32_testutil, but this is the only place
  9. # that currently needs such a function...
  10. def ob2bytes(ob):
  11. if sys.version_info < (3,0):
  12. return str(buffer(ob))
  13. # py3k.
  14. return bytes(ob)
  15. class TestPyGetString(unittest.TestCase):
  16. def test_get_string(self):
  17. # test invalid addresses cause a ValueError rather than crash!
  18. self.assertRaises(ValueError, win32gui.PyGetString, 0)
  19. self.assertRaises(ValueError, win32gui.PyGetString, 1)
  20. self.assertRaises(ValueError, win32gui.PyGetString, 1,1)
  21. class TestPyGetMemory(unittest.TestCase):
  22. def test_ob(self):
  23. # Check the PyGetMemory result and a bytes string can be compared
  24. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6")
  25. c = array.array("b", test_data)
  26. addr, buflen = c.buffer_info()
  27. got = win32gui.PyGetMemory(addr, buflen)
  28. self.failUnlessEqual(len(got), len(test_data))
  29. self.failUnlessEqual(ob2bytes(got), test_data)
  30. def test_memory_index(self):
  31. # Check we can index into the buffer object returned by PyGetMemory
  32. test_data = pywin32_testutil.str2bytes("\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.failUnlessEqual(got[0], pywin32_testutil.str2bytes('\0'))
  37. def test_memory_slice(self):
  38. # Check we can slice the buffer object returned by PyGetMemory
  39. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6")
  40. c = array.array("b", test_data)
  41. addr, buflen = c.buffer_info()
  42. got = win32gui.PyGetMemory(addr, buflen)
  43. self.failUnlessEqual(got[0:3], pywin32_testutil.str2bytes('\0\1\2'))
  44. def test_real_view(self):
  45. # Do the PyGetMemory, then change the original memory, then ensure
  46. # the initial object we fetched sees the new value.
  47. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6")
  48. c = array.array("b", test_data)
  49. addr, buflen = c.buffer_info()
  50. got = win32gui.PyGetMemory(addr, buflen)
  51. self.failUnlessEqual(got[0], pywin32_testutil.str2bytes('\0'))
  52. new = pywin32_testutil.str2bytes('\1')
  53. c[0] = 1
  54. self.failUnlessEqual(got[0], new)
  55. def test_memory_not_writable(self):
  56. # Check the buffer object fetched by PyGetMemory isn't writable.
  57. test_data = pywin32_testutil.str2bytes("\0\1\2\3\4\5\6")
  58. c = array.array("b", test_data)
  59. addr, buflen = c.buffer_info()
  60. got = win32gui.PyGetMemory(addr, buflen)
  61. new = pywin32_testutil.str2bytes('\1')
  62. self.failUnlessRaises(TypeError, operator.setitem, got, 0, new)
  63. if __name__=='__main__':
  64. unittest.main()