mshtml: Added IHTMLWindow2::focus implementation.
[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     r = IShellDispatch_NameSpace(sd, var, &folder);
88     todo_wine
89     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
90
91     V_VT(&var) = VT_I4;
92     V_I4(&var) = ssfPROGRAMFILES;
93     r = IShellDispatch_NameSpace(sd, var, &folder);
94     ok(r == S_OK ||
95      broken(r == S_FALSE), /* NT4 */
96      "IShellDispatch::NameSpace failed: %08x\n", r);
97     if (r == S_OK)
98     {
99         static WCHAR path[MAX_PATH];
100
101         if (pSHGetFolderPathW)
102         {
103             r = pSHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL,
104              SHGFP_TYPE_CURRENT, path);
105             ok(r == S_OK, "SHGetFolderPath failed: %08x\n", r);
106         }
107         r = Folder_get_Title(folder, &title);
108         todo_wine
109         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
110         if (r == S_OK)
111         {
112             /* On Win2000-2003 title is equal to program files directory name in
113                HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir.
114                On newer Windows it seems constant and is not changed
115                if the program files directory name is changed */
116             if (pSHGetSpecialFolderLocation && pSHGetNameFromIDList)
117             {
118                 LPITEMIDLIST pidl;
119                 PWSTR name;
120
121                 r = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl);
122                 ok(r == S_OK, "SHGetSpecialFolderLocation failed: %08x\n", r);
123                 r = pSHGetNameFromIDList(pidl, SIGDN_NORMALDISPLAY, &name);
124                 ok(r == S_OK, "SHGetNameFromIDList failed: %08x\n", r);
125                 todo_wine
126                 ok(!lstrcmpW(title, name), "expected %s, got %s\n",
127                  wine_dbgstr_w(name), wine_dbgstr_w(title));
128                 CoTaskMemFree(name);
129                 CoTaskMemFree(pidl);
130             }
131             else if (pSHGetFolderPathW)
132             {
133                 WCHAR *p;
134
135                 p = path + lstrlenW(path);
136                 while (path < p && *(p - 1) != '\\')
137                     p--;
138                 ok(!lstrcmpW(title, p), "expected %s, got %s\n",
139                  wine_dbgstr_w(p), wine_dbgstr_w(title));
140             }
141             else skip("skipping Folder::get_Title test\n");
142             SysFreeString(title);
143         }
144         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
145         ok(r == S_OK, "Folder::QueryInterface failed: %08x\n", r);
146         if (r == S_OK)
147         {
148             r = Folder2_get_Self(folder2, &item);
149             todo_wine
150             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
151             if (r == S_OK)
152             {
153                 r = FolderItem_get_Path(item, &item_path);
154                 todo_wine
155                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
156                 if (pSHGetFolderPathW)
157                     todo_wine
158                     ok(!lstrcmpW(item_path, path), "expected %s, got %s\n",
159                      wine_dbgstr_w(path), wine_dbgstr_w(item_path));
160                 SysFreeString(item_path);
161                 FolderItem_Release(item);
162             }
163             Folder2_Release(folder2);
164         }
165         Folder_Release(folder);
166     }
167
168     V_VT(&var) = VT_I4;
169     V_I4(&var) = ssfBITBUCKET;
170     r = IShellDispatch_NameSpace(sd, var, &folder);
171     ok(r == S_OK ||
172      broken(r == S_FALSE), /* NT4 */
173      "IShellDispatch::NameSpace failed: %08x\n", r);
174     if (r == S_OK)
175     {
176         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
177         ok(r == S_OK ||
178          broken(r == E_NOINTERFACE), /* NT4 */
179          "Folder::QueryInterface failed: %08x\n", r);
180         if (r == S_OK)
181         {
182             r = Folder2_get_Self(folder2, &item);
183             todo_wine
184             ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
185             if (r == S_OK)
186             {
187                 r = FolderItem_get_Path(item, &item_path);
188                 todo_wine
189                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
190                 todo_wine
191                 ok(!lstrcmpW(item_path, clsidW), "expected %s, got %s\n",
192                  wine_dbgstr_w(clsidW), wine_dbgstr_w(item_path));
193                 SysFreeString(item_path);
194                 FolderItem_Release(item);
195             }
196             Folder2_Release(folder2);
197         }
198         Folder_Release(folder);
199     }
200
201     GetTempPathW(MAX_PATH, tempW);
202     GetCurrentDirectoryW(MAX_PATH, curW);
203     SetCurrentDirectoryW(tempW);
204     CreateDirectoryW(winetestW, NULL);
205     V_VT(&var) = VT_BSTR;
206     V_BSTR(&var) = SysAllocString(winetestW);
207     r = IShellDispatch_NameSpace(sd, var, &folder);
208     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
209     SysFreeString(V_BSTR(&var));
210
211     GetFullPathNameW(winetestW, MAX_PATH, tempW, NULL);
212     if (pGetLongPathNameW)
213     {
214         len = pGetLongPathNameW(tempW, NULL, 0);
215         long_pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
216         if (long_pathW)
217             pGetLongPathNameW(tempW, long_pathW, len);
218     }
219     V_VT(&var) = VT_BSTR;
220     V_BSTR(&var) = SysAllocString(tempW);
221     r = IShellDispatch_NameSpace(sd, var, &folder);
222     ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
223     if (r == S_OK)
224     {
225         r = Folder_get_Title(folder, &title);
226         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
227         if (r == S_OK)
228         {
229             ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
230              wine_dbgstr_w(title));
231             SysFreeString(title);
232         }
233         r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
234         ok(r == S_OK ||
235          broken(r == E_NOINTERFACE), /* NT4 */
236          "Folder::QueryInterface failed: %08x\n", r);
237         if (r == S_OK)
238         {
239             r = Folder2_get_Self(folder2, &item);
240             todo_wine
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                 todo_wine
246                 ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
247                 if (long_pathW)
248                     todo_wine
249                     ok(!lstrcmpW(item_path, long_pathW),
250                      "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
251                      wine_dbgstr_w(item_path));
252                 SysFreeString(item_path);
253                 FolderItem_Release(item);
254             }
255             Folder2_Release(folder2);
256         }
257         Folder_Release(folder);
258     }
259     SysFreeString(V_BSTR(&var));
260
261     len = lstrlenW(tempW);
262     if (len < MAX_PATH - 1)
263     {
264         lstrcatW(tempW, backslashW);
265         V_VT(&var) = VT_BSTR;
266         V_BSTR(&var) = SysAllocString(tempW);
267         r = IShellDispatch_NameSpace(sd, var, &folder);
268         ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
269         if (r == S_OK)
270         {
271             r = Folder_get_Title(folder, &title);
272             ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
273             if (r == S_OK)
274             {
275                 ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
276                  wine_dbgstr_w(title));
277                 SysFreeString(title);
278             }
279             r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2);
280             ok(r == S_OK ||
281              broken(r == E_NOINTERFACE), /* NT4 */
282              "Folder::QueryInterface failed: %08x\n", r);
283             if (r == S_OK)
284             {
285                 r = Folder2_get_Self(folder2, &item);
286                 todo_wine
287                 ok(r == S_OK, "Folder::get_Self failed: %08x\n", r);
288                 if (r == S_OK)
289                 {
290                     r = FolderItem_get_Path(item, &item_path);
291                     todo_wine
292                     ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r);
293                     if (long_pathW)
294                         todo_wine
295                         ok(!lstrcmpW(item_path, long_pathW),
296                          "expected %s, got %s\n", wine_dbgstr_w(long_pathW),
297                          wine_dbgstr_w(item_path));
298                     SysFreeString(item_path);
299                     FolderItem_Release(item);
300                 }
301                 Folder2_Release(folder2);
302             }
303             Folder_Release(folder);
304         }
305         SysFreeString(V_BSTR(&var));
306     }
307
308     HeapFree(GetProcessHeap(), 0, long_pathW);
309     RemoveDirectoryW(winetestW);
310     SetCurrentDirectoryW(curW);
311     IShellDispatch_Release(sd);
312 }
313
314 START_TEST(shelldispatch)
315 {
316     HRESULT r;
317
318     r = CoInitialize(NULL);
319     ok(SUCCEEDED(r), "CoInitialize failed: %08x\n", r);
320     if (FAILED(r))
321         return;
322
323     init_function_pointers();
324     test_namespace();
325
326     CoUninitialize();
327 }