2 * comctl32 MRU unit tests
4 * Copyright (C) 2004 Jon Griffiths <jon_p_griffiths@yahoo.com>
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
31 #include "wine/test.h"
33 /* Keys for testing MRU functions */
34 #define REG_TEST_BASEKEYA "Software\\Wine"
35 #define REG_TEST_BASESUBKEYA "Test"
36 #define REG_TEST_KEYA REG_TEST_BASEKEYA "\\" REG_TEST_BASESUBKEYA
37 #define REG_TEST_SUBKEYA "MRUTest"
38 #define REG_TEST_FULLKEY REG_TEST_KEYA "\\" REG_TEST_SUBKEYA
40 /* Undocumented MRU functions */
41 typedef struct tagMRUINFOA
48 int (CALLBACK *lpfnCompare)(LPCSTR, LPCSTR);
51 typedef struct tagMRUINFOW
58 int (CALLBACK *lpfnCompare)(LPCWSTR, LPCWSTR);
61 #define MRU_STRING 0 /* this one's invented */
63 #define MRU_CACHEWRITE 2
65 #define LIST_SIZE 3 /* Max entries for each mru */
67 static HMODULE hComctl32;
68 static HANDLE (WINAPI *pCreateMRUListA)(MRUINFOA*);
69 static void (WINAPI *pFreeMRUList)(HANDLE);
70 static INT (WINAPI *pAddMRUStringA)(HANDLE,LPCSTR);
71 static INT (WINAPI *pEnumMRUListA)(HANDLE,INT,LPVOID,DWORD);
72 static INT (WINAPI *pEnumMRUListW)(HANDLE,INT,LPVOID,DWORD);
73 static HANDLE (WINAPI *pCreateMRUListLazyA)(MRUINFOA*, DWORD, DWORD, DWORD);
74 static HANDLE (WINAPI *pCreateMRUListLazyW)(MRUINFOW*, DWORD, DWORD, DWORD);
75 static INT (WINAPI *pFindMRUData)(HANDLE, LPCVOID, DWORD, LPINT);
76 static INT (WINAPI *pAddMRUData)(HANDLE, LPCVOID, DWORD);
77 static HANDLE (WINAPI *pCreateMRUListW)(MRUINFOW*);
79 static void InitPointers(void)
81 pCreateMRUListA = (void*)GetProcAddress(hComctl32,(LPCSTR)151);
82 pFreeMRUList = (void*)GetProcAddress(hComctl32,(LPCSTR)152);
83 pAddMRUStringA = (void*)GetProcAddress(hComctl32,(LPCSTR)153);
84 pEnumMRUListA = (void*)GetProcAddress(hComctl32,(LPCSTR)154);
85 pCreateMRUListLazyA = (void*)GetProcAddress(hComctl32,(LPCSTR)157);
86 pAddMRUData = (void*)GetProcAddress(hComctl32,(LPCSTR)167);
87 pFindMRUData = (void*)GetProcAddress(hComctl32,(LPCSTR)169);
88 pCreateMRUListW = (void*)GetProcAddress(hComctl32,(LPCSTR)400);
89 pEnumMRUListW = (void*)GetProcAddress(hComctl32,(LPCSTR)403);
90 pCreateMRUListLazyW = (void*)GetProcAddress(hComctl32,(LPCSTR)404);
93 /* Based on RegDeleteTreeW from dlls/advapi32/registry.c */
94 static LSTATUS mru_RegDeleteTreeA(HKEY hKey, LPCSTR lpszSubKey)
97 DWORD dwMaxSubkeyLen, dwMaxValueLen;
98 DWORD dwMaxLen, dwSize;
99 CHAR szNameBuf[MAX_PATH], *lpszName = szNameBuf;
104 ret = RegOpenKeyExA(hKey, lpszSubKey, 0, KEY_READ, &hSubKey);
108 /* Get highest length for keys, values */
109 ret = RegQueryInfoKeyA(hSubKey, NULL, NULL, NULL, NULL,
110 &dwMaxSubkeyLen, NULL, NULL, &dwMaxValueLen, NULL, NULL, NULL);
111 if (ret) goto cleanup;
115 dwMaxLen = max(dwMaxSubkeyLen, dwMaxValueLen);
116 if (dwMaxLen > sizeof(szNameBuf)/sizeof(CHAR))
118 /* Name too big: alloc a buffer for it */
119 if (!(lpszName = HeapAlloc( GetProcessHeap(), 0, dwMaxLen*sizeof(CHAR))))
121 ret = ERROR_NOT_ENOUGH_MEMORY;
127 /* Recursively delete all the subkeys */
131 if (RegEnumKeyExA(hSubKey, 0, lpszName, &dwSize, NULL,
132 NULL, NULL, NULL)) break;
134 ret = mru_RegDeleteTreeA(hSubKey, lpszName);
135 if (ret) goto cleanup;
139 ret = RegDeleteKeyA(hKey, lpszSubKey);
144 if (RegEnumValueA(hKey, 0, lpszName, &dwSize,
145 NULL, NULL, NULL, NULL)) break;
147 ret = RegDeleteValueA(hKey, lpszName);
148 if (ret) goto cleanup;
152 /* Free buffer if allocated */
153 if (lpszName != szNameBuf)
154 HeapFree( GetProcessHeap(), 0, lpszName);
156 RegCloseKey(hSubKey);
160 static BOOL create_reg_entries(void)
164 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_FULLKEY, &hKey),
165 "Couldn't create test key \"%s\"\n", REG_TEST_KEYA);
166 if (!hKey) return FALSE;
171 static void delete_reg_entries(void)
175 if (RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_BASEKEYA, 0, KEY_ALL_ACCESS,
178 mru_RegDeleteTreeA(hKey, REG_TEST_BASESUBKEYA);
182 static void check_reg_entries(const char *mrulist, const char**items)
186 DWORD type, size, ret;
189 ok(!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_FULLKEY, &hKey),
190 "Couldn't open test key \"%s\"\n", REG_TEST_FULLKEY);
196 ret = RegQueryValueExA(hKey, "MRUList", NULL, &type, (LPBYTE)buff, &size);
198 ok(!ret && buff[0], "Checking MRU: got %d from RegQueryValueExW\n", ret);
199 if(ret || !buff[0]) return;
201 ok(strcmp(buff, mrulist) == 0, "Checking MRU: Expected list %s, got %s\n",
203 if(strcmp(buff, mrulist)) return;
205 for (i = 0; i < strlen(mrulist); i++)
208 name[0] = mrulist[i];
213 ret = RegQueryValueExA(hKey, name, NULL, &type, (LPBYTE)buff, &size);
215 "Checking MRU item %d ('%c'): got %d from RegQueryValueExW\n",
217 if(ret || !buff[0]) return;
218 ok(!strcmp(buff, items[mrulist[i]-'a']),
219 "Checking MRU item %d ('%c'): expected \"%s\", got \"%s\"\n",
220 i, mrulist[i], buff, items[mrulist[i] - 'a']);
224 static int CALLBACK cmp_mru_strA(LPCSTR data1, LPCSTR data2)
226 return lstrcmpiA(data1, data2);
229 static void test_MRUListA(void)
231 const char *checks[LIST_SIZE+1];
237 if (!pCreateMRUListA || !pFreeMRUList || !pAddMRUStringA || !pEnumMRUListA)
239 skip("MRU entry points not found\n");
245 /* Create (NULL) - crashes native */
246 hMRU = pCreateMRUListA(NULL);
250 infoA.cbSize = sizeof(infoA) - 2;
251 infoA.uMax = LIST_SIZE;
252 infoA.fFlags = MRU_STRING;
254 infoA.lpszSubKey = REG_TEST_SUBKEYA;
255 infoA.lpfnCompare = cmp_mru_strA;
258 hMRU = pCreateMRUListA(&infoA);
259 ok (!hMRU && !GetLastError(),
260 "CreateMRUListA(too small) expected NULL,0 got %p,%d\n",
261 hMRU, GetLastError());
264 infoA.cbSize = sizeof(infoA) + 2;
265 infoA.uMax = LIST_SIZE;
266 infoA.fFlags = MRU_STRING;
268 infoA.lpszSubKey = REG_TEST_SUBKEYA;
269 infoA.lpfnCompare = cmp_mru_strA;
272 hMRU = pCreateMRUListA(&infoA);
273 ok (!hMRU && !GetLastError(),
274 "CreateMRUListA(too big) expected NULL,0 got %p,%d\n",
275 hMRU, GetLastError());
278 infoA.cbSize = sizeof(infoA);
279 infoA.uMax = LIST_SIZE;
280 infoA.fFlags = MRU_STRING;
282 infoA.lpszSubKey = REG_TEST_SUBKEYA;
283 infoA.lpfnCompare = cmp_mru_strA;
286 hMRU = pCreateMRUListA(&infoA);
287 ok (!hMRU && !GetLastError(),
288 "CreateMRUListA(NULL key) expected NULL,0 got %p,%d\n",
289 hMRU, GetLastError());
291 /* NULL subkey name */
292 infoA.cbSize = sizeof(infoA);
293 infoA.uMax = LIST_SIZE;
294 infoA.fFlags = MRU_STRING;
296 infoA.lpszSubKey = NULL;
297 infoA.lpfnCompare = cmp_mru_strA;
300 hMRU = pCreateMRUListA(&infoA);
301 ok (!hMRU && !GetLastError(),
302 "CreateMRUListA(NULL name) expected NULL,0 got %p,%d\n",
303 hMRU, GetLastError());
305 /* Create a string MRU */
306 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEYA, &hKey),
307 "Couldn't create test key \"%s\"\n", REG_TEST_KEYA);
310 infoA.cbSize = sizeof(infoA);
311 infoA.uMax = LIST_SIZE;
312 infoA.fFlags = MRU_STRING;
314 infoA.lpszSubKey = REG_TEST_SUBKEYA;
315 infoA.lpfnCompare = cmp_mru_strA;
317 hMRU = pCreateMRUListA(&infoA);
318 ok(hMRU && !GetLastError(),
319 "CreateMRUListA(string) expected non-NULL,0 got %p,%d\n",
320 hMRU, GetLastError());
325 checks[0] = "Test 1";
326 checks[1] = "Test 2";
327 checks[2] = "Test 3";
328 checks[3] = "Test 4";
330 /* Add (NULL list) */
332 iRet = pAddMRUStringA(NULL, checks[0]);
333 ok(iRet == -1 && !GetLastError(),
334 "AddMRUStringA(NULL list) expected -1,0 got %d,%d\n",
335 iRet, GetLastError());
337 /* Add (NULL string) */
340 /* Some native versions crash when passed NULL or fail to SetLastError() */
342 iRet = pAddMRUStringA(hMRU, NULL);
343 ok(iRet == 0 && GetLastError() == ERROR_INVALID_PARAMETER,
344 "AddMRUStringA(NULL str) expected 0,ERROR_INVALID_PARAMETER got %d,%d\n",
345 iRet, GetLastError());
348 /* Add 3 strings. Check the registry is correct after each add */
350 iRet = pAddMRUStringA(hMRU, checks[0]);
351 ok(iRet == 0 && !GetLastError(),
352 "AddMRUStringA(1) expected 0,0 got %d,%d\n",
353 iRet, GetLastError());
354 check_reg_entries("a", checks);
357 iRet = pAddMRUStringA(hMRU, checks[1]);
358 ok(iRet == 1 && !GetLastError(),
359 "AddMRUStringA(2) expected 1,0 got %d,%d\n",
360 iRet, GetLastError());
361 check_reg_entries("ba", checks);
364 iRet = pAddMRUStringA(hMRU, checks[2]);
365 ok(iRet == 2 && !GetLastError(),
366 "AddMRUStringA(2) expected 2,0 got %d,%d\n",
367 iRet, GetLastError());
368 check_reg_entries("cba", checks);
370 /* Add a duplicate of the 2nd string - it should move to the front,
371 * but keep the same index in the registry.
374 iRet = pAddMRUStringA(hMRU, checks[1]);
375 ok(iRet == 1 && !GetLastError(),
376 "AddMRUStringA(re-add 1) expected 1,0 got %d,%d\n",
377 iRet, GetLastError());
378 check_reg_entries("bca", checks);
380 /* Add a new string - replaces the oldest string + moves to the front */
382 iRet = pAddMRUStringA(hMRU, checks[3]);
383 ok(iRet == 0 && !GetLastError(),
384 "AddMRUStringA(add new) expected 0,0 got %d,%d\n",
385 iRet, GetLastError());
386 checks[0] = checks[3];
387 check_reg_entries("abc", checks);
389 /* NULL buffer = get list size */
390 iRet = pEnumMRUListA(hMRU, 0, NULL, 0);
391 ok(iRet == 3 || iRet == -1 /* Vista */, "EnumMRUList expected %d or -1, got %d\n", LIST_SIZE, iRet);
393 /* negative item pos = get list size */
394 iRet = pEnumMRUListA(hMRU, -1, NULL, 0);
395 ok(iRet == 3 || iRet == -1 /* Vista */, "EnumMRUList expected %d or -1, got %d\n", LIST_SIZE, iRet);
397 /* negative item pos = get list size */
398 iRet = pEnumMRUListA(hMRU, -5, NULL, 0);
399 ok(iRet == 3 || iRet == -1 /* Vista */, "EnumMRUList expected %d or -1, got %d\n", LIST_SIZE, iRet);
401 /* negative item pos = get list size */
402 iRet = pEnumMRUListA(hMRU, -1, buffer, 255);
403 ok(iRet == 3, "EnumMRUList expected %d, got %d\n", LIST_SIZE, iRet);
405 /* negative item pos = get list size */
406 iRet = pEnumMRUListA(hMRU, -5, buffer, 255);
407 ok(iRet == 3, "EnumMRUList expected %d, got %d\n", LIST_SIZE, iRet);
411 iRet = pEnumMRUListA(hMRU, 0, buffer, 255);
412 ok(iRet == lstrlen(checks[3]), "EnumMRUList expected %d, got %d\n", lstrlen(checks[3]), iRet);
413 ok(strcmp(buffer, checks[3]) == 0, "EnumMRUList expected %s, got %s\n", checks[3], buffer);
415 /* check entry 0 with a too small buffer */
416 buffer[0] = 0; /* overwritten with 'T' */
417 buffer[1] = 'A'; /* overwritten with 0 */
418 buffer[2] = 'A'; /* unchanged */
419 buffer[3] = 0; /* unchanged */
420 iRet = pEnumMRUListA(hMRU, 0, buffer, 2);
421 ok(iRet == lstrlen(checks[3]), "EnumMRUList expected %d, got %d\n", lstrlen(checks[3]), iRet);
422 ok(strcmp(buffer, "T") == 0, "EnumMRUList expected %s, got %s\n", "T", buffer);
423 /* make sure space after buffer has old values */
424 ok(buffer[2] == 'A', "EnumMRUList expected %02x, got %02x\n", 'A', buffer[2]);
428 iRet = pEnumMRUListA(hMRU, 1, buffer, 255);
429 ok(iRet == lstrlen(checks[1]), "EnumMRUList expected %d, got %d\n", lstrlen(checks[1]), iRet);
430 ok(strcmp(buffer, checks[1]) == 0, "EnumMRUList expected %s, got %s\n", checks[1], buffer);
434 iRet = pEnumMRUListA(hMRU, 2, buffer, 255);
435 ok(iRet == lstrlen(checks[2]), "EnumMRUList expected %d, got %d\n", lstrlen(checks[2]), iRet);
436 ok(strcmp(buffer, checks[2]) == 0, "EnumMRUList expected %s, got %s\n", checks[2], buffer);
438 /* check out of bounds entry 3 */
439 strcpy(buffer, "dummy");
440 iRet = pEnumMRUListA(hMRU, 3, buffer, 255);
441 ok(iRet == -1, "EnumMRUList expected %d, got %d\n", -1, iRet);
442 ok(strcmp(buffer, "dummy") == 0, "EnumMRUList expected unchanged buffer %s, got %s\n", "dummy", buffer);
444 /* Finished with this MRU */
448 /* FreeMRUList(NULL) crashes on Win98 OSR0 */
456 static const create_lazya_t create_lazyA[] = {
457 {{ sizeof(MRUINFOA) + 1, 0, 0, HKEY_CURRENT_USER, NULL, NULL }, FALSE },
458 {{ sizeof(MRUINFOA) - 1, 0, 0, HKEY_CURRENT_USER, NULL, NULL }, FALSE },
459 {{ sizeof(MRUINFOA) + 1, 0, 0, HKEY_CURRENT_USER, "WineTest", NULL }, TRUE },
460 {{ sizeof(MRUINFOA) - 1, 0, 0, HKEY_CURRENT_USER, "WineTest", NULL }, TRUE },
461 {{ sizeof(MRUINFOA), 0, 0, HKEY_CURRENT_USER, "WineTest", NULL }, TRUE },
462 {{ sizeof(MRUINFOA), 0, 0, HKEY_CURRENT_USER, NULL, NULL }, FALSE },
463 {{ sizeof(MRUINFOA), 0, 0, NULL, "WineTest", NULL }, FALSE },
464 {{ 0, 0, 0, NULL, "WineTest", NULL }, FALSE },
465 {{ 0, 0, 0, HKEY_CURRENT_USER, "WineTest", NULL }, TRUE }
468 static void test_CreateMRUListLazyA(void)
472 if (!pCreateMRUListLazyA || !pFreeMRUList)
474 win_skip("CreateMRUListLazyA or FreeMRUList entry points not found\n");
478 for (i = 0; i < sizeof(create_lazyA)/sizeof(create_lazya_t); i++)
480 const create_lazya_t *ptr = &create_lazyA[i];
483 hMRU = pCreateMRUListLazyA((MRUINFOA*)&ptr->mruA, 0, 0, 0);
486 ok(hMRU != NULL, "%d: got %p\n", i, hMRU);
490 ok(hMRU == NULL, "%d: got %p\n", i, hMRU);
494 static void test_EnumMRUList(void)
496 if (!pEnumMRUListA || !pEnumMRUListW)
498 win_skip("EnumMRUListA/EnumMRUListW entry point not found\n");
505 /* crashes on NT4, passed on Win2k, XP, 2k3, Vista, 2k8 */
506 pEnumMRUListA(NULL, 0, NULL, 0);
507 pEnumMRUListW(NULL, 0, NULL, 0);
511 static void test_FindMRUData(void)
517 win_skip("FindMRUData entry point not found\n");
522 iRet = pFindMRUData(NULL, NULL, 0, NULL);
523 ok(iRet == -1, "FindMRUData expected -1, got %d\n", iRet);
526 static void test_AddMRUData(void)
532 win_skip("AddMRUData entry point not found\n");
537 iRet = pFindMRUData(NULL, NULL, 0, NULL);
538 ok(iRet == -1, "AddMRUData expected -1, got %d\n", iRet);
541 static void test_CreateMRUListW(void)
543 static const WCHAR mrutestW[] = {'M','R','U','T','e','s','t',0};
549 if (!pCreateMRUListW)
551 win_skip("CreateMRUListW entry point not found\n");
555 /* exported by name too on recent versions */
556 named = GetProcAddress(hComctl32, "CreateMRUListW");
558 ok(named == pCreateMRUListW, "got %p, expected %p\n", named, pCreateMRUListW);
560 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEYA, &hKey),
561 "Couldn't create test key \"%s\"\n", REG_TEST_KEYA);
563 infoW.cbSize = sizeof(infoW);
566 infoW.lpszSubKey = mrutestW;
568 infoW.lpfnCompare = NULL;
570 hMru = pCreateMRUListW(&infoW);
571 ok(hMru != NULL, "got %p\n", hMru);
575 infoW.cbSize = sizeof(infoW) - 1;
578 infoW.lpszSubKey = mrutestW;
580 infoW.lpfnCompare = NULL;
582 hMru = pCreateMRUListW(&infoW);
583 ok(hMru != NULL, "got %p\n", hMru);
587 infoW.cbSize = sizeof(infoW) + 1;
590 infoW.lpszSubKey = mrutestW;
592 infoW.lpfnCompare = NULL;
594 hMru = pCreateMRUListW(&infoW);
595 ok(hMru != NULL, "got %p\n", hMru);
602 infoW.lpszSubKey = mrutestW;
604 infoW.lpfnCompare = NULL;
606 hMru = pCreateMRUListW(&infoW);
607 ok(hMru != NULL, "got %p\n", hMru);
611 infoW.cbSize = sizeof(infoW);
614 infoW.lpszSubKey = mrutestW;
616 infoW.lpfnCompare = NULL;
618 hMru = pCreateMRUListW(&infoW);
619 ok(hMru == NULL, "got %p\n", hMru);
624 static void test_CreateMRUListLazyW(void)
626 static const WCHAR mrutestW[] = {'M','R','U','T','e','s','t',0};
632 if (!pCreateMRUListLazyW)
634 win_skip("CreateMRUListLazyW entry point not found\n");
638 /* check that it's not exported by name */
639 named = GetProcAddress(hComctl32, "CreateMRUListLazyW");
640 ok(named == NULL, "got %p\n", named);
642 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEYA, &hKey),
643 "Couldn't create test key \"%s\"\n", REG_TEST_KEYA);
645 infoW.cbSize = sizeof(infoW);
648 infoW.lpszSubKey = mrutestW;
650 infoW.lpfnCompare = NULL;
652 hMru = pCreateMRUListLazyW(&infoW, 0, 0, 0);
653 ok(hMru != NULL, "got %p\n", hMru);
657 infoW.cbSize = sizeof(infoW) - 1;
660 infoW.lpszSubKey = mrutestW;
662 infoW.lpfnCompare = NULL;
664 hMru = pCreateMRUListLazyW(&infoW, 0, 0, 0);
665 ok(hMru != NULL, "got %p\n", hMru);
669 infoW.cbSize = sizeof(infoW) + 1;
672 infoW.lpszSubKey = mrutestW;
674 infoW.lpfnCompare = NULL;
676 hMru = pCreateMRUListLazyW(&infoW, 0, 0, 0);
677 ok(hMru != NULL, "got %p\n", hMru);
684 infoW.lpszSubKey = mrutestW;
686 infoW.lpfnCompare = NULL;
688 hMru = pCreateMRUListLazyW(&infoW, 0, 0, 0);
689 ok(hMru != NULL, "got %p\n", hMru);
693 infoW.cbSize = sizeof(infoW);
696 infoW.lpszSubKey = mrutestW;
698 infoW.lpfnCompare = NULL;
700 hMru = pCreateMRUListLazyW(&infoW, 0, 0, 0);
701 ok(hMru == NULL, "got %p\n", hMru);
708 hComctl32 = GetModuleHandleA("comctl32.dll");
710 delete_reg_entries();
711 if (!create_reg_entries())
717 test_CreateMRUListLazyA();
718 test_CreateMRUListLazyW();
722 test_CreateMRUListW();
724 delete_reg_entries();