msctf/tests: Correct wine_todo handling in sinks.
[wine] / dlls / msctf / tests / inputprocessor.c
1 /*
2  * Unit tests for ITfInputProcessor
3  *
4  * Copyright 2009 Aric Stewart, CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdio.h>
22
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"
32
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;
40
41 static ITextStoreACPSink *ACPSink;
42
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
48
49 #define SINK_ACTION_MASK 0xff
50 #define SINK_OPTION_MASK 0xff00
51 #define SINK_EXPECTED_COUNT_MASK 0xff0000
52
53 #define SINK_OPTION_TODO      0x0100
54
55 static BOOL test_ShouldActivate = FALSE;
56 static BOOL test_ShouldDeactivate = FALSE;
57
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;
77
78
79 static inline int expected_count(int *sink)
80 {
81     return (*sink & SINK_EXPECTED_COUNT_MASK)>>16;
82 }
83
84 static inline void _sink_fire_ok(INT *sink, const CHAR* name)
85 {
86     int count;
87     int todo = *sink & SINK_OPTION_TODO;
88     int action = *sink & SINK_ACTION_MASK;
89
90     if (winetest_interactive)
91         winetest_trace("firing %s\n",name);
92
93     switch (action)
94     {
95         case SINK_OPTIONAL:
96         case SINK_EXPECTED:
97             count = expected_count(sink);
98             if (count > 1)
99             {
100                 count --;
101                 *sink = (*sink & ~SINK_EXPECTED_COUNT_MASK) + (count << 16);
102                 return;
103             }
104             break;
105         case SINK_IGNORE:
106             winetest_trace("Ignoring %s\n",name);
107             return;
108         default:
109             if (todo)
110                 todo_wine winetest_ok(0, "Unexpected %s sink\n",name);
111             else
112                 winetest_ok(0, "Unexpected %s sink\n",name);
113     }
114     *sink = SINK_FIRED;
115 }
116
117 #define sink_fire_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_fire_ok(a,b)
118
119 inline void _sink_check_ok(INT *sink, const CHAR* name)
120 {
121     int action = *sink & SINK_ACTION_MASK;
122
123     switch (action)
124     {
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);
132     }
133     *sink = SINK_UNEXPECTED;
134 }
135
136 #define sink_check_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_check_ok(a,b)
137
138 /**********************************************************************
139  * ITextStoreACP
140  **********************************************************************/
141 typedef struct tagTextStoreACP
142 {
143     const ITextStoreACPVtbl *TextStoreACPVtbl;
144     LONG refCount;
145
146 } TextStoreACP;
147
148 static void TextStoreACP_Destructor(TextStoreACP *This)
149 {
150     HeapFree(GetProcessHeap(),0,This);
151 }
152
153 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
154 {
155     TextStoreACP *This = (TextStoreACP *)iface;
156     *ppvOut = NULL;
157
158     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
159     {
160         *ppvOut = This;
161     }
162
163     if (*ppvOut)
164     {
165         IUnknown_AddRef(iface);
166         return S_OK;
167     }
168
169     return E_NOINTERFACE;
170 }
171
172 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
173 {
174     TextStoreACP *This = (TextStoreACP *)iface;
175     return InterlockedIncrement(&This->refCount);
176 }
177
178 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
179 {
180     TextStoreACP *This = (TextStoreACP *)iface;
181     ULONG ret;
182
183     ret = InterlockedDecrement(&This->refCount);
184     if (ret == 0)
185         TextStoreACP_Destructor(This);
186     return ret;
187 }
188
189 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
190     REFIID riid, IUnknown *punk, DWORD dwMask)
191 {
192     HRESULT hr;
193
194     sink_fire_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
195
196     hr = IUnknown_QueryInterface(punk, &IID_ITextStoreACPSink,(LPVOID*)(&ACPSink));
197     ok(SUCCEEDED(hr),"Unable to QueryInterface on sink\n");
198     return S_OK;
199 }
200
201 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
202     IUnknown *punk)
203 {
204     trace("\n");
205     return S_OK;
206 }
207
208 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
209     DWORD dwLockFlags, HRESULT *phrSession)
210 {
211     sink_fire_ok(&test_ACP_RequestLock,"TextStoreACP_RequestLock");
212     *phrSession = ITextStoreACPSink_OnLockGranted(ACPSink, dwLockFlags);
213     return S_OK;
214 }
215 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
216     TS_STATUS *pdcs)
217 {
218     sink_fire_ok(&test_ACP_GetStatus,"TextStoreACP_GetStatus");
219     pdcs->dwDynamicFlags = documentStatus;
220     return S_OK;
221 }
222 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
223     LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
224     LONG *pacpResultEnd)
225 {
226     trace("\n");
227     return S_OK;
228 }
229 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
230     ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
231 {
232     sink_fire_ok(&test_ACP_GetSelection,"TextStoreACP_GetSelection");
233
234     pSelection->acpStart = 10;
235     pSelection->acpEnd = 20;
236     pSelection->style.fInterimChar = 0;
237     pSelection->style.ase = TS_AE_NONE;
238     *pcFetched = 1;
239
240     return S_OK;
241 }
242 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
243     ULONG ulCount, const TS_SELECTION_ACP *pSelection)
244 {
245     sink_fire_ok(&test_ACP_SetSelection,"TextStoreACP_SetSelection");
246     return S_OK;
247 }
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)
252 {
253     trace("\n");
254     return S_OK;
255 }
256 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
257     DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
258     ULONG cch, TS_TEXTCHANGE *pChange)
259 {
260     trace("\n");
261     return S_OK;
262 }
263 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
264     LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
265 {
266     trace("\n");
267     return S_OK;
268 }
269 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
270     LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
271 {
272     trace("\n");
273     return S_OK;
274 }
275 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
276     const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
277 {
278     trace("\n");
279     return S_OK;
280 }
281 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
282     DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
283     TS_TEXTCHANGE *pChange)
284 {
285     trace("\n");
286     return S_OK;
287 }
288 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
289     DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
290     LONG *pacpEnd, TS_TEXTCHANGE *pChange)
291 {
292     sink_fire_ok(&test_ACP_InsertTextAtSelection,"TextStoreACP_InsertTextAtSelection");
293     return S_OK;
294 }
295 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
296     DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
297     TS_TEXTCHANGE *pChange)
298 {
299     trace("\n");
300     return S_OK;
301 }
302 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
303     DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
304 {
305     trace("\n");
306     return S_OK;
307 }
308 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
309     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
310     DWORD dwFlags)
311 {
312     trace("\n");
313     return S_OK;
314 }
315 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
316     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
317     DWORD dwFlags)
318 {
319     trace("\n");
320     return S_OK;
321 }
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)
325 {
326     trace("\n");
327     return S_OK;
328 }
329 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
330     ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
331 {
332     trace("\n");
333     return S_OK;
334 }
335 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
336     LONG *pacp)
337 {
338     sink_fire_ok(&test_ACP_GetEndACP,"TextStoreACP_GetEndACP");
339     return S_OK;
340 }
341 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
342     TsViewCookie *pvcView)
343 {
344     trace("\n");
345     return S_OK;
346 }
347 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
348     TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
349     LONG *pacp)
350 {
351     trace("\n");
352     return S_OK;
353 }
354 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
355     TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
356     BOOL *pfClipped)
357 {
358     trace("\n");
359     return S_OK;
360 }
361 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
362     TsViewCookie vcView, RECT *prc)
363 {
364     trace("\n");
365     return S_OK;
366 }
367 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
368     TsViewCookie vcView, HWND *phwnd)
369 {
370     trace("\n");
371     return S_OK;
372 }
373
374 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
375 {
376     TextStoreACP_QueryInterface,
377     TextStoreACP_AddRef,
378     TextStoreACP_Release,
379
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
406 };
407
408 static HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
409 {
410     TextStoreACP *This;
411
412     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
413     if (This == NULL)
414         return E_OUTOFMEMORY;
415
416     This->TextStoreACPVtbl = &TextStoreACP_TextStoreACPVtbl;
417     This->refCount = 1;
418
419     *ppOut = (IUnknown *)This;
420     return S_OK;
421 }
422
423 /**********************************************************************
424  * ITfThreadMgrEventSink
425  **********************************************************************/
426 typedef struct tagThreadMgrEventSink
427 {
428     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl;
429     LONG refCount;
430 } ThreadMgrEventSink;
431
432 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
433 {
434     HeapFree(GetProcessHeap(),0,This);
435 }
436
437 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
438 {
439     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
440     *ppvOut = NULL;
441
442     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
443     {
444         *ppvOut = This;
445     }
446
447     if (*ppvOut)
448     {
449         IUnknown_AddRef(iface);
450         return S_OK;
451     }
452
453     return E_NOINTERFACE;
454 }
455
456 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
457 {
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);
461 }
462
463 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
464 {
465     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
466     ULONG ret;
467
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;
473 }
474
475 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
476 ITfDocumentMgr *pdim)
477 {
478     sink_fire_ok(&test_OnInitDocumentMgr,"ThreadMgrEventSink_OnInitDocumentMgr");
479     return S_OK;
480 }
481
482 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
483 ITfDocumentMgr *pdim)
484 {
485     trace("\n");
486     return S_OK;
487 }
488
489 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
490 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
491 {
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;
496 }
497
498 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(ITfThreadMgrEventSink *iface,
499 ITfContext *pic)
500 {
501     HRESULT hr;
502     ITfDocumentMgr *docmgr;
503     ITfContext *test;
504
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);
514
515     sink_fire_ok(&test_OnPushContext,"ThreadMgrEventSink_OnPushContext");
516     return S_OK;
517 }
518
519 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
520 ITfContext *pic)
521 {
522     HRESULT hr;
523     ITfDocumentMgr *docmgr;
524     ITfContext *test;
525
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);
535
536     sink_fire_ok(&test_OnPopContext,"ThreadMgrEventSink_OnPopContext");
537     return S_OK;
538 }
539
540 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
541 {
542     ThreadMgrEventSink_QueryInterface,
543     ThreadMgrEventSink_AddRef,
544     ThreadMgrEventSink_Release,
545
546     ThreadMgrEventSink_OnInitDocumentMgr,
547     ThreadMgrEventSink_OnUninitDocumentMgr,
548     ThreadMgrEventSink_OnSetFocus,
549     ThreadMgrEventSink_OnPushContext,
550     ThreadMgrEventSink_OnPopContext
551 };
552
553 static HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
554 {
555     ThreadMgrEventSink *This;
556
557     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
558     if (This == NULL)
559         return E_OUTOFMEMORY;
560
561     This->ThreadMgrEventSinkVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
562     This->refCount = 1;
563
564     *ppOut = (IUnknown *)This;
565     return S_OK;
566 }
567
568
569 /********************************************************************************************
570  * Stub text service for testing
571  ********************************************************************************************/
572
573 static LONG TS_refCount;
574 static IClassFactory *cf;
575 static DWORD regid;
576
577 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
578
579 typedef struct tagClassFactory
580 {
581     const IClassFactoryVtbl *vtbl;
582     LONG   ref;
583     LPFNCONSTRUCTOR ctor;
584 } ClassFactory;
585
586 typedef struct tagTextService
587 {
588     const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
589     LONG refCount;
590 } TextService;
591
592 static void ClassFactory_Destructor(ClassFactory *This)
593 {
594     HeapFree(GetProcessHeap(),0,This);
595     TS_refCount--;
596 }
597
598 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
599 {
600     *ppvOut = NULL;
601     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
602     {
603         IClassFactory_AddRef(iface);
604         *ppvOut = iface;
605         return S_OK;
606     }
607
608     return E_NOINTERFACE;
609 }
610
611 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
612 {
613     ClassFactory *This = (ClassFactory *)iface;
614     return InterlockedIncrement(&This->ref);
615 }
616
617 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
618 {
619     ClassFactory *This = (ClassFactory *)iface;
620     ULONG ret = InterlockedDecrement(&This->ref);
621
622     if (ret == 0)
623         ClassFactory_Destructor(This);
624     return ret;
625 }
626
627 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
628 {
629     ClassFactory *This = (ClassFactory *)iface;
630     HRESULT ret;
631     IUnknown *obj;
632
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;
639 }
640
641 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
642 {
643     if(fLock)
644         InterlockedIncrement(&TS_refCount);
645     else
646         InterlockedDecrement(&TS_refCount);
647
648     return S_OK;
649 }
650
651 static const IClassFactoryVtbl ClassFactoryVtbl = {
652     /* IUnknown */
653     ClassFactory_QueryInterface,
654     ClassFactory_AddRef,
655     ClassFactory_Release,
656
657     /* IClassFactory*/
658     ClassFactory_CreateInstance,
659     ClassFactory_LockServer
660 };
661
662 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
663 {
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;
671 }
672
673 static void TextService_Destructor(TextService *This)
674 {
675     HeapFree(GetProcessHeap(),0,This);
676 }
677
678 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
679 {
680     TextService *This = (TextService *)iface;
681     *ppvOut = NULL;
682
683     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
684     {
685         *ppvOut = This;
686     }
687
688     if (*ppvOut)
689     {
690         IUnknown_AddRef(iface);
691         return S_OK;
692     }
693
694     return E_NOINTERFACE;
695 }
696
697 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
698 {
699     TextService *This = (TextService *)iface;
700     return InterlockedIncrement(&This->refCount);
701 }
702
703 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
704 {
705     TextService *This = (TextService *)iface;
706     ULONG ret;
707
708     ret = InterlockedDecrement(&This->refCount);
709     if (ret == 0)
710         TextService_Destructor(This);
711     return ret;
712 }
713
714 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
715         ITfThreadMgr *ptim, TfClientId id)
716 {
717     trace("TextService_Activate\n");
718     ok(test_ShouldActivate,"Activation came unexpectedly\n");
719     tid = id;
720     return S_OK;
721 }
722
723 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
724 {
725     trace("TextService_Deactivate\n");
726     ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
727     return S_OK;
728 }
729
730 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
731 {
732     TextService_QueryInterface,
733     TextService_AddRef,
734     TextService_Release,
735
736     TextService_Activate,
737     TextService_Deactivate
738 };
739
740 static HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
741 {
742     TextService *This;
743     if (pUnkOuter)
744         return CLASS_E_NOAGGREGATION;
745
746     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
747     if (This == NULL)
748         return E_OUTOFMEMORY;
749
750     This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
751     This->refCount = 1;
752
753     *ppOut = (IUnknown *)This;
754     return S_OK;
755 }
756
757 static HRESULT RegisterTextService(REFCLSID rclsid)
758 {
759     ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
760     return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
761 }
762
763 static HRESULT UnregisterTextService()
764 {
765     return CoRevokeClassObject(regid);
766 }
767
768 /*
769  * The tests
770  */
771
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);
791
792 static HRESULT initialize(void)
793 {
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;
805 }
806
807 static void cleanup(void)
808 {
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();
816 }
817
818 static void test_Register(void)
819 {
820     HRESULT hr;
821
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};
824
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);
828
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);
835 }
836
837 static void test_Unregister(void)
838 {
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();
843 }
844
845 static void test_EnumInputProcessorInfo(void)
846 {
847     IEnumGUID *ppEnum;
848     BOOL found = FALSE;
849
850     if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
851     {
852         ULONG fetched;
853         GUID g;
854         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
855         {
856             if(IsEqualGUID(&g,&CLSID_FakeService))
857                 found = TRUE;
858         }
859     }
860     ok(found,"Did not find registered text service\n");
861 }
862
863 static void test_EnumLanguageProfiles(void)
864 {
865     BOOL found = FALSE;
866     IEnumTfLanguageProfiles *ppEnum;
867     if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
868     {
869         TF_LANGUAGEPROFILE profile;
870         while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
871         {
872             if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
873             {
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");
878             }
879         }
880     }
881     ok(found,"Registered text service not found\n");
882 }
883
884 static void test_RegisterCategory(void)
885 {
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");
891 }
892
893 static void test_UnregisterCategory(void)
894 {
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");
900 }
901
902 static void test_FindClosestCategory(void)
903 {
904     GUID output;
905     HRESULT hr;
906     const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
907
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");
911
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");
915
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");
919 }
920
921 static void test_Enable(void)
922 {
923     HRESULT hr;
924     BOOL enabled = FALSE;
925
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");
931 }
932
933 static void test_Disable(void)
934 {
935     HRESULT hr;
936
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");
940 }
941
942 static void test_ThreadMgrAdviseSinks(void)
943 {
944     ITfSource *source = NULL;
945     HRESULT hr;
946     IUnknown *sink;
947
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;
952
953     hr = ThreadMgrEventSink_Constructor(&sink);
954     ok(hr == S_OK, "got %08x\n", hr);
955     if(FAILED(hr)) return;
956
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");
962
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);
968 }
969
970 static void test_ThreadMgrUnadviseSinks(void)
971 {
972     ITfSource *source = NULL;
973     HRESULT hr;
974
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;
979
980     tmSinkRefCount = 1;
981     hr = ITfSource_UnadviseSink(source, tmSinkCookie);
982     ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
983     ITfSource_Release(source);
984 }
985
986 /**********************************************************************
987  * ITfKeyEventSink
988  **********************************************************************/
989 typedef struct tagKeyEventSink
990 {
991     const ITfKeyEventSinkVtbl *KeyEventSinkVtbl;
992     LONG refCount;
993 } KeyEventSink;
994
995 static void KeyEventSink_Destructor(KeyEventSink *This)
996 {
997     HeapFree(GetProcessHeap(),0,This);
998 }
999
1000 static HRESULT WINAPI KeyEventSink_QueryInterface(ITfKeyEventSink *iface, REFIID iid, LPVOID *ppvOut)
1001 {
1002     KeyEventSink *This = (KeyEventSink *)iface;
1003     *ppvOut = NULL;
1004
1005     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfKeyEventSink))
1006     {
1007         *ppvOut = This;
1008     }
1009
1010     if (*ppvOut)
1011     {
1012         IUnknown_AddRef(iface);
1013         return S_OK;
1014     }
1015
1016     return E_NOINTERFACE;
1017 }
1018
1019 static ULONG WINAPI KeyEventSink_AddRef(ITfKeyEventSink *iface)
1020 {
1021     KeyEventSink *This = (KeyEventSink *)iface;
1022     return InterlockedIncrement(&This->refCount);
1023 }
1024
1025 static ULONG WINAPI KeyEventSink_Release(ITfKeyEventSink *iface)
1026 {
1027     KeyEventSink *This = (KeyEventSink *)iface;
1028     ULONG ret;
1029
1030     ret = InterlockedDecrement(&This->refCount);
1031     if (ret == 0)
1032         KeyEventSink_Destructor(This);
1033     return ret;
1034 }
1035
1036 static HRESULT WINAPI KeyEventSink_OnSetFocus(ITfKeyEventSink *iface,
1037         BOOL fForeground)
1038 {
1039     sink_fire_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1040     return S_OK;
1041 }
1042
1043 static HRESULT WINAPI KeyEventSink_OnTestKeyDown(ITfKeyEventSink *iface,
1044         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1045 {
1046     trace("\n");
1047     return S_OK;
1048 }
1049
1050 static HRESULT WINAPI KeyEventSink_OnTestKeyUp(ITfKeyEventSink *iface,
1051         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1052 {
1053     trace("\n");
1054     return S_OK;
1055 }
1056
1057 static HRESULT WINAPI KeyEventSink_OnKeyDown(ITfKeyEventSink *iface,
1058         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1059 {
1060     trace("\n");
1061     return S_OK;
1062 }
1063
1064 static HRESULT WINAPI KeyEventSink_OnKeyUp(ITfKeyEventSink *iface,
1065         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1066 {
1067     trace("\n");
1068     return S_OK;
1069 }
1070
1071 static HRESULT WINAPI KeyEventSink_OnPreservedKey(ITfKeyEventSink *iface,
1072     ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
1073 {
1074     trace("\n");
1075     return S_OK;
1076 }
1077
1078 static const ITfKeyEventSinkVtbl KeyEventSink_KeyEventSinkVtbl =
1079 {
1080     KeyEventSink_QueryInterface,
1081     KeyEventSink_AddRef,
1082     KeyEventSink_Release,
1083
1084     KeyEventSink_OnSetFocus,
1085     KeyEventSink_OnTestKeyDown,
1086     KeyEventSink_OnTestKeyUp,
1087     KeyEventSink_OnKeyDown,
1088     KeyEventSink_OnKeyUp,
1089     KeyEventSink_OnPreservedKey
1090 };
1091
1092 static HRESULT KeyEventSink_Constructor(ITfKeyEventSink **ppOut)
1093 {
1094     KeyEventSink *This;
1095
1096     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(KeyEventSink));
1097     if (This == NULL)
1098         return E_OUTOFMEMORY;
1099
1100     This->KeyEventSinkVtbl = &KeyEventSink_KeyEventSinkVtbl;
1101     This->refCount = 1;
1102
1103     *ppOut = (ITfKeyEventSink*)This;
1104     return S_OK;
1105 }
1106
1107
1108 static void test_KeystrokeMgr(void)
1109 {
1110     ITfKeystrokeMgr *keymgr= NULL;
1111     HRESULT hr;
1112     TF_PRESERVEDKEY tfpk;
1113     BOOL preserved;
1114     ITfKeyEventSink *sink = NULL;
1115
1116     KeyEventSink_Constructor(&sink);
1117
1118     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfKeystrokeMgr, (LPVOID*)&keymgr);
1119     ok(SUCCEEDED(hr),"Failed to get IID_ITfKeystrokeMgr for ThreadMgr\n");
1120
1121     tfpk.uVKey = 'A';
1122     tfpk.uModifiers = TF_MOD_SHIFT;
1123
1124     test_KEV_OnSetFocus = SINK_EXPECTED;
1125     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1126     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
1127     sink_check_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1128     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1129     ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
1130     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
1131     ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
1132
1133     hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1134     ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1135
1136     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1137     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1138
1139     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1140     ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1141
1142     preserved = FALSE;
1143     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1144     ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1145     if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
1146
1147     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1148     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1149
1150     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1151     ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1152     if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
1153
1154     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1155     ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
1156
1157     hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1158     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1159
1160     ITfKeystrokeMgr_Release(keymgr);
1161     ITfKeyEventSink_Release(sink);
1162 }
1163
1164 static void test_Activate(void)
1165 {
1166     HRESULT hr;
1167
1168     hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1169     ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1170 }
1171
1172
1173 static void test_EnumContexts(ITfDocumentMgr *dm, ITfContext *search)
1174 {
1175     HRESULT hr;
1176     IEnumTfContexts* pEnum;
1177     BOOL found = FALSE;
1178
1179     hr = ITfDocumentMgr_EnumContexts(dm,&pEnum);
1180     ok(SUCCEEDED(hr),"EnumContexts failed\n");
1181     if (SUCCEEDED(hr))
1182     {
1183         ULONG fetched;
1184         ITfContext *cxt;
1185         while (IEnumTfContexts_Next(pEnum, 1, &cxt, &fetched) == S_OK)
1186         {
1187             if (!search)
1188                 found = TRUE;
1189             else if (search == cxt)
1190                 found = TRUE;
1191             ITfContext_Release(cxt);
1192         }
1193         IEnumTfContexts_Release(pEnum);
1194     }
1195     if (search)
1196         ok(found,"Did not find proper ITfContext\n");
1197     else
1198         ok(!found,"Found an ITfContext we should should not have\n");
1199 }
1200
1201 static void test_EnumDocumentMgr(ITfThreadMgr *tm, ITfDocumentMgr *search, ITfDocumentMgr *absent)
1202 {
1203     HRESULT hr;
1204     IEnumTfDocumentMgrs* pEnum;
1205     BOOL found = FALSE;
1206     BOOL notfound = TRUE;
1207
1208     hr = ITfThreadMgr_EnumDocumentMgrs(tm,&pEnum);
1209     ok(SUCCEEDED(hr),"EnumDocumentMgrs failed\n");
1210     if (SUCCEEDED(hr))
1211     {
1212         ULONG fetched;
1213         ITfDocumentMgr *dm;
1214         while (IEnumTfDocumentMgrs_Next(pEnum, 1, &dm, &fetched) == S_OK)
1215         {
1216             if (!search)
1217                 found = TRUE;
1218             else if (search == dm)
1219                 found = TRUE;
1220             if (absent && dm == absent)
1221                 notfound = FALSE;
1222             ITfDocumentMgr_Release(dm);
1223         }
1224         IEnumTfDocumentMgrs_Release(pEnum);
1225     }
1226     if (search)
1227         ok(found,"Did not find proper ITfDocumentMgr\n");
1228     else
1229         ok(!found,"Found an ITfDocumentMgr we should should not have\n");
1230     if (absent)
1231         ok(notfound,"Found an ITfDocumentMgr we believe should be absent\n");
1232 }
1233
1234 static inline int check_context_refcount(ITfContext *iface)
1235 {
1236     IUnknown_AddRef(iface);
1237     return IUnknown_Release(iface);
1238 }
1239
1240
1241 /**********************************************************************
1242  * ITfTextEditSink
1243  **********************************************************************/
1244 typedef struct tagTextEditSink
1245 {
1246     const ITfTextEditSinkVtbl *TextEditSinkVtbl;
1247     LONG refCount;
1248 } TextEditSink;
1249
1250 static void TextEditSink_Destructor(TextEditSink *This)
1251 {
1252     HeapFree(GetProcessHeap(),0,This);
1253 }
1254
1255 static HRESULT WINAPI TextEditSink_QueryInterface(ITfTextEditSink *iface, REFIID iid, LPVOID *ppvOut)
1256 {
1257     TextEditSink *This = (TextEditSink *)iface;
1258     *ppvOut = NULL;
1259
1260     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextEditSink))
1261     {
1262         *ppvOut = This;
1263     }
1264
1265     if (*ppvOut)
1266     {
1267         IUnknown_AddRef(iface);
1268         return S_OK;
1269     }
1270
1271     return E_NOINTERFACE;
1272 }
1273
1274 static ULONG WINAPI TextEditSink_AddRef(ITfTextEditSink *iface)
1275 {
1276     TextEditSink *This = (TextEditSink *)iface;
1277     return InterlockedIncrement(&This->refCount);
1278 }
1279
1280 static ULONG WINAPI TextEditSink_Release(ITfTextEditSink *iface)
1281 {
1282     TextEditSink *This = (TextEditSink *)iface;
1283     ULONG ret;
1284
1285     ret = InterlockedDecrement(&This->refCount);
1286     if (ret == 0)
1287         TextEditSink_Destructor(This);
1288     return ret;
1289 }
1290
1291 static HRESULT WINAPI TextEditSink_OnEndEdit(ITfTextEditSink *iface,
1292     ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
1293 {
1294     sink_fire_ok(&test_OnEndEdit,"TextEditSink_OnEndEdit");
1295     return S_OK;
1296 }
1297
1298 static const ITfTextEditSinkVtbl TextEditSink_TextEditSinkVtbl =
1299 {
1300     TextEditSink_QueryInterface,
1301     TextEditSink_AddRef,
1302     TextEditSink_Release,
1303
1304     TextEditSink_OnEndEdit
1305 };
1306
1307 static HRESULT TextEditSink_Constructor(ITfTextEditSink **ppOut)
1308 {
1309     TextEditSink *This;
1310
1311     *ppOut = NULL;
1312     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextEditSink));
1313     if (This == NULL)
1314         return E_OUTOFMEMORY;
1315
1316     This->TextEditSinkVtbl = &TextEditSink_TextEditSinkVtbl;
1317     This->refCount = 1;
1318
1319     *ppOut = (ITfTextEditSink*)This;
1320     return S_OK;
1321 }
1322
1323 static void test_startSession(void)
1324 {
1325     HRESULT hr;
1326     DWORD cnt;
1327     DWORD editCookie;
1328     ITfDocumentMgr *dmtest;
1329     ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
1330     ITextStoreACP *ts;
1331     TfClientId cid2 = 0;
1332
1333     hr = ITfThreadMgr_Deactivate(g_tm);
1334     ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1335
1336     test_ShouldActivate = TRUE;
1337     hr  = ITfThreadMgr_Activate(g_tm,&cid);
1338     ok(SUCCEEDED(hr),"Failed to Activate\n");
1339     ok(cid != tid,"TextService id mistakenly matches Client id\n");
1340
1341     test_ShouldActivate = FALSE;
1342     hr = ITfThreadMgr_Activate(g_tm,&cid2);
1343     ok(SUCCEEDED(hr),"Failed to Activate\n");
1344     ok (cid == cid2, "Second activate client ID does not match\n");
1345
1346     hr = ITfThreadMgr_Deactivate(g_tm);
1347     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1348
1349     test_EnumDocumentMgr(g_tm,NULL,NULL);
1350
1351     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1352     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1353
1354     test_EnumDocumentMgr(g_tm,g_dm,NULL);
1355
1356     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dmtest);
1357     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1358
1359     test_EnumDocumentMgr(g_tm,dmtest,NULL);
1360
1361     ITfDocumentMgr_Release(dmtest);
1362     test_EnumDocumentMgr(g_tm,g_dm,dmtest);
1363
1364     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1365     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1366     ok(dmtest == NULL,"Initial focus not null\n");
1367
1368     test_CurrentFocus = g_dm;
1369     test_PrevFocus = NULL;
1370     test_OnSetFocus  = SINK_EXPECTED;
1371     hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
1372     ok(SUCCEEDED(hr),"SetFocus Failed\n");
1373     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1374
1375     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1376     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1377     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1378
1379     cnt = ITfDocumentMgr_Release(g_dm);
1380     ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
1381
1382     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1383     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1384     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1385     ITfDocumentMgr_Release(dmtest);
1386
1387     TextStoreACP_Constructor((IUnknown**)&ts);
1388
1389     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1390     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1391
1392     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1393     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1394
1395     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1396     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1397
1398     test_EnumContexts(g_dm, NULL);
1399
1400     hr = ITfContext_GetDocumentMgr(cxt,&dmtest);
1401     ok(hr == S_OK, "ITfContext_GetDocumentMgr failed with %x\n",hr);
1402     ok(dmtest == g_dm, "Wrong documentmgr\n");
1403     ITfDocumentMgr_Release(dmtest);
1404
1405     cnt = check_context_refcount(cxt);
1406     test_OnPushContext = SINK_EXPECTED;
1407     test_ACP_AdviseSink = SINK_EXPECTED;
1408     test_OnInitDocumentMgr = SINK_EXPECTED;
1409     hr = ITfDocumentMgr_Push(g_dm, cxt);
1410     ok(SUCCEEDED(hr),"Push Failed\n");
1411     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1412     sink_check_ok(&test_OnPushContext,"OnPushContext");
1413     sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
1414     sink_check_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
1415
1416     test_EnumContexts(g_dm, cxt);
1417
1418     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1419     ok(SUCCEEDED(hr),"GetTop Failed\n");
1420     ok(cxtTest == cxt, "Wrong context on top\n");
1421     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1422     cnt = ITfContext_Release(cxtTest);
1423
1424     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1425     ok(SUCCEEDED(hr),"GetBase Failed\n");
1426     ok(cxtTest == cxt, "Wrong context on Base\n");
1427     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1428     ITfContext_Release(cxtTest);
1429
1430     check_context_refcount(cxt2);
1431     test_OnPushContext = SINK_EXPECTED;
1432     hr = ITfDocumentMgr_Push(g_dm, cxt2);
1433     ok(SUCCEEDED(hr),"Push Failed\n");
1434     sink_check_ok(&test_OnPushContext,"OnPushContext");
1435
1436     cnt = check_context_refcount(cxt2);
1437     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1438     ok(SUCCEEDED(hr),"GetTop Failed\n");
1439     ok(cxtTest == cxt2, "Wrong context on top\n");
1440     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1441     ITfContext_Release(cxtTest);
1442
1443     cnt = check_context_refcount(cxt);
1444     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1445     ok(SUCCEEDED(hr),"GetBase Failed\n");
1446     ok(cxtTest == cxt, "Wrong context on Base\n");
1447     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1448     ITfContext_Release(cxtTest);
1449
1450     cnt = check_context_refcount(cxt3);
1451     hr = ITfDocumentMgr_Push(g_dm, cxt3);
1452     ok(FAILED(hr),"Push Succeeded\n");
1453     ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
1454
1455     cnt = check_context_refcount(cxt2);
1456     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1457     ok(SUCCEEDED(hr),"GetTop Failed\n");
1458     ok(cxtTest == cxt2, "Wrong context on top\n");
1459     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1460     ITfContext_Release(cxtTest);
1461
1462     cnt = check_context_refcount(cxt);
1463     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1464     ok(SUCCEEDED(hr),"GetBase Failed\n");
1465     ok(cxtTest == cxt, "Wrong context on Base\n");
1466     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1467     ITfContext_Release(cxtTest);
1468
1469     cnt = check_context_refcount(cxt2);
1470     test_OnPopContext = SINK_EXPECTED;
1471     hr = ITfDocumentMgr_Pop(g_dm, 0);
1472     ok(SUCCEEDED(hr),"Pop Failed\n");
1473     ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1474     sink_check_ok(&test_OnPopContext,"OnPopContext");
1475
1476     dmtest = (void *)0xfeedface;
1477     hr = ITfContext_GetDocumentMgr(cxt2,&dmtest);
1478     ok(hr == S_FALSE, "ITfContext_GetDocumentMgr wrong rc %x\n",hr);
1479     ok(dmtest == NULL,"returned documentmgr should be null\n");
1480
1481     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1482     ok(SUCCEEDED(hr),"GetTop Failed\n");
1483     ok(cxtTest == cxt, "Wrong context on top\n");
1484     ITfContext_Release(cxtTest);
1485
1486     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1487     ok(SUCCEEDED(hr),"GetBase Failed\n");
1488     ok(cxtTest == cxt, "Wrong context on base\n");
1489     ITfContext_Release(cxtTest);
1490
1491     hr = ITfDocumentMgr_Pop(g_dm, 0);
1492     ok(FAILED(hr),"Pop Succeeded\n");
1493
1494     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1495     ok(SUCCEEDED(hr),"GetTop Failed\n");
1496     ok(cxtTest == cxt, "Wrong context on top\n");
1497     ITfContext_Release(cxtTest);
1498
1499     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1500     ok(SUCCEEDED(hr),"GetBase Failed\n");
1501     ok(cxtTest == cxt, "Wrong context on base\n");
1502     ITfContext_Release(cxtTest);
1503
1504     ITfContext_Release(cxt);
1505     ITfContext_Release(cxt2);
1506     ITfContext_Release(cxt3);
1507 }
1508
1509 static void test_endSession(void)
1510 {
1511     HRESULT hr;
1512     test_ShouldDeactivate = TRUE;
1513     test_CurrentFocus = NULL;
1514     test_PrevFocus = g_dm;
1515     test_OnSetFocus  = SINK_EXPECTED;
1516     hr = ITfThreadMgr_Deactivate(g_tm);
1517     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1518     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1519     test_OnSetFocus  = SINK_UNEXPECTED;
1520 }
1521
1522 static void test_TfGuidAtom(void)
1523 {
1524     GUID gtest,g1;
1525     HRESULT hr;
1526     TfGuidAtom atom1,atom2;
1527     BOOL equal;
1528
1529     CoCreateGuid(&gtest);
1530
1531     /* msdn reports this should return E_INVALIDARG.  However my test show it crashing (winxp)*/
1532     /*
1533     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1534     ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1535     */
1536     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
1537     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1538     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
1539     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1540     ok(atom1 == atom2,"atoms do not match\n");
1541     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
1542     ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
1543     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
1544     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1545     ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
1546     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
1547     ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
1548     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
1549     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1550     ok(equal == TRUE,"Equal value invalid\n");
1551
1552     /* show that cid and tid TfClientIds are also TfGuidAtoms */
1553     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
1554     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1555     ok(equal == TRUE,"Equal value invalid\n");
1556     hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
1557     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1558     ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
1559 }
1560
1561 static void test_ClientId(void)
1562 {
1563     ITfClientId *pcid;
1564     TfClientId id1,id2;
1565     HRESULT hr;
1566     GUID g2;
1567
1568     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1569     ok(SUCCEEDED(hr),"Unable to acquire ITfClientId interface\n");
1570
1571     CoCreateGuid(&g2);
1572
1573     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
1574     ok(SUCCEEDED(hr),"GetClientId failed\n");
1575     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
1576     ok(SUCCEEDED(hr),"GetClientId failed\n");
1577     ok(id1==id2,"Id's for GUID_NULL do not match\n");
1578     hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
1579     ok(SUCCEEDED(hr),"GetClientId failed\n");
1580     ok(id2!=id1,"Id matches GUID_NULL\n");
1581     ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
1582     ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
1583     hr = ITfClientId_GetClientId(pcid,&g2,&id2);
1584     ok(SUCCEEDED(hr),"GetClientId failed\n");
1585     ok(id2!=id1,"Id matches GUID_NULL\n");
1586     ok(id2!=tid,"Id for random guid matching tid\n");
1587     ok(id2!=cid,"Id for random guid matching cid\n");
1588     ITfClientId_Release(pcid);
1589 }
1590
1591 /**********************************************************************
1592  * ITfEditSession
1593  **********************************************************************/
1594 typedef struct tagEditSession
1595 {
1596     const ITfEditSessionVtbl *EditSessionVtbl;
1597     LONG refCount;
1598 } EditSession;
1599
1600 static void EditSession_Destructor(EditSession *This)
1601 {
1602     HeapFree(GetProcessHeap(),0,This);
1603 }
1604
1605 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1606 {
1607     EditSession *This = (EditSession *)iface;
1608     *ppvOut = NULL;
1609
1610     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1611     {
1612         *ppvOut = This;
1613     }
1614
1615     if (*ppvOut)
1616     {
1617         IUnknown_AddRef(iface);
1618         return S_OK;
1619     }
1620
1621     return E_NOINTERFACE;
1622 }
1623
1624 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1625 {
1626     EditSession *This = (EditSession *)iface;
1627     return InterlockedIncrement(&This->refCount);
1628 }
1629
1630 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1631 {
1632     EditSession *This = (EditSession *)iface;
1633     ULONG ret;
1634
1635     ret = InterlockedDecrement(&This->refCount);
1636     if (ret == 0)
1637         EditSession_Destructor(This);
1638     return ret;
1639 }
1640
1641 static void test_InsertAtSelection(TfEditCookie ec, ITfContext *cxt)
1642 {
1643     HRESULT hr;
1644     ITfInsertAtSelection *iis;
1645     ITfRange *range=NULL;
1646     static const WCHAR txt[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
1647
1648     hr = ITfContext_QueryInterface(cxt, &IID_ITfInsertAtSelection , (LPVOID*)&iis);
1649     ok(SUCCEEDED(hr),"Failed to get ITfInsertAtSelection interface\n");
1650     test_ACP_InsertTextAtSelection = SINK_EXPECTED;
1651     hr = ITfInsertAtSelection_InsertTextAtSelection(iis, ec, 0, txt, 11, &range);
1652     ok(SUCCEEDED(hr),"ITfInsertAtSelection_InsertTextAtSelection failed %x\n",hr);
1653     sink_check_ok(&test_ACP_InsertTextAtSelection,"InsertTextAtSelection");
1654     ok(range != NULL,"No range returned\n");
1655     ITfRange_Release(range);
1656     ITfInsertAtSelection_Release(iis);
1657 }
1658
1659 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1660 TfEditCookie ec)
1661 {
1662     ITfContext *cxt;
1663     ITfDocumentMgr *dm;
1664     ITfRange *range;
1665     TF_SELECTION selection;
1666     ULONG fetched;
1667     HRESULT hr;
1668
1669     sink_fire_ok(&test_DoEditSession,"EditSession_DoEditSession");
1670     sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1671
1672     ITfThreadMgr_GetFocus(g_tm, &dm);
1673     ITfDocumentMgr_GetTop(dm,&cxt);
1674
1675     hr = ITfContext_GetStart(cxt,ec,NULL);
1676     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1677
1678     range = (ITfRange*)0xdeaddead;
1679     hr = ITfContext_GetStart(cxt,0xdeadcafe,&range);
1680     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1681     ok(range == NULL,"Range not set to NULL\n");
1682
1683     hr = ITfContext_GetStart(cxt,ec,&range);
1684     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1685     ok(range != NULL,"Range set to NULL\n");
1686
1687     ITfRange_Release(range);
1688
1689     hr = ITfContext_GetEnd(cxt,ec,NULL);
1690     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1691
1692     range = (ITfRange*)0xdeaddead;
1693     hr = ITfContext_GetEnd(cxt,0xdeadcafe,&range);
1694     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1695     ok(range == NULL,"Range not set to NULL\n");
1696
1697     test_ACP_GetEndACP = SINK_EXPECTED;
1698     hr = ITfContext_GetEnd(cxt,ec,&range);
1699     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1700     ok(range != NULL,"Range set to NULL\n");
1701     sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1702
1703     ITfRange_Release(range);
1704
1705     selection.range = NULL;
1706     test_ACP_GetSelection = SINK_EXPECTED;
1707     hr = ITfContext_GetSelection(cxt, ec, TF_DEFAULT_SELECTION, 1, &selection, &fetched);
1708     ok(SUCCEEDED(hr),"ITfContext_GetSelection failed\n");
1709     ok(fetched == 1,"fetched incorrect\n");
1710     ok(selection.range != NULL,"NULL range\n");
1711     sink_check_ok(&test_ACP_GetSelection,"ACP_GetSepection");
1712     ITfRange_Release(selection.range);
1713
1714     test_InsertAtSelection(ec, cxt);
1715
1716     test_ACP_GetEndACP = SINK_EXPECTED;
1717     hr = ITfContext_GetEnd(cxt,ec,&range);
1718     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1719     ok(range != NULL,"Range set to NULL\n");
1720     sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1721
1722     selection.range = range;
1723     selection.style.ase = TF_AE_NONE;
1724     selection.style.fInterimChar = FALSE;
1725     test_ACP_SetSelection = SINK_EXPECTED;
1726     hr = ITfContext_SetSelection(cxt, ec, 1, &selection);
1727     sink_check_ok(&test_ACP_SetSelection,"SetSelection");
1728     ITfRange_Release(range);
1729
1730     ITfContext_Release(cxt);
1731     ITfDocumentMgr_Release(dm);
1732     return 0xdeadcafe;
1733 }
1734
1735 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1736 {
1737     EditSession_QueryInterface,
1738     EditSession_AddRef,
1739     EditSession_Release,
1740
1741     EditSession_DoEditSession
1742 };
1743
1744 static HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1745 {
1746     EditSession *This;
1747
1748     *ppOut = NULL;
1749     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1750     if (This == NULL)
1751         return E_OUTOFMEMORY;
1752
1753     This->EditSessionVtbl = &EditSession_EditSessionVtbl;
1754     This->refCount = 1;
1755
1756     *ppOut = (ITfEditSession*)This;
1757     return S_OK;
1758 }
1759
1760 static void test_TStoApplicationText(void)
1761 {
1762     HRESULT hr, hrSession;
1763     ITfEditSession *es;
1764     ITfContext *cxt;
1765     ITfDocumentMgr *dm;
1766     ITfTextEditSink *sink;
1767     ITfSource *source = NULL;
1768     DWORD editSinkCookie = -1;
1769
1770     ITfThreadMgr_GetFocus(g_tm, &dm);
1771     EditSession_Constructor(&es);
1772     ITfDocumentMgr_GetTop(dm,&cxt);
1773
1774     TextEditSink_Constructor(&sink);
1775     hr = ITfContext_QueryInterface(cxt,&IID_ITfSource,(LPVOID*)&source);
1776     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for Context\n");
1777     if (source)
1778     {
1779         hr = ITfSource_AdviseSink(source, &IID_ITfTextEditSink, (LPVOID)sink, &editSinkCookie);
1780         ok(SUCCEEDED(hr),"Failed to advise Sink\n");
1781         ok(editSinkCookie != -1,"Failed to get sink cookie\n");
1782     }
1783
1784     hrSession = 0xfeedface;
1785     /* Test no premissions flags */
1786     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC, &hrSession);
1787     ok(hr == E_INVALIDARG,"RequestEditSession should have failed with %x not %x\n",E_INVALIDARG,hr);
1788     ok(hrSession == E_FAIL,"hrSession should be %x not %x\n",E_FAIL,hrSession);
1789
1790     documentStatus = TS_SD_READONLY;
1791     hrSession = 0xfeedface;
1792     test_ACP_GetStatus = SINK_EXPECTED;
1793     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1794     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1795     ok(hrSession == TS_E_READONLY,"Unexpected hrSession (%x)\n",hrSession);
1796     sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1797
1798     /* signal a change to allow readwrite sessions */
1799     documentStatus = 0;
1800     test_ACP_RequestLock = SINK_EXPECTED;
1801     ITextStoreACPSink_OnStatusChange(ACPSink,documentStatus);
1802     sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1803
1804     test_ACP_GetStatus = SINK_EXPECTED;
1805     test_ACP_RequestLock = SINK_EXPECTED;
1806     test_DoEditSession = SINK_EXPECTED;
1807     hrSession = 0xfeedface;
1808     test_OnEndEdit = SINK_EXPECTED;
1809     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1810     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1811     sink_check_ok(&test_OnEndEdit,"OnEndEdit");
1812     sink_check_ok(&test_DoEditSession,"DoEditSession");
1813     sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1814     ok(hrSession == 0xdeadcafe,"Unexpected hrSession (%x)\n",hrSession);
1815
1816     if (source)
1817     {
1818         hr = ITfSource_UnadviseSink(source, editSinkCookie);
1819         ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1820         ITfTextEditSink_Release(sink);
1821         ITfSource_Release(source);
1822     }
1823
1824     ITfContext_Release(cxt);
1825     ITfDocumentMgr_Release(dm);
1826     ITfEditSession_Release(es);
1827 }
1828
1829 static void enum_compartments(ITfCompartmentMgr *cmpmgr, REFGUID present, REFGUID absent)
1830 {
1831     BOOL found,found2;
1832     IEnumGUID *ppEnum;
1833     found = FALSE;
1834     found2 = FALSE;
1835     if (SUCCEEDED(ITfCompartmentMgr_EnumCompartments(cmpmgr, &ppEnum)))
1836     {
1837         ULONG fetched;
1838         GUID g;
1839         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
1840         {
1841             WCHAR str[50];
1842             CHAR strA[50];
1843             StringFromGUID2(&g,str,50);
1844             WideCharToMultiByte(CP_ACP,0,str,50,strA,50,0,0);
1845             trace("found %s\n",strA);
1846             if (present && IsEqualGUID(present,&g))
1847                 found = TRUE;
1848             if (absent && IsEqualGUID(absent, &g))
1849                 found2 = TRUE;
1850         }
1851         IEnumGUID_Release(ppEnum);
1852     }
1853     if (present)
1854         ok(found,"Did not find compartment\n");
1855     if (absent)
1856         ok(!found2,"Found compartment that should be absent\n");
1857 }
1858
1859 static void test_Compartments(void)
1860 {
1861     ITfContext *cxt;
1862     ITfDocumentMgr *dm;
1863     ITfCompartmentMgr *cmpmgr;
1864     ITfCompartment *cmp;
1865     HRESULT hr;
1866
1867     ITfThreadMgr_GetFocus(g_tm, &dm);
1868     ITfDocumentMgr_GetTop(dm,&cxt);
1869
1870     /* Global */
1871     hr = ITfThreadMgr_GetGlobalCompartment(g_tm, &cmpmgr);
1872     ok(SUCCEEDED(hr),"GetGlobalCompartment failed\n");
1873     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_SPEECH_OPENCLOSE, &cmp);
1874     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1875     ITfCompartment_Release(cmp);
1876     enum_compartments(cmpmgr,&GUID_COMPARTMENT_SPEECH_OPENCLOSE,NULL);
1877     ITfCompartmentMgr_Release(cmpmgr);
1878
1879     /* Thread */
1880     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1881     ok(SUCCEEDED(hr),"ThreadMgr QI for IID_ITfCompartmentMgr failed\n");
1882     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &CLSID_FakeService, &cmp);
1883     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1884     enum_compartments(cmpmgr,&CLSID_FakeService,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1885     ITfCompartmentMgr_ClearCompartment(cmpmgr,tid,&CLSID_FakeService);
1886     enum_compartments(cmpmgr,NULL,&CLSID_FakeService);
1887     ITfCompartmentMgr_Release(cmpmgr);
1888     ITfCompartment_Release(cmp);
1889
1890     /* DocumentMgr */
1891     hr = ITfDocumentMgr_QueryInterface(dm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1892     ok(SUCCEEDED(hr),"DocumentMgr QI for IID_ITfCompartmentMgr failed\n");
1893
1894     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_PERSISTMENUENABLED, &cmp);
1895     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1896     enum_compartments(cmpmgr,&GUID_COMPARTMENT_PERSISTMENUENABLED,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1897     ITfCompartmentMgr_Release(cmpmgr);
1898
1899     /* Context */
1900     hr = ITfContext_QueryInterface(cxt, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1901     ok(SUCCEEDED(hr),"Context QI for IID_ITfCompartmentMgr failed\n");
1902     enum_compartments(cmpmgr,NULL,&GUID_COMPARTMENT_PERSISTMENUENABLED);
1903     ITfCompartmentMgr_Release(cmpmgr);
1904
1905     ITfContext_Release(cxt);
1906     ITfDocumentMgr_Release(dm);
1907 }
1908
1909 static void processPendingMessages(void)
1910 {
1911     MSG msg;
1912     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1913         TranslateMessage(&msg);
1914         DispatchMessage(&msg);
1915     }
1916 }
1917
1918 static void test_AssociateFocus(void)
1919 {
1920     ITfDocumentMgr *dm1, *dm2, *olddm, *dmcheck, *dmorig;
1921     HWND wnd1, wnd2, wnd3;
1922     HRESULT hr;
1923
1924     ITfThreadMgr_GetFocus(g_tm, &dmorig);
1925     test_CurrentFocus = NULL;
1926     test_PrevFocus = dmorig;
1927     test_OnSetFocus  = SINK_EXPECTED;
1928     hr = ITfThreadMgr_SetFocus(g_tm,NULL);
1929     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1930     ITfDocumentMgr_Release(dmorig);
1931
1932     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm1);
1933     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1934
1935     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm2);
1936     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1937
1938     wnd1 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1939     ok(wnd1!=NULL,"Unable to create window 1\n");
1940     wnd2 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1941     ok(wnd2!=NULL,"Unable to create window 2\n");
1942     wnd3 = CreateWindow("edit",NULL,WS_POPUP|WS_VISIBLE,0,0,200,60,NULL,NULL,NULL,NULL);
1943     ok(wnd3!=NULL,"Unable to create window 3\n");
1944
1945     processPendingMessages();
1946
1947     SetFocus(wnd1);
1948     processPendingMessages();
1949     test_CurrentFocus = dm1;
1950     test_PrevFocus = NULL;
1951     test_OnSetFocus  = SINK_EXPECTED;
1952     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,dm1,&olddm);
1953     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1954     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1955     ok(olddm == NULL, "unexpected old DocumentMgr\n");
1956
1957     processPendingMessages();
1958
1959     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1960     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1961     ITfDocumentMgr_Release(dmcheck);
1962
1963     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,dm2,&olddm);
1964     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1965     processPendingMessages();
1966     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1967     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1968     ITfDocumentMgr_Release(dmcheck);
1969
1970     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,dm2,&olddm);
1971     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1972     processPendingMessages();
1973     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1974     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1975     ITfDocumentMgr_Release(dmcheck);
1976
1977     test_CurrentFocus = dm2;
1978     test_PrevFocus = dm1;
1979     test_OnSetFocus  = SINK_EXPECTED;
1980     SetFocus(wnd2);
1981     processPendingMessages();
1982     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1983
1984     SetFocus(wnd3);
1985     processPendingMessages();
1986
1987     test_CurrentFocus = dm1;
1988     test_PrevFocus = dm2;
1989     test_OnSetFocus = SINK_EXPECTED;
1990     SetFocus(wnd1);
1991     processPendingMessages();
1992     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1993
1994     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,NULL,&olddm);
1995     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
1996     ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
1997     ITfDocumentMgr_Release(olddm);
1998
1999     test_CurrentFocus = dmorig;
2000     test_PrevFocus = dm1;
2001     test_OnSetFocus  = SINK_EXPECTED;
2002     test_ACP_GetStatus = SINK_EXPECTED;
2003     ITfThreadMgr_SetFocus(g_tm,dmorig);
2004     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2005
2006     test_CurrentFocus = NULL;
2007     test_PrevFocus = dmorig;
2008     test_OnSetFocus  = SINK_EXPECTED;
2009     SetFocus(wnd3);
2010     processPendingMessages();
2011     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2012
2013     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,NULL,&olddm);
2014     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2015     ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2016     ITfDocumentMgr_Release(olddm);
2017     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,NULL,&olddm);
2018     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2019     ok(olddm == dm1, "incorrect old DocumentMgr returned\n");
2020     ITfDocumentMgr_Release(olddm);
2021
2022     SetFocus(wnd2);
2023     processPendingMessages();
2024     SetFocus(wnd1);
2025     processPendingMessages();
2026
2027     ITfDocumentMgr_Release(dm1);
2028     ITfDocumentMgr_Release(dm2);
2029     DestroyWindow(wnd1);
2030     DestroyWindow(wnd2);
2031     DestroyWindow(wnd3);
2032
2033     test_CurrentFocus = dmorig;
2034     test_PrevFocus = NULL;
2035     test_OnSetFocus  = SINK_EXPECTED;
2036     test_ACP_GetStatus = SINK_IGNORE;
2037     ITfThreadMgr_SetFocus(g_tm,dmorig);
2038     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2039 }
2040
2041 START_TEST(inputprocessor)
2042 {
2043     if (SUCCEEDED(initialize()))
2044     {
2045         test_Register();
2046         test_RegisterCategory();
2047         test_EnumInputProcessorInfo();
2048         test_Enable();
2049         test_ThreadMgrAdviseSinks();
2050         test_Activate();
2051         test_startSession();
2052         test_TfGuidAtom();
2053         test_ClientId();
2054         test_KeystrokeMgr();
2055         test_TStoApplicationText();
2056         test_Compartments();
2057         test_AssociateFocus();
2058         test_endSession();
2059         test_EnumLanguageProfiles();
2060         test_FindClosestCategory();
2061         test_Disable();
2062         test_ThreadMgrUnadviseSinks();
2063         test_UnregisterCategory();
2064         test_Unregister();
2065     }
2066     else
2067         skip("Unable to create InputProcessor\n");
2068     cleanup();
2069 }