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_win32guistruct.py 9.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import array
  2. import unittest
  3. import pythoncom
  4. import win32con
  5. import win32gui
  6. import win32gui_struct
  7. class TestBase(unittest.TestCase):
  8. def assertDictEquals(self, d, **kw):
  9. checked = dict()
  10. for n, v in kw.items():
  11. self.assertEqual(v, d[n], "'%s' doesn't match: %r != %r" % (n, v, d[n]))
  12. checked[n] = True
  13. checked_keys = list(checked.keys())
  14. passed_keys = list(kw.keys())
  15. checked_keys.sort()
  16. passed_keys.sort()
  17. self.assertEqual(checked_keys, passed_keys)
  18. class TestMenuItemInfo(TestBase):
  19. def _testPackUnpack(self, text):
  20. vals = dict(
  21. 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. )
  31. mii, extras = win32gui_struct.PackMENUITEMINFO(**vals)
  32. (
  33. fType,
  34. fState,
  35. wID,
  36. hSubMenu,
  37. hbmpChecked,
  38. hbmpUnchecked,
  39. dwItemData,
  40. text,
  41. hbmpItem,
  42. ) = win32gui_struct.UnpackMENUITEMINFO(mii)
  43. self.assertDictEquals(
  44. vals,
  45. fType=fType,
  46. fState=fState,
  47. wID=wID,
  48. hSubMenu=hSubMenu,
  49. hbmpChecked=hbmpChecked,
  50. hbmpUnchecked=hbmpUnchecked,
  51. dwItemData=dwItemData,
  52. text=text,
  53. hbmpItem=hbmpItem,
  54. )
  55. def testPackUnpack(self):
  56. self._testPackUnpack("Hello")
  57. def testPackUnpackNone(self):
  58. self._testPackUnpack(None)
  59. def testEmptyMenuItemInfo(self):
  60. mii, extra = win32gui_struct.EmptyMENUITEMINFO()
  61. (
  62. fType,
  63. fState,
  64. wID,
  65. hSubMenu,
  66. hbmpChecked,
  67. hbmpUnchecked,
  68. dwItemData,
  69. text,
  70. hbmpItem,
  71. ) = win32gui_struct.UnpackMENUITEMINFO(mii)
  72. self.assertEqual(fType, 0)
  73. self.assertEqual(fState, 0)
  74. self.assertEqual(wID, 0)
  75. self.assertEqual(hSubMenu, 0)
  76. self.assertEqual(hbmpChecked, 0)
  77. self.assertEqual(hbmpUnchecked, 0)
  78. self.assertEqual(dwItemData, 0)
  79. self.assertEqual(hbmpItem, 0)
  80. # it's not clear if UnpackMENUITEMINFO() should ignore cch, instead
  81. # assuming it is a buffer size rather than 'current length' - but it
  82. # never has (and this gives us every \0 in the string), and actually
  83. # helps us test the unicode/str semantics.
  84. self.assertEqual(text, "\0" * len(text))
  85. class TestMenuInfo(TestBase):
  86. def testPackUnpack(self):
  87. vals = dict(dwStyle=1, cyMax=2, hbrBack=3, dwContextHelpID=4, dwMenuData=5)
  88. mi = win32gui_struct.PackMENUINFO(**vals)
  89. (
  90. dwStyle,
  91. cyMax,
  92. hbrBack,
  93. dwContextHelpID,
  94. dwMenuData,
  95. ) = win32gui_struct.UnpackMENUINFO(mi)
  96. self.assertDictEquals(
  97. vals,
  98. dwStyle=dwStyle,
  99. cyMax=cyMax,
  100. hbrBack=hbrBack,
  101. dwContextHelpID=dwContextHelpID,
  102. dwMenuData=dwMenuData,
  103. )
  104. def testEmptyMenuItemInfo(self):
  105. mi = win32gui_struct.EmptyMENUINFO()
  106. (
  107. dwStyle,
  108. cyMax,
  109. hbrBack,
  110. dwContextHelpID,
  111. dwMenuData,
  112. ) = win32gui_struct.UnpackMENUINFO(mi)
  113. self.assertEqual(dwStyle, 0)
  114. self.assertEqual(cyMax, 0)
  115. self.assertEqual(hbrBack, 0)
  116. self.assertEqual(dwContextHelpID, 0)
  117. self.assertEqual(dwMenuData, 0)
  118. class TestTreeViewItem(TestBase):
  119. def _testPackUnpack(self, text):
  120. vals = dict(
  121. hitem=1,
  122. state=2,
  123. stateMask=3,
  124. text=text,
  125. image=4,
  126. selimage=5,
  127. citems=6,
  128. param=7,
  129. )
  130. ti, extra = win32gui_struct.PackTVITEM(**vals)
  131. (
  132. hitem,
  133. state,
  134. stateMask,
  135. text,
  136. image,
  137. selimage,
  138. citems,
  139. param,
  140. ) = win32gui_struct.UnpackTVITEM(ti)
  141. self.assertDictEquals(
  142. vals,
  143. hitem=hitem,
  144. state=state,
  145. stateMask=stateMask,
  146. text=text,
  147. image=image,
  148. selimage=selimage,
  149. citems=citems,
  150. param=param,
  151. )
  152. def testPackUnpack(self):
  153. self._testPackUnpack("Hello")
  154. def testPackUnpackNone(self):
  155. self._testPackUnpack(None)
  156. def testEmpty(self):
  157. ti, extras = win32gui_struct.EmptyTVITEM(0)
  158. (
  159. hitem,
  160. state,
  161. stateMask,
  162. text,
  163. image,
  164. selimage,
  165. citems,
  166. param,
  167. ) = win32gui_struct.UnpackTVITEM(ti)
  168. self.assertEqual(hitem, 0)
  169. self.assertEqual(state, 0)
  170. self.assertEqual(stateMask, 0)
  171. self.assertEqual(text, "")
  172. self.assertEqual(image, 0)
  173. self.assertEqual(selimage, 0)
  174. self.assertEqual(citems, 0)
  175. self.assertEqual(param, 0)
  176. class TestListViewItem(TestBase):
  177. def _testPackUnpack(self, text):
  178. vals = dict(
  179. item=None,
  180. subItem=None,
  181. state=1,
  182. stateMask=2,
  183. text=text,
  184. image=3,
  185. param=4,
  186. indent=5,
  187. )
  188. ti, extra = win32gui_struct.PackLVITEM(**vals)
  189. (
  190. item,
  191. subItem,
  192. state,
  193. stateMask,
  194. text,
  195. image,
  196. param,
  197. indent,
  198. ) = win32gui_struct.UnpackLVITEM(ti)
  199. # patch expected values.
  200. vals["item"] = 0
  201. vals["subItem"] = 0
  202. self.assertDictEquals(
  203. vals,
  204. item=item,
  205. subItem=subItem,
  206. state=state,
  207. stateMask=stateMask,
  208. text=text,
  209. image=image,
  210. param=param,
  211. indent=indent,
  212. )
  213. def testPackUnpack(self):
  214. self._testPackUnpack("Hello")
  215. def testPackUnpackNone(self):
  216. self._testPackUnpack(None)
  217. def testEmpty(self):
  218. ti, extras = win32gui_struct.EmptyLVITEM(1, 2)
  219. (
  220. item,
  221. subItem,
  222. state,
  223. stateMask,
  224. text,
  225. image,
  226. param,
  227. indent,
  228. ) = win32gui_struct.UnpackLVITEM(ti)
  229. self.assertEqual(item, 1)
  230. self.assertEqual(subItem, 2)
  231. self.assertEqual(state, 0)
  232. self.assertEqual(stateMask, 0)
  233. self.assertEqual(text, "")
  234. self.assertEqual(image, 0)
  235. self.assertEqual(param, 0)
  236. self.assertEqual(indent, 0)
  237. class TestLVColumn(TestBase):
  238. def _testPackUnpack(self, text):
  239. vals = dict(fmt=1, cx=2, text=text, subItem=3, image=4, order=5)
  240. ti, extra = win32gui_struct.PackLVCOLUMN(**vals)
  241. fmt, cx, text, subItem, image, order = win32gui_struct.UnpackLVCOLUMN(ti)
  242. self.assertDictEquals(
  243. vals, fmt=fmt, cx=cx, text=text, subItem=subItem, image=image, order=order
  244. )
  245. def testPackUnpack(self):
  246. self._testPackUnpack("Hello")
  247. def testPackUnpackNone(self):
  248. self._testPackUnpack(None)
  249. def testEmpty(self):
  250. ti, extras = win32gui_struct.EmptyLVCOLUMN()
  251. fmt, cx, text, subItem, image, order = win32gui_struct.UnpackLVCOLUMN(ti)
  252. self.assertEqual(fmt, 0)
  253. self.assertEqual(cx, 0)
  254. self.assertEqual(text, "")
  255. self.assertEqual(subItem, 0)
  256. self.assertEqual(image, 0)
  257. self.assertEqual(order, 0)
  258. class TestDEV_BROADCAST_HANDLE(TestBase):
  259. def testPackUnpack(self):
  260. s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123)
  261. c = array.array("b", s)
  262. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  263. self.assertEqual(got.handle, 123)
  264. def testGUID(self):
  265. s = win32gui_struct.PackDEV_BROADCAST_HANDLE(123, guid=pythoncom.IID_IUnknown)
  266. c = array.array("b", s)
  267. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  268. self.assertEqual(got.handle, 123)
  269. self.assertEqual(got.eventguid, pythoncom.IID_IUnknown)
  270. class TestDEV_BROADCAST_DEVICEINTERFACE(TestBase):
  271. def testPackUnpack(self):
  272. s = win32gui_struct.PackDEV_BROADCAST_DEVICEINTERFACE(
  273. pythoncom.IID_IUnknown, "hello"
  274. )
  275. c = array.array("b", s)
  276. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  277. self.assertEqual(got.classguid, pythoncom.IID_IUnknown)
  278. self.assertEqual(got.name, "hello")
  279. class TestDEV_BROADCAST_VOLUME(TestBase):
  280. def testPackUnpack(self):
  281. s = win32gui_struct.PackDEV_BROADCAST_VOLUME(123, 456)
  282. c = array.array("b", s)
  283. got = win32gui_struct.UnpackDEV_BROADCAST(c.buffer_info()[0])
  284. self.assertEqual(got.unitmask, 123)
  285. self.assertEqual(got.flags, 456)
  286. if __name__ == "__main__":
  287. unittest.main()