Reverse the order for deleting the items in resetcontent to correctly
[wine] / dlls / shell32 / tests / shelllink.c
1 /*
2  * Unit tests for shelllinks
3  *
4  * Copyright 2004 Mike McCormack
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  * This is a test program for the SHGet{Special}Folder{Path|Location} functions
20  * of shell32, that get either a filesytem path or a LPITEMIDLIST (shell
21  * namespace) path for a given folder (CSIDL value).
22  *
23  */
24
25 #define COBJMACROS
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "shlguid.h"
32 #include "shobjidl.h"
33 #include "shlobj.h"
34 #include "wine/test.h"
35
36 static const WCHAR lnkfile[]= { 'C',':','\\','t','e','s','t','.','l','n','k',0 };
37 static const WCHAR notafile[]= { 'C',':','\\','n','o','n','e','x','i','s','t','e','n','t','\\','f','i','l','e',0 };
38
39
40 /* For some reason SHILCreateFromPath does not work on Win98 and
41  * SHSimpleIDListFromPathA does not work on NT4. But if we call both we
42  * get what we want on all platforms.
43  */
44 static LPITEMIDLIST (WINAPI *pSHSimpleIDListFromPathA)(LPCSTR)=NULL;
45
46 static LPITEMIDLIST path_to_pidl(const char* path)
47 {
48     LPITEMIDLIST pidl;
49
50     if (!pSHSimpleIDListFromPathA)
51     {
52         HMODULE hdll=LoadLibraryA("shell32.dll");
53         pSHSimpleIDListFromPathA=(void*)GetProcAddress(hdll, (char*)162);
54         if (!pSHSimpleIDListFromPathA)
55             trace("SHSimpleIDListFromPathA not found in shell32.dll");
56     }
57
58     pidl=NULL;
59     if (pSHSimpleIDListFromPathA)
60         pidl=pSHSimpleIDListFromPathA(path);
61
62     if (!pidl)
63     {
64         WCHAR* pathW;
65         HRESULT r;
66         int len;
67
68         len=MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
69         pathW=HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
70         MultiByteToWideChar(CP_ACP, 0, path, -1, pathW, len);
71
72         r=SHILCreateFromPath(pathW, &pidl, NULL);
73         todo_wine {
74         ok(SUCCEEDED(r), "SHILCreateFromPath failed (0x%08lx)\n", r);
75         }
76         HeapFree(GetProcessHeap(), 0, pathW);
77     }
78     return pidl;
79 }
80
81
82 /*
83  * Test manipulation of an IShellLink's properties.
84  */
85
86 static void test_get_set()
87 {
88     HRESULT r;
89     IShellLinkA *sl;
90     char mypath[MAX_PATH];
91     char buffer[INFOTIPSIZE];
92     LPITEMIDLIST pidl, tmp_pidl;
93     char * str;
94     int i;
95     WORD w;
96
97     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
98                          &IID_IShellLinkA, (LPVOID*)&sl);
99     ok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
100     if (!SUCCEEDED(r))
101         return;
102
103     /* Test Getting / Setting the description */
104     strcpy(buffer,"garbage");
105     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
106     ok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
107     ok(*buffer=='\0', "GetDescription returned '%s'\n", buffer);
108
109     str="Some description";
110     r = IShellLinkA_SetDescription(sl, str);
111     ok(SUCCEEDED(r), "SetDescription failed (0x%08lx)\n", r);
112
113     strcpy(buffer,"garbage");
114     r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
115     ok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
116     ok(lstrcmp(buffer,str)==0, "GetDescription returned '%s'\n", buffer);
117
118     /* Test Getting / Setting the work directory */
119     strcpy(buffer,"garbage");
120     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
121     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
122     ok(*buffer=='\0', "GetWorkingDirectory returned '%s'\n", buffer);
123
124     str="c:\\nonexistent\\directory";
125     r = IShellLinkA_SetWorkingDirectory(sl, str);
126     ok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08lx)\n", r);
127
128     strcpy(buffer,"garbage");
129     r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
130     ok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
131     ok(lstrcmpi(buffer,str)==0, "GetWorkingDirectory returned '%s'\n", buffer);
132
133     /* Test Getting / Setting the work directory */
134     strcpy(buffer,"garbage");
135     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
136     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
137     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
138
139     r = IShellLinkA_SetPath(sl, "");
140     ok(r==S_OK, "SetPath failed (0x%08lx)\n", r);
141
142     strcpy(buffer,"garbage");
143     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
144     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
145     ok(*buffer=='\0', "GetPath returned '%s'\n", buffer);
146
147     str="c:\\nonexistent\\file";
148     r = IShellLinkA_SetPath(sl, str);
149     ok(r==S_FALSE, "SetPath failed (0x%08lx)\n", r);
150
151     strcpy(buffer,"garbage");
152     r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
153     ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
154     ok(lstrcmpi(buffer,str)==0, "GetPath returned '%s'\n", buffer);
155
156     /* Get some a real path to play with */
157     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
158     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%ld)\n", r);
159
160     /* Test the interaction of SetPath and SetIDList */
161     tmp_pidl=NULL;
162     r = IShellLinkA_GetIDList(sl, &tmp_pidl);
163     ok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
164     if (SUCCEEDED(r))
165     {
166         strcpy(buffer,"garbage");
167         r=SHGetPathFromIDListA(tmp_pidl, buffer);
168         todo_wine {
169         ok(r, "SHGetPathFromIDListA failed\n");
170         }
171         if (r)
172             ok(lstrcmpi(buffer,str)==0, "GetIDList returned '%s'\n", buffer);
173     }
174
175     pidl=path_to_pidl(mypath);
176     todo_wine {
177     ok(pidl!=NULL, "path_to_pidl returned a NULL pidl\n");
178     }
179
180     if (pidl)
181     {
182         r = IShellLinkA_SetIDList(sl, pidl);
183         ok(SUCCEEDED(r), "SetIDList failed (0x%08lx)\n", r);
184
185         tmp_pidl=NULL;
186         r = IShellLinkA_GetIDList(sl, &tmp_pidl);
187         ok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
188         ok(tmp_pidl && ILIsEqual(pidl, tmp_pidl),
189            "GetIDList returned an incorrect pidl\n");
190
191         /* tmp_pidl is owned by IShellLink so we don't free it */
192         ILFree(pidl);
193
194         strcpy(buffer,"garbage");
195         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
196         ok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
197         ok(lstrcmpi(buffer, mypath)==0, "GetPath returned '%s'\n", buffer);
198     }
199
200     /* Test Getting / Setting the arguments */
201     strcpy(buffer,"garbage");
202     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
203     ok(SUCCEEDED(r), "GetArguments failed (0x%08lx)\n", r);
204     ok(*buffer=='\0', "GetArguments returned '%s'\n", buffer);
205
206     str="param1 \"spaced param2\"";
207     r = IShellLinkA_SetArguments(sl, str);
208     ok(SUCCEEDED(r), "SetArguments failed (0x%08lx)\n", r);
209
210     strcpy(buffer,"garbage");
211     r = IShellLinkA_GetArguments(sl, buffer, sizeof(buffer));
212     ok(SUCCEEDED(r), "GetArguments failed (0x%08lx)\n", r);
213     ok(lstrcmp(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
214
215     /* Test Getting / Setting showcmd */
216     i=0xdeadbeef;
217     r = IShellLinkA_GetShowCmd(sl, &i);
218     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
219     ok(i==SW_SHOWNORMAL, "GetShowCmd returned %d\n", i);
220
221     r = IShellLinkA_SetShowCmd(sl, SW_SHOWMAXIMIZED);
222     ok(SUCCEEDED(r), "SetShowCmd failed (0x%08lx)\n", r);
223
224     i=0xdeadbeef;
225     r = IShellLinkA_GetShowCmd(sl, &i);
226     ok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
227     ok(i==SW_SHOWMAXIMIZED, "GetShowCmd returned %d'\n", i);
228
229     /* Test Getting / Setting the icon */
230     i=0xdeadbeef;
231     strcpy(buffer,"garbage");
232     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
233     todo_wine {
234     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
235     }
236     ok(*buffer=='\0', "GetIconLocation returned '%s'\n", buffer);
237     todo_wine {
238     ok(i==0, "GetIconLocation returned %d\n", i);
239     }
240
241     str="c:\\nonexistent\\file";
242     r = IShellLinkA_SetIconLocation(sl, str, 0xbabecafe);
243     ok(SUCCEEDED(r), "SetIconLocation failed (0x%08lx)\n", r);
244
245     i=0xdeadbeef;
246     r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
247     ok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
248     ok(lstrcmpi(buffer,str)==0, "GetArguments returned '%s'\n", buffer);
249     ok(i==0xbabecafe, "GetIconLocation returned %d'\n", i);
250
251     /* Test Getting / Setting the hot key */
252     w=0xbeef;
253     r = IShellLinkA_GetHotkey(sl, &w);
254     ok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
255     ok(w==0, "GetHotkey returned %d\n", w);
256
257     r = IShellLinkA_SetHotkey(sl, 0x5678);
258     ok(SUCCEEDED(r), "SetHotkey failed (0x%08lx)\n", r);
259
260     w=0xbeef;
261     r = IShellLinkA_GetHotkey(sl, &w);
262     ok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
263     ok(w==0x5678, "GetHotkey returned %d'\n", w);
264
265     IShellLinkA_Release(sl);
266 }
267
268
269 /*
270  * Test saving and loading .lnk files
271  */
272
273 typedef struct
274 {
275     char* description;
276     char* workdir;
277     char* path;
278     LPITEMIDLIST pidl;
279     char* arguments;
280     int   showcmd;
281     char* icon;
282     int   icon_id;
283     WORD  hotkey;
284 } lnk_desc_t;
285
286 #define lok                   ok_(__FILE__, line)
287 #define create_lnk(a,b,c)     create_lnk_(__LINE__, (a), (b), (c))
288 #define check_lnk(a,b)        check_lnk_(__LINE__, (a), (b))
289
290 static void create_lnk_(int line, const WCHAR* path, lnk_desc_t* desc, int save_fails)
291 {
292     HRESULT r;
293     IShellLinkA *sl;
294     IPersistFile *pf;
295
296     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
297                          &IID_IShellLinkA, (LPVOID*)&sl);
298     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
299     if (!SUCCEEDED(r))
300         return;
301
302     if (desc->description)
303     {
304         r = IShellLinkA_SetDescription(sl, desc->description);
305         lok(SUCCEEDED(r), "SetDescription failed (0x%08lx)\n", r);
306     }
307     if (desc->workdir)
308     {
309         r = IShellLinkA_SetWorkingDirectory(sl, desc->workdir);
310         lok(SUCCEEDED(r), "SetWorkingDirectory failed (0x%08lx)\n", r);
311     }
312     if (desc->path)
313     {
314         r = IShellLinkA_SetPath(sl, desc->path);
315         lok(SUCCEEDED(r), "SetPath failed (0x%08lx)\n", r);
316     }
317     if (desc->pidl)
318     {
319         r = IShellLinkA_SetIDList(sl, desc->pidl);
320         lok(SUCCEEDED(r), "SetIDList failed (0x%08lx)\n", r);
321     }
322     if (desc->arguments)
323     {
324         r = IShellLinkA_SetArguments(sl, desc->arguments);
325         lok(SUCCEEDED(r), "SetArguments failed (0x%08lx)\n", r);
326     }
327     if (desc->showcmd)
328     {
329         r = IShellLinkA_SetShowCmd(sl, desc->showcmd);
330         lok(SUCCEEDED(r), "SetShowCmd failed (0x%08lx)\n", r);
331     }
332     if (desc->icon)
333     {
334         r = IShellLinkA_SetIconLocation(sl, desc->icon, desc->icon_id);
335         lok(SUCCEEDED(r), "SetIconLocation failed (0x%08lx)\n", r);
336     }
337     if (desc->hotkey)
338     {
339         r = IShellLinkA_SetHotkey(sl, desc->hotkey);
340         lok(SUCCEEDED(r), "SetHotkey failed (0x%08lx)\n", r);
341     }
342
343     r = IShellLinkW_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
344     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08lx)\n", r);
345     if (SUCCEEDED(r))
346     {
347         r = IPersistFile_Save(pf, path, TRUE);
348         if (save_fails)
349         {
350             todo_wine {
351             lok(SUCCEEDED(r), "save failed (0x%08lx)\n", r);
352             }
353         }
354         else
355         {
356             lok(SUCCEEDED(r), "save failed (0x%08lx)\n", r);
357         }
358         IPersistFile_Release(pf);
359     }
360
361     IShellLinkA_Release(sl);
362 }
363
364 static void check_lnk_(int line, const WCHAR* path, lnk_desc_t* desc)
365 {
366     HRESULT r;
367     IShellLinkA *sl;
368     IPersistFile *pf;
369     char buffer[INFOTIPSIZE];
370
371     r = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
372                          &IID_IShellLinkA, (LPVOID*)&sl);
373     lok(SUCCEEDED(r), "no IID_IShellLinkA (0x%08lx)\n", r);
374     if (!SUCCEEDED(r))
375         return;
376
377     r = IShellLinkA_QueryInterface(sl, &IID_IPersistFile, (LPVOID*)&pf);
378     lok(SUCCEEDED(r), "no IID_IPersistFile (0x%08lx)\n", r);
379     if (!SUCCEEDED(r))
380     {
381         IShellLinkA_Release(sl);
382         return;
383     }
384
385     r = IPersistFile_Load(pf, path, STGM_READ);
386     todo_wine {
387     lok(SUCCEEDED(r), "load failed (0x%08lx)\n", r);
388     }
389     IPersistFile_Release(pf);
390     if (!SUCCEEDED(r))
391     {
392         IShellLinkA_Release(sl);
393         return;
394     }
395
396     if (desc->description)
397     {
398         strcpy(buffer,"garbage");
399         r = IShellLinkA_GetDescription(sl, buffer, sizeof(buffer));
400         lok(SUCCEEDED(r), "GetDescription failed (0x%08lx)\n", r);
401         lok(lstrcmp(buffer, desc->description)==0,
402            "GetDescription returned '%s' instead of '%s'\n",
403            buffer, desc->description);
404     }
405     if (desc->workdir)
406     {
407         strcpy(buffer,"garbage");
408         r = IShellLinkA_GetWorkingDirectory(sl, buffer, sizeof(buffer));
409         lok(SUCCEEDED(r), "GetWorkingDirectory failed (0x%08lx)\n", r);
410         lok(lstrcmpi(buffer, desc->workdir)==0,
411            "GetWorkingDirectory returned '%s' instead of '%s'\n",
412            buffer, desc->workdir);
413     }
414     if (desc->path)
415     {
416         strcpy(buffer,"garbage");
417         r = IShellLinkA_GetPath(sl, buffer, sizeof(buffer), NULL, SLGP_RAWPATH);
418         lok(SUCCEEDED(r), "GetPath failed (0x%08lx)\n", r);
419         lok(lstrcmpi(buffer, desc->path)==0,
420            "GetPath returned '%s' instead of '%s'\n",
421            buffer, desc->path);
422     }
423     if (desc->pidl)
424     {
425         LPITEMIDLIST pidl=NULL;
426         r = IShellLinkA_GetIDList(sl, &pidl);
427         lok(SUCCEEDED(r), "GetIDList failed (0x%08lx)\n", r);
428         lok(ILIsEqual(pidl, desc->pidl),
429            "GetIDList returned an incorrect pidl\n");
430     }
431     if (desc->showcmd)
432     {
433         int i=0xdeadbeef;
434         r = IShellLinkA_GetShowCmd(sl, &i);
435         lok(SUCCEEDED(r), "GetShowCmd failed (0x%08lx)\n", r);
436         lok(i==desc->showcmd,
437            "GetShowCmd returned 0x%0x instead of 0x%0x\n",
438            i, desc->showcmd);
439     }
440     if (desc->icon)
441     {
442         int i=0xdeadbeef;
443         strcpy(buffer,"garbage");
444         r = IShellLinkA_GetIconLocation(sl, buffer, sizeof(buffer), &i);
445         lok(SUCCEEDED(r), "GetIconLocation failed (0x%08lx)\n", r);
446         lok(lstrcmpi(buffer, desc->icon)==0,
447            "GetIconLocation returned '%s' instead of '%s'\n",
448            buffer, desc->icon);
449         lok(i==desc->icon_id,
450            "GetIconLocation returned 0x%0x instead of 0x%0x\n",
451            i, desc->icon_id);
452     }
453     if (desc->hotkey)
454     {
455         WORD i=0xbeef;
456         r = IShellLinkA_GetHotkey(sl, &i);
457         lok(SUCCEEDED(r), "GetHotkey failed (0x%08lx)\n", r);
458         lok(i==desc->hotkey,
459            "GetHotkey returned 0x%04x instead of 0x%04x\n",
460            i, desc->hotkey);
461     }
462
463     IShellLinkA_Release(sl);
464 }
465
466 static void test_load_save()
467 {
468     lnk_desc_t desc;
469     char mypath[MAX_PATH];
470     char mydir[MAX_PATH];
471     char* p;
472     DWORD r;
473
474     /* Save an empty .lnk file */
475     memset(&desc, 0, sizeof(desc));
476     create_lnk(lnkfile, &desc, 1);
477
478     /* It should come back as a bunch of empty strings */
479     desc.description="";
480     desc.workdir="";
481     desc.path="";
482     desc.arguments="";
483     desc.icon="";
484     check_lnk(lnkfile, &desc);
485
486
487     /* Point a .lnk file to nonexistent files */
488     desc.description="";
489     desc.workdir="c:\\Nonexitent\\work\\directory";
490     desc.path="c:\\nonexistent\\path";
491     desc.pidl=NULL;
492     desc.arguments="";
493     desc.showcmd=0;
494     desc.icon="c:\\nonexistent\\icon\\file";
495     desc.icon_id=1234;
496     desc.hotkey=0;
497     create_lnk(lnkfile, &desc, 0);
498     check_lnk(lnkfile, &desc);
499
500     r=GetModuleFileName(NULL, mypath, sizeof(mypath));
501     ok(r>=0 && r<sizeof(mypath), "GetModuleFileName failed (%ld)\n", r);
502     strcpy(mydir, mypath);
503     p=strrchr(mydir, '\\');
504     if (p)
505         *p='\0';
506
507
508     /* Overwrite the existing lnk file and point it to existing files */
509     desc.description="test 2";
510     desc.workdir=mydir;
511     desc.path=mypath;
512     desc.pidl=NULL;
513     desc.arguments="/option1 /option2 \"Some string\"";
514     desc.showcmd=SW_SHOWNORMAL;
515     desc.icon=mypath;
516     desc.icon_id=0;
517     desc.hotkey=0x1234;
518     create_lnk(lnkfile, &desc, 0);
519     check_lnk(lnkfile, &desc);
520
521     /* FIXME: Also test saving a .lnk pointing to a pidl that cannot be
522      * represented as a path.
523      */
524
525     /* DeleteFileW is not implemented on Win9x */
526     r=DeleteFileA("c:\\test.lnk");
527     ok(r, "failed to delete link (%ld)\n", GetLastError());
528 }
529
530 START_TEST(shelllink)
531 {
532     HRESULT r;
533
534     r = CoInitialize(NULL);
535     ok(SUCCEEDED(r), "CoInitialize failed (0x%08lx)\b", r);
536     if (!SUCCEEDED(r))
537         return;
538
539     test_get_set();
540     test_load_save();
541
542     CoUninitialize();
543 }