shell32/autocomplete: Remove redundant memory initialization.
[wine] / dlls / shell32 / tests / autocomplete.c
1 /*
2  * Tests for autocomplete
3  *
4  * Copyright 2008 Jan de Mooij
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 #define COBJMACROS
22
23 #include <wine/test.h>
24 #include <stdarg.h>
25
26 #include "windows.h"
27 #include "shobjidl.h"
28 #include "shlguid.h"
29 #include "initguid.h"
30 #include "shldisp.h"
31
32 static HWND hMainWnd, hEdit;
33 static HINSTANCE hinst;
34 static int killfocus_count;
35
36 static void test_invalid_init(void)
37 {
38     HRESULT hr;
39     IAutoComplete *ac;
40     IUnknown *acSource;
41     HWND edit_control;
42
43     /* AutoComplete instance */
44     hr = CoCreateInstance(&CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER,
45                          &IID_IAutoComplete, (void **)&ac);
46     if (hr == REGDB_E_CLASSNOTREG)
47     {
48         win_skip("CLSID_AutoComplete is not registered\n");
49         return;
50     }
51     ok(hr == S_OK, "no IID_IAutoComplete (0x%08x)\n", hr);
52
53     /* AutoComplete source */
54     hr = CoCreateInstance(&CLSID_ACLMulti, NULL, CLSCTX_INPROC_SERVER,
55                         &IID_IACList, (void **)&acSource);
56     if (hr == REGDB_E_CLASSNOTREG)
57     {
58         win_skip("CLSID_ACLMulti is not registered\n");
59         IAutoComplete_Release(ac);
60         return;
61     }
62     ok(hr == S_OK, "no IID_IACList (0x%08x)\n", hr);
63
64     edit_control = CreateWindowExA(0, "EDIT", "Some text", 0, 10, 10, 300, 300,
65                        hMainWnd, NULL, hinst, NULL);
66     ok(edit_control != NULL, "Can't create edit control\n");
67
68     /* The refcount of acSource would be incremented on older Windows. */
69     hr = IAutoComplete_Init(ac, NULL, acSource, NULL, NULL);
70     ok(hr == E_INVALIDARG ||
71        broken(hr == S_OK), /* Win2k/XP/Win2k3 */
72        "Init returned 0x%08x\n", hr);
73     if (hr == E_INVALIDARG)
74     {
75         LONG ref;
76
77         IUnknown_AddRef(acSource);
78         ref = IUnknown_Release(acSource);
79         ok(ref == 1, "Expected AutoComplete source refcount to be 1, got %d\n", ref);
80     }
81
82 if (0)
83 {
84     /* Older Windows versions never check the window handle, while newer
85      * versions only check for NULL. Subsequent attempts to initialize the
86      * object after this call succeeds would fail, because initialization
87      * state is determined by whether a non-NULL window handle is stored. */
88     hr = IAutoComplete_Init(ac, (HWND)0xdeadbeef, acSource, NULL, NULL);
89     ok(hr == S_OK, "Init returned 0x%08x\n", hr);
90
91     /* Tests crash on older Windows. */
92     hr = IAutoComplete_Init(ac, NULL, NULL, NULL, NULL);
93     ok(hr == E_INVALIDARG, "Init returned 0x%08x\n", hr);
94
95     hr = IAutoComplete_Init(ac, edit_control, NULL, NULL, NULL);
96     ok(hr == E_INVALIDARG, "Init returned 0x%08x\n", hr);
97 }
98
99     /* bind to edit control */
100     hr = IAutoComplete_Init(ac, edit_control, acSource, NULL, NULL);
101     ok(hr == S_OK, "Init returned 0x%08x\n", hr);
102
103     /* try invalid parameters after successful initialization .*/
104     hr = IAutoComplete_Init(ac, NULL, NULL, NULL, NULL);
105     ok(hr == E_INVALIDARG ||
106        hr == E_FAIL, /* Win2k/XP/Win2k3 */
107        "Init returned 0x%08x\n", hr);
108
109     hr = IAutoComplete_Init(ac, NULL, acSource, NULL, NULL);
110     ok(hr == E_INVALIDARG ||
111        hr == E_FAIL, /* Win2k/XP/Win2k3 */
112        "Init returned 0x%08x\n", hr);
113
114     hr = IAutoComplete_Init(ac, edit_control, NULL, NULL, NULL);
115     ok(hr == E_INVALIDARG ||
116        hr == E_FAIL, /* Win2k/XP/Win2k3 */
117        "Init returned 0x%08x\n", hr);
118
119     /* try initializing twice on the same control */
120     hr = IAutoComplete_Init(ac, edit_control, acSource, NULL, NULL);
121     ok(hr == E_FAIL, "Init returned 0x%08x\n", hr);
122
123     /* try initializing with a different control */
124     hr = IAutoComplete_Init(ac, hEdit, acSource, NULL, NULL);
125     ok(hr == E_FAIL, "Init returned 0x%08x\n", hr);
126
127     DestroyWindow(edit_control);
128
129     /* try initializing with a different control after
130      * destroying the original initialization control */
131     hr = IAutoComplete_Init(ac, hEdit, acSource, NULL, NULL);
132     ok(hr == E_UNEXPECTED ||
133        hr == E_FAIL, /* Win2k/XP/Win2k3 */
134        "Init returned 0x%08x\n", hr);
135
136     IUnknown_Release(acSource);
137     IAutoComplete_Release(ac);
138 }
139 static IAutoComplete *test_init(void)
140 {
141     HRESULT r;
142     IAutoComplete *ac;
143     IUnknown *acSource;
144     LONG_PTR user_data;
145
146     /* AutoComplete instance */
147     r = CoCreateInstance(&CLSID_AutoComplete, NULL, CLSCTX_INPROC_SERVER,
148                          &IID_IAutoComplete, (LPVOID*)&ac);
149     if (r == REGDB_E_CLASSNOTREG)
150     {
151         win_skip("CLSID_AutoComplete is not registered\n");
152         return NULL;
153     }
154     ok(r == S_OK, "no IID_IAutoComplete (0x%08x)\n", r);
155
156     /* AutoComplete source */
157     r = CoCreateInstance(&CLSID_ACLMulti, NULL, CLSCTX_INPROC_SERVER,
158                         &IID_IACList, (LPVOID*)&acSource);
159     if (r == REGDB_E_CLASSNOTREG)
160     {
161         win_skip("CLSID_ACLMulti is not registered\n");
162         IAutoComplete_Release(ac);
163         return NULL;
164     }
165     ok(r == S_OK, "no IID_IACList (0x%08x)\n", r);
166
167     user_data = GetWindowLongPtrA(hEdit, GWLP_USERDATA);
168     ok(user_data == 0, "Expected the edit control user data to be zero\n");
169
170     /* bind to edit control */
171     r = IAutoComplete_Init(ac, hEdit, acSource, NULL, NULL);
172     ok(r == S_OK, "Init returned 0x%08x\n", r);
173
174     user_data = GetWindowLongPtrA(hEdit, GWLP_USERDATA);
175     ok(user_data == 0, "Expected the edit control user data to be zero\n");
176
177     IUnknown_Release(acSource);
178
179     return ac;
180 }
181
182 static void test_killfocus(void)
183 {
184     /* Test if WM_KILLFOCUS messages are handled properly by checking if
185      * the parent receives an EN_KILLFOCUS message. */
186     SetFocus(hEdit);
187     killfocus_count = 0;
188     SetFocus(0);
189     ok(killfocus_count == 1, "Expected one EN_KILLFOCUS message, got: %d\n", killfocus_count);
190 }
191
192 static LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
193 {
194     switch(msg) {
195     case WM_CREATE:
196         /* create edit control */
197         hEdit = CreateWindowEx(0, "EDIT", "Some text", 0, 10, 10, 300, 300,
198                     hWnd, NULL, hinst, NULL);
199         ok(hEdit != NULL, "Can't create edit control\n");
200         break;
201     case WM_COMMAND:
202         if(HIWORD(wParam) == EN_KILLFOCUS)
203             killfocus_count++;
204         break;
205     }
206     return DefWindowProcA(hWnd, msg, wParam, lParam);
207 }
208
209 static void createMainWnd(void)
210 {
211     WNDCLASSA wc;
212     wc.style = CS_HREDRAW | CS_VREDRAW;
213     wc.cbClsExtra = 0;
214     wc.cbWndExtra = 0;
215     wc.hInstance = GetModuleHandleA(NULL);
216     wc.hIcon = NULL;
217     wc.hCursor = LoadCursorA(NULL, IDC_IBEAM);
218     wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
219     wc.lpszMenuName = NULL;
220     wc.lpszClassName = "MyTestWnd";
221     wc.lpfnWndProc = MyWndProc;
222     RegisterClassA(&wc);
223
224     hMainWnd = CreateWindowExA(0, "MyTestWnd", "Blah", WS_OVERLAPPEDWINDOW,
225       CW_USEDEFAULT, CW_USEDEFAULT, 130, 105, NULL, NULL, GetModuleHandleA(NULL), 0);
226 }
227
228 START_TEST(autocomplete)
229 {
230     HRESULT r;
231     MSG msg;
232     IAutoComplete* ac;
233
234     r = CoInitialize(NULL);
235     ok(r == S_OK, "CoInitialize failed (0x%08x). Tests aborted.\n", r);
236     if (r != S_OK)
237         return;
238
239     createMainWnd();
240     ok(hMainWnd != NULL, "Failed to create parent window. Tests aborted.\n");
241     if (!hMainWnd) return;
242
243     test_invalid_init();
244     ac = test_init();
245     if (!ac)
246         goto cleanup;
247     test_killfocus();
248
249     PostQuitMessage(0);
250     while(GetMessageA(&msg,0,0,0)) {
251         TranslateMessage(&msg);
252         DispatchMessageA(&msg);
253     }
254
255     IAutoComplete_Release(ac);
256
257 cleanup:
258     DestroyWindow(hEdit);
259     DestroyWindow(hMainWnd);
260
261     CoUninitialize();
262 }