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