2 * Unit test of the IShellFolder functions.
4 * Copyright 2004 Vitaliy Margolen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "wine/unicode.h"
39 #include "wine/test.h"
44 /* creates a file with the specified name for tests */
45 static void CreateTestFile(const CHAR *name)
50 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
51 if (file != INVALID_HANDLE_VALUE)
53 WriteFile(file, name, strlen(name), &written, NULL);
54 WriteFile(file, "\n", strlen("\n"), &written, NULL);
60 /* initializes the tests */
61 static void CreateFilesFolders(void)
63 CreateDirectoryA(".\\testdir", NULL);
64 CreateDirectoryA(".\\testdir\\test.txt", NULL);
65 CreateTestFile (".\\testdir\\test1.txt ");
66 CreateTestFile (".\\testdir\\test2.txt ");
67 CreateTestFile (".\\testdir\\test3.txt ");
68 CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
69 CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
72 /* cleans after tests */
73 static void Cleanup(void)
75 DeleteFileA(".\\testdir\\test1.txt");
76 DeleteFileA(".\\testdir\\test2.txt");
77 DeleteFileA(".\\testdir\\test3.txt");
78 RemoveDirectoryA(".\\testdir\\test.txt");
79 RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
80 RemoveDirectoryA(".\\testdir\\testdir2");
81 RemoveDirectoryA(".\\testdir");
86 static void test_EnumObjects(IShellFolder *iFolder)
88 IEnumIDList *iEnumList;
89 ITEMIDLIST *newPIDL, *(idlArr [5]);
94 static const WORD iResults [5][5] =
103 /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
104 static const ULONG attrs[5] =
106 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
107 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
113 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
114 ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
116 while (IEnumIDList_Next(iEnumList, 1, &newPIDL, &NumPIDLs) == S_OK)
118 idlArr[i++] = newPIDL;
121 hr = IEnumIDList_Release(iEnumList);
122 ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
124 /* Sort them first in case of wrong order from system */
125 for (i=0;i<5;i++) for (j=0;j<5;j++)
126 if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
129 idlArr[i] = idlArr[j];
133 for (i=0;i<5;i++) for (j=0;j<5;j++)
135 hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
136 ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
140 for (i = 0; i < 5; i++)
143 flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
144 hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
145 flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
146 ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
147 ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
151 IMalloc_Free(ppM, idlArr[i]);
154 static void test_BindToObject(void)
158 IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
159 SHITEMID emptyitem = { 0, { 0 } };
160 LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
161 WCHAR wszSystemDir[MAX_PATH];
162 WCHAR wszMyComputer[] = {
163 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
164 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
166 /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
167 * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
169 hr = SHGetDesktopFolder(&psfDesktop);
170 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
171 if (FAILED(hr)) return;
173 hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
174 ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
176 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
177 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
179 IShellFolder_Release(psfDesktop);
183 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
184 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
185 IShellFolder_Release(psfDesktop);
186 ILFree(pidlMyComputer);
187 if (FAILED(hr)) return;
189 hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
190 ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
192 cChars = GetSystemDirectoryW(wszSystemDir, MAX_PATH);
193 ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryW failed! LastError: %08lx\n", GetLastError());
194 if (cChars == 0 || cChars >= MAX_PATH) {
195 IShellFolder_Release(psfMyComputer);
199 hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
200 ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
202 IShellFolder_Release(psfMyComputer);
206 hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
207 ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
208 IShellFolder_Release(psfMyComputer);
209 ILFree(pidlSystemDir);
210 if (FAILED(hr)) return;
212 hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
213 ok (hr == E_INVALIDARG,
214 "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
216 IShellFolder_Release(psfSystemDir);
219 static void test_GetDisplayName(void)
224 WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
226 LPSHELLFOLDER psfDesktop, psfPersonal;
227 LPITEMIDLIST pidlTestFile;
228 LPCITEMIDLIST pidlLast;
229 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
230 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
232 /* I'm trying to figure if there is a functional difference between calling
233 * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
234 * binding to the shellfolder. One thing I thought of was that perhaps
235 * SHGetPathFromIDList would be able to get the path to a file, which does
236 * not exist anymore, while the other method would'nt. It turns out there's
237 * no functional difference in this respect.
240 /* First creating a directory in MyDocuments and a file in this directory. */
241 result = SHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
242 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
245 PathAddBackslashW(wszTestDir);
246 lstrcatW(wszTestDir, wszDirName);
247 result = CreateDirectoryW(wszTestDir, NULL);
248 ok(result, "CreateDirectoryW failed! Last error: %08lx\n", GetLastError());
251 lstrcpyW(wszTestFile, wszTestDir);
252 PathAddBackslashW(wszTestFile);
253 lstrcatW(wszTestFile, wszFileName);
255 hTestFile = CreateFileW(wszTestFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
256 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
257 if (hTestFile == INVALID_HANDLE_VALUE) return;
258 CloseHandle(hTestFile);
260 /* Getting a itemidlist for the file. */
261 hr = SHGetDesktopFolder(&psfDesktop);
262 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
263 if (FAILED(hr)) return;
265 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
266 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
268 IShellFolder_Release(psfDesktop);
272 /* Deleting the file and the directory */
273 DeleteFileW(wszTestFile);
274 RemoveDirectoryW(wszTestDir);
276 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
277 result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
278 ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
279 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
281 /* Binding to the folder and querying the display name of the file also works. */
282 hr = SHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
283 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
285 IShellFolder_Release(psfDesktop);
289 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
290 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
292 IShellFolder_Release(psfDesktop);
293 IShellFolder_Release(psfPersonal);
297 hr = StrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
298 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
299 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
301 IShellFolder_Release(psfDesktop);
302 IShellFolder_Release(psfPersonal);
305 START_TEST(shlfolder)
308 IShellFolder *IDesktopFolder, *testIShellFolder;
309 char cCurrDirA [MAX_PATH] = {0};
310 WCHAR cCurrDirW [MAX_PATH];
311 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
314 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
315 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
316 strcatW(cCurrDirW, cTestDirW);
320 hr = SHGetMalloc(&ppM);
321 ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
323 CreateFilesFolders();
325 hr = SHGetDesktopFolder(&IDesktopFolder);
326 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
328 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
329 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
331 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
332 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
334 test_EnumObjects(testIShellFolder);
336 test_GetDisplayName();
338 hr = IShellFolder_Release(testIShellFolder);
339 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
341 IMalloc_Free(ppM, newPIDL);