shell32/tests: SHBrowseForFolder should return new folder pidl.
[wine/multimedia.git] / dlls / shell32 / tests / brsfolder.c
blobe9a6e2c849f50f804b5bdd33471b19bc19f35001
1 /*
2 * Unit test of the SHBrowseForFolder function.
4 * Copyright 2009-2010 Michael Mc Donnell
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.
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.
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
20 #include <windows.h>
21 #include <shlobj.h>
22 #include <shobjidl.h>
23 #include <string.h>
25 #include "wine/test.h"
26 #define IDD_MAKENEWFOLDER 0x3746 /* From "../shresdef.h" */
27 #define TIMER_WAIT_MS 50 /* Should be long enough for slow systems */
29 static const char new_folder_name[] = "foo";
32 * Returns the number of folders in a folder.
34 static int get_number_of_folders(LPCSTR path)
36 int number_of_folders = 0;
37 char path_search_string[MAX_PATH];
38 WIN32_FIND_DATA find_data;
39 HANDLE find_handle;
41 strncpy(path_search_string, path, MAX_PATH);
42 strncat(path_search_string, "*", 1);
44 find_handle = FindFirstFile(path_search_string, &find_data);
45 if (find_handle == INVALID_HANDLE_VALUE)
46 return -1;
50 if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
51 strcmp(find_data.cFileName, ".") != 0 &&
52 strcmp(find_data.cFileName, "..") != 0)
54 number_of_folders++;
57 while (FindNextFile(find_handle, &find_data) != 0);
59 return number_of_folders;
62 static BOOL does_folder_or_file_exist(LPCSTR folder_path)
64 DWORD file_attributes = GetFileAttributesA(folder_path);
65 return !(file_attributes == INVALID_FILE_ATTRIBUTES);
69 * Timer callback used by test_click_make_new_folder_button. It simulates a user
70 * making a new folder and calling it "foo".
72 static void CALLBACK make_new_folder_timer_callback(HWND hwnd, UINT uMsg,
73 UINT_PTR idEvent, DWORD dwTime)
75 static int step = 0;
77 switch (step++)
79 case 0:
80 /* Click "Make New Folder" button */
81 PostMessage(hwnd, WM_COMMAND, IDD_MAKENEWFOLDER, 0);
82 break;
83 case 1:
84 /* Set the new folder name to foo by replacing text in edit control */
85 SendMessage(GetFocus(), EM_REPLACESEL, 0, (LPARAM) new_folder_name);
86 SetFocus(hwnd);
87 break;
88 case 2:
90 * The test does not trigger the correct state on Windows. This results
91 * in the new folder pidl not being returned. The result is as
92 * expected if the same steps are done manually.
93 * Sending the down key selects the new folder again which sets the
94 * correct state. This ensures that the correct pidl is returned.
96 keybd_event(VK_DOWN, 0, 0, 0);
97 break;
98 case 3:
99 keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
100 break;
101 case 4:
102 KillTimer(hwnd, idEvent);
103 /* Close dialog box */
104 SendMessage(hwnd, WM_COMMAND, IDOK, 0);
105 break;
106 default:
107 break;
112 * Callback used by test_click_make_new_folder_button. It sets up a timer to
113 * simulate user input.
115 static int CALLBACK create_new_folder_callback(HWND hwnd, UINT uMsg,
116 LPARAM lParam, LPARAM lpData)
118 switch (uMsg)
120 case BFFM_INITIALIZED:
121 /* User input is simulated in timer callback */
122 SetTimer(hwnd, 0, TIMER_WAIT_MS, make_new_folder_timer_callback);
123 return TRUE;
124 default:
125 return FALSE;
130 * Tests if clicking the "Make New Folder" button in a SHBrowseForFolder
131 * dialog box creates a new folder. (Bug 17986).
133 * Here follows a description of what happens on W2K,Vista, W2K8, W7:
134 * When the "Make New Folder" button is clicked a new folder is created and
135 * inserted into the tree. The folder is given a default name that depends on
136 * the locale (e.g. "New Folder"). The folder name is selected and the dialog
137 * waits for the user to type in a new name. The folder is renamed when the user
138 * types in a name and presses enter.
140 * Note that XP and W2K3 do not select the folder name or wait for the user
141 * to type in a new folder name. This behavior is considered broken as most
142 * users would like to give the folder a name after creating it. The fact that
143 * it originally waited for the user to type in a new folder name(W2K), and then
144 * again was changed back wait for the new folder name(Vista, W2K8, W7),
145 * indicates that MS also believes that it was broken in XP and W2K3.
147 static void test_click_make_new_folder_button(void)
149 HRESULT resCoInit;
150 BROWSEINFO bi;
151 LPITEMIDLIST pidl = NULL;
152 LPITEMIDLIST test_folder_pidl;
153 IShellFolder *test_folder_object;
154 char test_folder_path[MAX_PATH];
155 WCHAR test_folder_pathW[MAX_PATH];
156 CHAR new_folder_path[MAX_PATH];
157 CHAR new_folder_pidl_path[MAX_PATH];
158 char selected_folder[MAX_PATH];
159 const CHAR title[] = "test_click_make_new_folder_button";
160 int number_of_folders = -1;
161 SHFILEOPSTRUCT shfileop;
163 if (does_folder_or_file_exist(title))
165 skip("The test folder already exists.\n");
166 return;
169 /* Must initialize COM if using the NEWDIAlOGSTYLE according to MSDN. */
170 resCoInit = CoInitialize(NULL);
171 if(!(resCoInit == S_OK || resCoInit == S_FALSE))
173 skip("COM could not be initialized %u\n", GetLastError());
174 return;
177 /* Leave room for concatenating title, two backslashes, and an extra NULL. */
178 if (!GetCurrentDirectoryA(MAX_PATH-strlen(title)-3, test_folder_path))
180 skip("GetCurrentDirectoryA failed %u\n", GetLastError());
182 strncat(test_folder_path, "\\", 1);
183 strncat(test_folder_path, title, MAX_PATH-1);
184 strncat(test_folder_path, "\\", 1);
186 /* Avoid conflicts by creating a test folder. */
187 if (!CreateDirectoryA(title, NULL))
189 skip("CreateDirectoryA failed %u\n", GetLastError());
190 return;
193 /* Initialize browse info struct for SHBrowseForFolder */
194 bi.hwndOwner = NULL;
195 bi.pszDisplayName = (LPTSTR) &selected_folder;
196 bi.lpszTitle = (LPTSTR) title;
197 bi.ulFlags = BIF_NEWDIALOGSTYLE;
198 bi.lpfn = create_new_folder_callback;
199 /* Use test folder as the root folder for dialog box */
200 MultiByteToWideChar(CP_UTF8, 0, test_folder_path, MAX_PATH,
201 test_folder_pathW, MAX_PATH*sizeof(WCHAR));
202 SHGetDesktopFolder(&test_folder_object);
203 test_folder_object->lpVtbl->ParseDisplayName(test_folder_object, NULL, NULL,
204 test_folder_pathW, 0UL, &test_folder_pidl, 0UL);
205 bi.pidlRoot = test_folder_pidl;
207 /* Display dialog box and let callback click the buttons */
208 pidl = SHBrowseForFolder(&bi);
210 number_of_folders = get_number_of_folders(test_folder_path);
211 todo_wine ok(number_of_folders == 1 || broken(number_of_folders == 0) /* W95, W98 */,
212 "Clicking \"Make New Folder\" button did not result in a new folder.\n");
214 /* There should be a new folder foo inside the test folder */
215 strcpy(new_folder_path, test_folder_path);
216 strcat(new_folder_path, new_folder_name);
217 todo_wine ok(does_folder_or_file_exist(new_folder_path)
218 || broken(!does_folder_or_file_exist(new_folder_path)) /* W95, W98, XP, W2K3 */,
219 "The new folder did not get the name %s\n", new_folder_name);
221 /* Dialog should return a pidl pointing to the new folder */
222 ok(SHGetPathFromIDListA(pidl, new_folder_pidl_path),
223 "SHGetPathFromIDList failed for new folder.\n");
224 todo_wine ok(strcmp(new_folder_path, new_folder_pidl_path) == 0
225 || broken(strcmp(new_folder_path, new_folder_pidl_path) != 0) /* earlier than Vista */,
226 "SHBrowseForFolder did not return the pidl for the new folder. "
227 "Expected '%s' got '%s'\n", new_folder_path, new_folder_pidl_path);
229 /* Remove test folder and any subfolders created in this test */
230 shfileop.hwnd = NULL;
231 shfileop.wFunc = FO_DELETE;
232 /* Path must be double NULL terminated */
233 test_folder_path[strlen(test_folder_path)+1] = '\0';
234 shfileop.pFrom = test_folder_path;
235 shfileop.pTo = NULL;
236 shfileop.fFlags = FOF_NOCONFIRMATION|FOF_NOERRORUI|FOF_SILENT;
237 SHFileOperation(&shfileop);
239 if (pidl)
240 CoTaskMemFree(pidl);
241 if (test_folder_pidl)
242 CoTaskMemFree(test_folder_pidl);
243 if (test_folder_object)
244 test_folder_object->lpVtbl->Release(test_folder_object);
246 CoUninitialize();
249 START_TEST(brsfolder)
251 test_click_make_new_folder_button();