Use shell icon cache instead of an own IExtractIcon implementation.
[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
36
37 #include "wine/unicode.h"
38 #include "wine/test.h"
39
40
41 static IMalloc *ppM;
42
43 /* creates a file with the specified name for tests */
44 static void CreateTestFile(const CHAR *name)
45 {
46     HANDLE file;
47     DWORD written;
48
49     file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
50     if (file != INVALID_HANDLE_VALUE)
51     {
52         WriteFile(file, name, strlen(name), &written, NULL);
53         WriteFile(file, "\n", strlen("\n"), &written, NULL);
54         CloseHandle(file);
55     }
56 }
57
58
59 /* initializes the tests */
60 static void CreateFilesFolders(void)
61 {
62     CreateDirectoryA(".\\testdir", NULL);
63     CreateDirectoryA(".\\testdir\\test.txt", NULL);
64     CreateTestFile  (".\\testdir\\test1.txt ");
65     CreateTestFile  (".\\testdir\\test2.txt ");
66     CreateTestFile  (".\\testdir\\test3.txt ");
67     CreateDirectoryA(".\\testdir\\testdir2 ", NULL);
68     CreateDirectoryA(".\\testdir\\testdir2\\subdir", NULL);
69 }
70
71 /* cleans after tests */
72 static void Cleanup(void)
73 {
74     DeleteFileA(".\\testdir\\test1.txt");
75     DeleteFileA(".\\testdir\\test2.txt");
76     DeleteFileA(".\\testdir\\test3.txt");
77     RemoveDirectoryA(".\\testdir\\test.txt");
78     RemoveDirectoryA(".\\testdir\\testdir2\\subdir");
79     RemoveDirectoryA(".\\testdir\\testdir2");
80     RemoveDirectoryA(".\\testdir");
81 }
82
83
84 /* perform test */
85 static void test_EnumObjects(IShellFolder *iFolder)
86 {
87     IEnumIDList *iEnumList;
88     ITEMIDLIST *newPIDL, *(idlArr [5]);
89     ULONG NumPIDLs;
90     int i=0, j;
91     HRESULT hr;
92
93     static const WORD iResults [5][5] =
94     {
95         { 0,-1,-1,-1,-1},
96         { 1, 0,-1,-1,-1},
97         { 1, 1, 0,-1,-1},
98         { 1, 1, 1, 0,-1},
99         { 1, 1, 1, 1, 0}
100     };
101
102     /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
103     static const ULONG attrs[5] =
104     {
105         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
106         SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
107         SFGAO_FILESYSTEM,
108         SFGAO_FILESYSTEM,
109         SFGAO_FILESYSTEM,
110     };
111
112     hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
113     ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
114
115     while (IEnumIDList_Next(iEnumList, 1, &newPIDL, &NumPIDLs) == S_OK)
116     {
117         idlArr[i++] = newPIDL;
118     }
119
120     hr = IEnumIDList_Release(iEnumList);
121     ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
122     
123     /* Sort them first in case of wrong order from system */
124     for (i=0;i<5;i++) for (j=0;j<5;j++)
125         if ((SHORT)IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]) < 0)
126         {
127             newPIDL = idlArr[i];
128             idlArr[i] = idlArr[j];
129             idlArr[j] = newPIDL;
130         }
131             
132     for (i=0;i<5;i++) for (j=0;j<5;j++)
133     {
134         hr = IShellFolder_CompareIDs(iFolder, 0, idlArr[i], idlArr[j]);
135         ok(hr == iResults[i][j], "Got %lx expected [%d]-[%d]=%x\n", hr, i, j, iResults[i][j]);
136     }
137
138
139     for (i = 0; i < 5; i++)
140     {
141         SFGAOF flags;
142         flags = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
143         hr = IShellFolder_GetAttributesOf(iFolder, 1, (LPCITEMIDLIST*)(idlArr + i), &flags);
144         flags &= SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR;
145         ok(hr == S_OK, "GetAttributesOf returns %08lx\n", hr);
146         ok(flags == attrs[i], "GetAttributesOf gets attrs %08lx, expects %08lx\n", flags, attrs[i]);
147     }
148
149     for (i=0;i<5;i++)
150         IMalloc_Free(ppM, idlArr[i]);
151 }
152
153 static void test_BindToObject(void)
154 {
155     HRESULT hr;
156     UINT cChars;
157     IShellFolder *psfDesktop, *psfChild, *psfMyComputer, *psfSystemDir;
158     SHITEMID emptyitem = { 0, { 0 } };
159     LPITEMIDLIST pidlMyComputer, pidlSystemDir, pidlEmpty = (LPITEMIDLIST)&emptyitem;
160     WCHAR wszSystemDir[MAX_PATH];
161     WCHAR wszMyComputer[] = { 
162         ':',':','{','2','0','D','0','4','F','E','0','-','3','A','E','A','-','1','0','6','9','-',
163         'A','2','D','8','-','0','8','0','0','2','B','3','0','3','0','9','D','}',0 };
164
165     /* The following tests shows that BindToObject should fail with E_INVALIDARG if called
166      * with an empty pidl. This is tested for Desktop, MyComputer and the FS ShellFolder
167      */
168     hr = SHGetDesktopFolder(&psfDesktop);
169     ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
170     if (FAILED(hr)) return;
171     
172     hr = IShellFolder_BindToObject(psfDesktop, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
173     ok (hr == E_INVALIDARG, "Desktop's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
174
175     hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, wszMyComputer, NULL, &pidlMyComputer, NULL);
176     ok (SUCCEEDED(hr), "Desktop's ParseDisplayName failed to parse MyComputer's CLSID! hr = %08lx\n", hr);
177     if (FAILED(hr)) {
178         IShellFolder_Release(psfDesktop);
179         return;
180     }
181     
182     hr = IShellFolder_BindToObject(psfDesktop, pidlMyComputer, NULL, &IID_IShellFolder, (LPVOID*)&psfMyComputer);
183     ok (SUCCEEDED(hr), "Desktop failed to bind to MyComputer object! hr = %08lx\n", hr);
184     IShellFolder_Release(psfDesktop);
185     ILFree(pidlMyComputer);
186     if (FAILED(hr)) return;
187
188     hr = IShellFolder_BindToObject(psfMyComputer, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
189     ok (hr == E_INVALIDARG, "MyComputers's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
190
191     cChars = GetSystemDirectoryW(wszSystemDir, MAX_PATH);
192     ok (cChars > 0 && cChars < MAX_PATH, "GetSystemDirectoryW failed! LastError: %08lx\n", GetLastError());
193     if (cChars == 0 || cChars >= MAX_PATH) {
194         IShellFolder_Release(psfMyComputer);
195         return;
196     }
197     
198     hr = IShellFolder_ParseDisplayName(psfMyComputer, NULL, NULL, wszSystemDir, NULL, &pidlSystemDir, NULL);
199     ok (SUCCEEDED(hr), "MyComputers's ParseDisplayName failed to parse the SystemDirectory! hr = %08lx\n", hr);
200     if (FAILED(hr)) {
201         IShellFolder_Release(psfMyComputer);
202         return;
203     }
204
205     hr = IShellFolder_BindToObject(psfMyComputer, pidlSystemDir, NULL, &IID_IShellFolder, (LPVOID*)&psfSystemDir);
206     ok (SUCCEEDED(hr), "MyComputer failed to bind to a FileSystem ShellFolder! hr = %08lx\n", hr);
207     IShellFolder_Release(psfMyComputer);
208     ILFree(pidlSystemDir);
209     if (FAILED(hr)) return;
210
211     hr = IShellFolder_BindToObject(psfSystemDir, pidlEmpty, NULL, &IID_IShellFolder, (LPVOID*)&psfChild);
212     ok (hr == E_INVALIDARG, 
213         "FileSystem ShellFolder's BindToObject should fail, when called with empty pidl! hr = %08lx\n", hr);
214     
215     IShellFolder_Release(psfSystemDir);
216 }
217     
218 START_TEST(shlfolder)
219 {
220     ITEMIDLIST *newPIDL;
221     IShellFolder *IDesktopFolder, *testIShellFolder;
222     WCHAR cCurrDirW [MAX_PATH];
223     static const WCHAR cTestDirW[] = {'\\','t','e','s','t','d','i','r',0};
224     HRESULT hr;
225     
226     GetCurrentDirectoryW(MAX_PATH, cCurrDirW);
227     strcatW(cCurrDirW, cTestDirW);
228
229     OleInitialize(NULL);
230
231     hr = SHGetMalloc(&ppM);
232     ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
233
234     CreateFilesFolders();
235     
236     hr = SHGetDesktopFolder(&IDesktopFolder);
237     ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
238
239     hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
240     ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
241
242     hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
243     ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
244         
245     test_EnumObjects(testIShellFolder);
246     test_BindToObject();
247
248     hr = IShellFolder_Release(testIShellFolder);
249     ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
250
251     IMalloc_Free(ppM, newPIDL);
252
253     Cleanup();
254 }