msctf/tests: Add handler sink_check_ok for checking fired sinks.
[wine/hacks.git] / dlls / msctf / tests / inputprocessor.c
blob591deff18d2ccb4bdbd1dea07ffbcd9541091470
1 /*
2 * Unit tests for ITfInputProcessor
4 * Copyright 2009 Aric Stewart, CodeWeavers
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 #include <stdio.h>
23 #define COBJMACROS
24 #include "wine/test.h"
25 #include "winuser.h"
26 #include "initguid.h"
27 #include "shlwapi.h"
28 #include "shlguid.h"
29 #include "comcat.h"
30 #include "msctf.h"
31 #include "olectl.h"
33 static ITfInputProcessorProfiles* g_ipp;
34 static LANGID gLangid;
35 static ITfCategoryMgr * g_cm = NULL;
36 static ITfThreadMgr* g_tm = NULL;
37 static ITfDocumentMgr *g_dm = NULL;
38 static TfClientId cid = 0;
39 static TfClientId tid = 0;
41 static ITextStoreACPSink *ACPSink;
43 #define SINK_UNEXPECTED 0
44 #define SINK_EXPECTED 1
45 #define SINK_FIRED 2
46 #define SINK_IGNORE 3
47 #define SINK_OPTIONAL 4
49 #define SINK_ACTION_MASK 0xff
50 #define SINK_OPTION_MASK 0xff00
51 #define SINK_EXPECTED_COUNT_MASK 0xff0000
53 #define SINK_OPTION_TODO 0x0100
55 static BOOL test_ShouldActivate = FALSE;
56 static BOOL test_ShouldDeactivate = FALSE;
58 static DWORD tmSinkCookie;
59 static DWORD tmSinkRefCount;
60 static DWORD documentStatus;
61 static ITfDocumentMgr *test_CurrentFocus = NULL;
62 static ITfDocumentMgr *test_PrevFocus = NULL;
63 static INT test_OnSetFocus = SINK_UNEXPECTED;
64 static INT test_OnInitDocumentMgr = SINK_UNEXPECTED;
65 static INT test_OnPushContext = SINK_UNEXPECTED;
66 static INT test_OnPopContext = SINK_UNEXPECTED;
67 static INT test_KEV_OnSetFocus = SINK_UNEXPECTED;
68 static INT test_ACP_AdviseSink = SINK_UNEXPECTED;
69 static INT test_ACP_GetStatus = SINK_UNEXPECTED;
70 static INT test_ACP_RequestLock = SINK_UNEXPECTED;
71 static INT test_ACP_GetEndACP = SINK_UNEXPECTED;
72 static INT test_ACP_GetSelection = SINK_UNEXPECTED;
73 static INT test_DoEditSession = SINK_UNEXPECTED;
74 static INT test_ACP_InsertTextAtSelection = SINK_UNEXPECTED;
75 static INT test_ACP_SetSelection = SINK_UNEXPECTED;
76 static INT test_OnEndEdit = SINK_UNEXPECTED;
79 static inline int expected_count(int *sink)
81 return (*sink & SINK_EXPECTED_COUNT_MASK)>>16;
84 static inline void _sink_fire_ok(INT *sink, const CHAR* name)
86 int count;
87 int todo = *sink & SINK_OPTION_TODO;
88 int action = *sink & SINK_ACTION_MASK;
90 if (winetest_interactive)
91 winetest_trace("firing %s\n",name);
93 switch (action)
95 case SINK_OPTIONAL:
96 case SINK_EXPECTED:
97 count = expected_count(sink);
98 if (count > 1)
100 count --;
101 *sink = (*sink & ~SINK_EXPECTED_COUNT_MASK) + (count << 16);
102 return;
104 break;
105 case SINK_IGNORE:
106 winetest_trace("Ignoring %s\n",name);
107 return;
108 default:
109 if (todo) todo_wine
111 winetest_ok(0, "Unexpected %s sink\n",name);
114 *sink = SINK_FIRED;
117 #define sink_fire_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_fire_ok(a,b)
119 inline void _sink_check_ok(INT *sink, const CHAR* name)
121 int action = *sink & SINK_ACTION_MASK;
123 switch (action)
125 case SINK_FIRED:
126 case SINK_OPTIONAL:
127 break;
128 case SINK_IGNORE:
129 return;
130 default:
131 winetest_ok(0, "%s not fired as expected, in state %x\n",name,*sink);
133 *sink = SINK_UNEXPECTED;
136 #define sink_check_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_check_ok(a,b)
138 /**********************************************************************
139 * ITextStoreACP
140 **********************************************************************/
141 typedef struct tagTextStoreACP
143 const ITextStoreACPVtbl *TextStoreACPVtbl;
144 LONG refCount;
146 } TextStoreACP;
148 static void TextStoreACP_Destructor(TextStoreACP *This)
150 HeapFree(GetProcessHeap(),0,This);
153 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
155 TextStoreACP *This = (TextStoreACP *)iface;
156 *ppvOut = NULL;
158 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
160 *ppvOut = This;
163 if (*ppvOut)
165 IUnknown_AddRef(iface);
166 return S_OK;
169 return E_NOINTERFACE;
172 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
174 TextStoreACP *This = (TextStoreACP *)iface;
175 return InterlockedIncrement(&This->refCount);
178 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
180 TextStoreACP *This = (TextStoreACP *)iface;
181 ULONG ret;
183 ret = InterlockedDecrement(&This->refCount);
184 if (ret == 0)
185 TextStoreACP_Destructor(This);
186 return ret;
189 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
190 REFIID riid, IUnknown *punk, DWORD dwMask)
192 HRESULT hr;
194 sink_fire_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
196 hr = IUnknown_QueryInterface(punk, &IID_ITextStoreACPSink,(LPVOID*)(&ACPSink));
197 ok(SUCCEEDED(hr),"Unable to QueryInterface on sink\n");
198 return S_OK;
201 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
202 IUnknown *punk)
204 trace("\n");
205 return S_OK;
208 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
209 DWORD dwLockFlags, HRESULT *phrSession)
211 sink_fire_ok(&test_ACP_RequestLock,"TextStoreACP_RequestLock");
212 *phrSession = ITextStoreACPSink_OnLockGranted(ACPSink, dwLockFlags);
213 return S_OK;
215 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
216 TS_STATUS *pdcs)
218 sink_fire_ok(&test_ACP_GetStatus,"TextStoreACP_GetStatus");
219 pdcs->dwDynamicFlags = documentStatus;
220 return S_OK;
222 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
223 LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
224 LONG *pacpResultEnd)
226 trace("\n");
227 return S_OK;
229 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
230 ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
232 sink_fire_ok(&test_ACP_GetSelection,"TextStoreACP_GetSelection");
234 pSelection->acpStart = 10;
235 pSelection->acpEnd = 20;
236 pSelection->style.fInterimChar = 0;
237 pSelection->style.ase = TS_AE_NONE;
238 *pcFetched = 1;
240 return S_OK;
242 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
243 ULONG ulCount, const TS_SELECTION_ACP *pSelection)
245 sink_fire_ok(&test_ACP_SetSelection,"TextStoreACP_SetSelection");
246 return S_OK;
248 static HRESULT WINAPI TextStoreACP_GetText(ITextStoreACP *iface,
249 LONG acpStart, LONG acpEnd, WCHAR *pchPlain, ULONG cchPlainReq,
250 ULONG *pcchPlainRet, TS_RUNINFO *prgRunInfo, ULONG cRunInfoReq,
251 ULONG *pcRunInfoRet, LONG *pacpNext)
253 trace("\n");
254 return S_OK;
256 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
257 DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
258 ULONG cch, TS_TEXTCHANGE *pChange)
260 trace("\n");
261 return S_OK;
263 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
264 LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
266 trace("\n");
267 return S_OK;
269 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
270 LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
272 trace("\n");
273 return S_OK;
275 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
276 const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
278 trace("\n");
279 return S_OK;
281 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
282 DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
283 TS_TEXTCHANGE *pChange)
285 trace("\n");
286 return S_OK;
288 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
289 DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
290 LONG *pacpEnd, TS_TEXTCHANGE *pChange)
292 sink_fire_ok(&test_ACP_InsertTextAtSelection,"TextStoreACP_InsertTextAtSelection");
293 return S_OK;
295 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
296 DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
297 TS_TEXTCHANGE *pChange)
299 trace("\n");
300 return S_OK;
302 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
303 DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
305 trace("\n");
306 return S_OK;
308 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
309 LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
310 DWORD dwFlags)
312 trace("\n");
313 return S_OK;
315 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
316 LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
317 DWORD dwFlags)
319 trace("\n");
320 return S_OK;
322 static HRESULT WINAPI TextStoreACP_FindNextAttrTransition(ITextStoreACP *iface,
323 LONG acpStart, LONG acpHalt, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
324 DWORD dwFlags, LONG *pacpNext, BOOL *pfFound, LONG *plFoundOffset)
326 trace("\n");
327 return S_OK;
329 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
330 ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
332 trace("\n");
333 return S_OK;
335 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
336 LONG *pacp)
338 sink_fire_ok(&test_ACP_GetEndACP,"TextStoreACP_GetEndACP");
339 return S_OK;
341 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
342 TsViewCookie *pvcView)
344 trace("\n");
345 return S_OK;
347 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
348 TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
349 LONG *pacp)
351 trace("\n");
352 return S_OK;
354 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
355 TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
356 BOOL *pfClipped)
358 trace("\n");
359 return S_OK;
361 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
362 TsViewCookie vcView, RECT *prc)
364 trace("\n");
365 return S_OK;
367 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
368 TsViewCookie vcView, HWND *phwnd)
370 trace("\n");
371 return S_OK;
374 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
376 TextStoreACP_QueryInterface,
377 TextStoreACP_AddRef,
378 TextStoreACP_Release,
380 TextStoreACP_AdviseSink,
381 TextStoreACP_UnadviseSink,
382 TextStoreACP_RequestLock,
383 TextStoreACP_GetStatus,
384 TextStoreACP_QueryInsert,
385 TextStoreACP_GetSelection,
386 TextStoreACP_SetSelection,
387 TextStoreACP_GetText,
388 TextStoreACP_SetText,
389 TextStoreACP_GetFormattedText,
390 TextStoreACP_GetEmbedded,
391 TextStoreACP_QueryInsertEmbedded,
392 TextStoreACP_InsertEmbedded,
393 TextStoreACP_InsertTextAtSelection,
394 TextStoreACP_InsertEmbeddedAtSelection,
395 TextStoreACP_RequestSupportedAttrs,
396 TextStoreACP_RequestAttrsAtPosition,
397 TextStoreACP_RequestAttrsTransitioningAtPosition,
398 TextStoreACP_FindNextAttrTransition,
399 TextStoreACP_RetrieveRequestedAttrs,
400 TextStoreACP_GetEndACP,
401 TextStoreACP_GetActiveView,
402 TextStoreACP_GetACPFromPoint,
403 TextStoreACP_GetTextExt,
404 TextStoreACP_GetScreenExt,
405 TextStoreACP_GetWnd
408 static HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
410 TextStoreACP *This;
412 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
413 if (This == NULL)
414 return E_OUTOFMEMORY;
416 This->TextStoreACPVtbl = &TextStoreACP_TextStoreACPVtbl;
417 This->refCount = 1;
419 *ppOut = (IUnknown *)This;
420 return S_OK;
423 /**********************************************************************
424 * ITfThreadMgrEventSink
425 **********************************************************************/
426 typedef struct tagThreadMgrEventSink
428 const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl;
429 LONG refCount;
430 } ThreadMgrEventSink;
432 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
434 HeapFree(GetProcessHeap(),0,This);
437 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
439 ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
440 *ppvOut = NULL;
442 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
444 *ppvOut = This;
447 if (*ppvOut)
449 IUnknown_AddRef(iface);
450 return S_OK;
453 return E_NOINTERFACE;
456 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
458 ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
459 ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
460 return InterlockedIncrement(&This->refCount);
463 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
465 ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
466 ULONG ret;
468 ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
469 ret = InterlockedDecrement(&This->refCount);
470 if (ret == 0)
471 ThreadMgrEventSink_Destructor(This);
472 return ret;
475 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
476 ITfDocumentMgr *pdim)
478 sink_fire_ok(&test_OnInitDocumentMgr,"ThreadMgrEventSink_OnInitDocumentMgr");
479 return S_OK;
482 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
483 ITfDocumentMgr *pdim)
485 trace("\n");
486 return S_OK;
489 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
490 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
492 sink_fire_ok(&test_OnSetFocus,"ThreadMgrEventSink_OnSetFocus");
493 ok(pdimFocus == test_CurrentFocus,"Sink reports wrong focus\n");
494 ok(pdimPrevFocus == test_PrevFocus,"Sink reports wrong previous focus\n");
495 return S_OK;
498 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(ITfThreadMgrEventSink *iface,
499 ITfContext *pic)
501 HRESULT hr;
502 ITfDocumentMgr *docmgr;
503 ITfContext *test;
505 hr = ITfContext_GetDocumentMgr(pic,&docmgr);
506 ok(SUCCEEDED(hr),"GetDocumenMgr failed\n");
507 test = (ITfContext*)0xdeadbeef;
508 ITfDocumentMgr_Release(docmgr);
509 hr = ITfDocumentMgr_GetTop(docmgr,&test);
510 ok(SUCCEEDED(hr),"GetTop failed\n");
511 ok(test == pic, "Wrong context is on top\n");
512 if (test)
513 ITfContext_Release(test);
515 sink_fire_ok(&test_OnPushContext,"ThreadMgrEventSink_OnPushContext");
516 return S_OK;
519 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
520 ITfContext *pic)
522 HRESULT hr;
523 ITfDocumentMgr *docmgr;
524 ITfContext *test;
526 hr = ITfContext_GetDocumentMgr(pic,&docmgr);
527 ok(SUCCEEDED(hr),"GetDocumenMgr failed\n");
528 ITfDocumentMgr_Release(docmgr);
529 test = (ITfContext*)0xdeadbeef;
530 hr = ITfDocumentMgr_GetTop(docmgr,&test);
531 ok(SUCCEEDED(hr),"GetTop failed\n");
532 ok(test == pic, "Wrong context is on top\n");
533 if (test)
534 ITfContext_Release(test);
536 sink_fire_ok(&test_OnPopContext,"ThreadMgrEventSink_OnPopContext");
537 return S_OK;
540 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
542 ThreadMgrEventSink_QueryInterface,
543 ThreadMgrEventSink_AddRef,
544 ThreadMgrEventSink_Release,
546 ThreadMgrEventSink_OnInitDocumentMgr,
547 ThreadMgrEventSink_OnUninitDocumentMgr,
548 ThreadMgrEventSink_OnSetFocus,
549 ThreadMgrEventSink_OnPushContext,
550 ThreadMgrEventSink_OnPopContext
553 static HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
555 ThreadMgrEventSink *This;
557 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
558 if (This == NULL)
559 return E_OUTOFMEMORY;
561 This->ThreadMgrEventSinkVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
562 This->refCount = 1;
564 *ppOut = (IUnknown *)This;
565 return S_OK;
569 /********************************************************************************************
570 * Stub text service for testing
571 ********************************************************************************************/
573 static LONG TS_refCount;
574 static IClassFactory *cf;
575 static DWORD regid;
577 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
579 typedef struct tagClassFactory
581 const IClassFactoryVtbl *vtbl;
582 LONG ref;
583 LPFNCONSTRUCTOR ctor;
584 } ClassFactory;
586 typedef struct tagTextService
588 const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
589 LONG refCount;
590 } TextService;
592 static void ClassFactory_Destructor(ClassFactory *This)
594 HeapFree(GetProcessHeap(),0,This);
595 TS_refCount--;
598 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
600 *ppvOut = NULL;
601 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
603 IClassFactory_AddRef(iface);
604 *ppvOut = iface;
605 return S_OK;
608 return E_NOINTERFACE;
611 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
613 ClassFactory *This = (ClassFactory *)iface;
614 return InterlockedIncrement(&This->ref);
617 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
619 ClassFactory *This = (ClassFactory *)iface;
620 ULONG ret = InterlockedDecrement(&This->ref);
622 if (ret == 0)
623 ClassFactory_Destructor(This);
624 return ret;
627 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
629 ClassFactory *This = (ClassFactory *)iface;
630 HRESULT ret;
631 IUnknown *obj;
633 ret = This->ctor(punkOuter, &obj);
634 if (FAILED(ret))
635 return ret;
636 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
637 IUnknown_Release(obj);
638 return ret;
641 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
643 if(fLock)
644 InterlockedIncrement(&TS_refCount);
645 else
646 InterlockedDecrement(&TS_refCount);
648 return S_OK;
651 static const IClassFactoryVtbl ClassFactoryVtbl = {
652 /* IUnknown */
653 ClassFactory_QueryInterface,
654 ClassFactory_AddRef,
655 ClassFactory_Release,
657 /* IClassFactory*/
658 ClassFactory_CreateInstance,
659 ClassFactory_LockServer
662 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
664 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
665 This->vtbl = &ClassFactoryVtbl;
666 This->ref = 1;
667 This->ctor = ctor;
668 *ppvOut = (LPVOID)This;
669 TS_refCount++;
670 return S_OK;
673 static void TextService_Destructor(TextService *This)
675 HeapFree(GetProcessHeap(),0,This);
678 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
680 TextService *This = (TextService *)iface;
681 *ppvOut = NULL;
683 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
685 *ppvOut = This;
688 if (*ppvOut)
690 IUnknown_AddRef(iface);
691 return S_OK;
694 return E_NOINTERFACE;
697 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
699 TextService *This = (TextService *)iface;
700 return InterlockedIncrement(&This->refCount);
703 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
705 TextService *This = (TextService *)iface;
706 ULONG ret;
708 ret = InterlockedDecrement(&This->refCount);
709 if (ret == 0)
710 TextService_Destructor(This);
711 return ret;
714 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
715 ITfThreadMgr *ptim, TfClientId id)
717 trace("TextService_Activate\n");
718 ok(test_ShouldActivate,"Activation came unexpectedly\n");
719 tid = id;
720 return S_OK;
723 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
725 trace("TextService_Deactivate\n");
726 ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
727 return S_OK;
730 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
732 TextService_QueryInterface,
733 TextService_AddRef,
734 TextService_Release,
736 TextService_Activate,
737 TextService_Deactivate
740 static HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
742 TextService *This;
743 if (pUnkOuter)
744 return CLASS_E_NOAGGREGATION;
746 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
747 if (This == NULL)
748 return E_OUTOFMEMORY;
750 This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
751 This->refCount = 1;
753 *ppOut = (IUnknown *)This;
754 return S_OK;
757 static HRESULT RegisterTextService(REFCLSID rclsid)
759 ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
760 return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
763 static HRESULT UnregisterTextService()
765 return CoRevokeClassObject(regid);
769 * The tests
772 DEFINE_GUID(CLSID_FakeService, 0xEDE1A7AD,0x66DE,0x47E0,0xB6,0x20,0x3E,0x92,0xF8,0x24,0x6B,0xF3);
773 DEFINE_GUID(CLSID_TF_InputProcessorProfiles, 0x33c53a50,0xf456,0x4884,0xb0,0x49,0x85,0xfd,0x64,0x3e,0xcf,0xed);
774 DEFINE_GUID(CLSID_TF_CategoryMgr, 0xA4B544A1,0x438D,0x4B41,0x93,0x25,0x86,0x95,0x23,0xE2,0xD6,0xC7);
775 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD, 0x34745c63,0xb2f0,0x4784,0x8b,0x67,0x5e,0x12,0xc8,0x70,0x1a,0x31);
776 DEFINE_GUID(GUID_TFCAT_TIP_SPEECH, 0xB5A73CD1,0x8355,0x426B,0xA1,0x61,0x25,0x98,0x08,0xF2,0x6B,0x14);
777 DEFINE_GUID(GUID_TFCAT_TIP_HANDWRITING, 0x246ecb87,0xc2f2,0x4abe,0x90,0x5b,0xc8,0xb3,0x8a,0xdd,0x2c,0x43);
778 DEFINE_GUID (GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, 0x046B8C80,0x1647,0x40F7,0x9B,0x21,0xB9,0x3B,0x81,0xAA,0xBC,0x1B);
779 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
780 DEFINE_GUID(CLSID_TF_ThreadMgr, 0x529a9e6b,0x6587,0x4f23,0xab,0x9e,0x9c,0x7d,0x68,0x3e,0x3c,0x50);
781 DEFINE_GUID(CLSID_PreservedKey, 0xA0ED8E55,0xCD3B,0x4274,0xB2,0x95,0xF6,0xC9,0xBA,0x2B,0x84,0x72);
782 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_DISABLED, 0x71a5b253,0x1951,0x466b,0x9f,0xbc,0x9c,0x88,0x08,0xfa,0x84,0xf2);
783 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, 0x58273aad,0x01bb,0x4164,0x95,0xc6,0x75,0x5b,0xa0,0xb5,0x16,0x2d);
784 DEFINE_GUID(GUID_COMPARTMENT_HANDWRITING_OPENCLOSE, 0xf9ae2c6b,0x1866,0x4361,0xaf,0x72,0x7a,0xa3,0x09,0x48,0x89,0x0e);
785 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_DISABLED, 0x56c5c607,0x0703,0x4e59,0x8e,0x52,0xcb,0xc8,0x4e,0x8b,0xbe,0x35);
786 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_OPENCLOSE, 0x544d6a63,0xe2e8,0x4752,0xbb,0xd1,0x00,0x09,0x60,0xbc,0xa0,0x83);
787 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_GLOBALSTATE, 0x2a54fe8e,0x0d08,0x460c,0xa7,0x5d,0x87,0x03,0x5f,0xf4,0x36,0xc5);
788 DEFINE_GUID(GUID_COMPARTMENT_PERSISTMENUENABLED, 0x575f3783,0x70c8,0x47c8,0xae,0x5d,0x91,0xa0,0x1a,0x1f,0x75,0x92);
789 DEFINE_GUID(GUID_COMPARTMENT_EMPTYCONTEXT, 0xd7487dbf,0x804e,0x41c5,0x89,0x4d,0xad,0x96,0xfd,0x4e,0xea,0x13);
790 DEFINE_GUID(GUID_COMPARTMENT_TIPUISTATUS, 0x148ca3ec,0x0366,0x401c,0x8d,0x75,0xed,0x97,0x8d,0x85,0xfb,0xc9);
792 static HRESULT initialize(void)
794 HRESULT hr;
795 CoInitialize(NULL);
796 hr = CoCreateInstance (&CLSID_TF_InputProcessorProfiles, NULL,
797 CLSCTX_INPROC_SERVER, &IID_ITfInputProcessorProfiles, (void**)&g_ipp);
798 if (SUCCEEDED(hr))
799 hr = CoCreateInstance (&CLSID_TF_CategoryMgr, NULL,
800 CLSCTX_INPROC_SERVER, &IID_ITfCategoryMgr, (void**)&g_cm);
801 if (SUCCEEDED(hr))
802 hr = CoCreateInstance (&CLSID_TF_ThreadMgr, NULL,
803 CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (void**)&g_tm);
804 return hr;
807 static void cleanup(void)
809 if (g_ipp)
810 ITfInputProcessorProfiles_Release(g_ipp);
811 if (g_cm)
812 ITfCategoryMgr_Release(g_cm);
813 if (g_tm)
814 ITfThreadMgr_Release(g_tm);
815 CoUninitialize();
818 static void test_Register(void)
820 HRESULT hr;
822 static const WCHAR szDesc[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',0};
823 static const WCHAR szFile[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',' ','F','i','l','e',0};
825 hr = ITfInputProcessorProfiles_GetCurrentLanguage(g_ipp,&gLangid);
826 ok(SUCCEEDED(hr),"Unable to get current language id\n");
827 trace("Current Language %x\n",gLangid);
829 hr = RegisterTextService(&CLSID_FakeService);
830 ok(SUCCEEDED(hr),"Unable to register COM for TextService\n");
831 hr = ITfInputProcessorProfiles_Register(g_ipp, &CLSID_FakeService);
832 ok(SUCCEEDED(hr),"Unable to register text service(%x)\n",hr);
833 hr = ITfInputProcessorProfiles_AddLanguageProfile(g_ipp, &CLSID_FakeService, gLangid, &CLSID_FakeService, szDesc, sizeof(szDesc)/sizeof(WCHAR), szFile, sizeof(szFile)/sizeof(WCHAR), 1);
834 ok(SUCCEEDED(hr),"Unable to add Language Profile (%x)\n",hr);
837 static void test_Unregister(void)
839 HRESULT hr;
840 hr = ITfInputProcessorProfiles_Unregister(g_ipp, &CLSID_FakeService);
841 ok(SUCCEEDED(hr),"Unable to unregister text service(%x)\n",hr);
842 UnregisterTextService();
845 static void test_EnumInputProcessorInfo(void)
847 IEnumGUID *ppEnum;
848 BOOL found = FALSE;
850 if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
852 ULONG fetched;
853 GUID g;
854 while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
856 if(IsEqualGUID(&g,&CLSID_FakeService))
857 found = TRUE;
860 ok(found,"Did not find registered text service\n");
863 static void test_EnumLanguageProfiles(void)
865 BOOL found = FALSE;
866 IEnumTfLanguageProfiles *ppEnum;
867 if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
869 TF_LANGUAGEPROFILE profile;
870 while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
872 if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
874 found = TRUE;
875 ok(profile.langid == gLangid, "LangId Incorrect\n");
876 ok(IsEqualGUID(&profile.catid,&GUID_TFCAT_TIP_KEYBOARD), "CatId Incorrect\n");
877 ok(IsEqualGUID(&profile.guidProfile,&CLSID_FakeService), "guidProfile Incorrect\n");
881 ok(found,"Registered text service not found\n");
884 static void test_RegisterCategory(void)
886 HRESULT hr;
887 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
888 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
889 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
890 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
893 static void test_UnregisterCategory(void)
895 HRESULT hr;
896 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
897 ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
898 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
899 ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
902 static void test_FindClosestCategory(void)
904 GUID output;
905 HRESULT hr;
906 const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
908 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, NULL, 0);
909 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
910 ok(IsEqualGUID(&output,&GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER),"Wrong GUID\n");
912 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 1);
913 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
914 ok(IsEqualGUID(&output,&GUID_NULL),"Wrong GUID\n");
916 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 3);
917 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
918 ok(IsEqualGUID(&output,&GUID_TFCAT_TIP_KEYBOARD),"Wrong GUID\n");
921 static void test_Enable(void)
923 HRESULT hr;
924 BOOL enabled = FALSE;
926 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, TRUE);
927 ok(SUCCEEDED(hr),"Failed to enable text service\n");
928 hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, &enabled);
929 ok(SUCCEEDED(hr),"Failed to get enabled state\n");
930 ok(enabled == TRUE,"enabled state incorrect\n");
933 static void test_Disable(void)
935 HRESULT hr;
937 trace("Disabling\n");
938 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, FALSE);
939 ok(SUCCEEDED(hr),"Failed to disable text service\n");
942 static void test_ThreadMgrAdviseSinks(void)
944 ITfSource *source = NULL;
945 HRESULT hr;
946 IUnknown *sink;
948 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
949 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
950 if (!source)
951 return;
953 hr = ThreadMgrEventSink_Constructor(&sink);
954 ok(hr == S_OK, "got %08x\n", hr);
955 if(FAILED(hr)) return;
957 tmSinkRefCount = 1;
958 tmSinkCookie = 0;
959 hr = ITfSource_AdviseSink(source,&IID_ITfThreadMgrEventSink, sink, &tmSinkCookie);
960 ok(SUCCEEDED(hr),"Failed to Advise Sink\n");
961 ok(tmSinkCookie!=0,"Failed to get sink cookie\n");
963 /* Advising the sink adds a ref, Relesing here lets the object be deleted
964 when unadvised */
965 tmSinkRefCount = 2;
966 IUnknown_Release(sink);
967 ITfSource_Release(source);
970 static void test_ThreadMgrUnadviseSinks(void)
972 ITfSource *source = NULL;
973 HRESULT hr;
975 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
976 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
977 if (!source)
978 return;
980 tmSinkRefCount = 1;
981 hr = ITfSource_UnadviseSink(source, tmSinkCookie);
982 ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
983 ITfSource_Release(source);
986 /**********************************************************************
987 * ITfKeyEventSink
988 **********************************************************************/
989 typedef struct tagKeyEventSink
991 const ITfKeyEventSinkVtbl *KeyEventSinkVtbl;
992 LONG refCount;
993 } KeyEventSink;
995 static void KeyEventSink_Destructor(KeyEventSink *This)
997 HeapFree(GetProcessHeap(),0,This);
1000 static HRESULT WINAPI KeyEventSink_QueryInterface(ITfKeyEventSink *iface, REFIID iid, LPVOID *ppvOut)
1002 KeyEventSink *This = (KeyEventSink *)iface;
1003 *ppvOut = NULL;
1005 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfKeyEventSink))
1007 *ppvOut = This;
1010 if (*ppvOut)
1012 IUnknown_AddRef(iface);
1013 return S_OK;
1016 return E_NOINTERFACE;
1019 static ULONG WINAPI KeyEventSink_AddRef(ITfKeyEventSink *iface)
1021 KeyEventSink *This = (KeyEventSink *)iface;
1022 return InterlockedIncrement(&This->refCount);
1025 static ULONG WINAPI KeyEventSink_Release(ITfKeyEventSink *iface)
1027 KeyEventSink *This = (KeyEventSink *)iface;
1028 ULONG ret;
1030 ret = InterlockedDecrement(&This->refCount);
1031 if (ret == 0)
1032 KeyEventSink_Destructor(This);
1033 return ret;
1036 static HRESULT WINAPI KeyEventSink_OnSetFocus(ITfKeyEventSink *iface,
1037 BOOL fForeground)
1039 sink_fire_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1040 return S_OK;
1043 static HRESULT WINAPI KeyEventSink_OnTestKeyDown(ITfKeyEventSink *iface,
1044 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1046 trace("\n");
1047 return S_OK;
1050 static HRESULT WINAPI KeyEventSink_OnTestKeyUp(ITfKeyEventSink *iface,
1051 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1053 trace("\n");
1054 return S_OK;
1057 static HRESULT WINAPI KeyEventSink_OnKeyDown(ITfKeyEventSink *iface,
1058 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1060 trace("\n");
1061 return S_OK;
1064 static HRESULT WINAPI KeyEventSink_OnKeyUp(ITfKeyEventSink *iface,
1065 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1067 trace("\n");
1068 return S_OK;
1071 static HRESULT WINAPI KeyEventSink_OnPreservedKey(ITfKeyEventSink *iface,
1072 ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
1074 trace("\n");
1075 return S_OK;
1078 static const ITfKeyEventSinkVtbl KeyEventSink_KeyEventSinkVtbl =
1080 KeyEventSink_QueryInterface,
1081 KeyEventSink_AddRef,
1082 KeyEventSink_Release,
1084 KeyEventSink_OnSetFocus,
1085 KeyEventSink_OnTestKeyDown,
1086 KeyEventSink_OnTestKeyUp,
1087 KeyEventSink_OnKeyDown,
1088 KeyEventSink_OnKeyUp,
1089 KeyEventSink_OnPreservedKey
1092 static HRESULT KeyEventSink_Constructor(ITfKeyEventSink **ppOut)
1094 KeyEventSink *This;
1096 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(KeyEventSink));
1097 if (This == NULL)
1098 return E_OUTOFMEMORY;
1100 This->KeyEventSinkVtbl = &KeyEventSink_KeyEventSinkVtbl;
1101 This->refCount = 1;
1103 *ppOut = (ITfKeyEventSink*)This;
1104 return S_OK;
1108 static void test_KeystrokeMgr(void)
1110 ITfKeystrokeMgr *keymgr= NULL;
1111 HRESULT hr;
1112 TF_PRESERVEDKEY tfpk;
1113 BOOL preserved;
1114 ITfKeyEventSink *sink = NULL;
1116 KeyEventSink_Constructor(&sink);
1118 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfKeystrokeMgr, (LPVOID*)&keymgr);
1119 ok(SUCCEEDED(hr),"Failed to get IID_ITfKeystrokeMgr for ThreadMgr\n");
1121 tfpk.uVKey = 'A';
1122 tfpk.uModifiers = TF_MOD_SHIFT;
1124 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1125 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
1126 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1127 ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
1128 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
1129 ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
1131 hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1132 ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1134 hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1135 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1137 hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1138 ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1140 preserved = FALSE;
1141 hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1142 ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1143 if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
1145 hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1146 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1148 hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1149 ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1150 if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
1152 hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1153 ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
1155 hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1156 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1158 ITfKeystrokeMgr_Release(keymgr);
1159 ITfKeyEventSink_Release(sink);
1162 static void test_Activate(void)
1164 HRESULT hr;
1166 hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1167 ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1171 static void test_EnumContexts(ITfDocumentMgr *dm, ITfContext *search)
1173 HRESULT hr;
1174 IEnumTfContexts* pEnum;
1175 BOOL found = FALSE;
1177 hr = ITfDocumentMgr_EnumContexts(dm,&pEnum);
1178 ok(SUCCEEDED(hr),"EnumContexts failed\n");
1179 if (SUCCEEDED(hr))
1181 ULONG fetched;
1182 ITfContext *cxt;
1183 while (IEnumTfContexts_Next(pEnum, 1, &cxt, &fetched) == S_OK)
1185 if (!search)
1186 found = TRUE;
1187 else if (search == cxt)
1188 found = TRUE;
1189 ITfContext_Release(cxt);
1191 IEnumTfContexts_Release(pEnum);
1193 if (search)
1194 ok(found,"Did not find proper ITfContext\n");
1195 else
1196 ok(!found,"Found an ITfContext we should should not have\n");
1199 static void test_EnumDocumentMgr(ITfThreadMgr *tm, ITfDocumentMgr *search, ITfDocumentMgr *absent)
1201 HRESULT hr;
1202 IEnumTfDocumentMgrs* pEnum;
1203 BOOL found = FALSE;
1204 BOOL notfound = TRUE;
1206 hr = ITfThreadMgr_EnumDocumentMgrs(tm,&pEnum);
1207 ok(SUCCEEDED(hr),"EnumDocumentMgrs failed\n");
1208 if (SUCCEEDED(hr))
1210 ULONG fetched;
1211 ITfDocumentMgr *dm;
1212 while (IEnumTfDocumentMgrs_Next(pEnum, 1, &dm, &fetched) == S_OK)
1214 if (!search)
1215 found = TRUE;
1216 else if (search == dm)
1217 found = TRUE;
1218 if (absent && dm == absent)
1219 notfound = FALSE;
1220 ITfDocumentMgr_Release(dm);
1222 IEnumTfDocumentMgrs_Release(pEnum);
1224 if (search)
1225 ok(found,"Did not find proper ITfDocumentMgr\n");
1226 else
1227 ok(!found,"Found an ITfDocumentMgr we should should not have\n");
1228 if (absent)
1229 ok(notfound,"Found an ITfDocumentMgr we believe should be absent\n");
1232 static inline int check_context_refcount(ITfContext *iface)
1234 IUnknown_AddRef(iface);
1235 return IUnknown_Release(iface);
1239 /**********************************************************************
1240 * ITfTextEditSink
1241 **********************************************************************/
1242 typedef struct tagTextEditSink
1244 const ITfTextEditSinkVtbl *TextEditSinkVtbl;
1245 LONG refCount;
1246 } TextEditSink;
1248 static void TextEditSink_Destructor(TextEditSink *This)
1250 HeapFree(GetProcessHeap(),0,This);
1253 static HRESULT WINAPI TextEditSink_QueryInterface(ITfTextEditSink *iface, REFIID iid, LPVOID *ppvOut)
1255 TextEditSink *This = (TextEditSink *)iface;
1256 *ppvOut = NULL;
1258 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextEditSink))
1260 *ppvOut = This;
1263 if (*ppvOut)
1265 IUnknown_AddRef(iface);
1266 return S_OK;
1269 return E_NOINTERFACE;
1272 static ULONG WINAPI TextEditSink_AddRef(ITfTextEditSink *iface)
1274 TextEditSink *This = (TextEditSink *)iface;
1275 return InterlockedIncrement(&This->refCount);
1278 static ULONG WINAPI TextEditSink_Release(ITfTextEditSink *iface)
1280 TextEditSink *This = (TextEditSink *)iface;
1281 ULONG ret;
1283 ret = InterlockedDecrement(&This->refCount);
1284 if (ret == 0)
1285 TextEditSink_Destructor(This);
1286 return ret;
1289 static HRESULT WINAPI TextEditSink_OnEndEdit(ITfTextEditSink *iface,
1290 ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
1292 sink_fire_ok(&test_OnEndEdit,"TextEditSink_OnEndEdit");
1293 return S_OK;
1296 static const ITfTextEditSinkVtbl TextEditSink_TextEditSinkVtbl =
1298 TextEditSink_QueryInterface,
1299 TextEditSink_AddRef,
1300 TextEditSink_Release,
1302 TextEditSink_OnEndEdit
1305 static HRESULT TextEditSink_Constructor(ITfTextEditSink **ppOut)
1307 TextEditSink *This;
1309 *ppOut = NULL;
1310 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextEditSink));
1311 if (This == NULL)
1312 return E_OUTOFMEMORY;
1314 This->TextEditSinkVtbl = &TextEditSink_TextEditSinkVtbl;
1315 This->refCount = 1;
1317 *ppOut = (ITfTextEditSink*)This;
1318 return S_OK;
1321 static void test_startSession(void)
1323 HRESULT hr;
1324 DWORD cnt;
1325 DWORD editCookie;
1326 ITfDocumentMgr *dmtest;
1327 ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
1328 ITextStoreACP *ts;
1329 TfClientId cid2 = 0;
1331 hr = ITfThreadMgr_Deactivate(g_tm);
1332 ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1334 test_ShouldActivate = TRUE;
1335 hr = ITfThreadMgr_Activate(g_tm,&cid);
1336 ok(SUCCEEDED(hr),"Failed to Activate\n");
1337 ok(cid != tid,"TextService id mistakenly matches Client id\n");
1339 test_ShouldActivate = FALSE;
1340 hr = ITfThreadMgr_Activate(g_tm,&cid2);
1341 ok(SUCCEEDED(hr),"Failed to Activate\n");
1342 ok (cid == cid2, "Second activate client ID does not match\n");
1344 hr = ITfThreadMgr_Deactivate(g_tm);
1345 ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1347 test_EnumDocumentMgr(g_tm,NULL,NULL);
1349 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1350 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1352 test_EnumDocumentMgr(g_tm,g_dm,NULL);
1354 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dmtest);
1355 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1357 test_EnumDocumentMgr(g_tm,dmtest,NULL);
1359 ITfDocumentMgr_Release(dmtest);
1360 test_EnumDocumentMgr(g_tm,g_dm,dmtest);
1362 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1363 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1364 ok(dmtest == NULL,"Initial focus not null\n");
1366 test_CurrentFocus = g_dm;
1367 test_PrevFocus = NULL;
1368 test_OnSetFocus = SINK_EXPECTED;
1369 hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
1370 ok(SUCCEEDED(hr),"SetFocus Failed\n");
1371 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1373 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1374 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1375 ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1377 cnt = ITfDocumentMgr_Release(g_dm);
1378 ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
1380 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1381 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1382 ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1383 ITfDocumentMgr_Release(dmtest);
1385 TextStoreACP_Constructor((IUnknown**)&ts);
1387 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1388 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1390 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1391 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1393 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1394 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1396 test_EnumContexts(g_dm, NULL);
1398 hr = ITfContext_GetDocumentMgr(cxt,&dmtest);
1399 ok(hr == S_OK, "ITfContext_GetDocumentMgr failed with %x\n",hr);
1400 ok(dmtest == g_dm, "Wrong documentmgr\n");
1401 ITfDocumentMgr_Release(dmtest);
1403 cnt = check_context_refcount(cxt);
1404 test_OnPushContext = SINK_EXPECTED;
1405 test_ACP_AdviseSink = SINK_EXPECTED;
1406 test_OnInitDocumentMgr = SINK_EXPECTED;
1407 hr = ITfDocumentMgr_Push(g_dm, cxt);
1408 ok(SUCCEEDED(hr),"Push Failed\n");
1409 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1410 sink_check_ok(&test_OnPushContext,"OnPushContext");
1411 sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
1412 sink_check_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
1414 test_EnumContexts(g_dm, cxt);
1416 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1417 ok(SUCCEEDED(hr),"GetTop Failed\n");
1418 ok(cxtTest == cxt, "Wrong context on top\n");
1419 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1420 cnt = ITfContext_Release(cxtTest);
1422 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1423 ok(SUCCEEDED(hr),"GetBase Failed\n");
1424 ok(cxtTest == cxt, "Wrong context on Base\n");
1425 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1426 ITfContext_Release(cxtTest);
1428 check_context_refcount(cxt2);
1429 test_OnPushContext = SINK_EXPECTED;
1430 hr = ITfDocumentMgr_Push(g_dm, cxt2);
1431 ok(SUCCEEDED(hr),"Push Failed\n");
1432 sink_check_ok(&test_OnPushContext,"OnPushContext");
1434 cnt = check_context_refcount(cxt2);
1435 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1436 ok(SUCCEEDED(hr),"GetTop Failed\n");
1437 ok(cxtTest == cxt2, "Wrong context on top\n");
1438 ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1439 ITfContext_Release(cxtTest);
1441 cnt = check_context_refcount(cxt);
1442 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1443 ok(SUCCEEDED(hr),"GetBase Failed\n");
1444 ok(cxtTest == cxt, "Wrong context on Base\n");
1445 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1446 ITfContext_Release(cxtTest);
1448 cnt = check_context_refcount(cxt3);
1449 hr = ITfDocumentMgr_Push(g_dm, cxt3);
1450 ok(FAILED(hr),"Push Succeeded\n");
1451 ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
1453 cnt = check_context_refcount(cxt2);
1454 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1455 ok(SUCCEEDED(hr),"GetTop Failed\n");
1456 ok(cxtTest == cxt2, "Wrong context on top\n");
1457 ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1458 ITfContext_Release(cxtTest);
1460 cnt = check_context_refcount(cxt);
1461 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1462 ok(SUCCEEDED(hr),"GetBase Failed\n");
1463 ok(cxtTest == cxt, "Wrong context on Base\n");
1464 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1465 ITfContext_Release(cxtTest);
1467 cnt = check_context_refcount(cxt2);
1468 test_OnPopContext = SINK_EXPECTED;
1469 hr = ITfDocumentMgr_Pop(g_dm, 0);
1470 ok(SUCCEEDED(hr),"Pop Failed\n");
1471 ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1472 sink_check_ok(&test_OnPopContext,"OnPopContext");
1474 dmtest = (void *)0xfeedface;
1475 hr = ITfContext_GetDocumentMgr(cxt2,&dmtest);
1476 ok(hr == S_FALSE, "ITfContext_GetDocumentMgr wrong rc %x\n",hr);
1477 ok(dmtest == NULL,"returned documentmgr should be null\n");
1479 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1480 ok(SUCCEEDED(hr),"GetTop Failed\n");
1481 ok(cxtTest == cxt, "Wrong context on top\n");
1482 ITfContext_Release(cxtTest);
1484 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1485 ok(SUCCEEDED(hr),"GetBase Failed\n");
1486 ok(cxtTest == cxt, "Wrong context on base\n");
1487 ITfContext_Release(cxtTest);
1489 hr = ITfDocumentMgr_Pop(g_dm, 0);
1490 ok(FAILED(hr),"Pop Succeeded\n");
1492 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1493 ok(SUCCEEDED(hr),"GetTop Failed\n");
1494 ok(cxtTest == cxt, "Wrong context on top\n");
1495 ITfContext_Release(cxtTest);
1497 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1498 ok(SUCCEEDED(hr),"GetBase Failed\n");
1499 ok(cxtTest == cxt, "Wrong context on base\n");
1500 ITfContext_Release(cxtTest);
1502 ITfContext_Release(cxt);
1503 ITfContext_Release(cxt2);
1504 ITfContext_Release(cxt3);
1507 static void test_endSession(void)
1509 HRESULT hr;
1510 test_ShouldDeactivate = TRUE;
1511 test_CurrentFocus = NULL;
1512 test_PrevFocus = g_dm;
1513 test_OnSetFocus = SINK_EXPECTED;
1514 hr = ITfThreadMgr_Deactivate(g_tm);
1515 ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1516 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1517 test_OnSetFocus = SINK_UNEXPECTED;
1520 static void test_TfGuidAtom(void)
1522 GUID gtest,g1;
1523 HRESULT hr;
1524 TfGuidAtom atom1,atom2;
1525 BOOL equal;
1527 CoCreateGuid(&gtest);
1529 /* msdn reports this should return E_INVALIDARG. However my test show it crashing (winxp)*/
1531 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1532 ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1534 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
1535 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1536 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
1537 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1538 ok(atom1 == atom2,"atoms do not match\n");
1539 hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
1540 ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
1541 hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
1542 ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1543 ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
1544 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
1545 ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
1546 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
1547 ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1548 ok(equal == TRUE,"Equal value invalid\n");
1550 /* show that cid and tid TfClientIds are also TfGuidAtoms */
1551 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
1552 ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1553 ok(equal == TRUE,"Equal value invalid\n");
1554 hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
1555 ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1556 ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
1559 static void test_ClientId(void)
1561 ITfClientId *pcid;
1562 TfClientId id1,id2;
1563 HRESULT hr;
1564 GUID g2;
1566 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1567 ok(SUCCEEDED(hr),"Unable to acquire ITfClientId interface\n");
1569 CoCreateGuid(&g2);
1571 hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
1572 ok(SUCCEEDED(hr),"GetClientId failed\n");
1573 hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
1574 ok(SUCCEEDED(hr),"GetClientId failed\n");
1575 ok(id1==id2,"Id's for GUID_NULL do not match\n");
1576 hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
1577 ok(SUCCEEDED(hr),"GetClientId failed\n");
1578 ok(id2!=id1,"Id matches GUID_NULL\n");
1579 ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
1580 ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
1581 hr = ITfClientId_GetClientId(pcid,&g2,&id2);
1582 ok(SUCCEEDED(hr),"GetClientId failed\n");
1583 ok(id2!=id1,"Id matches GUID_NULL\n");
1584 ok(id2!=tid,"Id for random guid matching tid\n");
1585 ok(id2!=cid,"Id for random guid matching cid\n");
1586 ITfClientId_Release(pcid);
1589 /**********************************************************************
1590 * ITfEditSession
1591 **********************************************************************/
1592 typedef struct tagEditSession
1594 const ITfEditSessionVtbl *EditSessionVtbl;
1595 LONG refCount;
1596 } EditSession;
1598 static void EditSession_Destructor(EditSession *This)
1600 HeapFree(GetProcessHeap(),0,This);
1603 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1605 EditSession *This = (EditSession *)iface;
1606 *ppvOut = NULL;
1608 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1610 *ppvOut = This;
1613 if (*ppvOut)
1615 IUnknown_AddRef(iface);
1616 return S_OK;
1619 return E_NOINTERFACE;
1622 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1624 EditSession *This = (EditSession *)iface;
1625 return InterlockedIncrement(&This->refCount);
1628 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1630 EditSession *This = (EditSession *)iface;
1631 ULONG ret;
1633 ret = InterlockedDecrement(&This->refCount);
1634 if (ret == 0)
1635 EditSession_Destructor(This);
1636 return ret;
1639 static void test_InsertAtSelection(TfEditCookie ec, ITfContext *cxt)
1641 HRESULT hr;
1642 ITfInsertAtSelection *iis;
1643 ITfRange *range=NULL;
1644 static const WCHAR txt[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
1646 hr = ITfContext_QueryInterface(cxt, &IID_ITfInsertAtSelection , (LPVOID*)&iis);
1647 ok(SUCCEEDED(hr),"Failed to get ITfInsertAtSelection interface\n");
1648 test_ACP_InsertTextAtSelection = SINK_EXPECTED;
1649 hr = ITfInsertAtSelection_InsertTextAtSelection(iis, ec, 0, txt, 11, &range);
1650 ok(SUCCEEDED(hr),"ITfInsertAtSelection_InsertTextAtSelection failed %x\n",hr);
1651 sink_check_ok(&test_ACP_InsertTextAtSelection,"InsertTextAtSelection");
1652 ok(range != NULL,"No range returned\n");
1653 ITfRange_Release(range);
1654 ITfInsertAtSelection_Release(iis);
1657 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1658 TfEditCookie ec)
1660 ITfContext *cxt;
1661 ITfDocumentMgr *dm;
1662 ITfRange *range;
1663 TF_SELECTION selection;
1664 ULONG fetched;
1665 HRESULT hr;
1667 sink_fire_ok(&test_DoEditSession,"EditSession_DoEditSession");
1668 sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1670 ITfThreadMgr_GetFocus(g_tm, &dm);
1671 ITfDocumentMgr_GetTop(dm,&cxt);
1673 hr = ITfContext_GetStart(cxt,ec,NULL);
1674 ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1676 range = (ITfRange*)0xdeaddead;
1677 hr = ITfContext_GetStart(cxt,0xdeadcafe,&range);
1678 ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1679 ok(range == NULL,"Range not set to NULL\n");
1681 hr = ITfContext_GetStart(cxt,ec,&range);
1682 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1683 ok(range != NULL,"Range set to NULL\n");
1685 ITfRange_Release(range);
1687 hr = ITfContext_GetEnd(cxt,ec,NULL);
1688 ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1690 range = (ITfRange*)0xdeaddead;
1691 hr = ITfContext_GetEnd(cxt,0xdeadcafe,&range);
1692 ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1693 ok(range == NULL,"Range not set to NULL\n");
1695 test_ACP_GetEndACP = SINK_EXPECTED;
1696 hr = ITfContext_GetEnd(cxt,ec,&range);
1697 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1698 ok(range != NULL,"Range set to NULL\n");
1699 sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1701 ITfRange_Release(range);
1703 selection.range = NULL;
1704 test_ACP_GetSelection = SINK_EXPECTED;
1705 hr = ITfContext_GetSelection(cxt, ec, TF_DEFAULT_SELECTION, 1, &selection, &fetched);
1706 ok(SUCCEEDED(hr),"ITfContext_GetSelection failed\n");
1707 ok(fetched == 1,"fetched incorrect\n");
1708 ok(selection.range != NULL,"NULL range\n");
1709 sink_check_ok(&test_ACP_GetSelection,"ACP_GetSepection");
1710 ITfRange_Release(selection.range);
1712 test_InsertAtSelection(ec, cxt);
1714 test_ACP_GetEndACP = SINK_EXPECTED;
1715 hr = ITfContext_GetEnd(cxt,ec,&range);
1716 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1717 ok(range != NULL,"Range set to NULL\n");
1718 sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1720 selection.range = range;
1721 selection.style.ase = TF_AE_NONE;
1722 selection.style.fInterimChar = FALSE;
1723 test_ACP_SetSelection = SINK_EXPECTED;
1724 hr = ITfContext_SetSelection(cxt, ec, 1, &selection);
1725 sink_check_ok(&test_ACP_SetSelection,"SetSelection");
1726 ITfRange_Release(range);
1728 ITfContext_Release(cxt);
1729 ITfDocumentMgr_Release(dm);
1730 return 0xdeadcafe;
1733 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1735 EditSession_QueryInterface,
1736 EditSession_AddRef,
1737 EditSession_Release,
1739 EditSession_DoEditSession
1742 static HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1744 EditSession *This;
1746 *ppOut = NULL;
1747 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1748 if (This == NULL)
1749 return E_OUTOFMEMORY;
1751 This->EditSessionVtbl = &EditSession_EditSessionVtbl;
1752 This->refCount = 1;
1754 *ppOut = (ITfEditSession*)This;
1755 return S_OK;
1758 static void test_TStoApplicationText(void)
1760 HRESULT hr, hrSession;
1761 ITfEditSession *es;
1762 ITfContext *cxt;
1763 ITfDocumentMgr *dm;
1764 ITfTextEditSink *sink;
1765 ITfSource *source = NULL;
1766 DWORD editSinkCookie = -1;
1768 ITfThreadMgr_GetFocus(g_tm, &dm);
1769 EditSession_Constructor(&es);
1770 ITfDocumentMgr_GetTop(dm,&cxt);
1772 TextEditSink_Constructor(&sink);
1773 hr = ITfContext_QueryInterface(cxt,&IID_ITfSource,(LPVOID*)&source);
1774 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for Context\n");
1775 if (source)
1777 hr = ITfSource_AdviseSink(source, &IID_ITfTextEditSink, (LPVOID)sink, &editSinkCookie);
1778 ok(SUCCEEDED(hr),"Failed to advise Sink\n");
1779 ok(editSinkCookie != -1,"Failed to get sink cookie\n");
1782 hrSession = 0xfeedface;
1783 /* Test no premissions flags */
1784 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC, &hrSession);
1785 ok(hr == E_INVALIDARG,"RequestEditSession should have failed with %x not %x\n",E_INVALIDARG,hr);
1786 ok(hrSession == E_FAIL,"hrSession should be %x not %x\n",E_FAIL,hrSession);
1788 documentStatus = TS_SD_READONLY;
1789 hrSession = 0xfeedface;
1790 test_ACP_GetStatus = SINK_EXPECTED;
1791 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1792 ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1793 ok(hrSession == TS_E_READONLY,"Unexpected hrSession (%x)\n",hrSession);
1794 sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1796 /* signal a change to allow readwrite sessions */
1797 documentStatus = 0;
1798 test_ACP_RequestLock = SINK_EXPECTED;
1799 ITextStoreACPSink_OnStatusChange(ACPSink,documentStatus);
1800 sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1802 test_ACP_GetStatus = SINK_EXPECTED;
1803 test_ACP_RequestLock = SINK_EXPECTED;
1804 test_DoEditSession = SINK_EXPECTED;
1805 hrSession = 0xfeedface;
1806 test_OnEndEdit = SINK_EXPECTED;
1807 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1808 ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1809 sink_check_ok(&test_OnEndEdit,"OnEndEdit");
1810 sink_check_ok(&test_DoEditSession,"DoEditSession");
1811 sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1812 ok(hrSession == 0xdeadcafe,"Unexpected hrSession (%x)\n",hrSession);
1814 if (source)
1816 hr = ITfSource_UnadviseSink(source, editSinkCookie);
1817 ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1818 ITfTextEditSink_Release(sink);
1819 ITfSource_Release(source);
1822 ITfContext_Release(cxt);
1823 ITfDocumentMgr_Release(dm);
1824 ITfEditSession_Release(es);
1827 static void enum_compartments(ITfCompartmentMgr *cmpmgr, REFGUID present, REFGUID absent)
1829 BOOL found,found2;
1830 IEnumGUID *ppEnum;
1831 found = FALSE;
1832 found2 = FALSE;
1833 if (SUCCEEDED(ITfCompartmentMgr_EnumCompartments(cmpmgr, &ppEnum)))
1835 ULONG fetched;
1836 GUID g;
1837 while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
1839 WCHAR str[50];
1840 CHAR strA[50];
1841 StringFromGUID2(&g,str,50);
1842 WideCharToMultiByte(CP_ACP,0,str,50,strA,50,0,0);
1843 trace("found %s\n",strA);
1844 if (present && IsEqualGUID(present,&g))
1845 found = TRUE;
1846 if (absent && IsEqualGUID(absent, &g))
1847 found2 = TRUE;
1849 IEnumGUID_Release(ppEnum);
1851 if (present)
1852 ok(found,"Did not find compartment\n");
1853 if (absent)
1854 ok(!found2,"Found compartment that should be absent\n");
1857 static void test_Compartments(void)
1859 ITfContext *cxt;
1860 ITfDocumentMgr *dm;
1861 ITfCompartmentMgr *cmpmgr;
1862 ITfCompartment *cmp;
1863 HRESULT hr;
1865 ITfThreadMgr_GetFocus(g_tm, &dm);
1866 ITfDocumentMgr_GetTop(dm,&cxt);
1868 /* Global */
1869 hr = ITfThreadMgr_GetGlobalCompartment(g_tm, &cmpmgr);
1870 ok(SUCCEEDED(hr),"GetGlobalCompartment failed\n");
1871 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_SPEECH_OPENCLOSE, &cmp);
1872 ok(SUCCEEDED(hr),"GetCompartment failed\n");
1873 ITfCompartment_Release(cmp);
1874 enum_compartments(cmpmgr,&GUID_COMPARTMENT_SPEECH_OPENCLOSE,NULL);
1875 ITfCompartmentMgr_Release(cmpmgr);
1877 /* Thread */
1878 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1879 ok(SUCCEEDED(hr),"ThreadMgr QI for IID_ITfCompartmentMgr failed\n");
1880 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &CLSID_FakeService, &cmp);
1881 ok(SUCCEEDED(hr),"GetCompartment failed\n");
1882 enum_compartments(cmpmgr,&CLSID_FakeService,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1883 ITfCompartmentMgr_ClearCompartment(cmpmgr,tid,&CLSID_FakeService);
1884 enum_compartments(cmpmgr,NULL,&CLSID_FakeService);
1885 ITfCompartmentMgr_Release(cmpmgr);
1886 ITfCompartment_Release(cmp);
1888 /* DocumentMgr */
1889 hr = ITfDocumentMgr_QueryInterface(dm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1890 ok(SUCCEEDED(hr),"DocumentMgr QI for IID_ITfCompartmentMgr failed\n");
1892 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_PERSISTMENUENABLED, &cmp);
1893 ok(SUCCEEDED(hr),"GetCompartment failed\n");
1894 enum_compartments(cmpmgr,&GUID_COMPARTMENT_PERSISTMENUENABLED,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1895 ITfCompartmentMgr_Release(cmpmgr);
1897 /* Context */
1898 hr = ITfContext_QueryInterface(cxt, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1899 ok(SUCCEEDED(hr),"Context QI for IID_ITfCompartmentMgr failed\n");
1900 enum_compartments(cmpmgr,NULL,&GUID_COMPARTMENT_PERSISTMENUENABLED);
1901 ITfCompartmentMgr_Release(cmpmgr);
1903 ITfContext_Release(cxt);
1904 ITfDocumentMgr_Release(dm);
1907 static void processPendingMessages(void)
1909 MSG msg;
1910 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1911 TranslateMessage(&msg);
1912 DispatchMessage(&msg);
1916 static void test_AssociateFocus(void)
1918 ITfDocumentMgr *dm1, *dm2, *olddm, *dmcheck, *dmorig;
1919 HWND wnd1, wnd2, wnd3;
1920 HRESULT hr;
1922 ITfThreadMgr_GetFocus(g_tm, &dmorig);
1923 test_CurrentFocus = NULL;
1924 test_PrevFocus = dmorig;
1925 test_OnSetFocus = SINK_EXPECTED;
1926 hr = ITfThreadMgr_SetFocus(g_tm,NULL);
1927 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1928 ITfDocumentMgr_Release(dmorig);
1930 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm1);
1931 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1933 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm2);
1934 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1936 wnd1 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1937 ok(wnd1!=NULL,"Unable to create window 1\n");
1938 wnd2 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1939 ok(wnd2!=NULL,"Unable to create window 2\n");
1940 wnd3 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1941 ok(wnd3!=NULL,"Unable to create window 3\n");
1943 processPendingMessages();
1945 SetFocus(wnd1);
1946 processPendingMessages();
1947 test_CurrentFocus = dm1;
1948 test_PrevFocus = NULL;
1949 test_OnSetFocus = SINK_EXPECTED;
1950 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,dm1,&olddm);
1951 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1952 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1953 ok(olddm == NULL, "unexpected old DocumentMgr\n");
1955 processPendingMessages();
1957 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1958 ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1959 ITfDocumentMgr_Release(dmcheck);
1961 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,dm2,&olddm);
1962 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1963 processPendingMessages();
1964 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1965 ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1966 ITfDocumentMgr_Release(dmcheck);
1968 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,dm2,&olddm);
1969 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1970 processPendingMessages();
1971 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1972 ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1973 ITfDocumentMgr_Release(dmcheck);
1975 test_CurrentFocus = dm2;
1976 test_PrevFocus = dm1;
1977 test_OnSetFocus = SINK_EXPECTED;
1978 SetFocus(wnd2);
1979 processPendingMessages();
1980 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1982 SetFocus(wnd3);
1983 processPendingMessages();
1985 test_CurrentFocus = dm1;
1986 test_PrevFocus = dm2;
1987 test_OnSetFocus = SINK_EXPECTED;
1988 SetFocus(wnd1);
1989 processPendingMessages();
1990 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1992 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,NULL,&olddm);
1993 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1994 ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
1995 ITfDocumentMgr_Release(olddm);
1997 test_CurrentFocus = dmorig;
1998 test_PrevFocus = dm1;
1999 test_OnSetFocus = SINK_EXPECTED;
2000 test_ACP_GetStatus = SINK_EXPECTED;
2001 ITfThreadMgr_SetFocus(g_tm,dmorig);
2002 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2004 test_CurrentFocus = NULL;
2005 test_PrevFocus = dmorig;
2006 test_OnSetFocus = SINK_EXPECTED;
2007 SetFocus(wnd3);
2008 processPendingMessages();
2009 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2011 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,NULL,&olddm);
2012 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2013 ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2014 ITfDocumentMgr_Release(olddm);
2015 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,NULL,&olddm);
2016 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2017 ok(olddm == dm1, "incorrect old DocumentMgr returned\n");
2018 ITfDocumentMgr_Release(olddm);
2020 SetFocus(wnd2);
2021 processPendingMessages();
2022 SetFocus(wnd1);
2023 processPendingMessages();
2025 ITfDocumentMgr_Release(dm1);
2026 ITfDocumentMgr_Release(dm2);
2027 DestroyWindow(wnd1);
2028 DestroyWindow(wnd2);
2029 DestroyWindow(wnd3);
2031 test_CurrentFocus = dmorig;
2032 test_PrevFocus = NULL;
2033 test_OnSetFocus = SINK_EXPECTED;
2034 test_ACP_GetStatus = SINK_IGNORE;
2035 ITfThreadMgr_SetFocus(g_tm,dmorig);
2036 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2039 START_TEST(inputprocessor)
2041 if (SUCCEEDED(initialize()))
2043 test_Register();
2044 test_RegisterCategory();
2045 test_EnumInputProcessorInfo();
2046 test_Enable();
2047 test_ThreadMgrAdviseSinks();
2048 test_Activate();
2049 test_startSession();
2050 test_TfGuidAtom();
2051 test_ClientId();
2052 test_KeystrokeMgr();
2053 test_TStoApplicationText();
2054 test_Compartments();
2055 test_AssociateFocus();
2056 test_endSession();
2057 test_EnumLanguageProfiles();
2058 test_FindClosestCategory();
2059 test_Disable();
2060 test_ThreadMgrUnadviseSinks();
2061 test_UnregisterCategory();
2062 test_Unregister();
2064 else
2065 skip("Unable to create InputProcessor\n");
2066 cleanup();