Assorted spelling fixes.
[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 #define EXPECT_HR(hr,hr_exp) \
31     ok(hr == hr_exp, "got 0x%08x, expected 0x%08x\n", hr, hr_exp)
32
33 static HRESULT (WINAPI *pSHGetFolderPathW)(HWND, int, HANDLE, DWORD, LPWSTR);
34 static HRESULT (WINAPI *pSHGetNameFromIDList)(PCIDLIST_ABSOLUTE,SIGDN,PWSTR*);
35 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
36 static DWORD (WINAPI *pGetLongPathNameW)(LPCWSTR, LPWSTR, DWORD);
37
38 static void init_function_pointers(void)
39 {
40     HMODULE hshell32, hkernel32;
41
42     hshell32 = GetModuleHandleA("shell32.dll");
43     hkernel32 = GetModuleHandleA("kernel32.dll");
44     pSHGetFolderPathW = (void*)GetProcAddress(hshell32, "SHGetFolderPathW");
45     pSHGetNameFromIDList = (void*)GetProcAddress(hshell32, "SHGetNameFromIDList");
46     pSHGetSpecialFolderLocation = (void*)GetProcAddress(hshell32,
47      "SHGetSpecialFolderLocation");
48     pGetLongPathNameW = (void*)GetProcAddress(hkernel32, "GetLongPathNameW");
49 }
50
51 static void test_namespace(void)
52 {
53     static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0};
54     static const WCHAR backslashW[] = {'\\',0};
55     static const WCHAR clsidW[] = {
56         ':',':','{','6','4','5','F','F','0','4','0','-','5','0','8','1','-',
57                     '1','0','1','B','-','9','F','0','8','-',
58                     '0','0','A','A','0','0','2','F','9','5','4','E','}',0};
59
60     static WCHAR tempW[MAX_PATH], curW[MAX_PATH];
61     WCHAR *long_pathW = NULL;
62     HRESULT r;
63     IShellDispatch *sd;
64     Folder *folder;
65     Folder2 *folder2;
66     FolderItem *item;
67     VARIANT var;
68     BSTR title, item_path;
69     int len;
70
71     r = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
72      &IID_IShellDispatch, (LPVOID*)&sd);
73     if (r == REGDB_E_CLASSNOTREG) /* NT4 */
74     {
75         win_skip("skipping IShellDispatch tests\n");
76         return;
77     }
78     ok(SUCCEEDED(r), "CoCreateInstance failed: %08x\n", r);
79     if (FAILED(r))
80         return;
81
82     VariantInit(&var);
83     folder = (void*)0xdeadbeef;
84     r = IShellDispatch_NameSpace(sd, var, &folder);
85     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
86     ok(folder == NULL, "expected NULL, got %p\n", folder);
87
88     V_VT(&var) = VT_I4;
89     V_I4(&var) = -1;
90     folder = (void*)0xdeadbeef;
91     r = IShellDispatch_NameSpace(sd, var, &folder);
92     todo_wine {
93     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
94     ok(folder == NULL, "got %p\n", folder);
95 }
96     V_VT(&var) = VT_I4;
97     V_I4(&var) = ssfPROGRAMFILES;
98     r = IShellDispatch_NameSpace(sd, var, &folder);
99     ok(r == S_OK ||
100      broken(r == S_FALSE), /* NT4 */
101      "IShellDispatch::NameSpace failed: %08x\n", r);
102     if (r == S_OK)
103     {
104         static WCHAR path[MAX_PATH];
105
106         if (pSHGetFolderPathW)
107         {
108             r = pSHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL,
109              SHGFP_TYPE_CURRENT, path);
110             ok(r == S_OK, "SHGetFolderPath failed: %08x\n", r);
111         }
112         r = Folder_get_Title(folder, &title);
113         todo_wine
114         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
115         if (r == S_OK)
116         {
117             /* On Win2000-2003 title is equal to program files directory name in
118                HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir.
119                On newer Windows it seems constant and is not changed
120                if the program files directory name is changed */
121             if (pSHGetSpecialFolderLocation && pSHGetNameFromIDList)
122             {
123                 LPITEMIDLIST pidl;
124                 PWSTR name;
125
126                 r = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl);
127                 ok(r == S_OK, "SHGetSpecialFolderLocation failed: %08x\n", r);
128                 r = pSHGetNameFromIDList(pidl, SIGDN_NORMALDISPLAY, &name);
129                 ok(r == S_OK, "SHGetNameFromIDList failed: %08x\n", r);
130                 todo_wine
131                 ok(!lstrcmpW(title, name), "expected %s, got %s\n",
132                  wine_dbgstr_w(name), wine_dbgstr_w(title));
133                 CoTaskMemFree(name);
134                 CoTaskMemFree(pidl);
135             }
136             else if (pSHGetFolderPathW)
137             {
138                 WCHAR *p;
139
140                 p = path + lstrlenW(path);
141                 while (path < p && *(p - 1) != '\\')
142                     p--;
143                 ok(!lstrcmpiW(title, p), "expected %s, got %s\n",
144                  wine_dbgstr_w(p), wine_dbgstr_w(title));
145             }
146             else skip("skipping Folder::get_Title test\n");
147             SysFreeString(title);
148         }
149         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
150         ok(r == S_OK, "Folder::QueryInterface failed: %08x\n", r);
151         if (r == S_OK)
152         {
153             r = Folder2_get_Self(folder2, &item);
154             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
155             if (r == S_OK)
156             {
157                 r = FolderItem_get_Path(item, &item_path);
158                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
159                 if (pSHGetFolderPathW)
160                     ok(!lstrcmpiW(item_path, path), "expected %s, got %s\n",
161                      wine_dbgstr_w(path), wine_dbgstr_w(item_path));
162                 SysFreeString(item_path);
163                 FolderItem_Release(item);
164             }
165             Folder2_Release(folder2);
166         }
167         Folder_Release(folder);
168     }
169
170     V_VT(&var) = VT_I4;
171     V_I4(&var) = ssfBITBUCKET;
172     r = IShellDispatch_NameSpace(sd, var, &folder);
173     ok(r == S_OK ||
174      broken(r == S_FALSE), /* NT4 */
175      "IShellDispatch::NameSpace failed: %08x\n", r);
176     if (r == S_OK)
177     {
178         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
179         ok(r == S_OK ||
180          broken(r == E_NOINTERFACE), /* NT4 */
181          "Folder::QueryInterface failed: %08x\n", r);
182         if (r == S_OK)
183         {
184             r = Folder2_get_Self(folder2, &item);
185             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
186             if (r == S_OK)
187             {
188                 r = FolderItem_get_Path(item, &item_path);
189                 todo_wine
190                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
191                 todo_wine
192                 ok(!lstrcmpW(item_path, clsidW), "expected %s, got %s\n",
193                  wine_dbgstr_w(clsidW), wine_dbgstr_w(item_path));
194                 SysFreeString(item_path);
195                 FolderItem_Release(item);
196             }
197             Folder2_Release(folder2);
198         }
199         Folder_Release(folder);
200     }
201
202     GetTempPathW(MAX_PATH, tempW);
203     GetCurrentDirectoryW(MAX_PATH, curW);
204     SetCurrentDirectoryW(tempW);
205     CreateDirectoryW(winetestW, NULL);
206     V_VT(&var) = VT_BSTR;
207     V_BSTR(&var) = SysAllocString(winetestW);
208     r = IShellDispatch_NameSpace(sd, var, &folder);
209     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
210     SysFreeString(V_BSTR(&var));
211
212     GetFullPathNameW(winetestW, MAX_PATH, tempW, NULL);
213     if (pGetLongPathNameW)
214     {
215         len = pGetLongPathNameW(tempW, NULL, 0);
216         long_pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
217         if (long_pathW)
218             pGetLongPathNameW(tempW, long_pathW, len);
219     }
220     V_VT(&var) = VT_BSTR;
221     V_BSTR(&var) = SysAllocString(tempW);
222     r = IShellDispatch_NameSpace(sd, var, &folder);
223     ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
224     if (r == S_OK)
225     {
226         r = Folder_get_Title(folder, &title);
227         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
228         if (r == S_OK)
229         {
230             ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
231              wine_dbgstr_w(title));
232             SysFreeString(title);
233         }
234         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
235         ok(r == S_OK ||
236          broken(r == E_NOINTERFACE), /* NT4 */
237          "Folder::QueryInterface failed: %08x\n", r);
238         if (r == S_OK)
239         {
240             r = Folder2_get_Self(folder2, &item);
241             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
242             if (r == S_OK)
243             {
244                 r = FolderItem_get_Path(item, &item_path);
245                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
246                 if (long_pathW)
247                     ok(!lstrcmpW(item_path, long_pathW),
248                      "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
249                      wine_dbgstr_w(item_path));
250                 SysFreeString(item_path);
251                 FolderItem_Release(item);
252             }
253             Folder2_Release(folder2);
254         }
255         Folder_Release(folder);
256     }
257     SysFreeString(V_BSTR(&var));
258
259     len = lstrlenW(tempW);
260     if (len < MAX_PATH - 1)
261     {
262         lstrcatW(tempW, backslashW);
263         V_VT(&var) = VT_BSTR;
264         V_BSTR(&var) = SysAllocString(tempW);
265         r = IShellDispatch_NameSpace(sd, var, &folder);
266         ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
267         if (r == S_OK)
268         {
269             r = Folder_get_Title(folder, &title);
270             ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
271             if (r == S_OK)
272             {
273                 ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
274                  wine_dbgstr_w(title));
275                 SysFreeString(title);
276             }
277             r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
278             ok(r == S_OK ||
279              broken(r == E_NOINTERFACE), /* NT4 */
280              "Folder::QueryInterface failed: %08x\n", r);
281             if (r == S_OK)
282             {
283                 r = Folder2_get_Self(folder2, &item);
284                 ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
285                 if (r == S_OK)
286                 {
287                     r = FolderItem_get_Path(item, &item_path);
288                     ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
289                     if (long_pathW)
290                         ok(!lstrcmpW(item_path, long_pathW),
291                          "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
292                          wine_dbgstr_w(item_path));
293                     SysFreeString(item_path);
294                     FolderItem_Release(item);
295                 }
296                 Folder2_Release(folder2);
297             }
298             Folder_Release(folder);
299         }
300         SysFreeString(V_BSTR(&var));
301     }
302
303     HeapFree(GetProcessHeap(), 0, long_pathW);
304     RemoveDirectoryW(winetestW);
305     SetCurrentDirectoryW(curW);
306     IShellDispatch_Release(sd);
307 }
308
309 static void test_service(void)
310 {
311     static const WCHAR spooler[] = {'S','p','o','o','l','e','r',0};
312     static const WCHAR dummyW[] = {'d','u','m','m','y',0};
313     SERVICE_STATUS_PROCESS status;
314     SC_HANDLE scm, service;
315     IShellDispatch2 *sd;
316     DWORD dummy;
317     HRESULT hr;
318     BSTR name;
319     VARIANT v;
320
321     hr = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
322         &IID_IShellDispatch2, (void**)&sd);
323     if (hr != S_OK)
324     {
325         win_skip("IShellDispatch2 not supported\n");
326         return;
327     }
328
329     V_VT(&v) = VT_I2;
330     V_I2(&v) = 10;
331     hr = IShellDispatch2_IsServiceRunning(sd, NULL, &v);
332     ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
333     ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
334     EXPECT_HR(hr, S_OK);
335
336     scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
337     service = OpenServiceW(scm, spooler, SERVICE_QUERY_STATUS);
338     QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, (BYTE *)&status, sizeof(SERVICE_STATUS_PROCESS), &dummy);
339     CloseServiceHandle(service);
340     CloseServiceHandle(scm);
341
342     /* service should exist */
343     name = SysAllocString(spooler);
344     V_VT(&v) = VT_I2;
345     hr = IShellDispatch2_IsServiceRunning(sd, name, &v);
346     EXPECT_HR(hr, S_OK);
347     ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
348     if (status.dwCurrentState == SERVICE_RUNNING)
349         ok(V_BOOL(&v) == VARIANT_TRUE, "got %d\n", V_BOOL(&v));
350     else
351         ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
352     SysFreeString(name);
353
354     /* service doesn't exist */
355     name = SysAllocString(dummyW);
356     V_VT(&v) = VT_I2;
357     hr = IShellDispatch2_IsServiceRunning(sd, name, &v);
358     EXPECT_HR(hr, S_OK);
359     ok(V_VT(&v) == VT_BOOL, "got %d\n", V_VT(&v));
360     ok(V_BOOL(&v) == VARIANT_FALSE, "got %d\n", V_BOOL(&v));
361     SysFreeString(name);
362
363     IShellDispatch2_Release(sd);
364 }
365
366 START_TEST(shelldispatch)
367 {
368     HRESULT r;
369
370     r = CoInitialize(NULL);
371     ok(SUCCEEDED(r), "CoInitialize failed: %08x\n", r);
372     if (FAILED(r))
373         return;
374
375     init_function_pointers();
376     test_namespace();
377     test_service();
378
379     CoUninitialize();
380 }