credui: Delete the static critical section when unloading the dll.
[wine] / dlls / shell32 / tests / shelldispatch.c
1 /*
2  * Unit tests for IShellDispatch
3  *
4  * Copyright 2010 Alexander Morozov for Etersoft
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
24
25 #include "shldisp.h"
26 #include "shlobj.h"
27 #include "shlwapi.h"
28 #include "wine/test.h"
29
30 static HRESULT (WINAPI *pSHGetFolderPathW)(HWND, int, HANDLE, DWORD, LPWSTR);
31 static HRESULT (WINAPI *pSHGetNameFromIDList)(PCIDLIST_ABSOLUTE,SIGDN,PWSTR*);
32 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
33 static DWORD (WINAPI *pGetLongPathNameW)(LPCWSTR, LPWSTR, DWORD);
34
35 static void init_function_pointers(void)
36 {
37     HMODULE hshell32, hkernel32;
38
39     hshell32 = GetModuleHandleA("shell32.dll");
40     hkernel32 = GetModuleHandleA("kernel32.dll");
41     pSHGetFolderPathW = (void*)GetProcAddress(hshell32, "SHGetFolderPathW");
42     pSHGetNameFromIDList = (void*)GetProcAddress(hshell32, "SHGetNameFromIDList");
43     pSHGetSpecialFolderLocation = (void*)GetProcAddress(hshell32,
44      "SHGetSpecialFolderLocation");
45     pGetLongPathNameW = (void*)GetProcAddress(hkernel32, "GetLongPathNameW");
46 }
47
48 static void test_namespace(void)
49 {
50     static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0};
51     static const WCHAR backslashW[] = {'\\',0};
52     static const WCHAR clsidW[] = {
53         ':',':','{','6','4','5','F','F','0','4','0','-','5','0','8','1','-',
54                     '1','0','1','B','-','9','F','0','8','-',
55                     '0','0','A','A','0','0','2','F','9','5','4','E','}',0};
56
57     static WCHAR tempW[MAX_PATH], curW[MAX_PATH];
58     WCHAR *long_pathW = NULL;
59     HRESULT r;
60     IShellDispatch *sd;
61     Folder *folder;
62     Folder2 *folder2;
63     FolderItem *item;
64     VARIANT var;
65     BSTR title, item_path;
66     int len;
67
68     r = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
69      &IID_IShellDispatch, (LPVOID*)&sd);
70     if (r == REGDB_E_CLASSNOTREG) /* NT4 */
71     {
72         win_skip("skipping IShellDispatch tests\n");
73         return;
74     }
75     ok(SUCCEEDED(r), "CoCreateInstance failed: %08x\n", r);
76     if (FAILED(r))
77         return;
78
79     VariantInit(&var);
80     folder = (void*)0xdeadbeef;
81     r = IShellDispatch_NameSpace(sd, var, &folder);
82     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
83     ok(folder == NULL, "expected NULL, got %p\n", folder);
84
85     V_VT(&var) = VT_I4;
86     V_I4(&var) = -1;
87     folder = (void*)0xdeadbeef;
88     r = IShellDispatch_NameSpace(sd, var, &folder);
89     todo_wine {
90     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
91     ok(folder == NULL, "got %p\n", folder);
92 }
93     V_VT(&var) = VT_I4;
94     V_I4(&var) = ssfPROGRAMFILES;
95     r = IShellDispatch_NameSpace(sd, var, &folder);
96     ok(r == S_OK ||
97      broken(r == S_FALSE), /* NT4 */
98      "IShellDispatch::NameSpace failed: %08x\n", r);
99     if (r == S_OK)
100     {
101         static WCHAR path[MAX_PATH];
102
103         if (pSHGetFolderPathW)
104         {
105             r = pSHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL,
106              SHGFP_TYPE_CURRENT, path);
107             ok(r == S_OK, "SHGetFolderPath failed: %08x\n", r);
108         }
109         r = Folder_get_Title(folder, &title);
110         todo_wine
111         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
112         if (r == S_OK)
113         {
114             /* On Win2000-2003 title is equal to program files directory name in
115                HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir.
116                On newer Windows it seems constant and is not changed
117                if the program files directory name is changed */
118             if (pSHGetSpecialFolderLocation && pSHGetNameFromIDList)
119             {
120                 LPITEMIDLIST pidl;
121                 PWSTR name;
122
123                 r = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl);
124                 ok(r == S_OK, "SHGetSpecialFolderLocation failed: %08x\n", r);
125                 r = pSHGetNameFromIDList(pidl, SIGDN_NORMALDISPLAY, &name);
126                 ok(r == S_OK, "SHGetNameFromIDList failed: %08x\n", r);
127                 todo_wine
128                 ok(!lstrcmpW(title, name), "expected %s, got %s\n",
129                  wine_dbgstr_w(name), wine_dbgstr_w(title));
130                 CoTaskMemFree(name);
131                 CoTaskMemFree(pidl);
132             }
133             else if (pSHGetFolderPathW)
134             {
135                 WCHAR *p;
136
137                 p = path + lstrlenW(path);
138                 while (path < p && *(p - 1) != '\\')
139                     p--;
140                 ok(!lstrcmpW(title, p), "expected %s, got %s\n",
141                  wine_dbgstr_w(p), wine_dbgstr_w(title));
142             }
143             else skip("skipping Folder::get_Title test\n");
144             SysFreeString(title);
145         }
146         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
147         ok(r == S_OK, "Folder::QueryInterface failed: %08x\n", r);
148         if (r == S_OK)
149         {
150             r = Folder2_get_Self(folder2, &item);
151             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
152             if (r == S_OK)
153             {
154                 r = FolderItem_get_Path(item, &item_path);
155                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
156                 if (pSHGetFolderPathW)
157                     ok(!lstrcmpW(item_path, path), "expected %s, got %s\n",
158                      wine_dbgstr_w(path), wine_dbgstr_w(item_path));
159                 SysFreeString(item_path);
160                 FolderItem_Release(item);
161             }
162             Folder2_Release(folder2);
163         }
164         Folder_Release(folder);
165     }
166
167     V_VT(&var) = VT_I4;
168     V_I4(&var) = ssfBITBUCKET;
169     r = IShellDispatch_NameSpace(sd, var, &folder);
170     ok(r == S_OK ||
171      broken(r == S_FALSE), /* NT4 */
172      "IShellDispatch::NameSpace failed: %08x\n", r);
173     if (r == S_OK)
174     {
175         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
176         ok(r == S_OK ||
177          broken(r == E_NOINTERFACE), /* NT4 */
178          "Folder::QueryInterface failed: %08x\n", r);
179         if (r == S_OK)
180         {
181             r = Folder2_get_Self(folder2, &item);
182             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
183             if (r == S_OK)
184             {
185                 r = FolderItem_get_Path(item, &item_path);
186                 todo_wine
187                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
188                 todo_wine
189                 ok(!lstrcmpW(item_path, clsidW), "expected %s, got %s\n",
190                  wine_dbgstr_w(clsidW), wine_dbgstr_w(item_path));
191                 SysFreeString(item_path);
192                 FolderItem_Release(item);
193             }
194             Folder2_Release(folder2);
195         }
196         Folder_Release(folder);
197     }
198
199     GetTempPathW(MAX_PATH, tempW);
200     GetCurrentDirectoryW(MAX_PATH, curW);
201     SetCurrentDirectoryW(tempW);
202     CreateDirectoryW(winetestW, NULL);
203     V_VT(&var) = VT_BSTR;
204     V_BSTR(&var) = SysAllocString(winetestW);
205     r = IShellDispatch_NameSpace(sd, var, &folder);
206     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
207     SysFreeString(V_BSTR(&var));
208
209     GetFullPathNameW(winetestW, MAX_PATH, tempW, NULL);
210     if (pGetLongPathNameW)
211     {
212         len = pGetLongPathNameW(tempW, NULL, 0);
213         long_pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
214         if (long_pathW)
215             pGetLongPathNameW(tempW, long_pathW, len);
216     }
217     V_VT(&var) = VT_BSTR;
218     V_BSTR(&var) = SysAllocString(tempW);
219     r = IShellDispatch_NameSpace(sd, var, &folder);
220     ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
221     if (r == S_OK)
222     {
223         r = Folder_get_Title(folder, &title);
224         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
225         if (r == S_OK)
226         {
227             ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
228              wine_dbgstr_w(title));
229             SysFreeString(title);
230         }
231         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
232         ok(r == S_OK ||
233          broken(r == E_NOINTERFACE), /* NT4 */
234          "Folder::QueryInterface failed: %08x\n", r);
235         if (r == S_OK)
236         {
237             r = Folder2_get_Self(folder2, &item);
238             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
239             if (r == S_OK)
240             {
241                 r = FolderItem_get_Path(item, &item_path);
242                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
243                 if (long_pathW)
244                     ok(!lstrcmpW(item_path, long_pathW),
245                      "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
246                      wine_dbgstr_w(item_path));
247                 SysFreeString(item_path);
248                 FolderItem_Release(item);
249             }
250             Folder2_Release(folder2);
251         }
252         Folder_Release(folder);
253     }
254     SysFreeString(V_BSTR(&var));
255
256     len = lstrlenW(tempW);
257     if (len < MAX_PATH - 1)
258     {
259         lstrcatW(tempW, backslashW);
260         V_VT(&var) = VT_BSTR;
261         V_BSTR(&var) = SysAllocString(tempW);
262         r = IShellDispatch_NameSpace(sd, var, &folder);
263         ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
264         if (r == S_OK)
265         {
266             r = Folder_get_Title(folder, &title);
267             ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
268             if (r == S_OK)
269             {
270                 ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
271                  wine_dbgstr_w(title));
272                 SysFreeString(title);
273             }
274             r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
275             ok(r == S_OK ||
276              broken(r == E_NOINTERFACE), /* NT4 */
277              "Folder::QueryInterface failed: %08x\n", r);
278             if (r == S_OK)
279             {
280                 r = Folder2_get_Self(folder2, &item);
281                 ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
282                 if (r == S_OK)
283                 {
284                     r = FolderItem_get_Path(item, &item_path);
285                     ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
286                     if (long_pathW)
287                         ok(!lstrcmpW(item_path, long_pathW),
288                          "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
289                          wine_dbgstr_w(item_path));
290                     SysFreeString(item_path);
291                     FolderItem_Release(item);
292                 }
293                 Folder2_Release(folder2);
294             }
295             Folder_Release(folder);
296         }
297         SysFreeString(V_BSTR(&var));
298     }
299
300     HeapFree(GetProcessHeap(), 0, long_pathW);
301     RemoveDirectoryW(winetestW);
302     SetCurrentDirectoryW(curW);
303     IShellDispatch_Release(sd);
304 }
305
306 START_TEST(shelldispatch)
307 {
308     HRESULT r;
309
310     r = CoInitialize(NULL);
311     ok(SUCCEEDED(r), "CoInitialize failed: %08x\n", r);
312     if (FAILED(r))
313         return;
314
315     init_function_pointers();
316     test_namespace();
317
318     CoUninitialize();
319 }