test_win32guistruct.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import unittest
  2. import win32gui
  3. import win32gui_struct
  4. import win32con
  5. import array
  6. import pythoncom
  7. class TestBase(unittest.TestCase):
  8. def assertDictEquals(self, d, **kw):
  9. checked = dict()
  10. for n, v in kw.items():
  11. self.failUnlessEqual(v, d[n],
  12. "'%s' doesn't match: %r != %r" % (n, v, d[n]))
  13. checked[n] = True
  14. checked_keys = list(checked.keys())
  15. passed_keys = list(kw.keys())
  16. checked_keys.sort()
  17. passed_keys.sort()
  18. self.failUnlessEqual(checked_keys, passed_keys)
  19. class TestMenuItemInfo(TestBase):
  20. def _testPackUnpack(self, text):
  21. vals = dict(fType=win32con.MFT_MENUBARBREAK,
  22. fState=win32con.MFS_CHECKED,
  23. wID=123,
  24. hSubMenu=1234,
  25. hbmpChecked=12345,
  26. hbmpUnchecked=123456,
  27. dwItemData=1234567,
  28. text=text,
  29. hbmpItem=321)
  30. mii, extras = win32gui_struct.PackMENUITEMINFO(**vals)
  31. fType, fState, wID, hSubMenu, hbmpChecked, hbmpUnchecked, \
  32. dwItemData, text, hbmpItem = win32gui_struct.UnpackMENUITEMINFO(mii)
  33. self.assertDictEquals(vals, fType=fType, fState=fState, wID=wID,
  34. hSubMenu=hSubMenu, hbmpChecked=hbmpChecked,
  35. hbmpUnchecked=hbmpUnchecked,
  36. dwItemData=dwItemData, text=text,
  37. hbmpItem=hbmpItem)
  38. def testPackUnpack(self):
  39. self._testPackUnpack("Hello")
  40. def testPackUnpackNone(self):
  41. self._testPackUnpack(None)
  42. def testEmptyMenuItemInfo(self):
  43. mii, extra = win32gui_struct.EmptyMENUITEMINFO()
  44. fType, fState, wID, hSubMenu, hbmpChecked, hbmpUnchecked, \
  45. dwItemData, text, hbmpItem = win32gui_struct.UnpackMENUITEMINFO(mii)
  46. self.failUnlessEqual(fType, 0)
  47. self.failUnlessEqual(fState, 0)
  48. self.failUnlessEqual(wID, 0)
  49. self.failUnlessEqual(hSubMenu, 0)
  50. self.failUnlessEqual(hbmpChecked, 0)
  51. self.failUnlessEqual(hbmpUnchecked, 0)
  52. self.failUnlessEqual(dwItemData, 0)
  53. self.failUnlessEqual(hbmpItem, 0)
  54. # it's not clear if UnpackMENUITEMINFO() should ignore cch, instead
  55. # assuming it is a buffer size rather than 'current length' - but it
  56. # never has (and this gives us every \0 in the string), and actually
  57. # helps us test the unicode/str semantics.
  58. self.failUnlessEqual(text, '\0' * len(text))
  59. class TestMenuInfo(TestBase):
  60. def testPackUnpack(self):
  61. vals = dict(dwStyle=1, cyMax=2, hbrBack=3, dwContextHelpID=4,
  62. dwMenuData=5)
  63. mi = win32gui_struct.PackMENUINFO(**vals)
  64. dwStyle, cyMax, hbrBack, dwContextHelpID, dwMenuData = \
  65. win32gui_struct.UnpackMENUINFO(mi)
  66. self.assertDictEquals(vals, dwStyle=dwStyle, cyMax=cyMax,
  67. hbrBack=hbrBack,
  68. dwContextHelpID=dwContextHelpID,
  69. dwMenuData=dwMenuData)
  70. def testEmptyMenuItemInfo(self):
  71. mi = win32gui_struct.EmptyMENUINFO()
  72. dwStyle, cyMax, hbrBack, dwContextHelpID, dwMenuData = \
  73. win32gui_struct.UnpackMENUINFO(mi)
  74. self.failUnlessEqual(dwStyle, 0)
  75. self.failUnlessEqual(cyMax, 0)
  76. self.failUnlessEqual(hbrBack, 0)
  77. self.failUnlessEqual(dwContextHelpID, 0)
  78. self.failUnlessEqual(dwMenuData, 0)
  79. class TestTreeViewItem(TestBase):
  80. def _testPackUnpack(self, text):
  81. vals = dict(hitem=1, state=2, stateMask=3, text=text, image=4,
  82. selimage=5, citems=6, param=7)
  83. ti, extra = win32gui_struct.PackTVITEM(**vals)
  84. hitem, state, stateMask, text, image, selimage, citems, param = \
  85. win32gui_struct.UnpackTVITEM(ti)
  86. self.assertDictEquals(vals, hitem=hitem, state=state,
  87. stateMask=stateMask, text=text, image=image,
  88. selimage=selimage, citems=citems, param=param)
  89. def testPackUnpack(self):
  90. self._testPackUnpack("Hello")
  91. def testPackUnpackNone(self):
  92. self._testPackUnpack(None)
  93. def testEmpty(self):
  94. ti, extras = win32gui_struct.EmptyTVITEM(0)
  95. hitem, state, stateMask, text, image, selimage, citems, param = \
  96. win32gui_struct.UnpackTVITEM(ti)
  97. self.failUnlessEqual(hitem, 0)
  98. self.failUnlessEqual(state, 0)
  99. self.failUnlessEqual(stateMask, 0)
  100. self.failUnlessEqual(text, '')
  101. self.failUnlessEqual(image, 0)
  102. self.failUnlessEqual(selimage, 0)
  103. self.failUnlessEqual(citems, 0)
  104. self.failUnlessEqual(param, 0)
  105. class TestListViewItem(TestBase):
  106. def _testPackUnpack(self, text):
  107. vals = dict(item=None, subItem=None, state=1, stateMask=2,
  108. text=text, image=3, param=4, indent=5)
  109. ti, extra = win32gui_struct.PackLVITEM(**vals)
  110. item, subItem, state, stateMask, text, image, param, indent = \
  111. win32gui_struct.UnpackLVITEM(ti)
  112. # patch expected values.
  113. vals['item'] = 0
  114. vals['subItem'] = 0
  115. self.assertDictEquals(vals, item=item, subItem=subItem, state=state,
  116. stateMask=stateMask, text=text, image=image,
  117. param=param, indent=indent)
  118. def testPackUnpack(self):
  119. self._testPackUnpack("Hello")
  120. def testPackUnpackNone(self):
  121. self._testPackUnpack(None)
  122. def testEmpty(self):
  123. ti, extras = win32gui_struct.EmptyLVITEM(1, 2)
  124. item, subItem, state, stateMask, text, image, param, indent = \
  125. win32gui_struct.UnpackLVITEM(ti)
  126. self.failUnlessEqual(item, 1)
  127. self.failUnlessEqual(subItem, 2)
  128. self.failUnlessEqual(state, 0)
  129. self.failUnlessEqual(stateMask, 0)
  130. self.failUnlessEqual(text, '')
  131. self.failUnlessEqual(image, 0)
  132. self.failUnlessEqual(param, 0)
  133. self.failUnlessEqual(indent, 0)
  134. class TestLVColumn(TestBase):
  135. def _testPackUnpack(self, text):
  136. vals = dict(fmt=1, cx=2, text=text, subItem=3, image=4, order=5)
  137. ti, extra = win32gui_struct.PackLVCOLUMN(**vals)
  138. fmt, cx, text, subItem, image, order = \
  139. win32gui_struct.UnpackLVCOLUMN(ti)
  140. self.assertDictEquals(vals, fmt=fmt, cx=cx, text=text, subItem=subItem,
  141. image=image, order=order)
  142. def testPackUnpack(self):
  143. self._testPackUnpack("Hello")
  144. def testPackUnpackNone(self):
  145. self._testPackUnpack(None)
  146. def testEmpty(self):
  147. ti, extras = win32gui_struct.EmptyLVCOLUMN()
  148. fmt, cx, text, subItem, image, order = \
  149. win32gui_struct.UnpackLVCOLUMN(ti)
  150. self.failUnlessEqual(fmt, 0)
  151. self.failUnlessEqual(cx, 0)
  152. self.failUnlessEqual(text, '')
  153. self.failUnlessEqual(subItem, 0)
  154. self.failUnlessEqual(image, 0)
  155. self.failUnlessEqual(order, 0)
  156. class TestDEV_BROADCAST_HANDLE(TestBase):
  157. def testPackUnpack(self):
  158. s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123)
  159. c = array.array("b", s)
  160. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  161. self.failUnlessEqual(got.handle, 123)
  162. def testGUID(self):
  163. s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123,
  164. guid=pythoncom.IID_IUnknown)
  165. c = array.array("b", s)
  166. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  167. self.failUnlessEqual(got.handle, 123)
  168. self.failUnlessEqual(got.eventguid, pythoncom.IID_IUnknown)
  169. class TestDEV_BROADCAST_DEVICEINTERFACE(TestBase):
  170. def testPackUnpack(self):
  171. s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(pythoncom.IID_IUnknown,
  172. "hello")
  173. c = array.array("b", s)
  174. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  175. self.failUnlessEqual(got.classguid, pythoncom.IID_IUnknown)
  176. self.failUnlessEqual(got.name, "hello")
  177. class TestDEV_BROADCAST_VOLUME(TestBase):
  178. def testPackUnpack(self):
  179. s = win32gui_struct.PackDEV_BROADCAST_VOLUME(123, 456)
  180. c = array.array("b", s)
  181. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  182. self.failUnlessEqual(got.unitmask, 123)
  183. self.failUnlessEqual(got.flags, 456)
  184. if __name__=='__main__':
  185. unittest.main()