Enhance uxtheme to store the themed system metrics in the registry and
[wine] / dlls / shell32 / tests / shlfolder.c
1 /*
2  * Unit test of the IShellFolder functions.
3  *
4  * Copyright 2004 Vitaliy Margolen
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wtypes.h"
29 #include "shellapi.h"
30
31
32 #include "shlguid.h"
33 #include "shlobj.h"
34 #include "shobjidl.h"
35 #include "shlwapi.h"
36
37
38 #include "wine/unicode.h"
39 #include "wine/test.h"
40
41
42 static IMalloc *ppM;
43
44 static HRESULT (WINAPI *pSHBindToParent)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*);
45 static BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL);
46
47 static void init_function_pointers(void)
48 {
49     HMODULE hmod = GetModuleHandleA("shell32.dll");
50     HRESULT hr;
51
52     if(hmod)
53     {
54         pSHBindToParent = (void*)GetProcAddress(hmod, "SHBindToParent");
55         pSHGetSpecialFolderPathW = (void*)GetProcAddress(hmod, "SHGetSpecialFolderPathW");
56     }
57
58     hr = SHGetMalloc(&ppM);
59     ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
60 }
61
62 static void test_ParseDisplayName(void)
63 {
64     HRESULT hr;
65     IShellFolder *IDesktopFolder;
66     static const char *cNonExistDir1A = "c:\\nonexist_subdir";
67     static const char *cNonExistDir2A = "c:\\\\nonexist_subdir";
68     DWORD res;
69     WCHAR cTestDirW [MAX_PATH] = {0};
70     ITEMIDLIST *newPIDL;
71
72     hr = SHGetDesktopFolder(&IDesktopFolder);
73     if(hr != S_OK) return;
74
75     res = GetFileAttributesA(cNonExistDir1A);
76     if(res != INVALID_FILE_ATTRIBUTES) return;
77
78     MultiByteToWideChar(CP_ACP, 0, cNonExistDir1A, -1, cTestDirW, MAX_PATH);
79     hr = IShellFolder_ParseDisplayName(IDesktopFolder, 
80         NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
81     ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL), 
82         "ParseDisplayName returned %08lx, expected 80070002 or E_FAIL\n", hr);
83
84     res = GetFileAttributesA(cNonExistDir2A);
85     if(res != INVALID_FILE_ATTRIBUTES) return;
86
87     MultiByteToWideChar(CP_ACP, 0, cNonExistDir2A, -1, cTestDirW, MAX_PATH);
88     hr = IShellFolder_ParseDisplayName(IDesktopFolder, 
89         NULL, NULL, cTestDirW, NULL, &newPIDL, 0);
90     ok((hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) || (hr == E_FAIL), 
91         "ParseDisplayName returned %08lx, expected 80070002 or E_FAIL\n", hr);
92 }
93
94 /* creates a file with the specified name for tests */
95 static void CreateTestFile(const CHAR *name)
96 {
97     HANDLE file;
98     DWORD written;
99
100     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
101     if (file != INVALID_HANDLE_VALUE)
102     {
103         WriteFile(file, name, strlen(name), &written, NULL);
104         WriteFile(file, "\n", strlen("\n"), &written, NULL);
105         CloseHandle(file);
106     }
107 }
108
109
110 /* initializes the tests */
111 static void CreateFilesFolders(void)
112 {
113     CreateDirectoryA(".\\testdir", NULL);
114     CreateDirectoryA(".\\testdir\\test.txt", NULL);
115     CreateTestFile  (".\\testdir\\test1.txt ");
116     CreateTestFile  (".\\testdir\\test2.txt ");
117     CreateTestFile  (".\\testdir\\test3.txt ");
118     CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
119     CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
120 }
121
122 /* cleans after tests */
123 static void Cleanup(void)
124 {
125     DeleteFileA(".\\testdir\\test1.txt");
126     DeleteFileA(".\\testdir\\test2.txt");
127     DeleteFileA(".\\testdir\\test3.txt");
128     RemoveDirectoryA(".\\testdir\\test.txt");
129     RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
130     RemoveDirectoryA(".\\testdir\\testdir2");
131     RemoveDirectoryA(".\\testdir");
132 }
133
134
135 /* perform test */
136 static void test_EnumObjects(IShellFolder *iFolder)
137 {
138     IEnumIDList *iEnumList;
139     LPITEMIDLIST newPIDL, idlArr[10];
140     ULONG NumPIDLs;
141     int i=0, j;
142     HRESULT hr;
143
144     static const WORD iResults [5][5] =
145     {
146         { 0,-1,-1,-1,-1},
147         { 1, 0,-1,-1,-1},
148         { 1, 1, 0,-1,-1},
149         { 1, 1, 1, 0,-1},
150         { 1, 1, 1, 1, 0}
151     };
152
153     /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
154     static const ULONG attrs[5] =
155     {
156         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
157         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
158         SFGAO_FILESYSTEM,
159         SFGAO_FILESYSTEM,
160         SFGAO_FILESYSTEM,
161     };
162
163     hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
164     ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
165
166     /* This is to show that, contrary to what is said on MSDN, on IEnumIDList::Next,
167      * the filesystem shellfolders return S_OK even if less than 'celt' items are
168      * returned (in contrast to S_FALSE). We have to do it in a loop since WinXP
169      * only ever returns a single entry per call. */
170     while (IEnumIDList_Next(iEnumList, 10-i, &idlArr[i], &NumPIDLs) == S_OK) 
171         i += NumPIDLs;
172     ok (i == 5, "i: %d\n", i);
173
174     hr = IEnumIDList_Release(iEnumList);
175     ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
176     
177     /* Sort them first in case of wrong order from system */
178     for (i=0;i<5;i++) for (j=0;j<5;j++)
179         if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
180         {
181             newPIDL = idlArr[i];
182             idlArr[i] = idlArr[j];
183             idlArr[j] = newPIDL;
184         }
185             
186     for (i=0;i<5;i++) for (j=0;j<5;j++)
187     {
188         hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
189         ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
190     }
191
192
193     for (i = 0; i < 5; i++)
194     {
195         SFGAOF flags;
196         flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
197         hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
198         flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
199         ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
200         ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
201     }
202
203     for (i=0;i<5;i++)
204         IMalloc_Free(ppM, idlArr[i]);
205 }
206
207 static void test_BindToObject(void)
208 {
209     HRESULT hr;
210     UINT cChars;
211     IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
212     SHITEMID emptyitem = { 0, { 0 } };
213     LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
214     WCHAR wszSystemDir[MAX_PATH];
215     WCHAR wszMyComputer[] = { 
216         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
217         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
218
219     /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
220      * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
221      */
222     hr = SHGetDesktopFolder(&psfDesktop);
223     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
224     if (FAILED(hr)) return;
225     
226     hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
227     ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
228
229     hr = IShellFolder_BindToObject(psfDesktop, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
230     ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
231
232     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
233     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
234     if (FAILED(hr)) {
235         IShellFolder_Release(psfDesktop);
236         return;
237     }
238     
239     hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
240     ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
241     IShellFolder_Release(psfDesktop);
242     IMalloc_Free(ppM, pidlMyComputer);
243     if (FAILED(hr)) return;
244
245     hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
246     ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
247
248     hr = IShellFolder_BindToObject(psfMyComputer, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
249     ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
250
251     cChars = GetSystemDirectoryW(wszSystemDir, MAX_PATH);
252     ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryW failed! LastError: %08lx\n", GetLastError());
253     if (cChars == 0 || cChars >= MAX_PATH) {
254         IShellFolder_Release(psfMyComputer);
255         return;
256     }
257     
258     hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
259     ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
260     if (FAILED(hr)) {
261         IShellFolder_Release(psfMyComputer);
262         return;
263     }
264
265     hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
266     ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
267     IShellFolder_Release(psfMyComputer);
268     IMalloc_Free(ppM, pidlSystemDir);
269     if (FAILED(hr)) return;
270
271     hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
272     ok (hr == E_INVALIDARG, 
273         "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
274     
275     hr = IShellFolder_BindToObject(psfSystemDir, NULL, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
276     ok (hr == E_INVALIDARG, 
277         "FileSystem ShellFolder's BindToObject should fail, when called with NULL pidl! hr = %08lx\n", hr);
278
279     IShellFolder_Release(psfSystemDir);
280 }
281   
282 static void test_GetDisplayName(void)
283 {
284     BOOL result;
285     HRESULT hr;
286     HANDLE hTestFile;
287     WCHAR wszTestFile[MAX_PATH], wszTestFile2[MAX_PATH], wszTestDir[MAX_PATH];
288     STRRET strret;
289     LPSHELLFOLDER psfDesktop, psfPersonal;
290     IUnknown *psfFile;
291     LPITEMIDLIST pidlTestFile;
292     LPCITEMIDLIST pidlLast;
293     static const WCHAR wszFileName[] = { 'w','i','n','e','t','e','s','t','.','f','o','o',0 };
294     static const WCHAR wszDirName[] = { 'w','i','n','e','t','e','s','t',0 };
295
296     /* I'm trying to figure if there is a functional difference between calling
297      * SHGetPathFromIDList and calling GetDisplayNameOf(SHGDN_FORPARSING) after
298      * binding to the shellfolder. One thing I thought of was that perhaps 
299      * SHGetPathFromIDList would be able to get the path to a file, which does
300      * not exist anymore, while the other method would'nt. It turns out there's
301      * no functional difference in this respect.
302      */
303
304     if(!pSHGetSpecialFolderPathW) return;
305
306     /* First creating a directory in MyDocuments and a file in this directory. */
307     result = pSHGetSpecialFolderPathW(NULL, wszTestDir, CSIDL_PERSONAL, FALSE);
308     ok(result, "SHGetSpecialFolderPathW failed! Last error: %08lx\n", GetLastError());
309     if (!result) return;
310
311     PathAddBackslashW(wszTestDir);
312     lstrcatW(wszTestDir, wszDirName);
313     result = CreateDirectoryW(wszTestDir, NULL);
314     ok(result, "CreateDirectoryW failed! Last error: %08lx\n", GetLastError());
315     if (!result) return;
316
317     lstrcpyW(wszTestFile, wszTestDir);
318     PathAddBackslashW(wszTestFile);
319     lstrcatW(wszTestFile, wszFileName);
320
321     hTestFile = CreateFileW(wszTestFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
322     ok(hTestFile != INVALID_HANDLE_VALUE, "CreateFileW failed! Last error: %08lx\n", GetLastError());
323     if (hTestFile == INVALID_HANDLE_VALUE) return;
324     CloseHandle(hTestFile);
325
326     /* Getting an itemidlist for the file. */
327     hr = SHGetDesktopFolder(&psfDesktop);
328     ok(SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
329     if (FAILED(hr)) return;
330
331     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszTestFile, NULL, &pidlTestFile, NULL);
332     ok(SUCCEEDED(hr), "Desktop->ParseDisplayName failed! hr = %08lx\n", hr);
333     if (FAILED(hr)) {
334         IShellFolder_Release(psfDesktop);
335         return;
336     }
337
338     /* It seems as if we cannot bind to regular files on windows, but only directories. 
339      */
340     hr = IShellFolder_BindToObject(psfDesktop, pidlTestFile, NULL, &IID_IUnknown, (VOID**)&psfFile);
341     todo_wine { ok (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "hr = %08lx\n", hr); }
342     if (SUCCEEDED(hr)) {
343         IShellFolder_Release(psfFile);
344     }
345     
346     /* Deleting the file and the directory */
347     DeleteFileW(wszTestFile);
348     RemoveDirectoryW(wszTestDir);
349
350     /* SHGetPathFromIDListW still works, although the file is not present anymore. */
351     result = SHGetPathFromIDListW(pidlTestFile, wszTestFile2);
352     ok (result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
353     ok (!lstrcmpiW(wszTestFile, wszTestFile2), "SHGetPathFromIDListW returns incorrect path!\n");
354
355     if(!pSHBindToParent) return;
356
357     /* Binding to the folder and querying the display name of the file also works. */
358     hr = pSHBindToParent(pidlTestFile, &IID_IShellFolder, (VOID**)&psfPersonal, &pidlLast); 
359     ok (SUCCEEDED(hr), "SHBindToParent failed! hr = %08lx\n", hr);
360     if (FAILED(hr)) {
361         IShellFolder_Release(psfDesktop);
362         return;
363     }
364
365     hr = IShellFolder_GetDisplayNameOf(psfPersonal, pidlLast, SHGDN_FORPARSING, &strret);
366     ok (SUCCEEDED(hr), "Personal->GetDisplayNameOf failed! hr = %08lx\n", hr);
367     if (FAILED(hr)) {
368         IShellFolder_Release(psfDesktop);
369         IShellFolder_Release(psfPersonal);
370         return;
371     }
372     
373     hr = StrRetToBufW(&strret, pidlLast, wszTestFile2, MAX_PATH);
374     ok (SUCCEEDED(hr), "StrRetToBufW failed! hr = %08lx\n", hr);
375     ok (!lstrcmpiW(wszTestFile, wszTestFile2), "GetDisplayNameOf returns incorrect path!\n");
376     
377     IShellFolder_Release(psfDesktop);
378     IShellFolder_Release(psfPersonal);
379 }
380
381 static void test_CallForAttributes(void)
382 {
383     HKEY hKey;
384     LONG lResult;
385     HRESULT hr;
386     DWORD dwSize;
387     LPSHELLFOLDER psfDesktop;
388     LPITEMIDLIST pidlMyDocuments;
389     DWORD dwAttributes, dwCallForAttributes, dwOrigAttributes, dwOrigCallForAttributes;
390     static const WCHAR wszAttributes[] = { 'A','t','t','r','i','b','u','t','e','s',0 };
391     static const WCHAR wszCallForAttributes[] = { 
392         'C','a','l','l','F','o','r','A','t','t','r','i','b','u','t','e','s',0 };
393     static const WCHAR wszMyDocumentsKey[] = {
394         'C','L','S','I','D','\\','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-',
395         '1','1','D','0','-','9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',
396         '\\','S','h','e','l','l','F','o','l','d','e','r',0 };
397     WCHAR wszMyDocuments[] = {
398         ':',':','{','4','5','0','D','8','F','B','A','-','A','D','2','5','-','1','1','D','0','-',
399         '9','8','A','8','-','0','8','0','0','3','6','1','B','1','1','0','3','}',0 };
400     
401     /* For the root of a namespace extension, the attributes are not queried by binding
402      * to the object and calling GetAttributesOf. Instead, the attributes are read from 
403      * the registry value HKCR/CLSID/{...}/ShellFolder/Attributes. This is documented on MSDN.
404      *
405      * The MyDocuments shellfolder on WinXP has a HKCR/CLSID/{...}/ShellFolder/CallForAttributes
406      * value. It seems that if the folder is queried for one of the flags set in CallForAttributes,
407      * the shell does bind to the folder object and calls GetAttributesOf. This is not documented
408      * on MSDN. This test is meant to document the observed behaviour on WinXP SP2.
409      */
410     hr = SHGetDesktopFolder(&psfDesktop);
411     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
412     if (FAILED(hr)) return;
413     
414     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyDocuments, NULL, 
415                                        &pidlMyDocuments, NULL);
416     ok (SUCCEEDED(hr), 
417         "Desktop's ParseDisplayName failed to parse MyDocuments's CLSID! hr = %08lx\n", hr);
418     if (FAILED(hr)) {
419         IShellFolder_Release(psfDesktop);
420         return;
421     }
422
423     dwAttributes = 0xffffffff;
424     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
425                                       (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
426     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
427
428     /* We need the following setup (as observed on WinXP SP2), for the tests to make sense. */
429     todo_wine{ ok (dwAttributes & SFGAO_FILESYSTEM, 
430                    "SFGAO_FILESYSTEM attribute is not set for MyDocuments!\n"); }
431     ok (!(dwAttributes & SFGAO_ISSLOW), "SFGAO_ISSLOW attribute is set for MyDocuments!\n");
432     ok (!(dwAttributes & SFGAO_GHOSTED), "SFGAO_GHOSTED attribute is set for MyDocuments!\n");
433
434     /* We don't have the MyDocuments shellfolder in wine yet, and thus we don't have the registry
435      * key. So the test will return at this point, if run on wine. 
436      */
437     lResult = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMyDocumentsKey, 0, KEY_WRITE|KEY_READ, &hKey);
438     todo_wine { ok (lResult == ERROR_SUCCESS, "RegOpenKeyEx failed! result: %08lx\n", lResult); }
439     if (lResult != ERROR_SUCCESS) {
440         IMalloc_Free(ppM, pidlMyDocuments);
441         IShellFolder_Release(psfDesktop);
442         return;
443     }
444     
445     /* Query MyDocuments' Attributes value, to be able to restore it later. */
446     dwSize = sizeof(DWORD);
447     lResult = RegQueryValueExW(hKey, wszAttributes, NULL, NULL, (LPBYTE)&dwOrigAttributes, &dwSize);
448     ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
449     if (lResult != ERROR_SUCCESS) {
450         RegCloseKey(hKey);
451         IMalloc_Free(ppM, pidlMyDocuments);
452         IShellFolder_Release(psfDesktop);
453         return;
454     }
455
456     /* Query MyDocuments' CallForAttributes value, to be able to restore it later. */
457     dwSize = sizeof(DWORD);
458     lResult = RegQueryValueExW(hKey, wszCallForAttributes, NULL, NULL, 
459                               (LPBYTE)&dwOrigCallForAttributes, &dwSize);
460     ok (lResult == ERROR_SUCCESS, "RegQueryValueEx failed! result: %08lx\n", lResult);
461     if (lResult != ERROR_SUCCESS) {
462         RegCloseKey(hKey);
463         IMalloc_Free(ppM, pidlMyDocuments);
464         IShellFolder_Release(psfDesktop);
465         return;
466     }
467     
468     /* Define via the Attributes value that MyDocuments attributes are SFGAO_ISSLOW and 
469      * SFGAO_GHOSTED and that MyDocuments should be called for the SFGAO_ISSLOW and
470      * SFGAO_FILESYSTEM attributes. */
471     dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED;
472     RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwAttributes, sizeof(DWORD));
473     dwCallForAttributes = SFGAO_ISSLOW|SFGAO_FILESYSTEM;
474     RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
475                    (LPBYTE)&dwCallForAttributes, sizeof(DWORD));
476
477     /* Although it is not set in CallForAttributes, the SFGAO_GHOSTED flag is reset by 
478      * GetAttributesOf. It seems that once there is a single attribute queried, for which
479      * CallForAttributes is set, all flags are taken from the GetAttributesOf call and
480      * the flags in Attributes are ignored. 
481      */
482     dwAttributes = SFGAO_ISSLOW|SFGAO_GHOSTED|SFGAO_FILESYSTEM;
483     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, 
484                                       (LPCITEMIDLIST*)&pidlMyDocuments, &dwAttributes);
485     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyDocuments) failed! hr = %08lx\n", hr);
486     if (SUCCEEDED(hr)) 
487         ok (dwAttributes == SFGAO_FILESYSTEM, 
488             "Desktop->GetAttributes(MyDocuments) returned unexpected attributes: %08lx\n", 
489             dwAttributes);
490
491     /* Restore MyDocuments' original Attributes and CallForAttributes registry values */
492     RegSetValueExW(hKey, wszAttributes, 0, REG_DWORD, (LPBYTE)&dwOrigAttributes, sizeof(DWORD));
493     RegSetValueExW(hKey, wszCallForAttributes, 0, REG_DWORD, 
494                    (LPBYTE)&dwOrigCallForAttributes, sizeof(DWORD));
495     RegCloseKey(hKey);
496     IMalloc_Free(ppM, pidlMyDocuments);
497     IShellFolder_Release(psfDesktop);
498 }
499
500 static void test_GetAttributesOf(void) 
501 {
502     HRESULT hr;
503     LPSHELLFOLDER psfDesktop, psfMyComputer;
504     SHITEMID emptyitem = { 0, { 0 } };
505     LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
506     LPITEMIDLIST pidlMyComputer;
507     DWORD dwFlags;
508     const static DWORD dwDesktopFlags = /* As observed on WinXP SP2 */
509         SFGAO_STORAGE | SFGAO_HASPROPSHEET | SFGAO_STORAGEANCESTOR |
510         SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_HASSUBFOLDER;
511     const static DWORD dwMyComputerFlags = /* As observed on WinXP SP2 */
512         SFGAO_CANRENAME | SFGAO_CANDELETE | SFGAO_HASPROPSHEET |
513         SFGAO_DROPTARGET | SFGAO_FILESYSANCESTOR | SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
514     WCHAR wszMyComputer[] = { 
515         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
516         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
517
518     hr = SHGetDesktopFolder(&psfDesktop);
519     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
520     if (FAILED(hr)) return;
521
522     /* The Desktop attributes can be queried with a single empty itemidlist, .. */
523     dwFlags = 0xffffffff;
524     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, &pidlEmpty, &dwFlags);
525     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(empty pidl) failed! hr = %08lx\n", hr);
526     ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n", 
527         dwFlags, dwDesktopFlags);
528
529     /* .. or with no itemidlist at all. */
530     dwFlags = 0xffffffff;
531     hr = IShellFolder_GetAttributesOf(psfDesktop, 0, NULL, &dwFlags);
532     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(NULL) failed! hr = %08lx\n", hr);
533     ok (dwFlags == dwDesktopFlags, "Wrong Desktop attributes: %08lx, expected: %08lx\n", 
534         dwFlags, dwDesktopFlags);
535    
536     /* Testing the attributes of the MyComputer shellfolder */
537     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
538     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
539     if (FAILED(hr)) {
540         IShellFolder_Release(psfDesktop);
541         return;
542     }
543
544     /* WinXP SP2 sets the SFGAO_CANLINK flag, when MyComputer is queried via the Desktop 
545      * folder object. It doesn't do this, if MyComputer is queried directly (see below).
546      * SFGAO_CANLINK is the same as DROPEFFECT_LINK, which MSDN says means: "Drag source
547      * should create a link to the original data". You can't create links on MyComputer on
548      * Windows, so this flag shouldn't be set. Seems like a bug in Windows. As long as nobody
549      * depends on this bug, we probably shouldn't imitate it.
550      */
551     dwFlags = 0xffffffff;
552     hr = IShellFolder_GetAttributesOf(psfDesktop, 1, (LPCITEMIDLIST*)&pidlMyComputer, &dwFlags);
553     ok (SUCCEEDED(hr), "Desktop->GetAttributesOf(MyComputer) failed! hr = %08lx\n", hr);
554     todo_wine { ok ((dwFlags & ~(DWORD)SFGAO_CANLINK) == dwMyComputerFlags, 
555                     "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
556
557     hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
558     ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
559     IShellFolder_Release(psfDesktop);
560     IMalloc_Free(ppM, pidlMyComputer);
561     if (FAILED(hr)) return;
562
563     hr = IShellFolder_GetAttributesOf(psfMyComputer, 1, &pidlEmpty, &dwFlags);
564     todo_wine {ok (hr == E_INVALIDARG, "MyComputer->GetAttributesOf(emtpy pidl) should fail! hr = %08lx\n", hr); }
565
566     dwFlags = 0xffffffff;
567     hr = IShellFolder_GetAttributesOf(psfMyComputer, 0, NULL, &dwFlags);
568     ok (SUCCEEDED(hr), "MyComputer->GetAttributesOf(NULL) failed! hr = %08lx\n", hr); 
569     todo_wine { ok (dwFlags == dwMyComputerFlags, 
570                     "Wrong MyComputer attributes: %08lx, expected: %08lx\n", dwFlags, dwMyComputerFlags); }
571
572     IShellFolder_Release(psfMyComputer);
573 }    
574
575 static void test_SHGetPathFromIDList(void)
576 {
577     SHITEMID emptyitem = { 0, { 0 } };
578     LPCITEMIDLIST pidlEmpty = (LPCITEMIDLIST)&emptyitem;
579     LPITEMIDLIST pidlMyComputer;
580     WCHAR wszPath[MAX_PATH], wszDesktop[MAX_PATH];
581     BOOL result;
582     HRESULT hr;
583     LPSHELLFOLDER psfDesktop;
584     WCHAR wszMyComputer[] = { 
585         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
586         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
587
588     if(!pSHGetSpecialFolderPathW) return;
589
590     /* Calling SHGetPathFromIDList with an empty pidl should return the desktop folder's path. */
591     result = pSHGetSpecialFolderPathW(NULL, wszDesktop, CSIDL_DESKTOP, FALSE);
592     ok(result, "SHGetSpecialFolderPathW(CSIDL_DESKTOP) failed! Last error: %08lx\n", GetLastError());
593     if (!result) return;
594     
595     result = SHGetPathFromIDListW(pidlEmpty, wszPath);
596     ok(result, "SHGetPathFromIDListW failed! Last error: %08lx\n", GetLastError());
597     if (!result) return;
598     ok(!lstrcmpiW(wszDesktop, wszPath), "SHGetPathFromIDList didn't return desktop path for empty pidl!\n");
599
600     /* MyComputer does not map to a filesystem path. SHGetPathFromIDList should fail. */
601     hr = SHGetDesktopFolder(&psfDesktop);
602     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
603     if (FAILED(hr)) return;
604
605     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
606     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
607     IShellFolder_Release(psfDesktop);
608     if (FAILED(hr)) return;
609
610     SetLastError(0xdeadbeef);
611     result = SHGetPathFromIDListW(pidlMyComputer, wszPath);
612     ok (!result, "SHGetPathFromIDList succeeded where it shouldn't!\n");
613     ok (GetLastError()==0xdeadbeef, "SHGetPathFromIDList shouldn't set last error! Last error: %08lx\n", GetLastError());
614
615     IMalloc_Free(ppM, pidlMyComputer);
616 }
617
618 static void test_EnumObjects_and_CompareIDs(void)
619 {
620     ITEMIDLIST *newPIDL;
621     IShellFolder *IDesktopFolder, *testIShellFolder;
622     char  cCurrDirA [MAX_PATH] = {0};
623     WCHAR cCurrDirW [MAX_PATH];
624     static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
625     int len;
626     HRESULT hr;
627
628     GetCurrentDirectoryA(MAX_PATH, cCurrDirA);
629     len = lstrlenA(cCurrDirA);
630
631     if(len == 0) {
632         trace("GetCurrentDirectoryA returned empty string. Skipping test_EnumObjects_and_CompareIDs\n");
633         return;
634     }
635     if(cCurrDirA[len-1] == '\\')
636         cCurrDirA[len-1] = 0;
637
638     MultiByteToWideChar(CP_ACP, 0, cCurrDirA, -1, cCurrDirW, MAX_PATH);
639     strcatW(cCurrDirW, cTestDirW);
640
641     hr = SHGetDesktopFolder(&IDesktopFolder);
642     ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
643
644     CreateFilesFolders();
645
646     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
647     ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
648
649     hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
650     ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
651
652     test_EnumObjects(testIShellFolder);
653
654     hr = IShellFolder_Release(testIShellFolder);
655     ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
656
657     Cleanup();
658
659     IMalloc_Free(ppM, newPIDL);
660 }
661
662 START_TEST(shlfolder)
663 {
664     init_function_pointers();
665     /* if OleInitialize doesn't get called, ParseDisplayName returns
666        CO_E_NOTINITIALIZED for malformed directory names on win2k. */
667     OleInitialize(NULL);
668
669     test_ParseDisplayName();
670     test_BindToObject();
671     test_EnumObjects_and_CompareIDs();
672     test_GetDisplayName();
673     test_GetAttributesOf();
674     test_SHGetPathFromIDList();
675     test_CallForAttributes();
676
677     OleUninitialize();
678 }