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