shell32: Implement IShellDispatch::NameSpace.
[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
34 static void init_function_pointers(void)
35 {
36     HMODULE hmod;
37
38     hmod = GetModuleHandleA("shell32.dll");
39     pSHGetFolderPathW = (void*)GetProcAddress(hmod, "SHGetFolderPathW");
40     pSHGetNameFromIDList = (void*)GetProcAddress(hmod, "SHGetNameFromIDList");
41     pSHGetSpecialFolderLocation = (void*)GetProcAddress(hmod,
42      "SHGetSpecialFolderLocation");
43 }
44
45 static void test_namespace(void)
46 {
47     static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0};
48     static const WCHAR backslashW[] = {'\\',0};
49
50     static WCHAR tempW[MAX_PATH], curW[MAX_PATH];
51     HRESULT r;
52     IShellDispatch *sd;
53     Folder *folder;
54     VARIANT var;
55     BSTR title;
56     int len;
57
58     r = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
59      &IID_IShellDispatch, (LPVOID*)&sd);
60     if (r == REGDB_E_CLASSNOTREG) /* NT4 */
61     {
62         win_skip("skipping IShellDispatch tests\n");
63         return;
64     }
65     ok(SUCCEEDED(r), "CoCreateInstance failed: %08x\n", r);
66     if (FAILED(r))
67         return;
68
69     VariantInit(&var);
70     folder = (void*)0xdeadbeef;
71     r = IShellDispatch_NameSpace(sd, var, &folder);
72     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
73     ok(folder == NULL, "expected NULL, got %p\n", folder);
74
75     V_VT(&var) = VT_I4;
76     V_I4(&var) = -1;
77     r = IShellDispatch_NameSpace(sd, var, &folder);
78     todo_wine
79     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
80
81     V_VT(&var) = VT_I4;
82     V_I4(&var) = ssfPROGRAMFILES;
83     r = IShellDispatch_NameSpace(sd, var, &folder);
84     ok(r == S_OK ||
85      broken(r == S_FALSE), /* NT4 */
86      "IShellDispatch::NameSpace failed: %08x\n", r);
87     if (r == S_OK)
88     {
89         r = Folder_get_Title(folder, &title);
90         todo_wine
91         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
92         if (r == S_OK)
93         {
94             /* On Win2000-2003 title is equal to program files directory name in
95                HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir.
96                On newer Windows it seems constant and is not changed
97                if the program files directory name is changed */
98             if (pSHGetSpecialFolderLocation && pSHGetNameFromIDList)
99             {
100                 LPITEMIDLIST pidl;
101                 PWSTR name;
102
103                 r = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl);
104                 ok(r == S_OK, "SHGetSpecialFolderLocation failed: %08x\n", r);
105                 r = pSHGetNameFromIDList(pidl, SIGDN_NORMALDISPLAY, &name);
106                 ok(r == S_OK, "SHGetNameFromIDList failed: %08x\n", r);
107                 todo_wine
108                 ok(!lstrcmpW(title, name), "expected %s, got %s\n",
109                  wine_dbgstr_w(name), wine_dbgstr_w(title));
110                 CoTaskMemFree(name);
111                 CoTaskMemFree(pidl);
112             }
113             else if (pSHGetFolderPathW)
114             {
115                 static WCHAR path[MAX_PATH];
116                 WCHAR *p;
117
118                 r = pSHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL,
119                  SHGFP_TYPE_CURRENT, path);
120                 ok(r == S_OK, "SHGetFolderPath failed: %08x\n", r);
121                 p = path + lstrlenW(path);
122                 while (path < p && *(p - 1) != '\\')
123                     p--;
124                 ok(!lstrcmpW(title, p), "expected %s, got %s\n",
125                  wine_dbgstr_w(p), wine_dbgstr_w(title));
126             }
127             else skip("skipping Folder::get_Title test\n");
128             SysFreeString(title);
129         }
130         Folder_Release(folder);
131     }
132
133     GetTempPathW(MAX_PATH, tempW);
134     GetCurrentDirectoryW(MAX_PATH, curW);
135     SetCurrentDirectoryW(tempW);
136     CreateDirectoryW(winetestW, NULL);
137     V_VT(&var) = VT_BSTR;
138     V_BSTR(&var) = SysAllocString(winetestW);
139     r = IShellDispatch_NameSpace(sd, var, &folder);
140     ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r);
141     SysFreeString(V_BSTR(&var));
142
143     GetFullPathNameW(winetestW, MAX_PATH, tempW, NULL);
144     V_VT(&var) = VT_BSTR;
145     V_BSTR(&var) = SysAllocString(tempW);
146     r = IShellDispatch_NameSpace(sd, var, &folder);
147     ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
148     if (r == S_OK)
149     {
150         r = Folder_get_Title(folder, &title);
151         todo_wine
152         ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
153         if (r == S_OK)
154         {
155             todo_wine
156             ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
157              wine_dbgstr_w(title));
158             SysFreeString(title);
159         }
160         Folder_Release(folder);
161     }
162     SysFreeString(V_BSTR(&var));
163
164     len = lstrlenW(tempW);
165     if (len < MAX_PATH - 1)
166     {
167         lstrcatW(tempW, backslashW);
168         V_VT(&var) = VT_BSTR;
169         V_BSTR(&var) = SysAllocString(tempW);
170         r = IShellDispatch_NameSpace(sd, var, &folder);
171         ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r);
172         if (r == S_OK)
173         {
174             r = Folder_get_Title(folder, &title);
175             todo_wine
176             ok(r == S_OK, "Folder::get_Title failed: %08x\n", r);
177             if (r == S_OK)
178             {
179                 todo_wine
180                 ok(!lstrcmpW(title, winetestW), "bad title: %s\n",
181                  wine_dbgstr_w(title));
182                 SysFreeString(title);
183             }
184             Folder_Release(folder);
185         }
186         SysFreeString(V_BSTR(&var));
187     }
188
189     RemoveDirectoryW(winetestW);
190     SetCurrentDirectoryW(curW);
191     IShellDispatch_Release(sd);
192 }
193
194 START_TEST(shelldispatch)
195 {
196     HRESULT r;
197
198     r = CoInitialize(NULL);
199     ok(SUCCEEDED(r), "CoInitialize failed: %08x\n", r);
200     if (FAILED(r))
201         return;
202
203     init_function_pointers();
204     test_namespace();
205
206     CoUninitialize();
207 }