[DSOUND]
[reactos.git] / rostests / winetests / comdlg32 / finddlg.c
1 /*
2 * Unit test suite for comdlg32 API functions: find/replace dialogs
3 *
4 * Copyright 2010 by Dylan Smith
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
22 //#include "windows.h"
23
24 #define WIN32_NO_STATUS
25
26 #include <wine/test.h>
27
28 #include <wingdi.h>
29 #include <winuser.h>
30 #include <cderr.h>
31 #include <commdlg.h>
32
33 static UINT ID_FINDMSGSTRING;
34
35 static LRESULT handle_findmsg(FINDREPLACEA *fr)
36 {
37 return 0;
38 }
39
40 static LRESULT CALLBACK OwnerWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
41 {
42 if(msg == ID_FINDMSGSTRING) {
43 return handle_findmsg((FINDREPLACEA*)lParam);
44 }
45 return DefWindowProcA(hwnd, msg, wParam, lParam);
46 }
47
48 static void test_param_check(void)
49 {
50 char findbuffer[64];
51 char replacebuffer[64];
52 FINDREPLACEA fr, *pFr;
53 WNDCLASSA wc;
54
55 ZeroMemory(&wc, sizeof(wc));
56 wc.lpfnWndProc = OwnerWndProc;
57 wc.lpszClassName = "test_param_check";
58 RegisterClassA(&wc);
59
60 #define CHECK_FIND_OR_REPLACE(FUNC, FAIL, ERR_CODE) \
61 do { \
62 HWND hwnd = FUNC(pFr); \
63 BOOL is_ok = !!hwnd == !FAIL; \
64 ok(is_ok, "%s should%s fail\n", #FUNC, FAIL ? "" : "n't"); \
65 if (FAIL && is_ok) { \
66 DWORD ext_err = CommDlgExtendedError(); \
67 ok(ext_err == ERR_CODE, "expected err %x got %x\n", \
68 ERR_CODE, ext_err); \
69 } else { \
70 DestroyWindow(hwnd); \
71 } \
72 } while (0)
73
74 #define CHECK_FIND_FAIL(ERR_CODE) \
75 CHECK_FIND_OR_REPLACE(FindTextA, TRUE, ERR_CODE)
76
77 #define CHECK_FIND_SUCCEED() \
78 CHECK_FIND_OR_REPLACE(FindTextA, FALSE, 0)
79
80 #define CHECK_REPLACE_FAIL(ERR_CODE) \
81 CHECK_FIND_OR_REPLACE(ReplaceTextA, TRUE, ERR_CODE)
82
83 #define CHECK_REPLACE_SUCCEED() \
84 CHECK_FIND_OR_REPLACE(ReplaceTextA, FALSE, 0)
85
86 #define CHECK_FINDREPLACE_FAIL(ERR_CODE) \
87 do { \
88 CHECK_FIND_FAIL(ERR_CODE); \
89 CHECK_REPLACE_FAIL(ERR_CODE); \
90 } while (0)
91
92 pFr = NULL;
93 CHECK_FINDREPLACE_FAIL(CDERR_INITIALIZATION);
94 pFr = &fr;
95
96 ZeroMemory(&fr, sizeof(fr));
97 /* invalid lStructSize (0) */
98 CHECK_FINDREPLACE_FAIL(CDERR_STRUCTSIZE);
99 fr.lStructSize = sizeof(fr);
100
101 /* invalid hwndOwner (NULL) */
102 CHECK_FINDREPLACE_FAIL(CDERR_DIALOGFAILURE);
103 fr.hwndOwner = CreateWindowA(wc.lpszClassName, NULL, WS_VISIBLE, 0, 0, 200, 100,
104 NULL, NULL, GetModuleHandleA(NULL), NULL);
105
106 /* invalid wFindWhatLen (0) */
107 CHECK_FINDREPLACE_FAIL(FRERR_BUFFERLENGTHZERO);
108 fr.wFindWhatLen = sizeof(findbuffer);
109
110 /* invalid lpstrFindWhat (NULL) */
111 CHECK_FINDREPLACE_FAIL(FRERR_BUFFERLENGTHZERO);
112 fr.lpstrFindWhat = findbuffer;
113 strcpy(findbuffer, "abc");
114
115 /* invalid lpstrReplaceWith (NULL) for ReplaceText */
116 CHECK_FIND_SUCCEED();
117 CHECK_REPLACE_FAIL(FRERR_BUFFERLENGTHZERO);
118 fr.lpstrReplaceWith = replacebuffer;
119 strcpy(replacebuffer, "def");
120
121 /* wReplaceWithLen may be 0, even for ReplaceText */
122 CHECK_FIND_SUCCEED();
123 CHECK_REPLACE_SUCCEED();
124 fr.wReplaceWithLen = sizeof(replacebuffer);
125
126 /* invalid lpfnHook (NULL) when Flags has FR_ENABLEHOOK */
127 fr.Flags = FR_ENABLEHOOK;
128 CHECK_FINDREPLACE_FAIL(CDERR_NOHOOK);
129
130 /* invalid hInstance (NULL)
131 * when Flags has FR_ENABLETEMPLATE or FR_ENABLETEMPLATEHANDLE */
132 fr.Flags = FR_ENABLETEMPLATE;
133 CHECK_FINDREPLACE_FAIL(CDERR_FINDRESFAILURE);
134 fr.Flags = FR_ENABLETEMPLATEHANDLE;
135 CHECK_FINDREPLACE_FAIL(CDERR_NOHINSTANCE);
136 fr.hInstance = GetModuleHandleA(NULL);
137
138 /* invalid lpTemplateName (NULL) when Flags has FR_ENABLETEMPLATE */
139 fr.Flags = FR_ENABLETEMPLATE;
140 CHECK_FINDREPLACE_FAIL(CDERR_FINDRESFAILURE);
141 fr.Flags = 0;
142
143 CHECK_FIND_SUCCEED();
144 CHECK_REPLACE_SUCCEED();
145
146 DestroyWindow(fr.hwndOwner);
147 }
148
149 START_TEST(finddlg)
150 {
151 ID_FINDMSGSTRING = RegisterWindowMessageA(FINDMSGSTRINGA);
152
153 test_param_check();
154 }