Warning fixes for -Wmissing-declarations and -Wwrite-strings.
[wine.git] / dlls / shell32 / tests / shlfolder.c
blob96d0bc66aec54d005afe3565b1c48363dad80180
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wtypes.h"
29 #include "shellapi.h"
32 #include "shlguid.h"
33 #include "shlobj.h"
34 #include "shobjidl.h"
37 #include "wine/unicode.h"
38 #include "wine/test.h"
41 static IMalloc *ppM;
43 /* creates a file with the specified name for tests */
44 static void CreateTestFile(const CHAR *name)
46 HANDLE file;
47 DWORD written;
49 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
50 if (file != INVALID_HANDLE_VALUE)
52 WriteFile(file, name, strlen(name), &written, NULL);
53 WriteFile(file, "\n", strlen("\n"), &written, NULL);
54 CloseHandle(file);
59 /* initializes the tests */
60 static void CreateFilesFolders(void)
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);
71 /* cleans after tests */
72 static void Cleanup(void)
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");
84 /* perform test */
85 static void test_EnumObjects(IShellFolder *iFolder)
87 IEnumIDList *iEnumList;
88 ITEMIDLIST *newPIDL, *(idlArr [5]);
89 ULONG NumPIDLs;
90 int i=0, j;
91 HRESULT hr;
93 static const WORD iResults [5][5] =
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}
102 /* Just test SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR for now */
103 static const ULONG attrs[5] =
105 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
106 SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_FILESYSANCESTOR,
107 SFGAO_FILESYSTEM,
108 SFGAO_FILESYSTEM,
109 SFGAO_FILESYSTEM,
112 hr = IShellFolder_EnumObjects(iFolder, NULL, SHCONTF_FOLDERS | SHCONTF_NONFOLDERS | SHCONTF_INCLUDEHIDDEN, &iEnumList);
113 ok(hr == S_OK, "EnumObjects failed %08lx\n", hr);
115 while (IEnumIDList_Next(iEnumList, 1, &newPIDL, &NumPIDLs) == S_OK)
117 idlArr[i++] = newPIDL;
120 hr = IEnumIDList_Release(iEnumList);
121 ok(hr == S_OK, "IEnumIDList_Release failed %08lx\n", hr);
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)
127 newPIDL = idlArr[i];
128 idlArr[i] = idlArr[j];
129 idlArr[j] = newPIDL;
132 for (i=0;i<5;i++) for (j=0;j<5;j++)
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]);
139 for (i = 0; i < 5; i++)
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]);
149 for (i=0;i<5;i++)
150 IMalloc_Free(ppM, idlArr[i]);
153 static void test_BindToObject(void)
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 };
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
168 hr = SHGetDesktopFolder(&psfDesktop);
169 ok (SUCCEEDED(hr), "SHGetDesktopFolder failed! hr = %08lx\n", hr);
170 if (FAILED(hr)) return;
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);
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;
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;
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);
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;
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;
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;
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);
215 IShellFolder_Release(psfSystemDir);
218 START_TEST(shlfolder)
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;
226 GetCurrentDirectoryW(MAX_PATH, cCurrDirW);
227 strcatW(cCurrDirW, cTestDirW);
229 OleInitialize(NULL);
231 hr = SHGetMalloc(&ppM);
232 ok(hr == S_OK, "SHGetMalloc failed %08lx\n", hr);
234 CreateFilesFolders();
236 hr = SHGetDesktopFolder(&IDesktopFolder);
237 ok(hr == S_OK, "SHGetDesktopfolder failed %08lx\n", hr);
239 hr = IShellFolder_ParseDisplayName(IDesktopFolder, NULL, NULL, cCurrDirW, NULL, &newPIDL, 0);
240 ok(hr == S_OK, "ParseDisplayName failed %08lx\n", hr);
242 hr = IShellFolder_BindToObject(IDesktopFolder, newPIDL, NULL, (REFIID)&IID_IShellFolder, (LPVOID *)&testIShellFolder);
243 ok(hr == S_OK, "BindToObject failed %08lx\n", hr);
245 test_EnumObjects(testIShellFolder);
246 test_BindToObject();
248 hr = IShellFolder_Release(testIShellFolder);
249 ok(hr == S_OK, "IShellFolder_Release failed %08lx\n", hr);
251 IMalloc_Free(ppM, newPIDL);
253 Cleanup();