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