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