testShellItem.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Test IShellItem and related interfaces
  2. from win32com.shell import shell, shellcon, knownfolders
  3. import unittest
  4. class TestShellItem(unittest.TestCase):
  5. def assertShellItemsEqual(self, i1, i2):
  6. n1 = i1.GetDisplayName(shellcon.SHGDN_FORPARSING)
  7. n2 = i2.GetDisplayName(shellcon.SHGDN_FORPARSING)
  8. self.assertEqual(n1, n2)
  9. def test_idlist_roundtrip(self):
  10. pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
  11. item = shell.SHCreateItemFromIDList(pidl, shell.IID_IShellItem)
  12. pidl_back = shell.SHGetIDListFromObject(item)
  13. self.assertEqual(pidl, pidl_back)
  14. def test_parsing_name(self):
  15. sf = shell.SHGetDesktopFolder()
  16. flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
  17. children = sf.EnumObjects(0, flags)
  18. child_pidl = next(children)
  19. name = sf.GetDisplayNameOf(child_pidl, shellcon.SHGDN_FORPARSING)
  20. item = shell.SHCreateItemFromParsingName(name, None, shell.IID_IShellItem)
  21. # test the name we get from the item is the same as from the folder.
  22. self.assertEqual(name, item.GetDisplayName(shellcon.SHGDN_FORPARSING))
  23. def test_parsing_relative(self):
  24. desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
  25. desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
  26. sf = shell.SHGetDesktopFolder()
  27. flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
  28. children = sf.EnumObjects(0, flags)
  29. child_pidl = next(children)
  30. name_flags = shellcon.SHGDN_FORPARSING | shellcon.SHGDN_INFOLDER
  31. name = sf.GetDisplayNameOf(child_pidl, name_flags)
  32. item = shell.SHCreateItemFromRelativeName(desktop_item, name, None,
  33. shell.IID_IShellItem)
  34. # test the name we get from the item is the same as from the folder.
  35. self.assertEqual(name, item.GetDisplayName(name_flags))
  36. def test_create_in_known_folder(self):
  37. item = shell.SHCreateItemInKnownFolder(knownfolders.FOLDERID_Desktop, 0,
  38. None, shell.IID_IShellItem)
  39. # this will do for now :)
  40. def test_create_item_with_parent(self):
  41. desktop_pidl = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)
  42. desktop_item = shell.SHCreateItemFromIDList(desktop_pidl, shell.IID_IShellItem)
  43. sf = shell.SHGetDesktopFolder()
  44. flags = shellcon.SHCONTF_FOLDERS | shellcon.SHCONTF_NONFOLDERS
  45. children = sf.EnumObjects(0, flags)
  46. child_pidl = next(children)
  47. item1 = shell.SHCreateItemWithParent(desktop_pidl, None, child_pidl, shell.IID_IShellItem)
  48. item2 = shell.SHCreateItemWithParent(None, sf, child_pidl, shell.IID_IShellItem)
  49. self.assertShellItemsEqual(item1, item2)
  50. if __name__=='__main__':
  51. unittest.main()