shell32: Add some selection tests.
[wine/multimedia.git] / dlls / shell32 / tests / brsfolder.c
blob7adaa1f4f82a26b0bba3fbff58ce7b02f9866a1b
1 /*
2 * Unit test of the SHBrowseForFolder function.
4 * Copyright 2009-2010 Michael Mc Donnell
5 * Copyright 2011 André Hentschel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <windows.h>
22 #include <shlobj.h>
23 #include <shobjidl.h>
24 #include <string.h>
26 #include "wine/test.h"
27 #define IDD_MAKENEWFOLDER 0x3746 /* From "../shresdef.h" */
28 #define TIMER_WAIT_MS 50 /* Should be long enough for slow systems */
30 static const char new_folder_name[] = "foo";
31 static LPITEMIDLIST selected_folder_pidl;
34 * Returns the number of folders in a folder.
36 static int get_number_of_folders(LPCSTR path)
38 int number_of_folders = 0;
39 char path_search_string[MAX_PATH];
40 WIN32_FIND_DATA find_data;
41 HANDLE find_handle;
43 strncpy(path_search_string, path, MAX_PATH);
44 strncat(path_search_string, "*", 1);
46 find_handle = FindFirstFile(path_search_string, &find_data);
47 if (find_handle == INVALID_HANDLE_VALUE)
48 return -1;
52 if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
53 strcmp(find_data.cFileName, ".") != 0 &&
54 strcmp(find_data.cFileName, "..") != 0)
56 number_of_folders++;
59 while (FindNextFile(find_handle, &find_data) != 0);
61 FindClose(find_handle);
62 return number_of_folders;
65 static BOOL does_folder_or_file_exist(LPCSTR folder_path)
67 DWORD file_attributes = GetFileAttributesA(folder_path);
68 return !(file_attributes == INVALID_FILE_ATTRIBUTES);
72 * Timer callback used by test_click_make_new_folder_button. It simulates a user
73 * making a new folder and calling it "foo".
75 static void CALLBACK make_new_folder_timer_callback(HWND hwnd, UINT uMsg,
76 UINT_PTR idEvent, DWORD dwTime)
78 static int step = 0;
80 switch (step++)
82 case 0:
83 /* Click "Make New Folder" button */
84 PostMessage(hwnd, WM_COMMAND, IDD_MAKENEWFOLDER, 0);
85 break;
86 case 1:
87 /* Set the new folder name to foo by replacing text in edit control */
88 SendMessage(GetFocus(), EM_REPLACESEL, 0, (LPARAM) new_folder_name);
89 SetFocus(hwnd);
90 break;
91 case 2:
93 * The test does not trigger the correct state on Windows. This results
94 * in the new folder pidl not being returned. The result is as
95 * expected if the same steps are done manually.
96 * Sending the down key selects the new folder again which sets the
97 * correct state. This ensures that the correct pidl is returned.
99 keybd_event(VK_DOWN, 0, 0, 0);
100 break;
101 case 3:
102 keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
103 break;
104 case 4:
105 KillTimer(hwnd, idEvent);
106 /* Close dialog box */
107 SendMessage(hwnd, WM_COMMAND, IDOK, 0);
108 break;
109 default:
110 break;
115 * Callback used by test_click_make_new_folder_button. It sets up a timer to
116 * simulate user input.
118 static int CALLBACK create_new_folder_callback(HWND hwnd, UINT uMsg,
119 LPARAM lParam, LPARAM lpData)
121 switch (uMsg)
123 case BFFM_INITIALIZED:
124 /* User input is simulated in timer callback */
125 SetTimer(hwnd, 0, TIMER_WAIT_MS, make_new_folder_timer_callback);
126 return TRUE;
127 default:
128 return FALSE;
133 * Tests if clicking the "Make New Folder" button in a SHBrowseForFolder
134 * dialog box creates a new folder. (Bug 17986).
136 * Here follows a description of what happens on W2K,Vista, W2K8, W7:
137 * When the "Make New Folder" button is clicked a new folder is created and
138 * inserted into the tree. The folder is given a default name that depends on
139 * the locale (e.g. "New Folder"). The folder name is selected and the dialog
140 * waits for the user to type in a new name. The folder is renamed when the user
141 * types in a name and presses enter.
143 * Note that XP and W2K3 do not select the folder name or wait for the user
144 * to type in a new folder name. This behavior is considered broken as most
145 * users would like to give the folder a name after creating it. The fact that
146 * it originally waited for the user to type in a new folder name(W2K), and then
147 * again was changed back wait for the new folder name(Vista, W2K8, W7),
148 * indicates that MS also believes that it was broken in XP and W2K3.
150 static void test_click_make_new_folder_button(void)
152 HRESULT resCoInit;
153 BROWSEINFO bi;
154 LPITEMIDLIST pidl = NULL;
155 LPITEMIDLIST test_folder_pidl;
156 IShellFolder *test_folder_object;
157 char test_folder_path[MAX_PATH];
158 WCHAR test_folder_pathW[MAX_PATH];
159 CHAR new_folder_path[MAX_PATH];
160 CHAR new_folder_pidl_path[MAX_PATH];
161 char selected_folder[MAX_PATH];
162 const CHAR title[] = "test_click_make_new_folder_button";
163 int number_of_folders = -1;
164 SHFILEOPSTRUCT shfileop;
166 if (does_folder_or_file_exist(title))
168 skip("The test folder already exists.\n");
169 return;
172 /* Must initialize COM if using the NEWDIAlOGSTYLE according to MSDN. */
173 resCoInit = CoInitialize(NULL);
174 if(!(resCoInit == S_OK || resCoInit == S_FALSE))
176 skip("COM could not be initialized %u\n", GetLastError());
177 return;
180 /* Leave room for concatenating title, two backslashes, and an extra NULL. */
181 if (!GetCurrentDirectoryA(MAX_PATH-strlen(title)-3, test_folder_path))
183 skip("GetCurrentDirectoryA failed %u\n", GetLastError());
185 strncat(test_folder_path, "\\", 1);
186 strncat(test_folder_path, title, MAX_PATH-1);
187 strncat(test_folder_path, "\\", 1);
189 /* Avoid conflicts by creating a test folder. */
190 if (!CreateDirectoryA(title, NULL))
192 skip("CreateDirectoryA failed %u\n", GetLastError());
193 return;
196 /* Initialize browse info struct for SHBrowseForFolder */
197 bi.hwndOwner = NULL;
198 bi.pszDisplayName = (LPTSTR) &selected_folder;
199 bi.lpszTitle = (LPTSTR) title;
200 bi.ulFlags = BIF_NEWDIALOGSTYLE;
201 bi.lpfn = create_new_folder_callback;
202 /* Use test folder as the root folder for dialog box */
203 MultiByteToWideChar(CP_UTF8, 0, test_folder_path, MAX_PATH,
204 test_folder_pathW, MAX_PATH);
205 SHGetDesktopFolder(&test_folder_object);
206 test_folder_object->lpVtbl->ParseDisplayName(test_folder_object, NULL, NULL,
207 test_folder_pathW, 0UL, &test_folder_pidl, 0UL);
208 bi.pidlRoot = test_folder_pidl;
210 /* Display dialog box and let callback click the buttons */
211 pidl = SHBrowseForFolder(&bi);
213 number_of_folders = get_number_of_folders(test_folder_path);
214 todo_wine ok(number_of_folders == 1 || broken(number_of_folders == 0) /* W95, W98 */,
215 "Clicking \"Make New Folder\" button did not result in a new folder.\n");
217 /* There should be a new folder foo inside the test folder */
218 strcpy(new_folder_path, test_folder_path);
219 strcat(new_folder_path, new_folder_name);
220 todo_wine ok(does_folder_or_file_exist(new_folder_path)
221 || broken(!does_folder_or_file_exist(new_folder_path)) /* W95, W98, XP, W2K3 */,
222 "The new folder did not get the name %s\n", new_folder_name);
224 /* Dialog should return a pidl pointing to the new folder */
225 ok(SHGetPathFromIDListA(pidl, new_folder_pidl_path),
226 "SHGetPathFromIDList failed for new folder.\n");
227 todo_wine ok(strcmp(new_folder_path, new_folder_pidl_path) == 0
228 || broken(strcmp(new_folder_path, new_folder_pidl_path) != 0) /* earlier than Vista */,
229 "SHBrowseForFolder did not return the pidl for the new folder. "
230 "Expected '%s' got '%s'\n", new_folder_path, new_folder_pidl_path);
232 /* Remove test folder and any subfolders created in this test */
233 shfileop.hwnd = NULL;
234 shfileop.wFunc = FO_DELETE;
235 /* Path must be double NULL terminated */
236 test_folder_path[strlen(test_folder_path)+1] = '\0';
237 shfileop.pFrom = test_folder_path;
238 shfileop.pTo = NULL;
239 shfileop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
240 SHFileOperation(&shfileop);
242 if (pidl)
243 CoTaskMemFree(pidl);
244 if (test_folder_pidl)
245 CoTaskMemFree(test_folder_pidl);
246 if (test_folder_object)
247 test_folder_object->lpVtbl->Release(test_folder_object);
249 CoUninitialize();
254 * Callback used by test_selection.
256 static int CALLBACK selection_callback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
258 DWORD ret;
260 switch (uMsg)
262 case BFFM_INITIALIZED:
263 /* test with zero values */
264 ret = SendMessage(hwnd, BFFM_SETSELECTIONA, 0, 0);
265 ok(!ret, "SendMessage returned: %u\n", ret);
266 ret = SendMessage(hwnd, BFFM_SETSELECTIONW, 0, 0);
267 ok(!ret, "SendMessage returned: %u\n", ret);
269 ret = SendMessage(hwnd, BFFM_SETSELECTIONA, 1, 0);
270 ok(!ret, "SendMessage returned: %u\n", ret);
272 if(0)
274 /* Crashes on NT4 */
275 ret = SendMessage(hwnd, BFFM_SETSELECTIONW, 1, 0);
276 ok(!ret, "SendMessage returned: %u\n", ret);
279 ret = SendMessage(hwnd, BFFM_SETSELECTIONA, 0, (LPARAM)selected_folder_pidl);
280 ok(!ret, "SendMessage returned: %u\n", ret);
281 ret = SendMessage(hwnd, BFFM_SETSELECTIONW, 0, (LPARAM)selected_folder_pidl);
282 ok(!ret, "SendMessage returned: %u\n", ret);
284 ret = SendMessage(hwnd, BFFM_SETSELECTIONA, 1, (LPARAM)selected_folder_pidl);
285 ok(!ret, "SendMessage returned: %u\n", ret);
286 ret = SendMessage(hwnd, BFFM_SETSELECTIONW, 1, (LPARAM)selected_folder_pidl);
287 ok(!ret, "SendMessage returned: %u\n", ret);
289 ret = SendMessage(hwnd, BFFM_SETSELECTIONA, 1, (LPARAM)new_folder_name);
290 ok(!ret, "SendMessage returned: %u\n", ret);
291 ret = SendMessage(hwnd, BFFM_SETSELECTIONW, 1, (LPARAM)new_folder_name);
292 ok(!ret, "SendMessage returned: %u\n", ret);
294 SendMessage(hwnd, WM_COMMAND, IDOK, 0);
295 return TRUE;
296 default:
297 return FALSE;
301 static void test_selection(void)
303 HRESULT resCoInit;
304 BROWSEINFO bi;
305 LPITEMIDLIST pidl = NULL;
306 IShellFolder *desktop_object;
307 WCHAR selected_folderW[MAX_PATH];
308 const CHAR title[] = "test_selection";
310 resCoInit = CoInitialize(NULL);
311 if(!(resCoInit == S_OK || resCoInit == S_FALSE))
313 skip("COM could not be initialized %u\n", GetLastError());
314 return;
317 if (!GetCurrentDirectoryW(MAX_PATH, selected_folderW))
319 skip("GetCurrentDirectoryW failed %u\n", GetLastError());
322 /* Initialize browse info struct for SHBrowseForFolder */
323 bi.hwndOwner = NULL;
324 bi.pszDisplayName = NULL;
325 bi.lpszTitle = (LPTSTR) title;
326 bi.ulFlags = BIF_NEWDIALOGSTYLE;
327 bi.lpfn = selection_callback;
329 SHGetDesktopFolder(&desktop_object);
330 desktop_object->lpVtbl->ParseDisplayName(desktop_object, NULL, NULL,
331 selected_folderW, 0UL, &selected_folder_pidl, 0UL);
332 bi.pidlRoot = selected_folder_pidl;
334 pidl = SHBrowseForFolder(&bi);
336 if (pidl)
337 CoTaskMemFree(pidl);
339 CoUninitialize();
342 START_TEST(brsfolder)
344 test_click_make_new_folder_button();
345 test_selection();