msctf/tests: Add handler sink_check_ok for checking fired 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) todo_wine
110             {
111                 winetest_ok(0, "Unexpected %s sink\n",name);
112             }
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     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");
1130
1131     hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1132     ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1133
1134     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1135     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1136
1137     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1138     ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1139
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");
1144
1145     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1146     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1147
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");
1151
1152     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1153     ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
1154
1155     hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1156     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1157
1158     ITfKeystrokeMgr_Release(keymgr);
1159     ITfKeyEventSink_Release(sink);
1160 }
1161
1162 static void test_Activate(void)
1163 {
1164     HRESULT hr;
1165
1166     hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1167     ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1168 }
1169
1170
1171 static void test_EnumContexts(ITfDocumentMgr *dm, ITfContext *search)
1172 {
1173     HRESULT hr;
1174     IEnumTfContexts* pEnum;
1175     BOOL found = FALSE;
1176
1177     hr = ITfDocumentMgr_EnumContexts(dm,&pEnum);
1178     ok(SUCCEEDED(hr),"EnumContexts failed\n");
1179     if (SUCCEEDED(hr))
1180     {
1181         ULONG fetched;
1182         ITfContext *cxt;
1183         while (IEnumTfContexts_Next(pEnum, 1, &cxt, &fetched) == S_OK)
1184         {
1185             if (!search)
1186                 found = TRUE;
1187             else if (search == cxt)
1188                 found = TRUE;
1189             ITfContext_Release(cxt);
1190         }
1191         IEnumTfContexts_Release(pEnum);
1192     }
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");
1197 }
1198
1199 static void test_EnumDocumentMgr(ITfThreadMgr *tm, ITfDocumentMgr *search, ITfDocumentMgr *absent)
1200 {
1201     HRESULT hr;
1202     IEnumTfDocumentMgrs* pEnum;
1203     BOOL found = FALSE;
1204     BOOL notfound = TRUE;
1205
1206     hr = ITfThreadMgr_EnumDocumentMgrs(tm,&pEnum);
1207     ok(SUCCEEDED(hr),"EnumDocumentMgrs failed\n");
1208     if (SUCCEEDED(hr))
1209     {
1210         ULONG fetched;
1211         ITfDocumentMgr *dm;
1212         while (IEnumTfDocumentMgrs_Next(pEnum, 1, &dm, &fetched) == S_OK)
1213         {
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);
1221         }
1222         IEnumTfDocumentMgrs_Release(pEnum);
1223     }
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");
1230 }
1231
1232 static inline int check_context_refcount(ITfContext *iface)
1233 {
1234     IUnknown_AddRef(iface);
1235     return IUnknown_Release(iface);
1236 }
1237
1238
1239 /**********************************************************************
1240  * ITfTextEditSink
1241  **********************************************************************/
1242 typedef struct tagTextEditSink
1243 {
1244     const ITfTextEditSinkVtbl *TextEditSinkVtbl;
1245     LONG refCount;
1246 } TextEditSink;
1247
1248 static void TextEditSink_Destructor(TextEditSink *This)
1249 {
1250     HeapFree(GetProcessHeap(),0,This);
1251 }
1252
1253 static HRESULT WINAPI TextEditSink_QueryInterface(ITfTextEditSink *iface, REFIID iid, LPVOID *ppvOut)
1254 {
1255     TextEditSink *This = (TextEditSink *)iface;
1256     *ppvOut = NULL;
1257
1258     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextEditSink))
1259     {
1260         *ppvOut = This;
1261     }
1262
1263     if (*ppvOut)
1264     {
1265         IUnknown_AddRef(iface);
1266         return S_OK;
1267     }
1268
1269     return E_NOINTERFACE;
1270 }
1271
1272 static ULONG WINAPI TextEditSink_AddRef(ITfTextEditSink *iface)
1273 {
1274     TextEditSink *This = (TextEditSink *)iface;
1275     return InterlockedIncrement(&This->refCount);
1276 }
1277
1278 static ULONG WINAPI TextEditSink_Release(ITfTextEditSink *iface)
1279 {
1280     TextEditSink *This = (TextEditSink *)iface;
1281     ULONG ret;
1282
1283     ret = InterlockedDecrement(&This->refCount);
1284     if (ret == 0)
1285         TextEditSink_Destructor(This);
1286     return ret;
1287 }
1288
1289 static HRESULT WINAPI TextEditSink_OnEndEdit(ITfTextEditSink *iface,
1290     ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
1291 {
1292     sink_fire_ok(&test_OnEndEdit,"TextEditSink_OnEndEdit");
1293     return S_OK;
1294 }
1295
1296 static const ITfTextEditSinkVtbl TextEditSink_TextEditSinkVtbl =
1297 {
1298     TextEditSink_QueryInterface,
1299     TextEditSink_AddRef,
1300     TextEditSink_Release,
1301
1302     TextEditSink_OnEndEdit
1303 };
1304
1305 static HRESULT TextEditSink_Constructor(ITfTextEditSink **ppOut)
1306 {
1307     TextEditSink *This;
1308
1309     *ppOut = NULL;
1310     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextEditSink));
1311     if (This == NULL)
1312         return E_OUTOFMEMORY;
1313
1314     This->TextEditSinkVtbl = &TextEditSink_TextEditSinkVtbl;
1315     This->refCount = 1;
1316
1317     *ppOut = (ITfTextEditSink*)This;
1318     return S_OK;
1319 }
1320
1321 static void test_startSession(void)
1322 {
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;
1330
1331     hr = ITfThreadMgr_Deactivate(g_tm);
1332     ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1333
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");
1338
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");
1343
1344     hr = ITfThreadMgr_Deactivate(g_tm);
1345     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1346
1347     test_EnumDocumentMgr(g_tm,NULL,NULL);
1348
1349     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1350     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1351
1352     test_EnumDocumentMgr(g_tm,g_dm,NULL);
1353
1354     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dmtest);
1355     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1356
1357     test_EnumDocumentMgr(g_tm,dmtest,NULL);
1358
1359     ITfDocumentMgr_Release(dmtest);
1360     test_EnumDocumentMgr(g_tm,g_dm,dmtest);
1361
1362     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1363     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1364     ok(dmtest == NULL,"Initial focus not null\n");
1365
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");
1372
1373     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1374     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1375     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1376
1377     cnt = ITfDocumentMgr_Release(g_dm);
1378     ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
1379
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);
1384
1385     TextStoreACP_Constructor((IUnknown**)&ts);
1386
1387     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1388     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1389
1390     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1391     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1392
1393     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1394     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1395
1396     test_EnumContexts(g_dm, NULL);
1397
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);
1402
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");
1413
1414     test_EnumContexts(g_dm, cxt);
1415
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);
1421
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);
1427
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");
1433
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);
1440
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);
1447
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");
1452
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);
1459
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);
1466
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");
1473
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");
1478
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);
1483
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);
1488
1489     hr = ITfDocumentMgr_Pop(g_dm, 0);
1490     ok(FAILED(hr),"Pop Succeeded\n");
1491
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);
1496
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);
1501
1502     ITfContext_Release(cxt);
1503     ITfContext_Release(cxt2);
1504     ITfContext_Release(cxt3);
1505 }
1506
1507 static void test_endSession(void)
1508 {
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;
1518 }
1519
1520 static void test_TfGuidAtom(void)
1521 {
1522     GUID gtest,g1;
1523     HRESULT hr;
1524     TfGuidAtom atom1,atom2;
1525     BOOL equal;
1526
1527     CoCreateGuid(&gtest);
1528
1529     /* msdn reports this should return E_INVALIDARG.  However my test show it crashing (winxp)*/
1530     /*
1531     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1532     ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1533     */
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");
1549
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");
1557 }
1558
1559 static void test_ClientId(void)
1560 {
1561     ITfClientId *pcid;
1562     TfClientId id1,id2;
1563     HRESULT hr;
1564     GUID g2;
1565
1566     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1567     ok(SUCCEEDED(hr),"Unable to acquire ITfClientId interface\n");
1568
1569     CoCreateGuid(&g2);
1570
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);
1587 }
1588
1589 /**********************************************************************
1590  * ITfEditSession
1591  **********************************************************************/
1592 typedef struct tagEditSession
1593 {
1594     const ITfEditSessionVtbl *EditSessionVtbl;
1595     LONG refCount;
1596 } EditSession;
1597
1598 static void EditSession_Destructor(EditSession *This)
1599 {
1600     HeapFree(GetProcessHeap(),0,This);
1601 }
1602
1603 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1604 {
1605     EditSession *This = (EditSession *)iface;
1606     *ppvOut = NULL;
1607
1608     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1609     {
1610         *ppvOut = This;
1611     }
1612
1613     if (*ppvOut)
1614     {
1615         IUnknown_AddRef(iface);
1616         return S_OK;
1617     }
1618
1619     return E_NOINTERFACE;
1620 }
1621
1622 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1623 {
1624     EditSession *This = (EditSession *)iface;
1625     return InterlockedIncrement(&This->refCount);
1626 }
1627
1628 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1629 {
1630     EditSession *This = (EditSession *)iface;
1631     ULONG ret;
1632
1633     ret = InterlockedDecrement(&This->refCount);
1634     if (ret == 0)
1635         EditSession_Destructor(This);
1636     return ret;
1637 }
1638
1639 static void test_InsertAtSelection(TfEditCookie ec, ITfContext *cxt)
1640 {
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};
1645
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);
1655 }
1656
1657 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1658 TfEditCookie ec)
1659 {
1660     ITfContext *cxt;
1661     ITfDocumentMgr *dm;
1662     ITfRange *range;
1663     TF_SELECTION selection;
1664     ULONG fetched;
1665     HRESULT hr;
1666
1667     sink_fire_ok(&test_DoEditSession,"EditSession_DoEditSession");
1668     sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1669
1670     ITfThreadMgr_GetFocus(g_tm, &dm);
1671     ITfDocumentMgr_GetTop(dm,&cxt);
1672
1673     hr = ITfContext_GetStart(cxt,ec,NULL);
1674     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1675
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");
1680
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");
1684
1685     ITfRange_Release(range);
1686
1687     hr = ITfContext_GetEnd(cxt,ec,NULL);
1688     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1689
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");
1694
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");
1700
1701     ITfRange_Release(range);
1702
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);
1711
1712     test_InsertAtSelection(ec, cxt);
1713
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");
1719
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);
1727
1728     ITfContext_Release(cxt);
1729     ITfDocumentMgr_Release(dm);
1730     return 0xdeadcafe;
1731 }
1732
1733 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1734 {
1735     EditSession_QueryInterface,
1736     EditSession_AddRef,
1737     EditSession_Release,
1738
1739     EditSession_DoEditSession
1740 };
1741
1742 static HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1743 {
1744     EditSession *This;
1745
1746     *ppOut = NULL;
1747     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1748     if (This == NULL)
1749         return E_OUTOFMEMORY;
1750
1751     This->EditSessionVtbl = &EditSession_EditSessionVtbl;
1752     This->refCount = 1;
1753
1754     *ppOut = (ITfEditSession*)This;
1755     return S_OK;
1756 }
1757
1758 static void test_TStoApplicationText(void)
1759 {
1760     HRESULT hr, hrSession;
1761     ITfEditSession *es;
1762     ITfContext *cxt;
1763     ITfDocumentMgr *dm;
1764     ITfTextEditSink *sink;
1765     ITfSource *source = NULL;
1766     DWORD editSinkCookie = -1;
1767
1768     ITfThreadMgr_GetFocus(g_tm, &dm);
1769     EditSession_Constructor(&es);
1770     ITfDocumentMgr_GetTop(dm,&cxt);
1771
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)
1776     {
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");
1780     }
1781
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);
1787
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");
1795
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");
1801
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);
1813
1814     if (source)
1815     {
1816         hr = ITfSource_UnadviseSink(source, editSinkCookie);
1817         ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1818         ITfTextEditSink_Release(sink);
1819         ITfSource_Release(source);
1820     }
1821
1822     ITfContext_Release(cxt);
1823     ITfDocumentMgr_Release(dm);
1824     ITfEditSession_Release(es);
1825 }
1826
1827 static void enum_compartments(ITfCompartmentMgr *cmpmgr, REFGUID present, REFGUID absent)
1828 {
1829     BOOL found,found2;
1830     IEnumGUID *ppEnum;
1831     found = FALSE;
1832     found2 = FALSE;
1833     if (SUCCEEDED(ITfCompartmentMgr_EnumCompartments(cmpmgr, &ppEnum)))
1834     {
1835         ULONG fetched;
1836         GUID g;
1837         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
1838         {
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;
1848         }
1849         IEnumGUID_Release(ppEnum);
1850     }
1851     if (present)
1852         ok(found,"Did not find compartment\n");
1853     if (absent)
1854         ok(!found2,"Found compartment that should be absent\n");
1855 }
1856
1857 static void test_Compartments(void)
1858 {
1859     ITfContext *cxt;
1860     ITfDocumentMgr *dm;
1861     ITfCompartmentMgr *cmpmgr;
1862     ITfCompartment *cmp;
1863     HRESULT hr;
1864
1865     ITfThreadMgr_GetFocus(g_tm, &dm);
1866     ITfDocumentMgr_GetTop(dm,&cxt);
1867
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);
1876
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);
1887
1888     /* DocumentMgr */
1889     hr = ITfDocumentMgr_QueryInterface(dm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1890     ok(SUCCEEDED(hr),"DocumentMgr QI for IID_ITfCompartmentMgr failed\n");
1891
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);
1896
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);
1902
1903     ITfContext_Release(cxt);
1904     ITfDocumentMgr_Release(dm);
1905 }
1906
1907 static void processPendingMessages(void)
1908 {
1909     MSG msg;
1910     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1911         TranslateMessage(&msg);
1912         DispatchMessage(&msg);
1913     }
1914 }
1915
1916 static void test_AssociateFocus(void)
1917 {
1918     ITfDocumentMgr *dm1, *dm2, *olddm, *dmcheck, *dmorig;
1919     HWND wnd1, wnd2, wnd3;
1920     HRESULT hr;
1921
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);
1929
1930     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm1);
1931     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1932
1933     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm2);
1934     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1935
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");
1942
1943     processPendingMessages();
1944
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");
1954
1955     processPendingMessages();
1956
1957     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
1958     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
1959     ITfDocumentMgr_Release(dmcheck);
1960
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);
1967
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);
1974
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");
1981
1982     SetFocus(wnd3);
1983     processPendingMessages();
1984
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");
1991
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);
1996
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");
2003
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");
2010
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);
2019
2020     SetFocus(wnd2);
2021     processPendingMessages();
2022     SetFocus(wnd1);
2023     processPendingMessages();
2024
2025     ITfDocumentMgr_Release(dm1);
2026     ITfDocumentMgr_Release(dm2);
2027     DestroyWindow(wnd1);
2028     DestroyWindow(wnd2);
2029     DestroyWindow(wnd3);
2030
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");
2037 }
2038
2039 START_TEST(inputprocessor)
2040 {
2041     if (SUCCEEDED(initialize()))
2042     {
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();
2063     }
2064     else
2065         skip("Unable to create InputProcessor\n");
2066     cleanup();
2067 }