user32: Get the graphics driver name from the desktop window registry entry.
[wine.git] / dlls / riched20 / tests / richole.c
blob63208eeb10d3d001dbea800b6e80706c2ae1d116
1 /*
2 * Tests for IRichEditOle and friends.
4 * Copyright 2008 Google (Dan Hipschman)
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.
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.
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
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include <windef.h>
26 #include <winbase.h>
27 #include <wingdi.h>
28 #include <winuser.h>
29 #include <initguid.h>
30 #include <ole2.h>
31 #include <richedit.h>
32 #include <richole.h>
33 #include <tom.h>
34 #include <wine/test.h>
36 static HMODULE hmoduleRichEdit;
38 static HWND new_window(LPCSTR lpClassName, DWORD dwStyle, HWND parent)
40 HWND hwnd = CreateWindowA(lpClassName, NULL,
41 dwStyle | WS_POPUP | WS_HSCROLL | WS_VSCROLL | WS_VISIBLE,
42 0, 0, 200, 60, parent, NULL, hmoduleRichEdit, NULL);
43 return hwnd;
46 static HWND new_richedit(HWND parent)
48 return new_window(RICHEDIT_CLASS20A, ES_MULTILINE, parent);
51 static BOOL touch_file(LPCWSTR filename)
53 HANDLE file;
55 file = CreateFileW(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
56 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
58 if(file == INVALID_HANDLE_VALUE)
59 return FALSE;
60 CloseHandle(file);
61 return TRUE;
64 static BOOL is_existing_file(LPCWSTR filename)
66 HANDLE file;
68 file = CreateFileW(filename, GENERIC_READ, 0, NULL,
69 OPEN_EXISTING, 0, NULL);
70 if(file == INVALID_HANDLE_VALUE)
71 return FALSE;
72 CloseHandle(file);
73 return TRUE;
76 static void create_interfaces(HWND *w, IRichEditOle **reOle, ITextDocument **txtDoc,
77 ITextSelection **txtSel)
79 *w = new_richedit(NULL);
80 SendMessageA(*w, EM_GETOLEINTERFACE, 0, (LPARAM)reOle);
81 IRichEditOle_QueryInterface(*reOle, &IID_ITextDocument,
82 (void **) txtDoc);
83 ITextDocument_GetSelection(*txtDoc, txtSel);
86 static void release_interfaces(HWND *w, IRichEditOle **reOle, ITextDocument **txtDoc,
87 ITextSelection **txtSel)
89 ITextDocument_Release(*txtDoc);
90 IRichEditOle_Release(*reOle);
91 DestroyWindow(*w);
92 ITextSelection_Release(*txtSel);
95 static void test_Interfaces(void)
97 IRichEditOle *reOle = NULL;
98 ITextDocument *txtDoc = NULL;
99 ITextSelection *txtSel = NULL;
100 IUnknown *punk;
101 HRESULT hres;
102 LRESULT res;
103 HWND w;
105 w = new_richedit(NULL);
106 if (!w) {
107 skip("Couldn't create window\n");
108 return;
111 res = SendMessageA(w, EM_GETOLEINTERFACE, 0, (LPARAM)&reOle);
112 ok(res, "SendMessage\n");
113 ok(reOle != NULL, "EM_GETOLEINTERFACE\n");
115 hres = IRichEditOle_QueryInterface(reOle, &IID_ITextDocument,
116 (void **) &txtDoc);
117 ok(hres == S_OK, "IRichEditOle_QueryInterface\n");
118 ok(txtDoc != NULL, "IRichEditOle_QueryInterface\n");
120 ITextDocument_GetSelection(txtDoc, &txtSel);
122 punk = NULL;
123 hres = ITextSelection_QueryInterface(txtSel, &IID_ITextSelection, (void **) &punk);
124 ok(hres == S_OK, "ITextSelection_QueryInterface\n");
125 ok(punk != NULL, "ITextSelection_QueryInterface\n");
126 IUnknown_Release(punk);
128 punk = NULL;
129 hres = ITextSelection_QueryInterface(txtSel, &IID_ITextRange, (void **) &punk);
130 ok(hres == S_OK, "ITextSelection_QueryInterface\n");
131 ok(punk != NULL, "ITextSelection_QueryInterface\n");
132 IUnknown_Release(punk);
134 punk = NULL;
135 hres = ITextSelection_QueryInterface(txtSel, &IID_IDispatch, (void **) &punk);
136 ok(hres == S_OK, "ITextSelection_QueryInterface\n");
137 ok(punk != NULL, "ITextSelection_QueryInterface\n");
138 IUnknown_Release(punk);
140 ITextDocument_Release(txtDoc);
141 IRichEditOle_Release(reOle);
142 DestroyWindow(w);
144 /* Methods should return CO_E_RELEASED if the backing document has
145 been released. One test should suffice. */
146 hres = ITextSelection_CanEdit(txtSel, NULL);
147 ok(hres == CO_E_RELEASED, "ITextSelection after ITextDocument destroyed\n");
149 ITextSelection_Release(txtSel);
152 static void test_ITextDocument_Open(void)
154 IRichEditOle *reOle = NULL;
155 ITextDocument *txtDoc = NULL;
156 ITextSelection *txtSel = NULL;
157 HRESULT hres;
158 HWND w;
159 HANDLE hFile;
160 VARIANT testfile;
161 WCHAR filename[] = {'t', 'e', 's', 't','.','t','x','t', 0};
163 static const int tomConstantsSingle[] =
165 tomReadOnly, tomShareDenyRead, tomShareDenyWrite,
166 tomCreateAlways, tomOpenExisting, tomOpenAlways,
167 tomTruncateExisting, tomRTF, tomText
170 static const int tomConstantsMulti[] =
172 tomReadOnly|tomShareDenyRead|tomPasteFile, tomReadOnly|tomPasteFile,
173 tomReadOnly|tomShareDenyWrite|tomPasteFile,
174 tomReadOnly|tomShareDenyRead|tomShareDenyWrite|tomPasteFile, tomShareDenyWrite|tomPasteFile,
175 tomShareDenyRead|tomShareDenyWrite|tomPasteFile, tomShareDenyRead|tomPasteFile,
176 tomShareDenyRead|tomShareDenyWrite, tomReadOnly|tomShareDenyRead|tomShareDenyWrite,
177 tomReadOnly|tomShareDenyWrite, tomReadOnly|tomShareDenyRead
180 int tomNumSingle = sizeof(tomConstantsSingle)/sizeof(tomConstantsSingle[0]);
181 int tomNumMulti = sizeof(tomConstantsMulti)/sizeof(tomConstantsMulti[0]);
182 int i;
183 VariantInit(&testfile);
184 V_VT(&testfile) = VT_BSTR;
185 V_BSTR(&testfile) = SysAllocString(filename);
187 for(i=0; i < tomNumSingle; i++)
189 touch_file(filename);
190 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
191 hres = ITextDocument_Open(txtDoc, &testfile, tomConstantsSingle[i], CP_ACP);
192 todo_wine ok(hres == S_OK, "ITextDocument_Open: Filename:test.txt Flags:0x%x Codepage:CP_ACP hres:0x%x\n",
193 tomConstantsSingle[i], hres);
194 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
195 DeleteFileW(filename);
197 touch_file(filename);
198 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
199 hres = ITextDocument_Open(txtDoc, &testfile, tomConstantsSingle[i], CP_UTF8);
200 todo_wine ok(hres == S_OK, "ITextDocument_Open: Filename:test.txt Flags:0x%x Codepage:CP_UTF8 hres:0x%x\n",
201 tomConstantsSingle[i], hres);
202 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
203 DeleteFileW(filename);
206 for(i=0; i < tomNumMulti; i++)
208 touch_file(filename);
209 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
210 hres = ITextDocument_Open(txtDoc, &testfile, tomConstantsMulti[i], CP_ACP);
211 todo_wine ok(hres == S_OK, "ITextDocument_Open: Filename:test.txt Flags:0x%x Codepage:CP_ACP hres:0x%x\n",
212 tomConstantsMulti[i], hres);
213 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
214 DeleteFileW(filename);
216 touch_file(filename);
217 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
218 hres = ITextDocument_Open(txtDoc, &testfile, tomConstantsMulti[i], CP_UTF8);
219 todo_wine ok(hres == S_OK, "ITextDocument_Open: Filename:test.txt Flags:0x%x Codepage:CP_UTF8 hres:0x%x\n",
220 tomConstantsMulti[i], hres);
221 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
222 DeleteFileW(filename);
225 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
226 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateAlways, CP_ACP);
227 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_ACP\n");
228 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
229 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
230 DeleteFileW(filename);
232 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
233 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateAlways, CP_UTF8);
234 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_UTF8\n");
235 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
236 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
237 DeleteFileW(filename);
239 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
240 hres = ITextDocument_Open(txtDoc, &testfile, tomOpenAlways, CP_ACP);
241 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_ACP\n");
242 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
243 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
244 DeleteFileW(filename);
246 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
247 hres = ITextDocument_Open(txtDoc, &testfile, tomOpenAlways, CP_UTF8);
248 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_UTF8\n");
249 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
250 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
251 DeleteFileW(filename);
253 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
254 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateNew, CP_ACP);
255 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_ACP\n");
256 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
257 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
258 DeleteFileW(filename);
260 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
261 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateNew, CP_UTF8);
262 todo_wine ok(hres == S_OK, "ITextDocument_Open should success Codepage:CP_UTF8\n");
263 todo_wine ok(is_existing_file(filename), "ITextDocument_Open should create a file\n");
264 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
265 DeleteFileW(filename);
267 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
268 touch_file(filename);
269 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateNew, CP_ACP);
270 todo_wine ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "ITextDocument_Open should fail Codepage:CP_ACP\n");
271 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
272 DeleteFileW(filename);
274 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
275 touch_file(filename);
276 hres = ITextDocument_Open(txtDoc, &testfile, tomCreateNew, CP_UTF8);
277 todo_wine ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_EXISTS), "ITextDocument_Open should fail Codepage:CP_UTF8\n");
278 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
279 DeleteFileW(filename);
281 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
282 hres = ITextDocument_Open(txtDoc, &testfile, tomOpenExisting, CP_ACP);
283 todo_wine ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "ITextDocument_Open should fail Codepage:CP_ACP\n");
284 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
286 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
287 hres = ITextDocument_Open(txtDoc, &testfile, tomOpenExisting, CP_UTF8);
288 todo_wine ok(hres == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "ITextDocument_Open should fail Codepage:CP_UTF8\n");
289 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
291 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
292 DeleteFileW(filename);
293 ITextDocument_Open(txtDoc, &testfile, tomText, CP_ACP);
294 todo_wine ok(is_existing_file(filename) == TRUE, "a file should be created default\n");
295 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
296 DeleteFileW(filename);
298 /* test of share mode */
299 touch_file(filename);
300 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
301 ITextDocument_Open(txtDoc, &testfile, tomShareDenyRead, CP_ACP);
302 SetLastError(0xdeadbeef);
303 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
304 FILE_ATTRIBUTE_NORMAL, NULL);
305 todo_wine ok(GetLastError() == ERROR_SHARING_VIOLATION, "ITextDocument_Open should fail\n");
306 CloseHandle(hFile);
307 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
308 DeleteFileW(filename);
310 touch_file(filename);
311 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
312 ITextDocument_Open(txtDoc, &testfile, tomShareDenyWrite, CP_ACP);
313 SetLastError(0xdeadbeef);
314 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
315 FILE_ATTRIBUTE_NORMAL, NULL);
316 todo_wine ok(GetLastError() == ERROR_SHARING_VIOLATION, "ITextDocument_Open should fail\n");
317 CloseHandle(hFile);
318 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
319 DeleteFileW(filename);
321 touch_file(filename);
322 create_interfaces(&w, &reOle, &txtDoc, &txtSel);
323 SetLastError(0xdeadbeef);
324 ITextDocument_Open(txtDoc, &testfile, tomShareDenyWrite|tomShareDenyRead, CP_ACP);
325 hFile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
326 FILE_ATTRIBUTE_NORMAL, NULL);
327 todo_wine ok(GetLastError() == ERROR_SHARING_VIOLATION, "ITextDocument_Open should fail\n");
328 CloseHandle(hFile);
329 release_interfaces(&w, &reOle, &txtDoc, &txtSel);
330 DeleteFileW(filename);
333 START_TEST(richole)
335 /* Must explicitly LoadLibrary(). The test has no references to functions in
336 * RICHED20.DLL, so the linker doesn't actually link to it. */
337 hmoduleRichEdit = LoadLibraryA("riched20.dll");
338 ok(hmoduleRichEdit != NULL, "error: %d\n", (int) GetLastError());
340 test_Interfaces();
341 test_ITextDocument_Open();