ole32/tests: OleGetClipboard doesn't return the same object under win9x, winme and...
[wine/hacks.git] / dlls / ole32 / tests / clipboard.c
bloba377801683ef955ec716dedd93fd2e90bf7d210a
1 /*
2 * Clipboard unit tests
4 * Copyright 2006 Kevin Koltzau
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
22 #define NONAMELESSUNION
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "objbase.h"
31 #include "wine/test.h"
33 #define InitFormatEtc(fe, cf, med) \
35 (fe).cfFormat=cf;\
36 (fe).dwAspect=DVASPECT_CONTENT;\
37 (fe).ptd=NULL;\
38 (fe).tymed=med;\
39 (fe).lindex=-1;\
42 static inline char *dump_fmtetc(FORMATETC *fmt)
44 static char buf[100];
46 snprintf(buf, sizeof(buf), "cf %04x ptd %p aspect %x lindex %d tymed %x",
47 fmt->cfFormat, fmt->ptd, fmt->dwAspect, fmt->lindex, fmt->tymed);
48 return buf;
51 typedef struct DataObjectImpl {
52 const IDataObjectVtbl *lpVtbl;
53 LONG ref;
55 FORMATETC *fmtetc;
56 UINT fmtetc_cnt;
58 HANDLE text;
59 IStream *stm;
60 IStorage *stg;
61 } DataObjectImpl;
63 typedef struct EnumFormatImpl {
64 const IEnumFORMATETCVtbl *lpVtbl;
65 LONG ref;
67 FORMATETC *fmtetc;
68 UINT fmtetc_cnt;
70 UINT cur;
71 } EnumFormatImpl;
73 static BOOL expect_DataObjectImpl_QueryGetData = TRUE;
74 static ULONG DataObjectImpl_GetData_calls = 0;
75 static ULONG DataObjectImpl_GetDataHere_calls = 0;
76 static ULONG DataObjectImpl_EnumFormatEtc_calls = 0;
78 static UINT cf_stream, cf_storage, cf_global, cf_another, cf_onemore;
80 static HRESULT EnumFormatImpl_Create(FORMATETC *fmtetc, UINT size, LPENUMFORMATETC *lplpformatetc);
82 static HRESULT WINAPI EnumFormatImpl_QueryInterface(IEnumFORMATETC *iface, REFIID riid, LPVOID *ppvObj)
84 EnumFormatImpl *This = (EnumFormatImpl*)iface;
86 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumFORMATETC)) {
87 IEnumFORMATETC_AddRef(iface);
88 *ppvObj = This;
89 return S_OK;
91 *ppvObj = NULL;
92 return E_NOINTERFACE;
95 static ULONG WINAPI EnumFormatImpl_AddRef(IEnumFORMATETC *iface)
97 EnumFormatImpl *This = (EnumFormatImpl*)iface;
98 LONG ref = InterlockedIncrement(&This->ref);
99 return ref;
102 static ULONG WINAPI EnumFormatImpl_Release(IEnumFORMATETC *iface)
104 EnumFormatImpl *This = (EnumFormatImpl*)iface;
105 ULONG ref = InterlockedDecrement(&This->ref);
107 if(!ref) {
108 HeapFree(GetProcessHeap(), 0, This->fmtetc);
109 HeapFree(GetProcessHeap(), 0, This);
112 return ref;
115 static HRESULT WINAPI EnumFormatImpl_Next(IEnumFORMATETC *iface, ULONG celt,
116 FORMATETC *rgelt, ULONG *pceltFetched)
118 EnumFormatImpl *This = (EnumFormatImpl*)iface;
119 ULONG count, i;
121 if(!rgelt)
122 return E_INVALIDARG;
124 count = min(celt, This->fmtetc_cnt - This->cur);
125 for(i = 0; i < count; i++, This->cur++, rgelt++)
127 *rgelt = This->fmtetc[This->cur];
128 if(rgelt->ptd)
130 DWORD size = This->fmtetc[This->cur].ptd->tdSize;
131 rgelt->ptd = CoTaskMemAlloc(size);
132 memcpy(rgelt->ptd, This->fmtetc[This->cur].ptd, size);
135 if(pceltFetched)
136 *pceltFetched = count;
137 return count == celt ? S_OK : S_FALSE;
140 static HRESULT WINAPI EnumFormatImpl_Skip(IEnumFORMATETC *iface, ULONG celt)
142 ok(0, "unexpected call\n");
143 return E_NOTIMPL;
146 static HRESULT WINAPI EnumFormatImpl_Reset(IEnumFORMATETC *iface)
148 EnumFormatImpl *This = (EnumFormatImpl*)iface;
150 This->cur = 0;
151 return S_OK;
154 static HRESULT WINAPI EnumFormatImpl_Clone(IEnumFORMATETC *iface, IEnumFORMATETC **ppenum)
156 ok(0, "unexpected call\n");
157 return E_NOTIMPL;
160 static const IEnumFORMATETCVtbl VT_EnumFormatImpl = {
161 EnumFormatImpl_QueryInterface,
162 EnumFormatImpl_AddRef,
163 EnumFormatImpl_Release,
164 EnumFormatImpl_Next,
165 EnumFormatImpl_Skip,
166 EnumFormatImpl_Reset,
167 EnumFormatImpl_Clone
170 static HRESULT EnumFormatImpl_Create(FORMATETC *fmtetc, UINT fmtetc_cnt, IEnumFORMATETC **lplpformatetc)
172 EnumFormatImpl *ret;
174 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(EnumFormatImpl));
175 ret->lpVtbl = &VT_EnumFormatImpl;
176 ret->ref = 1;
177 ret->cur = 0;
178 ret->fmtetc_cnt = fmtetc_cnt;
179 ret->fmtetc = HeapAlloc(GetProcessHeap(), 0, fmtetc_cnt*sizeof(FORMATETC));
180 memcpy(ret->fmtetc, fmtetc, fmtetc_cnt*sizeof(FORMATETC));
181 *lplpformatetc = (LPENUMFORMATETC)ret;
182 return S_OK;
185 static HRESULT WINAPI DataObjectImpl_QueryInterface(IDataObject *iface, REFIID riid, LPVOID *ppvObj)
187 DataObjectImpl *This = (DataObjectImpl*)iface;
189 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IDataObject)) {
190 IDataObject_AddRef(iface);
191 *ppvObj = This;
192 return S_OK;
194 *ppvObj = NULL;
195 return E_NOINTERFACE;
198 static ULONG WINAPI DataObjectImpl_AddRef(IDataObject* iface)
200 DataObjectImpl *This = (DataObjectImpl*)iface;
201 ULONG ref = InterlockedIncrement(&This->ref);
202 return ref;
205 static ULONG WINAPI DataObjectImpl_Release(IDataObject* iface)
207 DataObjectImpl *This = (DataObjectImpl*)iface;
208 ULONG ref = InterlockedDecrement(&This->ref);
210 if(!ref)
212 int i;
213 if(This->text) GlobalFree(This->text);
214 for(i = 0; i < This->fmtetc_cnt; i++)
215 HeapFree(GetProcessHeap(), 0, This->fmtetc[i].ptd);
216 HeapFree(GetProcessHeap(), 0, This->fmtetc);
217 if(This->stm) IStream_Release(This->stm);
218 if(This->stg) IStorage_Release(This->stg);
219 HeapFree(GetProcessHeap(), 0, This);
222 return ref;
225 static HRESULT WINAPI DataObjectImpl_GetData(IDataObject* iface, FORMATETC *pformatetc, STGMEDIUM *pmedium)
227 DataObjectImpl *This = (DataObjectImpl*)iface;
228 UINT i;
229 BOOL foundFormat = FALSE;
231 trace("getdata: %s\n", dump_fmtetc(pformatetc));
233 DataObjectImpl_GetData_calls++;
235 if(pformatetc->lindex != -1)
236 return DV_E_FORMATETC;
238 for(i = 0; i < This->fmtetc_cnt; i++)
240 if(This->fmtetc[i].cfFormat == pformatetc->cfFormat)
242 foundFormat = TRUE;
243 if(This->fmtetc[i].tymed & pformatetc->tymed)
245 pmedium->pUnkForRelease = (LPUNKNOWN)iface;
246 IUnknown_AddRef(pmedium->pUnkForRelease);
248 if(pformatetc->cfFormat == CF_TEXT || pformatetc->cfFormat == cf_global)
250 pmedium->tymed = TYMED_HGLOBAL;
251 U(*pmedium).hGlobal = This->text;
253 else if(pformatetc->cfFormat == cf_stream)
255 pmedium->tymed = TYMED_ISTREAM;
256 IStream_AddRef(This->stm);
257 U(*pmedium).pstm = This->stm;
259 else if(pformatetc->cfFormat == cf_storage || pformatetc->cfFormat == cf_another)
261 pmedium->tymed = TYMED_ISTORAGE;
262 IStorage_AddRef(This->stg);
263 U(*pmedium).pstg = This->stg;
265 return S_OK;
270 return foundFormat ? DV_E_TYMED : DV_E_FORMATETC;
273 static HRESULT WINAPI DataObjectImpl_GetDataHere(IDataObject* iface, FORMATETC *pformatetc, STGMEDIUM *pmedium)
275 trace("getdatahere: %s\n", dump_fmtetc(pformatetc));
276 DataObjectImpl_GetDataHere_calls++;
278 return E_NOTIMPL;
281 static HRESULT WINAPI DataObjectImpl_QueryGetData(IDataObject* iface, FORMATETC *pformatetc)
283 DataObjectImpl *This = (DataObjectImpl*)iface;
284 UINT i;
285 BOOL foundFormat = FALSE;
287 trace("querygetdata: %s\n", dump_fmtetc(pformatetc));
288 if (!expect_DataObjectImpl_QueryGetData)
289 ok(0, "unexpected call to DataObjectImpl_QueryGetData\n");
291 if(pformatetc->lindex != -1)
292 return DV_E_LINDEX;
294 for(i=0; i<This->fmtetc_cnt; i++) {
295 if(This->fmtetc[i].cfFormat == pformatetc->cfFormat) {
296 foundFormat = TRUE;
297 if(This->fmtetc[i].tymed == pformatetc->tymed)
298 return S_OK;
301 return foundFormat?DV_E_FORMATETC:DV_E_TYMED;
304 static HRESULT WINAPI DataObjectImpl_GetCanonicalFormatEtc(IDataObject* iface, FORMATETC *pformatectIn,
305 FORMATETC *pformatetcOut)
307 ok(0, "unexpected call\n");
308 return E_NOTIMPL;
311 static HRESULT WINAPI DataObjectImpl_SetData(IDataObject* iface, FORMATETC *pformatetc,
312 STGMEDIUM *pmedium, BOOL fRelease)
314 ok(0, "unexpected call\n");
315 return E_NOTIMPL;
318 static HRESULT WINAPI DataObjectImpl_EnumFormatEtc(IDataObject* iface, DWORD dwDirection,
319 IEnumFORMATETC **ppenumFormatEtc)
321 DataObjectImpl *This = (DataObjectImpl*)iface;
323 DataObjectImpl_EnumFormatEtc_calls++;
325 if(dwDirection != DATADIR_GET) {
326 ok(0, "unexpected direction %d\n", dwDirection);
327 return E_NOTIMPL;
329 return EnumFormatImpl_Create(This->fmtetc, This->fmtetc_cnt, ppenumFormatEtc);
332 static HRESULT WINAPI DataObjectImpl_DAdvise(IDataObject* iface, FORMATETC *pformatetc, DWORD advf,
333 IAdviseSink *pAdvSink, DWORD *pdwConnection)
335 ok(0, "unexpected call\n");
336 return E_NOTIMPL;
339 static HRESULT WINAPI DataObjectImpl_DUnadvise(IDataObject* iface, DWORD dwConnection)
341 ok(0, "unexpected call\n");
342 return E_NOTIMPL;
345 static HRESULT WINAPI DataObjectImpl_EnumDAdvise(IDataObject* iface, IEnumSTATDATA **ppenumAdvise)
347 ok(0, "unexpected call\n");
348 return E_NOTIMPL;
351 static const IDataObjectVtbl VT_DataObjectImpl =
353 DataObjectImpl_QueryInterface,
354 DataObjectImpl_AddRef,
355 DataObjectImpl_Release,
356 DataObjectImpl_GetData,
357 DataObjectImpl_GetDataHere,
358 DataObjectImpl_QueryGetData,
359 DataObjectImpl_GetCanonicalFormatEtc,
360 DataObjectImpl_SetData,
361 DataObjectImpl_EnumFormatEtc,
362 DataObjectImpl_DAdvise,
363 DataObjectImpl_DUnadvise,
364 DataObjectImpl_EnumDAdvise
367 static HRESULT DataObjectImpl_CreateText(LPCSTR text, LPDATAOBJECT *lplpdataobj)
369 DataObjectImpl *obj;
371 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(DataObjectImpl));
372 obj->lpVtbl = &VT_DataObjectImpl;
373 obj->ref = 1;
374 obj->text = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1);
375 strcpy(GlobalLock(obj->text), text);
376 GlobalUnlock(obj->text);
377 obj->stm = NULL;
378 obj->stg = NULL;
380 obj->fmtetc_cnt = 1;
381 obj->fmtetc = HeapAlloc(GetProcessHeap(), 0, obj->fmtetc_cnt*sizeof(FORMATETC));
382 InitFormatEtc(obj->fmtetc[0], CF_TEXT, TYMED_HGLOBAL);
384 *lplpdataobj = (LPDATAOBJECT)obj;
385 return S_OK;
388 const char *cmpl_stm_data = "complex stream";
389 const char *cmpl_text_data = "complex text";
390 const WCHAR devname[] = {'m','y','d','e','v',0};
392 static HRESULT DataObjectImpl_CreateComplex(LPDATAOBJECT *lplpdataobj)
394 DataObjectImpl *obj;
395 ILockBytes *lbs;
396 DEVMODEW dm;
398 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(DataObjectImpl));
399 obj->lpVtbl = &VT_DataObjectImpl;
400 obj->ref = 1;
401 obj->text = GlobalAlloc(GMEM_MOVEABLE, strlen(cmpl_text_data) + 1);
402 strcpy(GlobalLock(obj->text), cmpl_text_data);
403 GlobalUnlock(obj->text);
404 CreateStreamOnHGlobal(NULL, TRUE, &obj->stm);
405 IStream_Write(obj->stm, cmpl_stm_data, strlen(cmpl_stm_data), NULL);
407 CreateILockBytesOnHGlobal(NULL, TRUE, &lbs);
408 StgCreateDocfileOnILockBytes(lbs, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &obj->stg);
409 ILockBytes_Release(lbs);
411 obj->fmtetc_cnt = 8;
412 /* zeroing here since FORMATETC has a hole in it, and it's confusing to have this uninitialised. */
413 obj->fmtetc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, obj->fmtetc_cnt*sizeof(FORMATETC));
414 InitFormatEtc(obj->fmtetc[0], CF_TEXT, TYMED_HGLOBAL);
415 InitFormatEtc(obj->fmtetc[1], cf_stream, TYMED_ISTREAM);
416 InitFormatEtc(obj->fmtetc[2], cf_storage, TYMED_ISTORAGE);
417 InitFormatEtc(obj->fmtetc[3], cf_another, TYMED_ISTORAGE|TYMED_ISTREAM|TYMED_HGLOBAL);
418 memset(&dm, 0, sizeof(dm));
419 dm.dmSize = sizeof(dm);
420 dm.dmDriverExtra = 0;
421 lstrcpyW(dm.dmDeviceName, devname);
422 obj->fmtetc[3].ptd = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(DVTARGETDEVICE, tdData) + sizeof(devname) + dm.dmSize + dm.dmDriverExtra);
423 obj->fmtetc[3].ptd->tdSize = FIELD_OFFSET(DVTARGETDEVICE, tdData) + sizeof(devname) + dm.dmSize + dm.dmDriverExtra;
424 obj->fmtetc[3].ptd->tdDriverNameOffset = FIELD_OFFSET(DVTARGETDEVICE, tdData);
425 obj->fmtetc[3].ptd->tdDeviceNameOffset = 0;
426 obj->fmtetc[3].ptd->tdPortNameOffset = 0;
427 obj->fmtetc[3].ptd->tdExtDevmodeOffset = obj->fmtetc[3].ptd->tdDriverNameOffset + sizeof(devname);
428 lstrcpyW((WCHAR*)obj->fmtetc[3].ptd->tdData, devname);
429 memcpy(obj->fmtetc[3].ptd->tdData + sizeof(devname), &dm, dm.dmSize + dm.dmDriverExtra);
431 InitFormatEtc(obj->fmtetc[4], cf_global, TYMED_HGLOBAL);
432 InitFormatEtc(obj->fmtetc[5], cf_another, TYMED_HGLOBAL);
433 InitFormatEtc(obj->fmtetc[6], cf_another, 0xfffff);
434 InitFormatEtc(obj->fmtetc[7], cf_another, 0xfffff);
435 obj->fmtetc[7].dwAspect = DVASPECT_ICON;
437 *lplpdataobj = (LPDATAOBJECT)obj;
438 return S_OK;
441 static void test_get_clipboard(void)
443 HRESULT hr;
444 IDataObject *data_obj;
445 FORMATETC fmtetc;
446 STGMEDIUM stgmedium;
448 hr = OleGetClipboard(NULL);
449 ok(hr == E_INVALIDARG, "OleGetClipboard(NULL) should return E_INVALIDARG instead of 0x%08x\n", hr);
451 hr = OleGetClipboard(&data_obj);
452 ok(hr == S_OK, "OleGetClipboard failed with error 0x%08x\n", hr);
454 /* test IDataObject_QueryGetData */
456 /* clipboard's IDataObject_QueryGetData shouldn't defer to our IDataObject_QueryGetData */
457 expect_DataObjectImpl_QueryGetData = FALSE;
459 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
460 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
461 ok(hr == S_OK, "IDataObject_QueryGetData failed with error 0x%08x\n", hr);
463 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
464 fmtetc.dwAspect = 0xdeadbeef;
465 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
466 ok(hr == DV_E_FORMATETC, "IDataObject_QueryGetData should have failed with DV_E_FORMATETC instead of 0x%08x\n", hr);
468 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
469 fmtetc.dwAspect = DVASPECT_THUMBNAIL;
470 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
471 ok(hr == DV_E_FORMATETC, "IDataObject_QueryGetData should have failed with DV_E_FORMATETC instead of 0x%08x\n", hr);
473 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
474 fmtetc.lindex = 256;
475 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
476 ok(hr == DV_E_FORMATETC || broken(hr == S_OK),
477 "IDataObject_QueryGetData should have failed with DV_E_FORMATETC instead of 0x%08x\n", hr);
478 if (hr == S_OK)
479 ReleaseStgMedium(&stgmedium);
481 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
482 fmtetc.cfFormat = CF_RIFF;
483 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
484 ok(hr == DV_E_CLIPFORMAT, "IDataObject_QueryGetData should have failed with DV_E_CLIPFORMAT instead of 0x%08x\n", hr);
486 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
487 fmtetc.tymed = TYMED_FILE;
488 hr = IDataObject_QueryGetData(data_obj, &fmtetc);
489 ok(hr == S_OK, "IDataObject_QueryGetData failed with error 0x%08x\n", hr);
491 expect_DataObjectImpl_QueryGetData = TRUE;
493 /* test IDataObject_GetData */
495 DataObjectImpl_GetData_calls = 0;
497 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
498 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
499 ok(hr == S_OK, "IDataObject_GetData failed with error 0x%08x\n", hr);
500 ReleaseStgMedium(&stgmedium);
502 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
503 fmtetc.dwAspect = 0xdeadbeef;
504 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
505 ok(hr == S_OK, "IDataObject_GetData failed with error 0x%08x\n", hr);
506 ReleaseStgMedium(&stgmedium);
508 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
509 fmtetc.dwAspect = DVASPECT_THUMBNAIL;
510 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
511 ok(hr == S_OK, "IDataObject_GetData failed with error 0x%08x\n", hr);
512 ReleaseStgMedium(&stgmedium);
514 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
515 fmtetc.lindex = 256;
516 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
517 ok(hr == DV_E_FORMATETC || broken(hr == S_OK), "IDataObject_GetData should have failed with DV_E_FORMATETC instead of 0x%08x\n", hr);
518 if (hr == S_OK)
520 /* undo the unexpected success */
521 DataObjectImpl_GetData_calls--;
522 ReleaseStgMedium(&stgmedium);
525 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
526 fmtetc.cfFormat = CF_RIFF;
527 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
528 ok(hr == DV_E_FORMATETC, "IDataObject_GetData should have failed with DV_E_FORMATETC instead of 0x%08x\n", hr);
530 InitFormatEtc(fmtetc, CF_TEXT, TYMED_HGLOBAL);
531 fmtetc.tymed = TYMED_FILE;
532 hr = IDataObject_GetData(data_obj, &fmtetc, &stgmedium);
533 ok(hr == DV_E_TYMED, "IDataObject_GetData should have failed with DV_E_TYMED instead of 0x%08x\n", hr);
535 ok(DataObjectImpl_GetData_calls == 6, "DataObjectImpl_GetData should have been called 6 times instead of %d times\n", DataObjectImpl_GetData_calls);
537 IDataObject_Release(data_obj);
540 static void test_enum_fmtetc(IDataObject *src)
542 HRESULT hr;
543 IDataObject *data;
544 IEnumFORMATETC *enum_fmt, *src_enum;
545 FORMATETC fmt, src_fmt;
546 DWORD count = 0;
548 hr = OleGetClipboard(&data);
549 ok(hr == S_OK, "OleGetClipboard failed with error 0x%08x\n", hr);
551 hr = IDataObject_EnumFormatEtc(data, DATADIR_SET, &enum_fmt);
552 ok(hr == E_NOTIMPL ||
553 broken(hr == E_INVALIDARG), /* win98 (not win98SE) */
554 "got %08x\n", hr);
556 DataObjectImpl_EnumFormatEtc_calls = 0;
557 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
558 ok(hr == S_OK, "got %08x\n", hr);
559 ok(DataObjectImpl_EnumFormatEtc_calls == 0, "EnumFormatEtc was called\n");
561 if(src) IDataObject_EnumFormatEtc(src, DATADIR_GET, &src_enum);
563 while((hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL)) == S_OK)
565 ok(src != NULL, "shouldn't be here\n");
566 hr = IEnumFORMATETC_Next(src_enum, 1, &src_fmt, NULL);
567 ok(hr == S_OK, "%d: got %08x\n", count, hr);
568 trace("%d: cf %04x aspect %x tymed %x\n", count, fmt.cfFormat, fmt.dwAspect, fmt.tymed);
569 ok(fmt.cfFormat == src_fmt.cfFormat, "%d: %04x %04x\n", count, fmt.cfFormat, src_fmt.cfFormat);
570 ok(fmt.dwAspect == src_fmt.dwAspect, "%d: %08x %08x\n", count, fmt.dwAspect, src_fmt.dwAspect);
571 ok(fmt.lindex == src_fmt.lindex, "%d: %08x %08x\n", count, fmt.lindex, src_fmt.lindex);
572 ok(fmt.tymed == src_fmt.tymed, "%d: %08x %08x\n", count, fmt.tymed, src_fmt.tymed);
573 if(fmt.ptd)
575 ok(src_fmt.ptd != NULL, "%d: expected non-NULL\n", count);
576 CoTaskMemFree(fmt.ptd);
577 CoTaskMemFree(src_fmt.ptd);
579 count++;
582 ok(hr == S_FALSE, "%d: got %08x\n", count, hr);
584 if(src)
586 hr = IEnumFORMATETC_Next(src_enum, 1, &src_fmt, NULL);
587 ok(hr == S_FALSE, "%d: got %08x\n", count, hr);
588 IEnumFORMATETC_Release(src_enum);
591 hr = IEnumFORMATETC_Reset(enum_fmt);
592 ok(hr == S_OK, "got %08x\n", hr);
594 IEnumFORMATETC_Release(enum_fmt);
595 IDataObject_Release(data);
598 static void test_no_cf_dataobject(void)
600 UINT cf_dataobject = RegisterClipboardFormatA("DataObject");
601 UINT cf_ole_priv_data = RegisterClipboardFormatA("Ole Private Data");
602 HANDLE h;
603 OpenClipboard(NULL);
605 h = GetClipboardData(cf_dataobject);
606 ok(!h, "got %p\n", h);
607 h = GetClipboardData(cf_ole_priv_data);
608 ok(!h, "got %p\n", h);
610 CloseClipboard();
613 static void test_cf_dataobject(IDataObject *data)
615 UINT cf = 0;
616 UINT cf_dataobject = RegisterClipboardFormatA("DataObject");
617 UINT cf_ole_priv_data = RegisterClipboardFormatA("Ole Private Data");
618 BOOL found_dataobject = FALSE, found_priv_data = FALSE;
620 OpenClipboard(NULL);
623 cf = EnumClipboardFormats(cf);
624 if(cf == cf_dataobject)
626 HGLOBAL h = GetClipboardData(cf);
627 HWND *ptr = GlobalLock(h);
628 DWORD size = GlobalSize(h);
629 HWND clip_owner = GetClipboardOwner();
631 found_dataobject = TRUE;
632 ok(size >= sizeof(*ptr), "size %d\n", size);
633 if(data)
634 ok(*ptr == clip_owner, "hwnd %p clip_owner %p\n", *ptr, clip_owner);
635 else /* ole clipboard flushed */
636 ok(*ptr == NULL, "hwnd %p\n", *ptr);
637 GlobalUnlock(h);
639 else if(cf == cf_ole_priv_data)
641 found_priv_data = TRUE;
642 if(data)
644 HGLOBAL h = GetClipboardData(cf);
645 DWORD *ptr = GlobalLock(h);
646 DWORD size = GlobalSize(h);
648 if(size != ptr[1])
649 win_skip("Ole Private Data in win9x format\n");
650 else
652 HRESULT hr;
653 IEnumFORMATETC *enum_fmt;
654 DWORD count = 0;
655 FORMATETC fmt;
656 struct formatetcetc
658 FORMATETC fmt;
659 BOOL first_use_of_cf;
660 DWORD res[2];
661 } *fmt_ptr;
662 struct priv_data
664 DWORD res1;
665 DWORD size;
666 DWORD res2;
667 DWORD count;
668 DWORD res3[2];
669 struct formatetcetc fmts[1];
670 } *priv = (struct priv_data*)ptr;
671 CLIPFORMAT cfs_seen[10];
673 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
674 ok(hr == S_OK, "got %08x\n", hr);
675 fmt_ptr = priv->fmts;
677 while(IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL) == S_OK)
679 int i;
680 BOOL seen_cf = FALSE;
682 ok(fmt_ptr->fmt.cfFormat == fmt.cfFormat,
683 "got %08x expected %08x\n", fmt_ptr->fmt.cfFormat, fmt.cfFormat);
684 ok(fmt_ptr->fmt.dwAspect == fmt.dwAspect, "got %08x expected %08x\n",
685 fmt_ptr->fmt.dwAspect, fmt.dwAspect);
686 ok(fmt_ptr->fmt.lindex == fmt.lindex, "got %08x expected %08x\n",
687 fmt_ptr->fmt.lindex, fmt.lindex);
688 ok(fmt_ptr->fmt.tymed == fmt.tymed, "got %08x expected %08x\n",
689 fmt_ptr->fmt.tymed, fmt.tymed);
690 for(i = 0; i < count; i++)
691 if(fmt_ptr->fmt.cfFormat == cfs_seen[i])
693 seen_cf = TRUE;
694 break;
696 cfs_seen[count] = fmt.cfFormat;
697 ok(fmt_ptr->first_use_of_cf == seen_cf ? FALSE : TRUE, "got %08x expected %08x\n",
698 fmt_ptr->first_use_of_cf, !seen_cf);
699 ok(fmt_ptr->res[0] == 0, "got %08x\n", fmt_ptr->res[1]);
700 ok(fmt_ptr->res[1] == 0, "got %08x\n", fmt_ptr->res[2]);
701 if(fmt.ptd)
703 DVTARGETDEVICE *target;
705 ok(fmt_ptr->fmt.ptd != NULL, "target device offset zero\n");
706 target = (DVTARGETDEVICE*)((char*)priv + (DWORD)fmt_ptr->fmt.ptd);
707 ok(!memcmp(target, fmt.ptd, fmt.ptd->tdSize), "target devices differ\n");
708 CoTaskMemFree(fmt.ptd);
710 fmt_ptr++;
711 count++;
713 ok(priv->res1 == 0, "got %08x\n", priv->res1);
714 ok(priv->res2 == 1, "got %08x\n", priv->res2);
715 ok(priv->count == count, "got %08x expected %08x\n", priv->count, count);
716 ok(priv->res3[0] == 0, "got %08x\n", priv->res3[0]);
717 ok(priv->res3[1] == 0, "got %08x\n", priv->res3[1]);
719 GlobalUnlock(h);
720 IEnumFORMATETC_Release(enum_fmt);
724 else if(cf == cf_stream)
726 HGLOBAL h;
727 void *ptr;
728 DWORD size;
730 DataObjectImpl_GetDataHere_calls = 0;
731 h = GetClipboardData(cf);
732 ok(DataObjectImpl_GetDataHere_calls == 1, "got %d\n", DataObjectImpl_GetDataHere_calls);
733 ptr = GlobalLock(h);
734 size = GlobalSize(h);
735 ok(size == strlen(cmpl_stm_data), "expected %d got %d\n", lstrlenA(cmpl_stm_data), size);
736 ok(!memcmp(ptr, cmpl_stm_data, size), "mismatch\n");
737 GlobalUnlock(h);
739 else if(cf == cf_global)
741 HGLOBAL h;
742 void *ptr;
743 DWORD size;
745 DataObjectImpl_GetDataHere_calls = 0;
746 h = GetClipboardData(cf);
747 ok(DataObjectImpl_GetDataHere_calls == 0, "got %d\n", DataObjectImpl_GetDataHere_calls);
748 ptr = GlobalLock(h);
749 size = GlobalSize(h);
750 ok(size == strlen(cmpl_text_data) + 1, "expected %d got %d\n", lstrlenA(cmpl_text_data) + 1, size);
751 ok(!memcmp(ptr, cmpl_text_data, size), "mismatch\n");
752 GlobalUnlock(h);
754 } while(cf);
755 CloseClipboard();
756 ok(found_dataobject, "didn't find cf_dataobject\n");
757 ok(found_priv_data, "didn't find cf_ole_priv_data\n");
760 static void test_set_clipboard(void)
762 HRESULT hr;
763 ULONG ref;
764 LPDATAOBJECT data1, data2, data_cmpl;
765 HGLOBAL hblob, h;
767 cf_stream = RegisterClipboardFormatA("stream format");
768 cf_storage = RegisterClipboardFormatA("storage format");
769 cf_global = RegisterClipboardFormatA("global format");
770 cf_another = RegisterClipboardFormatA("another format");
771 cf_onemore = RegisterClipboardFormatA("one more format");
773 hr = DataObjectImpl_CreateText("data1", &data1);
774 ok(SUCCEEDED(hr), "Failed to create data1 object: 0x%08x\n", hr);
775 if(FAILED(hr))
776 return;
777 hr = DataObjectImpl_CreateText("data2", &data2);
778 ok(SUCCEEDED(hr), "Failed to create data2 object: 0x%08x\n", hr);
779 if(FAILED(hr))
780 return;
781 hr = DataObjectImpl_CreateComplex(&data_cmpl);
782 ok(SUCCEEDED(hr), "Failed to create complex data object: 0x%08x\n", hr);
783 if(FAILED(hr))
784 return;
786 hr = OleSetClipboard(data1);
787 ok(hr == CO_E_NOTINITIALIZED, "OleSetClipboard should have failed with CO_E_NOTINITIALIZED instead of 0x%08x\n", hr);
789 CoInitialize(NULL);
790 hr = OleSetClipboard(data1);
791 ok(hr == CO_E_NOTINITIALIZED ||
792 hr == CLIPBRD_E_CANT_SET, /* win9x */
793 "OleSetClipboard should have failed with "
794 "CO_E_NOTINITIALIZED or CLIPBRD_E_CANT_SET instead of 0x%08x\n", hr);
795 CoUninitialize();
797 hr = OleInitialize(NULL);
798 ok(hr == S_OK, "OleInitialize failed with error 0x%08x\n", hr);
800 hr = OleSetClipboard(data1);
801 ok(hr == S_OK, "failed to set clipboard to data1, hr = 0x%08x\n", hr);
803 test_cf_dataobject(data1);
805 hr = OleIsCurrentClipboard(data1);
806 ok(hr == S_OK, "expected current clipboard to be data1, hr = 0x%08x\n", hr);
807 hr = OleIsCurrentClipboard(data2);
808 ok(hr == S_FALSE, "did not expect current clipboard to be data2, hr = 0x%08x\n", hr);
809 hr = OleIsCurrentClipboard(NULL);
810 ok(hr == S_FALSE, "expect S_FALSE, hr = 0x%08x\n", hr);
812 test_get_clipboard();
814 hr = OleSetClipboard(data2);
815 ok(hr == S_OK, "failed to set clipboard to data2, hr = 0x%08x\n", hr);
816 hr = OleIsCurrentClipboard(data1);
817 ok(hr == S_FALSE, "did not expect current clipboard to be data1, hr = 0x%08x\n", hr);
818 hr = OleIsCurrentClipboard(data2);
819 ok(hr == S_OK, "expected current clipboard to be data2, hr = 0x%08x\n", hr);
820 hr = OleIsCurrentClipboard(NULL);
821 ok(hr == S_FALSE, "expect S_FALSE, hr = 0x%08x\n", hr);
823 /* put a format directly onto the clipboard to show
824 OleFlushClipboard doesn't empty the clipboard */
825 hblob = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT, 10);
826 OpenClipboard(NULL);
827 h = SetClipboardData(cf_onemore, hblob);
828 ok(h == hblob, "got %p\n", h);
829 h = GetClipboardData(cf_onemore);
830 ok(h == hblob ||
831 broken(h != NULL), /* win9x */
832 "got %p\n", h);
833 CloseClipboard();
835 hr = OleFlushClipboard();
836 ok(hr == S_OK, "failed to flush clipboard, hr = 0x%08x\n", hr);
837 hr = OleIsCurrentClipboard(data1);
838 ok(hr == S_FALSE, "did not expect current clipboard to be data1, hr = 0x%08x\n", hr);
839 hr = OleIsCurrentClipboard(data2);
840 ok(hr == S_FALSE, "did not expect current clipboard to be data2, hr = 0x%08x\n", hr);
841 hr = OleIsCurrentClipboard(NULL);
842 ok(hr == S_FALSE, "expect S_FALSE, hr = 0x%08x\n", hr);
844 /* format should survive the flush */
845 OpenClipboard(NULL);
846 h = GetClipboardData(cf_onemore);
847 ok(h == hblob ||
848 broken(h != NULL), /* win9x */
849 "got %p\n", h);
850 CloseClipboard();
852 test_cf_dataobject(NULL);
854 ok(OleSetClipboard(NULL) == S_OK, "failed to clear clipboard, hr = 0x%08x\n", hr);
856 OpenClipboard(NULL);
857 h = GetClipboardData(cf_onemore);
858 ok(h == NULL, "got %p\n", h);
859 CloseClipboard();
861 trace("setting complex\n");
862 hr = OleSetClipboard(data_cmpl);
863 ok(hr == S_OK, "failed to set clipboard to complex data, hr = 0x%08x\n", hr);
864 test_cf_dataobject(data_cmpl);
865 test_enum_fmtetc(data_cmpl);
867 ok(OleSetClipboard(NULL) == S_OK, "failed to clear clipboard, hr = 0x%08x\n", hr);
869 test_no_cf_dataobject();
870 test_enum_fmtetc(NULL);
872 ref = IDataObject_Release(data1);
873 ok(ref == 0, "expected data1 ref=0, got %d\n", ref);
874 ref = IDataObject_Release(data2);
875 ok(ref == 0, "expected data2 ref=0, got %d\n", ref);
876 ref = IDataObject_Release(data_cmpl);
877 ok(ref == 0, "expected data_cmpl ref=0, got %d\n", ref);
879 OleUninitialize();
882 static inline ULONG count_refs(IDataObject *d)
884 IDataObject_AddRef(d);
885 return IDataObject_Release(d);
888 static void test_consumer_refs(void)
890 HRESULT hr;
891 IDataObject *src, *src2, *get1, *get2, *get3;
892 ULONG refs, old_refs;
893 FORMATETC fmt;
894 STGMEDIUM med;
896 InitFormatEtc(fmt, CF_TEXT, TYMED_HGLOBAL);
898 OleInitialize(NULL);
900 /* First show that each clipboard state results in
901 a different data object */
903 hr = DataObjectImpl_CreateText("data1", &src);
904 ok(hr == S_OK, "got %08x\n", hr);
905 hr = DataObjectImpl_CreateText("data2", &src2);
906 ok(hr == S_OK, "got %08x\n", hr);
908 hr = OleSetClipboard(src);
909 ok(hr == S_OK, "got %08x\n", hr);
911 hr = OleGetClipboard(&get1);
912 ok(hr == S_OK, "got %08x\n", hr);
914 hr = OleGetClipboard(&get2);
915 ok(hr == S_OK, "got %08x\n", hr);
917 ok(get1 == get2 ||
918 broken(get1 != get2), /* win9x, winme & nt4 */
919 "data objects differ\n");
920 refs = IDataObject_Release(get2);
921 ok(refs == (get1 == get2 ? 1 : 0), "got %d\n", refs);
923 OleFlushClipboard();
925 DataObjectImpl_GetData_calls = 0;
926 hr = IDataObject_GetData(get1, &fmt, &med);
927 ok(hr == S_OK, "got %08x\n", hr);
928 ok(DataObjectImpl_GetData_calls == 0, "GetData called\n");
929 ReleaseStgMedium(&med);
931 hr = OleGetClipboard(&get2);
932 ok(hr == S_OK, "got %08x\n", hr);
934 ok(get1 != get2, "data objects match\n");
936 OleSetClipboard(NULL);
938 hr = OleGetClipboard(&get3);
939 ok(hr == S_OK, "got %08x\n", hr);
941 ok(get1 != get3, "data objects match\n");
942 ok(get2 != get3, "data objects match\n");
944 IDataObject_Release(get3);
945 IDataObject_Release(get2);
946 IDataObject_Release(get1);
948 /* Now call GetData before the flush and show that this
949 takes a ref on our src data obj. */
951 hr = OleSetClipboard(src);
952 ok(hr == S_OK, "got %08x\n", hr);
954 old_refs = count_refs(src);
956 hr = OleGetClipboard(&get1);
957 ok(hr == S_OK, "got %08x\n", hr);
959 refs = count_refs(src);
960 ok(refs == old_refs, "%d %d\n", refs, old_refs);
962 DataObjectImpl_GetData_calls = 0;
963 hr = IDataObject_GetData(get1, &fmt, &med);
964 ok(hr == S_OK, "got %08x\n", hr);
965 ok(DataObjectImpl_GetData_calls == 1, "GetData not called\n");
966 ReleaseStgMedium(&med);
967 refs = count_refs(src);
968 ok(refs == old_refs + 1, "%d %d\n", refs, old_refs);
970 OleFlushClipboard();
972 DataObjectImpl_GetData_calls = 0;
973 hr = IDataObject_GetData(get1, &fmt, &med);
974 ok(hr == S_OK, "got %08x\n", hr);
975 ok(DataObjectImpl_GetData_calls == 1, "GetData not called\n");
976 ReleaseStgMedium(&med);
978 refs = count_refs(src);
979 ok(refs == 2, "%d\n", refs);
981 IDataObject_Release(get1);
983 refs = count_refs(src);
984 ok(refs == 1, "%d\n", refs);
986 /* Now set a second src object before the call to GetData
987 and show that GetData calls that second src. */
989 hr = OleSetClipboard(src);
990 ok(hr == S_OK, "got %08x\n", hr);
992 old_refs = count_refs(src);
994 hr = OleGetClipboard(&get1);
995 ok(hr == S_OK, "got %08x\n", hr);
997 refs = count_refs(src);
998 ok(refs == old_refs, "%d %d\n", refs, old_refs);
1000 hr = OleSetClipboard(src2);
1001 ok(hr == S_OK, "got %08x\n", hr);
1003 old_refs = count_refs(src2);
1005 DataObjectImpl_GetData_calls = 0;
1006 hr = IDataObject_GetData(get1, &fmt, &med);
1007 ok(hr == S_OK, "got %08x\n", hr);
1008 ok(DataObjectImpl_GetData_calls == 1, "GetData not called\n");
1009 ReleaseStgMedium(&med);
1011 refs = count_refs(src);
1012 ok(refs == 1, "%d\n", refs);
1013 refs = count_refs(src2);
1014 ok(refs == old_refs + 1, "%d %d\n", refs, old_refs);
1016 OleSetClipboard(NULL);
1018 refs = count_refs(src2);
1019 ok(refs == 2, "%d\n", refs);
1021 IDataObject_Release(get1);
1023 IDataObject_Release(src2);
1024 IDataObject_Release(src);
1026 OleUninitialize();
1029 static void test_flushed_getdata(void)
1031 HRESULT hr;
1032 IDataObject *src, *get;
1033 FORMATETC fmt;
1034 STGMEDIUM med;
1035 STATSTG stat;
1036 DEVMODEW dm;
1038 OleInitialize(NULL);
1040 hr = DataObjectImpl_CreateComplex(&src);
1041 ok(hr == S_OK, "got %08x\n", hr);
1043 hr = OleSetClipboard(src);
1044 ok(hr == S_OK, "got %08x\n", hr);
1046 hr = OleFlushClipboard();
1047 ok(hr == S_OK, "got %08x\n", hr);
1049 hr = OleGetClipboard(&get);
1050 ok(hr == S_OK, "got %08x\n", hr);
1052 /* global format -> global & stream */
1054 InitFormatEtc(fmt, CF_TEXT, TYMED_HGLOBAL);
1055 hr = IDataObject_GetData(get, &fmt, &med);
1056 ok(hr == S_OK, "got %08x\n", hr);
1057 ok(med.tymed == TYMED_HGLOBAL, "got %x\n", med.tymed);
1058 ReleaseStgMedium(&med);
1060 InitFormatEtc(fmt, CF_TEXT, TYMED_ISTREAM);
1061 hr = IDataObject_GetData(get, &fmt, &med);
1062 ok(hr == S_OK, "got %08x\n", hr);
1063 ok(med.tymed == TYMED_ISTREAM, "got %x\n", med.tymed);
1064 ReleaseStgMedium(&med);
1066 InitFormatEtc(fmt, CF_TEXT, TYMED_ISTORAGE);
1067 hr = IDataObject_GetData(get, &fmt, &med);
1068 ok(hr == E_FAIL, "got %08x\n", hr);
1070 InitFormatEtc(fmt, CF_TEXT, 0xffff);
1071 hr = IDataObject_GetData(get, &fmt, &med);
1072 ok(hr == S_OK, "got %08x\n", hr);
1073 ok(med.tymed == TYMED_HGLOBAL, "got %x\n", med.tymed);
1074 ReleaseStgMedium(&med);
1076 /* stream format -> global & stream */
1078 InitFormatEtc(fmt, cf_stream, TYMED_ISTREAM);
1079 hr = IDataObject_GetData(get, &fmt, &med);
1080 ok(hr == S_OK, "got %08x\n", hr);
1081 ok(med.tymed == TYMED_ISTREAM, "got %x\n", med.tymed);
1082 ReleaseStgMedium(&med);
1084 InitFormatEtc(fmt, cf_stream, TYMED_ISTORAGE);
1085 hr = IDataObject_GetData(get, &fmt, &med);
1086 ok(hr == E_FAIL, "got %08x\n", hr);
1088 InitFormatEtc(fmt, cf_stream, TYMED_HGLOBAL);
1089 hr = IDataObject_GetData(get, &fmt, &med);
1090 ok(hr == S_OK, "got %08x\n", hr);
1091 ok(med.tymed == TYMED_HGLOBAL, "got %x\n", med.tymed);
1092 ReleaseStgMedium(&med);
1094 InitFormatEtc(fmt, cf_stream, 0xffff);
1095 hr = IDataObject_GetData(get, &fmt, &med);
1096 ok(hr == S_OK, "got %08x\n", hr);
1097 ok(med.tymed == TYMED_ISTREAM, "got %x\n", med.tymed);
1098 ReleaseStgMedium(&med);
1100 /* storage format -> global, stream & storage */
1102 InitFormatEtc(fmt, cf_storage, TYMED_ISTORAGE);
1103 hr = IDataObject_GetData(get, &fmt, &med);
1104 ok(hr == S_OK, "got %08x\n", hr);
1105 ok(med.tymed == TYMED_ISTORAGE, "got %x\n", med.tymed);
1106 hr = IStorage_Stat(med.u.pstg, &stat, STATFLAG_NONAME);
1107 ok(hr == S_OK, "got %08x\n", hr);
1108 ok(stat.grfMode == (STGM_SHARE_EXCLUSIVE | STGM_READWRITE), "got %08x\n", stat.grfMode);
1109 ReleaseStgMedium(&med);
1111 InitFormatEtc(fmt, cf_storage, TYMED_ISTREAM);
1112 hr = IDataObject_GetData(get, &fmt, &med);
1113 ok(hr == S_OK, "got %08x\n", hr);
1114 ok(med.tymed == TYMED_ISTREAM, "got %x\n", med.tymed);
1115 ReleaseStgMedium(&med);
1117 InitFormatEtc(fmt, cf_storage, TYMED_HGLOBAL);
1118 hr = IDataObject_GetData(get, &fmt, &med);
1119 ok(hr == S_OK, "got %08x\n", hr);
1120 ok(med.tymed == TYMED_HGLOBAL, "got %x\n", med.tymed);
1121 ReleaseStgMedium(&med);
1123 InitFormatEtc(fmt, cf_storage, TYMED_HGLOBAL | TYMED_ISTREAM);
1124 hr = IDataObject_GetData(get, &fmt, &med);
1125 ok(hr == S_OK, "got %08x\n", hr);
1126 ok(med.tymed == TYMED_HGLOBAL, "got %x\n", med.tymed);
1127 ReleaseStgMedium(&med);
1129 InitFormatEtc(fmt, cf_storage, 0xffff);
1130 hr = IDataObject_GetData(get, &fmt, &med);
1131 ok(hr == S_OK, "got %08x\n", hr);
1132 ok(med.tymed == TYMED_ISTORAGE, "got %x\n", med.tymed);
1133 ReleaseStgMedium(&med);
1135 /* complex format with target device */
1137 InitFormatEtc(fmt, cf_another, 0xffff);
1138 hr = IDataObject_GetData(get, &fmt, &med);
1139 ok(hr == DV_E_FORMATETC, "got %08x\n", hr);
1141 InitFormatEtc(fmt, cf_another, 0xffff);
1142 memset(&dm, 0, sizeof(dm));
1143 dm.dmSize = sizeof(dm);
1144 dm.dmDriverExtra = 0;
1145 lstrcpyW(dm.dmDeviceName, devname);
1146 fmt.ptd = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(DVTARGETDEVICE, tdData) + sizeof(devname) + dm.dmSize + dm.dmDriverExtra);
1147 fmt.ptd->tdSize = FIELD_OFFSET(DVTARGETDEVICE, tdData) + sizeof(devname) + dm.dmSize + dm.dmDriverExtra;
1148 fmt.ptd->tdDriverNameOffset = FIELD_OFFSET(DVTARGETDEVICE, tdData);
1149 fmt.ptd->tdDeviceNameOffset = 0;
1150 fmt.ptd->tdPortNameOffset = 0;
1151 fmt.ptd->tdExtDevmodeOffset = fmt.ptd->tdDriverNameOffset + sizeof(devname);
1152 lstrcpyW((WCHAR*)fmt.ptd->tdData, devname);
1153 memcpy(fmt.ptd->tdData + sizeof(devname), &dm, dm.dmSize + dm.dmDriverExtra);
1155 hr = IDataObject_GetData(get, &fmt, &med);
1156 ok(hr == S_OK, "got %08x\n", hr);
1157 ok(med.tymed == TYMED_ISTORAGE, "got %x\n", med.tymed);
1158 ReleaseStgMedium(&med);
1160 HeapFree(GetProcessHeap(), 0, fmt.ptd);
1163 IDataObject_Release(get);
1164 IDataObject_Release(src);
1165 OleUninitialize();
1168 static HGLOBAL create_text(void)
1170 HGLOBAL h = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE, 5);
1171 char *p = GlobalLock(h);
1172 strcpy(p, "test");
1173 GlobalUnlock(h);
1174 return h;
1177 static HENHMETAFILE create_emf(void)
1179 const RECT rect = {0, 0, 100, 100};
1180 HDC hdc = CreateEnhMetaFileA(NULL, NULL, &rect, "HENHMETAFILE Ole Clipboard Test\0Test\0\0");
1181 ExtTextOutA(hdc, 0, 0, ETO_OPAQUE, &rect, "Test String", strlen("Test String"), NULL);
1182 return CloseEnhMetaFile(hdc);
1185 static void test_nonole_clipboard(void)
1187 HRESULT hr;
1188 BOOL r;
1189 IDataObject *get;
1190 IEnumFORMATETC *enum_fmt;
1191 FORMATETC fmt;
1192 HGLOBAL h, hblob, htext;
1193 HENHMETAFILE emf;
1195 r = OpenClipboard(NULL);
1196 ok(r, "gle %d\n", GetLastError());
1197 r = EmptyClipboard();
1198 ok(r, "gle %d\n", GetLastError());
1199 r = CloseClipboard();
1200 ok(r, "gle %d\n", GetLastError());
1202 OleInitialize(NULL);
1204 /* empty clipboard */
1205 hr = OleGetClipboard(&get);
1206 ok(hr == S_OK, "got %08x\n", hr);
1207 hr = IDataObject_EnumFormatEtc(get, DATADIR_GET, &enum_fmt);
1208 ok(hr == S_OK, "got %08x\n", hr);
1210 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1211 ok(hr == S_FALSE, "got %08x\n", hr);
1212 IEnumFORMATETC_Release(enum_fmt);
1214 IDataObject_Release(get);
1216 /* set a user defined clipboard type */
1218 htext = create_text();
1219 hblob = GlobalAlloc(GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT, 10);
1220 emf = create_emf();
1222 r = OpenClipboard(NULL);
1223 ok(r, "gle %d\n", GetLastError());
1224 h = SetClipboardData(CF_TEXT, htext);
1225 ok(h == htext, "got %p\n", h);
1226 h = SetClipboardData(cf_onemore, hblob);
1227 ok(h == hblob, "got %p\n", h);
1228 h = SetClipboardData(CF_ENHMETAFILE, emf);
1229 ok(h == emf, "got %p\n", h);
1230 r = CloseClipboard();
1231 ok(r, "gle %d\n", GetLastError());
1233 hr = OleGetClipboard(&get);
1234 ok(hr == S_OK, "got %08x\n", hr);
1235 hr = IDataObject_EnumFormatEtc(get, DATADIR_GET, &enum_fmt);
1236 ok(hr == S_OK, "got %08x\n", hr);
1238 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1239 ok(hr == S_OK, "got %08x\n", hr);
1240 ok(fmt.cfFormat == CF_TEXT, "cf %04x\n", fmt.cfFormat);
1241 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1242 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1243 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1244 ok(fmt.tymed == (TYMED_ISTREAM | TYMED_HGLOBAL), "tymed %x\n", fmt.tymed);
1246 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1247 ok(hr == S_OK, "got %08x\n", hr);
1248 ok(fmt.cfFormat == cf_onemore, "cf %04x\n", fmt.cfFormat);
1249 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1250 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1251 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1252 ok(fmt.tymed == (TYMED_ISTREAM | TYMED_HGLOBAL), "tymed %x\n", fmt.tymed);
1254 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1255 ok(hr == S_OK, "got %08x\n", hr);
1256 ok(fmt.cfFormat == CF_ENHMETAFILE, "cf %04x\n", fmt.cfFormat);
1257 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1258 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1259 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1260 ok(fmt.tymed == TYMED_ENHMF, "tymed %x\n", fmt.tymed);
1262 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1263 ok(hr == S_OK, "got %08x\n", hr); /* User32 adds some synthesised formats */
1265 todo_wine ok(fmt.cfFormat == CF_LOCALE, "cf %04x\n", fmt.cfFormat);
1266 if(fmt.cfFormat == CF_LOCALE)
1268 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1269 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1270 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1271 ok(fmt.tymed == (TYMED_ISTREAM | TYMED_HGLOBAL), "tymed %x\n", fmt.tymed);
1273 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1274 ok(hr == S_OK, "got %08x\n", hr);
1277 ok(fmt.cfFormat == CF_OEMTEXT, "cf %04x\n", fmt.cfFormat);
1278 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1279 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1280 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1281 ok(fmt.tymed == (TYMED_ISTREAM | TYMED_HGLOBAL), "tymed %x\n", fmt.tymed);
1283 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1284 ok(hr == S_OK, "got %08x\n", hr);
1285 ok(fmt.cfFormat == CF_UNICODETEXT, "cf %04x\n", fmt.cfFormat);
1286 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1287 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1288 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1289 ok(fmt.tymed == (TYMED_ISTREAM | TYMED_HGLOBAL), "tymed %x\n", fmt.tymed);
1291 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1292 ok(hr == S_OK, "got %08x\n", hr);
1293 ok(fmt.cfFormat == CF_METAFILEPICT, "cf %04x\n", fmt.cfFormat);
1294 ok(fmt.ptd == NULL, "ptd %p\n", fmt.ptd);
1295 ok(fmt.dwAspect == DVASPECT_CONTENT, "aspect %x\n", fmt.dwAspect);
1296 ok(fmt.lindex == -1, "lindex %d\n", fmt.lindex);
1297 ok(fmt.tymed == TYMED_MFPICT, "tymed %x\n", fmt.tymed);
1299 hr = IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL);
1300 ok(hr == S_FALSE, "got %08x\n", hr);
1301 IEnumFORMATETC_Release(enum_fmt);
1303 IDataObject_Release(get);
1305 r = OpenClipboard(NULL);
1306 ok(r, "gle %d\n", GetLastError());
1307 r = EmptyClipboard();
1308 ok(r, "gle %d\n", GetLastError());
1309 r = CloseClipboard();
1310 ok(r, "gle %d\n", GetLastError());
1312 OleUninitialize();
1315 START_TEST(clipboard)
1317 test_set_clipboard();
1318 test_consumer_refs();
1319 test_flushed_getdata();
1320 test_nonole_clipboard();