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