PyXR

c:\python24\lib\site-packages\win32 \ com \ test \ testShell.py



0001 import sys, os
0002 import struct
0003 import unittest
0004 import copy
0005 
0006 import win32con
0007 import pythoncom
0008 from win32com.shell import shell
0009 from win32com.shell.shellcon import *
0010 from win32com.storagecon import *
0011 
0012 import win32com.test.util
0013 
0014 class ShellTester(win32com.test.util.TestCase):
0015     def testShellLink(self):
0016         desktop = str(shell.SHGetSpecialFolderPath(0, CSIDL_DESKTOP))
0017         num = 0
0018         shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
0019         persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile)
0020         for base_name in os.listdir(desktop):
0021             name = os.path.join(desktop, base_name)
0022             try:
0023                 persistFile.Load(name,STGM_READ)
0024             except pythoncom.com_error:
0025                 continue
0026             # Resolve is slow - avoid it for our tests.
0027             #shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI)
0028             fname, findData = shellLink.GetPath(0)
0029             unc = shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0]
0030             num += 1
0031         self.failIf(num<3, "Only found %d items on the desktop??" % num)
0032 
0033     def testShellFolder(self):
0034         sf = shell.SHGetDesktopFolder()
0035         names_1 = []
0036         for i in sf: # Magically calls EnumObjects
0037             name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
0038             names_1.append(name)
0039         
0040         # And get the enumerator manually    
0041         enum = sf.EnumObjects(0, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN)
0042         names_2 = []
0043         for i in enum:
0044             name = sf.GetDisplayNameOf(i, SHGDN_NORMAL)
0045             names_2.append(name)
0046         names_1.sort()
0047         names_2.sort()
0048         self.assertEqual(names_1, names_2)
0049 
0050 class PIDLTester(win32com.test.util.TestCase):
0051     def _rtPIDL(self, pidl):
0052         pidl_str = shell.PIDLAsString(pidl)
0053         pidl_rt = shell.StringAsPIDL(pidl_str)
0054         self.assertEqual(pidl_rt, pidl)
0055         pidl_str_rt = shell.PIDLAsString(pidl_rt)
0056         self.assertEqual(pidl_str_rt, pidl_str)
0057 
0058     def _rtCIDA(self, parent, kids):
0059         cida = parent, kids
0060         cida_str = shell.CIDAAsString(cida)
0061         cida_rt = shell.StringAsCIDA(cida_str)
0062         self.assertEqual(cida, cida_rt)
0063         cida_str_rt = shell.CIDAAsString(cida_rt)
0064         self.assertEqual(cida_str_rt, cida_str)
0065 
0066     def testPIDL(self):
0067         # A PIDL of "\1" is:   cb    pidl   cb
0068         expect =            "\03\00" "\1"  "\0\0"
0069         self.assertEqual(shell.PIDLAsString(["\1"]), expect)
0070         self._rtPIDL(["\0"])
0071         self._rtPIDL(["\1", "\2", "\3"])
0072         self._rtPIDL(["\0" * 2048] * 2048)
0073         # PIDL must be a list
0074         self.assertRaises(TypeError, shell.PIDLAsString, "foo")
0075 
0076     def testCIDA(self):
0077         self._rtCIDA(["\0"], [ ["\0"] ])
0078         self._rtCIDA(["\1"], [ ["\2"] ])
0079         self._rtCIDA(["\0"], [ ["\0"], ["\1"], ["\2"] ])
0080 
0081 class FILEGROUPDESCRIPTORTester(win32com.test.util.TestCase):
0082     def _testRT(self, fd):
0083         fgd_string = shell.FILEGROUPDESCRIPTORAsString([fd])
0084         fd2 = shell.StringAsFILEGROUPDESCRIPTOR(fgd_string)[0]
0085         
0086         fd = fd.copy()
0087         fd2 = fd2.copy()
0088         
0089         # The returned objects *always* have dwFlags and cFileName.
0090         if not fd.has_key('dwFlags'):
0091             del fd2['dwFlags']
0092         if not fd.has_key('cFileName'):
0093             self.assertEqual(fd2['cFileName'], '')
0094             del fd2['cFileName']
0095 
0096         self.assertEqual(fd, fd2)
0097 
0098     def testSimple(self):
0099         fgd = shell.FILEGROUPDESCRIPTORAsString([])
0100         header = struct.pack("i", 0)
0101         self.assertEqual(header, fgd[:len(header)])
0102         self._testRT(dict())
0103         d = dict()
0104         fgd = shell.FILEGROUPDESCRIPTORAsString([d])
0105         header = struct.pack("i", 1)
0106         self.assertEqual(header, fgd[:len(header)])
0107         self._testRT(d)
0108     
0109     def testComplex(self):
0110         clsid = pythoncom.MakeIID("{CD637886-DB8B-4b04-98B5-25731E1495BE}")
0111         d = dict(cFileName="foo.txt",
0112                  clsid=clsid,
0113                  sizel=(1,2),
0114                  pointl=(3,4),
0115                  dwFileAttributes = win32con.FILE_ATTRIBUTE_NORMAL,
0116                  ftCreationTime=pythoncom.MakeTime(10),
0117                  ftLastAccessTime=pythoncom.MakeTime(11),
0118                  ftLastWriteTime=pythoncom.MakeTime(12),
0119                  nFileSize=sys.maxint + 1)
0120         self._testRT(d)
0121 
0122 class FileOperationTester(win32com.test.util.TestCase):
0123     def setUp(self):
0124         import tempfile
0125         self.src_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell")
0126         self.dest_name = os.path.join(tempfile.gettempdir(), "pywin32_testshell_dest")
0127         self.test_data = "Hello from\0Python"
0128         f=file(self.src_name, "wb")
0129         f.write(self.test_data)
0130         f.close()
0131         try:
0132             os.unlink(self.dest_name)
0133         except os.error:
0134             pass
0135 
0136     def tearDown(self):
0137         for fname in (self.src_name, self.dest_name):
0138             if os.path.isfile(fname):
0139                 os.unlink(fname)
0140 
0141     def testCopy(self):
0142         s = (0, # hwnd,
0143              FO_COPY, #operation
0144              self.src_name,
0145              self.dest_name)
0146 
0147         rc, aborted = shell.SHFileOperation(s)
0148         self.failUnless(not aborted)
0149         self.failUnlessEqual(0, rc)
0150         self.failUnless(os.path.isfile(self.src_name))
0151         self.failUnless(os.path.isfile(self.dest_name))
0152 
0153     def testRename(self):
0154         s = (0, # hwnd,
0155              FO_RENAME, #operation
0156              self.src_name,
0157              self.dest_name)
0158         rc, aborted = shell.SHFileOperation(s)
0159         self.failUnless(not aborted)
0160         self.failUnlessEqual(0, rc)
0161         self.failUnless(os.path.isfile(self.dest_name))
0162         self.failUnless(not os.path.isfile(self.src_name))
0163 
0164     def testMove(self):
0165         s = (0, # hwnd,
0166              FO_MOVE, #operation
0167              self.src_name,
0168              self.dest_name)
0169         rc, aborted = shell.SHFileOperation(s)
0170         self.failUnless(not aborted)
0171         self.failUnlessEqual(0, rc)
0172         self.failUnless(os.path.isfile(self.dest_name))
0173         self.failUnless(not os.path.isfile(self.src_name))
0174 
0175     def testDelete(self):
0176         s = (0, # hwnd,
0177              FO_DELETE, #operation
0178              self.src_name, None,
0179              FOF_NOCONFIRMATION)
0180         rc, aborted = shell.SHFileOperation(s)
0181         self.failUnless(not aborted)
0182         self.failUnlessEqual(0, rc)
0183         self.failUnless(not os.path.isfile(self.src_name))
0184 
0185 if __name__=='__main__':
0186     win32com.test.util.testmain()
0187 

Generated by PyXR 0.9.4
SourceForge.net Logo