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;
228 LPITEMIDLIST pidlTestFile;
229 LPCITEMIDLIST pidlLast;
230 static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
231 static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
233 /* I'm trying to figure if there is a functional difference between calling
234 * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
235 * binding to the shellfolder. One thing I thought of was that perhaps
236 * SHGetPathFromIDList would be able to get the path to a file, which does
237 * not exist anymore, while the other method would'nt. It turns out there's
238 * no functional difference in this respect.
241 /* First creating a directory in MyDocuments and a file in this directory. */
242 result = SHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
243 ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
246 PathAddBackslashW(wszTestDir);
247 lstrcatW(wszTestDir, wszDirName);
248 result = CreateDirectoryW(wszTestDir, NULL);
249 ok(result, "CreateDirectoryW failed! Last error: %08lx\n", GetLastError());
252 lstrcpyW(wszTestFile, wszTestDir);
253 PathAddBackslashW(wszTestFile);
254 lstrcatW(wszTestFile, wszFileName);
256 hTestFile = CreateFileW(wszTestFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
257 ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
258 if (hTestFile == INVALID_HANDLE_VALUE) return;
259 CloseHandle(hTestFile);
261 /* Getting a itemidlist for the file. */
262 hr = SHGetDesktopFolder(&psfDesktop);
263 ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
264 if (FAILED(hr)) return;
266 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
267 ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
269 IShellFolder_Release(psfDesktop);
273 /* It seems as if we can not bind to regular files on windows, but only directories.
275 hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
276 todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08lx\n", hr); }
278 IShellFolder_Release(psfFile);
281 /* Deleting the file and the directory */
282 DeleteFileW(wszTestFile);
283 RemoveDirectoryW(wszTestDir);
285 /* SHGetPathFromIDListW still works, although the file is not present anymore. */
286 result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
287 ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
288 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
290 /* Binding to the folder and querying the display name of the file also works. */
291 hr = SHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast);
292 ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
294 IShellFolder_Release(psfDesktop);
298 hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
299 ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
301 IShellFolder_Release(psfDesktop);
302 IShellFolder_Release(psfPersonal);
306 hr = StrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
307 ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
308 ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
310 IShellFolder_Release(psfDesktop);
311 IShellFolder_Release(psfPersonal);
314 static void test_GetAttributesOf(void)
317 LPSHELLFOLDER psfDesktop, psfMyComputer;
318 SHITEMID emptyitem = { 0, { 0 } };
319 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
320 LPITEMIDLIST pidlMyComputer;
322 const static DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
323 SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGE_ANCESTOR |
324 SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
325 const static DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
326 SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
327 SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
328 WCHAR wszMyComputer[] = {
329 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
330 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
332 hr = SHGetDesktopFolder(&psfDesktop);
333 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
334 if (FAILED(hr)) return;
336 /* The Desktop attributes can be queried with a single empty itemidlist, .. */
337 dwFlags = 0xffffffff;
338 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
339 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08lx\n", hr);
340 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
341 dwFlags, dwDesktopFlags);
343 /* .. or with no itemidlist at all. */
344 dwFlags = 0xffffffff;
345 hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
346 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
347 ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n",
348 dwFlags, dwDesktopFlags);
350 /* Testing the attributes of the MyComputer shellfolder */
351 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
352 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
354 IShellFolder_Release(psfDesktop);
358 /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop
359 * folder object. It doesn't do this, if MyComputer is queried directly (see below).
360 * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
361 * should create a link to the original data". You can't create links on MyComputer on
362 * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
363 * depends on this bug, we probably shouldn't imitate it.
365 dwFlags = 0xffffffff;
366 hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
367 ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08lx\n", hr);
368 todo_wine { ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags,
369 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
371 hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
372 ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
373 IShellFolder_Release(psfDesktop);
374 ILFree(pidlMyComputer);
375 if (FAILED(hr)) return;
377 hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
378 todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08lx\n", hr); }
380 dwFlags = 0xffffffff;
381 hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
382 ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
383 todo_wine { ok (dwFlags == dwMyComputerFlags,
384 "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
386 IShellFolder_Release(psfMyComputer);
389 static void test_SHGetPathFromIDList(void)
391 SHITEMID emptyitem = { 0, { 0 } };
392 LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
393 LPITEMIDLIST pidlMyComputer;
394 WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
397 LPSHELLFOLDER psfDesktop;
398 WCHAR wszMyComputer[] = {
399 ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
400 'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
402 /* Calling SHGetPathFromIDList with an empty pidl should return the desktop folder's path. */
403 result = SHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
404 ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %08lx\n", GetLastError());
407 result = SHGetPathFromIDListW(pidlEmpty, wszPath);
408 ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
410 ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDList didn't return desktop path for empty pidl!\n");
412 /* MyComputer does not map to a filesystem path. SHGetPathFromIDList should fail. */
413 hr = SHGetDesktopFolder(&psfDesktop);
414 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
415 if (FAILED(hr)) return;
417 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
418 ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
419 IShellFolder_Release(psfDesktop);
420 if (FAILED(hr)) return;
422 SetLastError(0xdeadbeef);
423 result = SHGetPathFromIDListW(pidlMyComputer, wszPath);
424 ok (!result, "SHGetPathFromIDList succeeded where it shouldn't!\n");
425 ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDList shouldn't set last error! Last error: %08lx\n", GetLastError());
427 ILFree(pidlMyComputer);
430 START_TEST(shlfolder)
433 IShellFolder *IDesktopFolder, *testIShellFolder;
434 char cCurrDirA [MAX_PATH] = {0};
435 WCHAR cCurrDirW [MAX_PATH];
436 static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
439 GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
440 MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
441 strcatW(cCurrDirW, cTestDirW);
445 hr = SHGetMalloc(&ppM);
446 ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
448 CreateFilesFolders();
450 hr = SHGetDesktopFolder(&IDesktopFolder);
451 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
453 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
454 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
456 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
457 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
459 test_EnumObjects(testIShellFolder);
461 test_GetDisplayName();
462 test_GetAttributesOf();
463 test_SHGetPathFromIDList();
465 hr = IShellFolder_Release(testIShellFolder);
466 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
468 IMalloc_Free(ppM, newPIDL);