wiaservc: COM cleanup for the IWiaDevMgr iface.
[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 #define CONST_VTABLE
25 #include "wine/test.h"
26 #include "winuser.h"
27 #include "initguid.h"
28 #include "shlwapi.h"
29 #include "shlguid.h"
30 #include "comcat.h"
31 #include "msctf.h"
32 #include "olectl.h"
33
34 static ITfInputProcessorProfiles* g_ipp;
35 static LANGID gLangid;
36 static ITfCategoryMgr * g_cm = NULL;
37 static ITfThreadMgr* g_tm = NULL;
38 static ITfDocumentMgr *g_dm = NULL;
39 static TfClientId cid = 0;
40 static TfClientId tid = 0;
41
42 static ITextStoreACPSink *ACPSink;
43
44 #define SINK_UNEXPECTED 0
45 #define SINK_EXPECTED 1
46 #define SINK_FIRED 2
47 #define SINK_IGNORE 3
48 #define SINK_OPTIONAL 4
49
50 #define SINK_ACTION_MASK 0xff
51 #define SINK_OPTION_MASK 0xff00
52 #define SINK_EXPECTED_COUNT_MASK 0xff0000
53
54 #define SINK_OPTION_TODO      0x0100
55
56 #define FOCUS_IGNORE    (ITfDocumentMgr*)0xffffffff
57
58 static BOOL test_ShouldActivate = FALSE;
59 static BOOL test_ShouldDeactivate = FALSE;
60
61 static DWORD tmSinkCookie;
62 static DWORD tmSinkRefCount;
63 static DWORD documentStatus;
64 static ITfDocumentMgr *test_CurrentFocus = NULL;
65 static ITfDocumentMgr *test_PrevFocus = NULL;
66 static INT  test_OnSetFocus = SINK_UNEXPECTED;
67 static INT  test_OnInitDocumentMgr = SINK_UNEXPECTED;
68 static INT  test_OnPushContext = SINK_UNEXPECTED;
69 static INT  test_OnPopContext = SINK_UNEXPECTED;
70 static INT  test_KEV_OnSetFocus = SINK_UNEXPECTED;
71 static INT  test_ACP_AdviseSink = SINK_UNEXPECTED;
72 static INT  test_ACP_GetStatus = SINK_UNEXPECTED;
73 static INT  test_ACP_RequestLock = SINK_UNEXPECTED;
74 static INT  test_ACP_GetEndACP = SINK_UNEXPECTED;
75 static INT  test_ACP_GetSelection = SINK_UNEXPECTED;
76 static INT  test_DoEditSession = SINK_UNEXPECTED;
77 static INT  test_ACP_InsertTextAtSelection = SINK_UNEXPECTED;
78 static INT  test_ACP_SetSelection = SINK_UNEXPECTED;
79 static INT  test_OnEndEdit = SINK_UNEXPECTED;
80
81
82 static inline int expected_count(int *sink)
83 {
84     return (*sink & SINK_EXPECTED_COUNT_MASK)>>16;
85 }
86
87 static inline void _sink_fire_ok(INT *sink, const CHAR* name)
88 {
89     int count;
90     int todo = *sink & SINK_OPTION_TODO;
91     int action = *sink & SINK_ACTION_MASK;
92
93     if (winetest_interactive)
94         winetest_trace("firing %s\n",name);
95
96     switch (action)
97     {
98         case SINK_OPTIONAL:
99         case SINK_EXPECTED:
100             count = expected_count(sink);
101             if (count > 1)
102             {
103                 count --;
104                 *sink = (*sink & ~SINK_EXPECTED_COUNT_MASK) + (count << 16);
105                 return;
106             }
107             break;
108         case SINK_IGNORE:
109             winetest_trace("Ignoring %s\n",name);
110             return;
111         default:
112             if (todo)
113                 todo_wine winetest_ok(0, "Unexpected %s sink\n",name);
114             else
115                 winetest_ok(0, "Unexpected %s sink\n",name);
116     }
117     *sink = SINK_FIRED;
118 }
119
120 #define sink_fire_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_fire_ok(a,b)
121
122 static inline void _sink_check_ok(INT *sink, const CHAR* name)
123 {
124     int action = *sink & SINK_ACTION_MASK;
125     int todo = *sink & SINK_OPTION_TODO;
126
127     switch (action)
128     {
129         case SINK_OPTIONAL:
130             if (winetest_interactive)
131                 winetest_trace("optional sink %s not fired\n",name);
132         case SINK_FIRED:
133             break;
134         case SINK_IGNORE:
135             return;
136         default:
137             if (todo)
138                 todo_wine winetest_ok(0, "%s not fired as expected, in state %x\n",name,*sink);
139             else
140                 winetest_ok(0, "%s not fired as expected, in state %x\n",name,*sink);
141     }
142     *sink = SINK_UNEXPECTED;
143 }
144
145 #define sink_check_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_check_ok(a,b)
146
147 /**********************************************************************
148  * ITextStoreACP
149  **********************************************************************/
150 typedef struct tagTextStoreACP
151 {
152     ITextStoreACP ITextStoreACP_iface;
153     LONG refCount;
154
155 } TextStoreACP;
156
157 static inline TextStoreACP *impl_from_ITextStoreACP(ITextStoreACP *iface)
158 {
159     return CONTAINING_RECORD(iface, TextStoreACP, ITextStoreACP_iface);
160 }
161
162 static void TextStoreACP_Destructor(TextStoreACP *This)
163 {
164     HeapFree(GetProcessHeap(),0,This);
165 }
166
167 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
168 {
169     TextStoreACP *This = impl_from_ITextStoreACP(iface);
170     *ppvOut = NULL;
171
172     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
173     {
174         *ppvOut = This;
175     }
176
177     if (*ppvOut)
178     {
179         IUnknown_AddRef(iface);
180         return S_OK;
181     }
182
183     return E_NOINTERFACE;
184 }
185
186 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
187 {
188     TextStoreACP *This = impl_from_ITextStoreACP(iface);
189     return InterlockedIncrement(&This->refCount);
190 }
191
192 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
193 {
194     TextStoreACP *This = impl_from_ITextStoreACP(iface);
195     ULONG ret;
196
197     ret = InterlockedDecrement(&This->refCount);
198     if (ret == 0)
199         TextStoreACP_Destructor(This);
200     return ret;
201 }
202
203 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
204     REFIID riid, IUnknown *punk, DWORD dwMask)
205 {
206     HRESULT hr;
207
208     sink_fire_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
209
210     hr = IUnknown_QueryInterface(punk, &IID_ITextStoreACPSink,(LPVOID*)(&ACPSink));
211     ok(SUCCEEDED(hr),"Unable to QueryInterface on sink\n");
212     return S_OK;
213 }
214
215 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
216     IUnknown *punk)
217 {
218     trace("\n");
219     return S_OK;
220 }
221
222 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
223     DWORD dwLockFlags, HRESULT *phrSession)
224 {
225     sink_fire_ok(&test_ACP_RequestLock,"TextStoreACP_RequestLock");
226     *phrSession = ITextStoreACPSink_OnLockGranted(ACPSink, dwLockFlags);
227     return S_OK;
228 }
229 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
230     TS_STATUS *pdcs)
231 {
232     sink_fire_ok(&test_ACP_GetStatus,"TextStoreACP_GetStatus");
233     pdcs->dwDynamicFlags = documentStatus;
234     return S_OK;
235 }
236 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
237     LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
238     LONG *pacpResultEnd)
239 {
240     trace("\n");
241     return S_OK;
242 }
243 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
244     ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
245 {
246     sink_fire_ok(&test_ACP_GetSelection,"TextStoreACP_GetSelection");
247
248     pSelection->acpStart = 10;
249     pSelection->acpEnd = 20;
250     pSelection->style.fInterimChar = 0;
251     pSelection->style.ase = TS_AE_NONE;
252     *pcFetched = 1;
253
254     return S_OK;
255 }
256 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
257     ULONG ulCount, const TS_SELECTION_ACP *pSelection)
258 {
259     sink_fire_ok(&test_ACP_SetSelection,"TextStoreACP_SetSelection");
260     return S_OK;
261 }
262 static HRESULT WINAPI TextStoreACP_GetText(ITextStoreACP *iface,
263     LONG acpStart, LONG acpEnd, WCHAR *pchPlain, ULONG cchPlainReq,
264     ULONG *pcchPlainRet, TS_RUNINFO *prgRunInfo, ULONG cRunInfoReq,
265     ULONG *pcRunInfoRet, LONG *pacpNext)
266 {
267     trace("\n");
268     return S_OK;
269 }
270 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
271     DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
272     ULONG cch, TS_TEXTCHANGE *pChange)
273 {
274     trace("\n");
275     return S_OK;
276 }
277 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
278     LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
279 {
280     trace("\n");
281     return S_OK;
282 }
283 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
284     LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
285 {
286     trace("\n");
287     return S_OK;
288 }
289 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
290     const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
291 {
292     trace("\n");
293     return S_OK;
294 }
295 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
296     DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
297     TS_TEXTCHANGE *pChange)
298 {
299     trace("\n");
300     return S_OK;
301 }
302 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
303     DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
304     LONG *pacpEnd, TS_TEXTCHANGE *pChange)
305 {
306     sink_fire_ok(&test_ACP_InsertTextAtSelection,"TextStoreACP_InsertTextAtSelection");
307     return S_OK;
308 }
309 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
310     DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
311     TS_TEXTCHANGE *pChange)
312 {
313     trace("\n");
314     return S_OK;
315 }
316 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
317     DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
318 {
319     trace("\n");
320     return S_OK;
321 }
322 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
323     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
324     DWORD dwFlags)
325 {
326     trace("\n");
327     return S_OK;
328 }
329 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
330     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
331     DWORD dwFlags)
332 {
333     trace("\n");
334     return S_OK;
335 }
336 static HRESULT WINAPI TextStoreACP_FindNextAttrTransition(ITextStoreACP *iface,
337     LONG acpStart, LONG acpHalt, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
338     DWORD dwFlags, LONG *pacpNext, BOOL *pfFound, LONG *plFoundOffset)
339 {
340     trace("\n");
341     return S_OK;
342 }
343 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
344     ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
345 {
346     trace("\n");
347     return S_OK;
348 }
349 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
350     LONG *pacp)
351 {
352     sink_fire_ok(&test_ACP_GetEndACP,"TextStoreACP_GetEndACP");
353     return S_OK;
354 }
355 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
356     TsViewCookie *pvcView)
357 {
358     trace("\n");
359     return S_OK;
360 }
361 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
362     TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
363     LONG *pacp)
364 {
365     trace("\n");
366     return S_OK;
367 }
368 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
369     TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
370     BOOL *pfClipped)
371 {
372     trace("\n");
373     return S_OK;
374 }
375 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
376     TsViewCookie vcView, RECT *prc)
377 {
378     trace("\n");
379     return S_OK;
380 }
381 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
382     TsViewCookie vcView, HWND *phwnd)
383 {
384     trace("\n");
385     return S_OK;
386 }
387
388 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
389 {
390     TextStoreACP_QueryInterface,
391     TextStoreACP_AddRef,
392     TextStoreACP_Release,
393
394     TextStoreACP_AdviseSink,
395     TextStoreACP_UnadviseSink,
396     TextStoreACP_RequestLock,
397     TextStoreACP_GetStatus,
398     TextStoreACP_QueryInsert,
399     TextStoreACP_GetSelection,
400     TextStoreACP_SetSelection,
401     TextStoreACP_GetText,
402     TextStoreACP_SetText,
403     TextStoreACP_GetFormattedText,
404     TextStoreACP_GetEmbedded,
405     TextStoreACP_QueryInsertEmbedded,
406     TextStoreACP_InsertEmbedded,
407     TextStoreACP_InsertTextAtSelection,
408     TextStoreACP_InsertEmbeddedAtSelection,
409     TextStoreACP_RequestSupportedAttrs,
410     TextStoreACP_RequestAttrsAtPosition,
411     TextStoreACP_RequestAttrsTransitioningAtPosition,
412     TextStoreACP_FindNextAttrTransition,
413     TextStoreACP_RetrieveRequestedAttrs,
414     TextStoreACP_GetEndACP,
415     TextStoreACP_GetActiveView,
416     TextStoreACP_GetACPFromPoint,
417     TextStoreACP_GetTextExt,
418     TextStoreACP_GetScreenExt,
419     TextStoreACP_GetWnd
420 };
421
422 static HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
423 {
424     TextStoreACP *This;
425
426     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
427     if (This == NULL)
428         return E_OUTOFMEMORY;
429
430     This->ITextStoreACP_iface.lpVtbl = &TextStoreACP_TextStoreACPVtbl;
431     This->refCount = 1;
432
433     *ppOut = (IUnknown *)This;
434     return S_OK;
435 }
436
437 /**********************************************************************
438  * ITfThreadMgrEventSink
439  **********************************************************************/
440 typedef struct tagThreadMgrEventSink
441 {
442     ITfThreadMgrEventSink ITfThreadMgrEventSink_iface;
443     LONG refCount;
444 } ThreadMgrEventSink;
445
446 static inline ThreadMgrEventSink *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
447 {
448     return CONTAINING_RECORD(iface, ThreadMgrEventSink, ITfThreadMgrEventSink_iface);
449 }
450
451 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
452 {
453     HeapFree(GetProcessHeap(),0,This);
454 }
455
456 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
457 {
458     ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
459     *ppvOut = NULL;
460
461     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
462     {
463         *ppvOut = This;
464     }
465
466     if (*ppvOut)
467     {
468         IUnknown_AddRef(iface);
469         return S_OK;
470     }
471
472     return E_NOINTERFACE;
473 }
474
475 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
476 {
477     ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
478     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
479     return InterlockedIncrement(&This->refCount);
480 }
481
482 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
483 {
484     ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
485     ULONG ret;
486
487     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
488     ret = InterlockedDecrement(&This->refCount);
489     if (ret == 0)
490         ThreadMgrEventSink_Destructor(This);
491     return ret;
492 }
493
494 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
495 ITfDocumentMgr *pdim)
496 {
497     sink_fire_ok(&test_OnInitDocumentMgr,"ThreadMgrEventSink_OnInitDocumentMgr");
498     return S_OK;
499 }
500
501 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
502 ITfDocumentMgr *pdim)
503 {
504     trace("\n");
505     return S_OK;
506 }
507
508 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
509 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
510 {
511     sink_fire_ok(&test_OnSetFocus,"ThreadMgrEventSink_OnSetFocus");
512     if (test_CurrentFocus != FOCUS_IGNORE)
513         ok(pdimFocus == test_CurrentFocus,"Sink reports wrong focus\n");
514     if (test_PrevFocus != FOCUS_IGNORE)
515         ok(pdimPrevFocus == test_PrevFocus,"Sink reports wrong previous focus\n");
516     return S_OK;
517 }
518
519 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(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     test = (ITfContext*)0xdeadbeef;
529     ITfDocumentMgr_Release(docmgr);
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_OnPushContext,"ThreadMgrEventSink_OnPushContext");
537     return S_OK;
538 }
539
540 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
541 ITfContext *pic)
542 {
543     HRESULT hr;
544     ITfDocumentMgr *docmgr;
545     ITfContext *test;
546
547     hr = ITfContext_GetDocumentMgr(pic,&docmgr);
548     ok(SUCCEEDED(hr),"GetDocumenMgr failed\n");
549     ITfDocumentMgr_Release(docmgr);
550     test = (ITfContext*)0xdeadbeef;
551     hr = ITfDocumentMgr_GetTop(docmgr,&test);
552     ok(SUCCEEDED(hr),"GetTop failed\n");
553     ok(test == pic, "Wrong context is on top\n");
554     if (test)
555         ITfContext_Release(test);
556
557     sink_fire_ok(&test_OnPopContext,"ThreadMgrEventSink_OnPopContext");
558     return S_OK;
559 }
560
561 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
562 {
563     ThreadMgrEventSink_QueryInterface,
564     ThreadMgrEventSink_AddRef,
565     ThreadMgrEventSink_Release,
566
567     ThreadMgrEventSink_OnInitDocumentMgr,
568     ThreadMgrEventSink_OnUninitDocumentMgr,
569     ThreadMgrEventSink_OnSetFocus,
570     ThreadMgrEventSink_OnPushContext,
571     ThreadMgrEventSink_OnPopContext
572 };
573
574 static HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
575 {
576     ThreadMgrEventSink *This;
577
578     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
579     if (This == NULL)
580         return E_OUTOFMEMORY;
581
582     This->ITfThreadMgrEventSink_iface.lpVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
583     This->refCount = 1;
584
585     *ppOut = (IUnknown *)This;
586     return S_OK;
587 }
588
589
590 /********************************************************************************************
591  * Stub text service for testing
592  ********************************************************************************************/
593
594 static LONG TS_refCount;
595 static IClassFactory *cf;
596 static DWORD regid;
597
598 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
599
600 typedef struct tagClassFactory
601 {
602     IClassFactory IClassFactory_iface;
603     LONG   ref;
604     LPFNCONSTRUCTOR ctor;
605 } ClassFactory;
606
607 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
608 {
609     return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
610 }
611
612 typedef struct tagTextService
613 {
614     ITfTextInputProcessor ITfTextInputProcessor_iface;
615     LONG refCount;
616 } TextService;
617
618 static inline TextService *impl_from_ITfTextInputProcessor(ITfTextInputProcessor *iface)
619 {
620     return CONTAINING_RECORD(iface, TextService, ITfTextInputProcessor_iface);
621 }
622
623 static void ClassFactory_Destructor(ClassFactory *This)
624 {
625     HeapFree(GetProcessHeap(),0,This);
626     TS_refCount--;
627 }
628
629 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
630 {
631     *ppvOut = NULL;
632     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
633     {
634         IClassFactory_AddRef(iface);
635         *ppvOut = iface;
636         return S_OK;
637     }
638
639     return E_NOINTERFACE;
640 }
641
642 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
643 {
644     ClassFactory *This = impl_from_IClassFactory(iface);
645     return InterlockedIncrement(&This->ref);
646 }
647
648 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
649 {
650     ClassFactory *This = impl_from_IClassFactory(iface);
651     ULONG ret = InterlockedDecrement(&This->ref);
652
653     if (ret == 0)
654         ClassFactory_Destructor(This);
655     return ret;
656 }
657
658 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
659 {
660     ClassFactory *This = impl_from_IClassFactory(iface);
661     HRESULT ret;
662     IUnknown *obj;
663
664     ret = This->ctor(punkOuter, &obj);
665     if (FAILED(ret))
666         return ret;
667     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
668     IUnknown_Release(obj);
669     return ret;
670 }
671
672 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
673 {
674     if(fLock)
675         InterlockedIncrement(&TS_refCount);
676     else
677         InterlockedDecrement(&TS_refCount);
678
679     return S_OK;
680 }
681
682 static const IClassFactoryVtbl ClassFactoryVtbl = {
683     /* IUnknown */
684     ClassFactory_QueryInterface,
685     ClassFactory_AddRef,
686     ClassFactory_Release,
687
688     /* IClassFactory*/
689     ClassFactory_CreateInstance,
690     ClassFactory_LockServer
691 };
692
693 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
694 {
695     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
696     This->IClassFactory_iface.lpVtbl = &ClassFactoryVtbl;
697     This->ref = 1;
698     This->ctor = ctor;
699     *ppvOut = (LPVOID)This;
700     TS_refCount++;
701     return S_OK;
702 }
703
704 static void TextService_Destructor(TextService *This)
705 {
706     HeapFree(GetProcessHeap(),0,This);
707 }
708
709 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
710 {
711     TextService *This = impl_from_ITfTextInputProcessor(iface);
712     *ppvOut = NULL;
713
714     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
715     {
716         *ppvOut = This;
717     }
718
719     if (*ppvOut)
720     {
721         IUnknown_AddRef(iface);
722         return S_OK;
723     }
724
725     return E_NOINTERFACE;
726 }
727
728 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
729 {
730     TextService *This = impl_from_ITfTextInputProcessor(iface);
731     return InterlockedIncrement(&This->refCount);
732 }
733
734 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
735 {
736     TextService *This = impl_from_ITfTextInputProcessor(iface);
737     ULONG ret;
738
739     ret = InterlockedDecrement(&This->refCount);
740     if (ret == 0)
741         TextService_Destructor(This);
742     return ret;
743 }
744
745 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
746         ITfThreadMgr *ptim, TfClientId id)
747 {
748     trace("TextService_Activate\n");
749     ok(test_ShouldActivate,"Activation came unexpectedly\n");
750     tid = id;
751     return S_OK;
752 }
753
754 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
755 {
756     trace("TextService_Deactivate\n");
757     ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
758     return S_OK;
759 }
760
761 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
762 {
763     TextService_QueryInterface,
764     TextService_AddRef,
765     TextService_Release,
766
767     TextService_Activate,
768     TextService_Deactivate
769 };
770
771 static HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
772 {
773     TextService *This;
774     if (pUnkOuter)
775         return CLASS_E_NOAGGREGATION;
776
777     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
778     if (This == NULL)
779         return E_OUTOFMEMORY;
780
781     This->ITfTextInputProcessor_iface.lpVtbl = &TextService_TextInputProcessorVtbl;
782     This->refCount = 1;
783
784     *ppOut = (IUnknown *)This;
785     return S_OK;
786 }
787
788 static HRESULT RegisterTextService(REFCLSID rclsid)
789 {
790     ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
791     return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
792 }
793
794 static HRESULT UnregisterTextService(void)
795 {
796     return CoRevokeClassObject(regid);
797 }
798
799 /*
800  * The tests
801  */
802
803 DEFINE_GUID(CLSID_FakeService, 0xEDE1A7AD,0x66DE,0x47E0,0xB6,0x20,0x3E,0x92,0xF8,0x24,0x6B,0xF3);
804 DEFINE_GUID(CLSID_TF_InputProcessorProfiles, 0x33c53a50,0xf456,0x4884,0xb0,0x49,0x85,0xfd,0x64,0x3e,0xcf,0xed);
805 DEFINE_GUID(CLSID_TF_CategoryMgr,         0xA4B544A1,0x438D,0x4B41,0x93,0x25,0x86,0x95,0x23,0xE2,0xD6,0xC7);
806 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD,     0x34745c63,0xb2f0,0x4784,0x8b,0x67,0x5e,0x12,0xc8,0x70,0x1a,0x31);
807 DEFINE_GUID(GUID_TFCAT_TIP_SPEECH,       0xB5A73CD1,0x8355,0x426B,0xA1,0x61,0x25,0x98,0x08,0xF2,0x6B,0x14);
808 DEFINE_GUID(GUID_TFCAT_TIP_HANDWRITING,  0x246ecb87,0xc2f2,0x4abe,0x90,0x5b,0xc8,0xb3,0x8a,0xdd,0x2c,0x43);
809 DEFINE_GUID (GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER,  0x046B8C80,0x1647,0x40F7,0x9B,0x21,0xB9,0x3B,0x81,0xAA,0xBC,0x1B);
810 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
811 DEFINE_GUID(CLSID_TF_ThreadMgr,           0x529a9e6b,0x6587,0x4f23,0xab,0x9e,0x9c,0x7d,0x68,0x3e,0x3c,0x50);
812 DEFINE_GUID(CLSID_PreservedKey,           0xA0ED8E55,0xCD3B,0x4274,0xB2,0x95,0xF6,0xC9,0xBA,0x2B,0x84,0x72);
813 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_DISABLED,     0x71a5b253,0x1951,0x466b,0x9f,0xbc,0x9c,0x88,0x08,0xfa,0x84,0xf2);
814 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE,    0x58273aad,0x01bb,0x4164,0x95,0xc6,0x75,0x5b,0xa0,0xb5,0x16,0x2d);
815 DEFINE_GUID(GUID_COMPARTMENT_HANDWRITING_OPENCLOSE, 0xf9ae2c6b,0x1866,0x4361,0xaf,0x72,0x7a,0xa3,0x09,0x48,0x89,0x0e);
816 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_DISABLED,       0x56c5c607,0x0703,0x4e59,0x8e,0x52,0xcb,0xc8,0x4e,0x8b,0xbe,0x35);
817 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_OPENCLOSE,      0x544d6a63,0xe2e8,0x4752,0xbb,0xd1,0x00,0x09,0x60,0xbc,0xa0,0x83);
818 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_GLOBALSTATE,    0x2a54fe8e,0x0d08,0x460c,0xa7,0x5d,0x87,0x03,0x5f,0xf4,0x36,0xc5);
819 DEFINE_GUID(GUID_COMPARTMENT_PERSISTMENUENABLED,    0x575f3783,0x70c8,0x47c8,0xae,0x5d,0x91,0xa0,0x1a,0x1f,0x75,0x92);
820 DEFINE_GUID(GUID_COMPARTMENT_EMPTYCONTEXT,          0xd7487dbf,0x804e,0x41c5,0x89,0x4d,0xad,0x96,0xfd,0x4e,0xea,0x13);
821 DEFINE_GUID(GUID_COMPARTMENT_TIPUISTATUS,           0x148ca3ec,0x0366,0x401c,0x8d,0x75,0xed,0x97,0x8d,0x85,0xfb,0xc9);
822
823 static HRESULT initialize(void)
824 {
825     HRESULT hr;
826     CoInitialize(NULL);
827     hr = CoCreateInstance (&CLSID_TF_InputProcessorProfiles, NULL,
828           CLSCTX_INPROC_SERVER, &IID_ITfInputProcessorProfiles, (void**)&g_ipp);
829     if (SUCCEEDED(hr))
830         hr = CoCreateInstance (&CLSID_TF_CategoryMgr, NULL,
831           CLSCTX_INPROC_SERVER, &IID_ITfCategoryMgr, (void**)&g_cm);
832     if (SUCCEEDED(hr))
833         hr = CoCreateInstance (&CLSID_TF_ThreadMgr, NULL,
834           CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (void**)&g_tm);
835     return hr;
836 }
837
838 static void cleanup(void)
839 {
840     if (g_ipp)
841         ITfInputProcessorProfiles_Release(g_ipp);
842     if (g_cm)
843         ITfCategoryMgr_Release(g_cm);
844     if (g_tm)
845         ITfThreadMgr_Release(g_tm);
846     CoUninitialize();
847 }
848
849 static void test_Register(void)
850 {
851     HRESULT hr;
852
853     static const WCHAR szDesc[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',0};
854     static const WCHAR szFile[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',' ','F','i','l','e',0};
855
856     hr = ITfInputProcessorProfiles_GetCurrentLanguage(g_ipp,&gLangid);
857     ok(SUCCEEDED(hr),"Unable to get current language id\n");
858     trace("Current Language %x\n",gLangid);
859
860     hr = RegisterTextService(&CLSID_FakeService);
861     ok(SUCCEEDED(hr),"Unable to register COM for TextService\n");
862     hr = ITfInputProcessorProfiles_Register(g_ipp, &CLSID_FakeService);
863     ok(SUCCEEDED(hr),"Unable to register text service(%x)\n",hr);
864     hr = ITfInputProcessorProfiles_AddLanguageProfile(g_ipp, &CLSID_FakeService, gLangid, &CLSID_FakeService, szDesc, sizeof(szDesc)/sizeof(WCHAR), szFile, sizeof(szFile)/sizeof(WCHAR), 1);
865     ok(SUCCEEDED(hr),"Unable to add Language Profile (%x)\n",hr);
866 }
867
868 static void test_Unregister(void)
869 {
870     HRESULT hr;
871     hr = ITfInputProcessorProfiles_Unregister(g_ipp, &CLSID_FakeService);
872     ok(SUCCEEDED(hr),"Unable to unregister text service(%x)\n",hr);
873     UnregisterTextService();
874 }
875
876 static void test_EnumInputProcessorInfo(void)
877 {
878     IEnumGUID *ppEnum;
879     BOOL found = FALSE;
880
881     if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
882     {
883         ULONG fetched;
884         GUID g;
885         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
886         {
887             if(IsEqualGUID(&g,&CLSID_FakeService))
888                 found = TRUE;
889         }
890     }
891     ok(found,"Did not find registered text service\n");
892 }
893
894 static void test_EnumLanguageProfiles(void)
895 {
896     BOOL found = FALSE;
897     IEnumTfLanguageProfiles *ppEnum;
898     if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
899     {
900         TF_LANGUAGEPROFILE profile;
901         while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
902         {
903             if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
904             {
905                 found = TRUE;
906                 ok(profile.langid == gLangid, "LangId Incorrect\n");
907                 ok(IsEqualGUID(&profile.catid,&GUID_TFCAT_TIP_KEYBOARD), "CatId Incorrect\n");
908                 ok(IsEqualGUID(&profile.guidProfile,&CLSID_FakeService), "guidProfile Incorrect\n");
909             }
910         }
911     }
912     ok(found,"Registered text service not found\n");
913 }
914
915 static void test_RegisterCategory(void)
916 {
917     HRESULT hr;
918     hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
919     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
920     hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
921     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
922 }
923
924 static void test_UnregisterCategory(void)
925 {
926     HRESULT hr;
927     hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
928     ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
929     hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
930     ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
931 }
932
933 static void test_FindClosestCategory(void)
934 {
935     GUID output;
936     HRESULT hr;
937     const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
938
939     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, NULL, 0);
940     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
941     ok(IsEqualGUID(&output,&GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER),"Wrong GUID\n");
942
943     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 1);
944     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
945     ok(IsEqualGUID(&output,&GUID_NULL),"Wrong GUID\n");
946
947     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 3);
948     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
949     ok(IsEqualGUID(&output,&GUID_TFCAT_TIP_KEYBOARD),"Wrong GUID\n");
950 }
951
952 static void test_Enable(void)
953 {
954     HRESULT hr;
955     BOOL enabled = FALSE;
956
957     hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, TRUE);
958     ok(SUCCEEDED(hr),"Failed to enable text service\n");
959     hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, &enabled);
960     ok(SUCCEEDED(hr),"Failed to get enabled state\n");
961     ok(enabled == TRUE,"enabled state incorrect\n");
962 }
963
964 static void test_Disable(void)
965 {
966     HRESULT hr;
967
968     trace("Disabling\n");
969     hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, FALSE);
970     ok(SUCCEEDED(hr),"Failed to disable text service\n");
971 }
972
973 static void test_ThreadMgrAdviseSinks(void)
974 {
975     ITfSource *source = NULL;
976     HRESULT hr;
977     IUnknown *sink;
978
979     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
980     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
981     if (!source)
982         return;
983
984     hr = ThreadMgrEventSink_Constructor(&sink);
985     ok(hr == S_OK, "got %08x\n", hr);
986     if(FAILED(hr)) return;
987
988     tmSinkRefCount = 1;
989     tmSinkCookie = 0;
990     hr = ITfSource_AdviseSink(source,&IID_ITfThreadMgrEventSink, sink, &tmSinkCookie);
991     ok(SUCCEEDED(hr),"Failed to Advise Sink\n");
992     ok(tmSinkCookie!=0,"Failed to get sink cookie\n");
993
994     /* Advising the sink adds a ref, Relesing here lets the object be deleted
995        when unadvised */
996     tmSinkRefCount = 2;
997     IUnknown_Release(sink);
998     ITfSource_Release(source);
999 }
1000
1001 static void test_ThreadMgrUnadviseSinks(void)
1002 {
1003     ITfSource *source = NULL;
1004     HRESULT hr;
1005
1006     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
1007     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
1008     if (!source)
1009         return;
1010
1011     tmSinkRefCount = 1;
1012     hr = ITfSource_UnadviseSink(source, tmSinkCookie);
1013     ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1014     ITfSource_Release(source);
1015 }
1016
1017 /**********************************************************************
1018  * ITfKeyEventSink
1019  **********************************************************************/
1020 typedef struct tagKeyEventSink
1021 {
1022     ITfKeyEventSink ITfKeyEventSink_iface;
1023     LONG refCount;
1024 } KeyEventSink;
1025
1026 static inline KeyEventSink *impl_from_ITfKeyEventSink(ITfKeyEventSink *iface)
1027 {
1028     return CONTAINING_RECORD(iface, KeyEventSink, ITfKeyEventSink_iface);
1029 }
1030
1031 static void KeyEventSink_Destructor(KeyEventSink *This)
1032 {
1033     HeapFree(GetProcessHeap(),0,This);
1034 }
1035
1036 static HRESULT WINAPI KeyEventSink_QueryInterface(ITfKeyEventSink *iface, REFIID iid, LPVOID *ppvOut)
1037 {
1038     KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1039     *ppvOut = NULL;
1040
1041     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfKeyEventSink))
1042     {
1043         *ppvOut = This;
1044     }
1045
1046     if (*ppvOut)
1047     {
1048         IUnknown_AddRef(iface);
1049         return S_OK;
1050     }
1051
1052     return E_NOINTERFACE;
1053 }
1054
1055 static ULONG WINAPI KeyEventSink_AddRef(ITfKeyEventSink *iface)
1056 {
1057     KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1058     return InterlockedIncrement(&This->refCount);
1059 }
1060
1061 static ULONG WINAPI KeyEventSink_Release(ITfKeyEventSink *iface)
1062 {
1063     KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1064     ULONG ret;
1065
1066     ret = InterlockedDecrement(&This->refCount);
1067     if (ret == 0)
1068         KeyEventSink_Destructor(This);
1069     return ret;
1070 }
1071
1072 static HRESULT WINAPI KeyEventSink_OnSetFocus(ITfKeyEventSink *iface,
1073         BOOL fForeground)
1074 {
1075     sink_fire_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1076     return S_OK;
1077 }
1078
1079 static HRESULT WINAPI KeyEventSink_OnTestKeyDown(ITfKeyEventSink *iface,
1080         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1081 {
1082     trace("\n");
1083     return S_OK;
1084 }
1085
1086 static HRESULT WINAPI KeyEventSink_OnTestKeyUp(ITfKeyEventSink *iface,
1087         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1088 {
1089     trace("\n");
1090     return S_OK;
1091 }
1092
1093 static HRESULT WINAPI KeyEventSink_OnKeyDown(ITfKeyEventSink *iface,
1094         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1095 {
1096     trace("\n");
1097     return S_OK;
1098 }
1099
1100 static HRESULT WINAPI KeyEventSink_OnKeyUp(ITfKeyEventSink *iface,
1101         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1102 {
1103     trace("\n");
1104     return S_OK;
1105 }
1106
1107 static HRESULT WINAPI KeyEventSink_OnPreservedKey(ITfKeyEventSink *iface,
1108     ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
1109 {
1110     trace("\n");
1111     return S_OK;
1112 }
1113
1114 static const ITfKeyEventSinkVtbl KeyEventSink_KeyEventSinkVtbl =
1115 {
1116     KeyEventSink_QueryInterface,
1117     KeyEventSink_AddRef,
1118     KeyEventSink_Release,
1119
1120     KeyEventSink_OnSetFocus,
1121     KeyEventSink_OnTestKeyDown,
1122     KeyEventSink_OnTestKeyUp,
1123     KeyEventSink_OnKeyDown,
1124     KeyEventSink_OnKeyUp,
1125     KeyEventSink_OnPreservedKey
1126 };
1127
1128 static HRESULT KeyEventSink_Constructor(ITfKeyEventSink **ppOut)
1129 {
1130     KeyEventSink *This;
1131
1132     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(KeyEventSink));
1133     if (This == NULL)
1134         return E_OUTOFMEMORY;
1135
1136     This->ITfKeyEventSink_iface.lpVtbl = &KeyEventSink_KeyEventSinkVtbl;
1137     This->refCount = 1;
1138
1139     *ppOut = &This->ITfKeyEventSink_iface;
1140     return S_OK;
1141 }
1142
1143
1144 static void test_KeystrokeMgr(void)
1145 {
1146     ITfKeystrokeMgr *keymgr= NULL;
1147     HRESULT hr;
1148     TF_PRESERVEDKEY tfpk;
1149     BOOL preserved;
1150     ITfKeyEventSink *sink = NULL;
1151
1152     KeyEventSink_Constructor(&sink);
1153
1154     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfKeystrokeMgr, (LPVOID*)&keymgr);
1155     ok(SUCCEEDED(hr),"Failed to get IID_ITfKeystrokeMgr for ThreadMgr\n");
1156
1157     tfpk.uVKey = 'A';
1158     tfpk.uModifiers = TF_MOD_SHIFT;
1159
1160     test_KEV_OnSetFocus = SINK_EXPECTED;
1161     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1162     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
1163     sink_check_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1164     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1165     ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
1166     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
1167     ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
1168
1169     hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1170     ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1171
1172     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1173     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1174
1175     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1176     ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1177
1178     preserved = FALSE;
1179     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1180     ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1181     if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
1182
1183     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1184     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1185
1186     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1187     ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1188     if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
1189
1190     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1191     ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
1192
1193     hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1194     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1195
1196     ITfKeystrokeMgr_Release(keymgr);
1197     ITfKeyEventSink_Release(sink);
1198 }
1199
1200 static void test_Activate(void)
1201 {
1202     HRESULT hr;
1203
1204     hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1205     ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1206 }
1207
1208
1209 static void test_EnumContexts(ITfDocumentMgr *dm, ITfContext *search)
1210 {
1211     HRESULT hr;
1212     IEnumTfContexts* pEnum;
1213     BOOL found = FALSE;
1214
1215     hr = ITfDocumentMgr_EnumContexts(dm,&pEnum);
1216     ok(SUCCEEDED(hr),"EnumContexts failed\n");
1217     if (SUCCEEDED(hr))
1218     {
1219         ULONG fetched;
1220         ITfContext *cxt;
1221         while (IEnumTfContexts_Next(pEnum, 1, &cxt, &fetched) == S_OK)
1222         {
1223             if (!search)
1224                 found = TRUE;
1225             else if (search == cxt)
1226                 found = TRUE;
1227             ITfContext_Release(cxt);
1228         }
1229         IEnumTfContexts_Release(pEnum);
1230     }
1231     if (search)
1232         ok(found,"Did not find proper ITfContext\n");
1233     else
1234         ok(!found,"Found an ITfContext we should should not have\n");
1235 }
1236
1237 static void test_EnumDocumentMgr(ITfThreadMgr *tm, ITfDocumentMgr *search, ITfDocumentMgr *absent)
1238 {
1239     HRESULT hr;
1240     IEnumTfDocumentMgrs* pEnum;
1241     BOOL found = FALSE;
1242     BOOL notfound = TRUE;
1243
1244     hr = ITfThreadMgr_EnumDocumentMgrs(tm,&pEnum);
1245     ok(SUCCEEDED(hr),"EnumDocumentMgrs failed\n");
1246     if (SUCCEEDED(hr))
1247     {
1248         ULONG fetched;
1249         ITfDocumentMgr *dm;
1250         while (IEnumTfDocumentMgrs_Next(pEnum, 1, &dm, &fetched) == S_OK)
1251         {
1252             if (!search)
1253                 found = TRUE;
1254             else if (search == dm)
1255                 found = TRUE;
1256             if (absent && dm == absent)
1257                 notfound = FALSE;
1258             ITfDocumentMgr_Release(dm);
1259         }
1260         IEnumTfDocumentMgrs_Release(pEnum);
1261     }
1262     if (search)
1263         ok(found,"Did not find proper ITfDocumentMgr\n");
1264     else
1265         ok(!found,"Found an ITfDocumentMgr we should should not have\n");
1266     if (absent)
1267         ok(notfound,"Found an ITfDocumentMgr we believe should be absent\n");
1268 }
1269
1270 static inline int check_context_refcount(ITfContext *iface)
1271 {
1272     IUnknown_AddRef(iface);
1273     return IUnknown_Release(iface);
1274 }
1275
1276
1277 /**********************************************************************
1278  * ITfTextEditSink
1279  **********************************************************************/
1280 typedef struct tagTextEditSink
1281 {
1282     ITfTextEditSink ITfTextEditSink_iface;
1283     LONG refCount;
1284 } TextEditSink;
1285
1286 static inline TextEditSink *impl_from_ITfTextEditSink(ITfTextEditSink *iface)
1287 {
1288     return CONTAINING_RECORD(iface, TextEditSink, ITfTextEditSink_iface);
1289 }
1290
1291 static void TextEditSink_Destructor(TextEditSink *This)
1292 {
1293     HeapFree(GetProcessHeap(),0,This);
1294 }
1295
1296 static HRESULT WINAPI TextEditSink_QueryInterface(ITfTextEditSink *iface, REFIID iid, LPVOID *ppvOut)
1297 {
1298     TextEditSink *This = impl_from_ITfTextEditSink(iface);
1299     *ppvOut = NULL;
1300
1301     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextEditSink))
1302     {
1303         *ppvOut = This;
1304     }
1305
1306     if (*ppvOut)
1307     {
1308         IUnknown_AddRef(iface);
1309         return S_OK;
1310     }
1311
1312     return E_NOINTERFACE;
1313 }
1314
1315 static ULONG WINAPI TextEditSink_AddRef(ITfTextEditSink *iface)
1316 {
1317     TextEditSink *This = impl_from_ITfTextEditSink(iface);
1318     return InterlockedIncrement(&This->refCount);
1319 }
1320
1321 static ULONG WINAPI TextEditSink_Release(ITfTextEditSink *iface)
1322 {
1323     TextEditSink *This = impl_from_ITfTextEditSink(iface);
1324     ULONG ret;
1325
1326     ret = InterlockedDecrement(&This->refCount);
1327     if (ret == 0)
1328         TextEditSink_Destructor(This);
1329     return ret;
1330 }
1331
1332 static HRESULT WINAPI TextEditSink_OnEndEdit(ITfTextEditSink *iface,
1333     ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
1334 {
1335     sink_fire_ok(&test_OnEndEdit,"TextEditSink_OnEndEdit");
1336     return S_OK;
1337 }
1338
1339 static const ITfTextEditSinkVtbl TextEditSink_TextEditSinkVtbl =
1340 {
1341     TextEditSink_QueryInterface,
1342     TextEditSink_AddRef,
1343     TextEditSink_Release,
1344
1345     TextEditSink_OnEndEdit
1346 };
1347
1348 static HRESULT TextEditSink_Constructor(ITfTextEditSink **ppOut)
1349 {
1350     TextEditSink *This;
1351
1352     *ppOut = NULL;
1353     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextEditSink));
1354     if (This == NULL)
1355         return E_OUTOFMEMORY;
1356
1357     This->ITfTextEditSink_iface.lpVtbl = &TextEditSink_TextEditSinkVtbl;
1358     This->refCount = 1;
1359
1360     *ppOut = &This->ITfTextEditSink_iface;
1361     return S_OK;
1362 }
1363
1364 static void test_startSession(void)
1365 {
1366     HRESULT hr;
1367     DWORD cnt;
1368     DWORD editCookie;
1369     ITfDocumentMgr *dmtest;
1370     ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
1371     ITextStoreACP *ts;
1372     TfClientId cid2 = 0;
1373
1374     hr = ITfThreadMgr_Deactivate(g_tm);
1375     ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1376
1377     test_ShouldActivate = TRUE;
1378     hr  = ITfThreadMgr_Activate(g_tm,&cid);
1379     ok(SUCCEEDED(hr),"Failed to Activate\n");
1380     ok(cid != tid,"TextService id mistakenly matches Client id\n");
1381
1382     test_ShouldActivate = FALSE;
1383     hr = ITfThreadMgr_Activate(g_tm,&cid2);
1384     ok(SUCCEEDED(hr),"Failed to Activate\n");
1385     ok (cid == cid2, "Second activate client ID does not match\n");
1386
1387     hr = ITfThreadMgr_Deactivate(g_tm);
1388     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1389
1390     test_EnumDocumentMgr(g_tm,NULL,NULL);
1391
1392     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1393     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1394
1395     test_EnumDocumentMgr(g_tm,g_dm,NULL);
1396
1397     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dmtest);
1398     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1399
1400     test_EnumDocumentMgr(g_tm,dmtest,NULL);
1401
1402     ITfDocumentMgr_Release(dmtest);
1403     test_EnumDocumentMgr(g_tm,g_dm,dmtest);
1404
1405     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1406     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1407     ok(dmtest == NULL,"Initial focus not null\n");
1408
1409     test_CurrentFocus = g_dm;
1410     test_PrevFocus = NULL;
1411     test_OnSetFocus  = SINK_EXPECTED;
1412     hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
1413     ok(SUCCEEDED(hr),"SetFocus Failed\n");
1414     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1415
1416     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1417     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1418     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1419
1420     cnt = ITfDocumentMgr_Release(g_dm);
1421     ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
1422
1423     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1424     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1425     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1426     ITfDocumentMgr_Release(dmtest);
1427
1428     TextStoreACP_Constructor((IUnknown**)&ts);
1429
1430     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1431     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1432
1433     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1434     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1435
1436     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1437     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1438
1439     test_EnumContexts(g_dm, NULL);
1440
1441     hr = ITfContext_GetDocumentMgr(cxt,&dmtest);
1442     ok(hr == S_OK, "ITfContext_GetDocumentMgr failed with %x\n",hr);
1443     ok(dmtest == g_dm, "Wrong documentmgr\n");
1444     ITfDocumentMgr_Release(dmtest);
1445
1446     cnt = check_context_refcount(cxt);
1447     test_OnPushContext = SINK_EXPECTED;
1448     test_ACP_AdviseSink = SINK_EXPECTED;
1449     test_OnInitDocumentMgr = SINK_EXPECTED;
1450     hr = ITfDocumentMgr_Push(g_dm, cxt);
1451     ok(SUCCEEDED(hr),"Push Failed\n");
1452     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1453     sink_check_ok(&test_OnPushContext,"OnPushContext");
1454     sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
1455     sink_check_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
1456
1457     test_EnumContexts(g_dm, cxt);
1458
1459     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1460     ok(SUCCEEDED(hr),"GetTop Failed\n");
1461     ok(cxtTest == cxt, "Wrong context on top\n");
1462     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1463     cnt = ITfContext_Release(cxtTest);
1464
1465     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1466     ok(SUCCEEDED(hr),"GetBase Failed\n");
1467     ok(cxtTest == cxt, "Wrong context on Base\n");
1468     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1469     ITfContext_Release(cxtTest);
1470
1471     check_context_refcount(cxt2);
1472     test_OnPushContext = SINK_EXPECTED;
1473     hr = ITfDocumentMgr_Push(g_dm, cxt2);
1474     ok(SUCCEEDED(hr),"Push Failed\n");
1475     sink_check_ok(&test_OnPushContext,"OnPushContext");
1476
1477     cnt = check_context_refcount(cxt2);
1478     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1479     ok(SUCCEEDED(hr),"GetTop Failed\n");
1480     ok(cxtTest == cxt2, "Wrong context on top\n");
1481     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1482     ITfContext_Release(cxtTest);
1483
1484     cnt = check_context_refcount(cxt);
1485     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1486     ok(SUCCEEDED(hr),"GetBase Failed\n");
1487     ok(cxtTest == cxt, "Wrong context on Base\n");
1488     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1489     ITfContext_Release(cxtTest);
1490
1491     cnt = check_context_refcount(cxt3);
1492     hr = ITfDocumentMgr_Push(g_dm, cxt3);
1493     ok(FAILED(hr),"Push Succeeded\n");
1494     ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
1495
1496     cnt = check_context_refcount(cxt2);
1497     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1498     ok(SUCCEEDED(hr),"GetTop Failed\n");
1499     ok(cxtTest == cxt2, "Wrong context on top\n");
1500     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1501     ITfContext_Release(cxtTest);
1502
1503     cnt = check_context_refcount(cxt);
1504     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1505     ok(SUCCEEDED(hr),"GetBase Failed\n");
1506     ok(cxtTest == cxt, "Wrong context on Base\n");
1507     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1508     ITfContext_Release(cxtTest);
1509
1510     cnt = check_context_refcount(cxt2);
1511     test_OnPopContext = SINK_EXPECTED;
1512     hr = ITfDocumentMgr_Pop(g_dm, 0);
1513     ok(SUCCEEDED(hr),"Pop Failed\n");
1514     ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1515     sink_check_ok(&test_OnPopContext,"OnPopContext");
1516
1517     dmtest = (void *)0xfeedface;
1518     hr = ITfContext_GetDocumentMgr(cxt2,&dmtest);
1519     ok(hr == S_FALSE, "ITfContext_GetDocumentMgr wrong rc %x\n",hr);
1520     ok(dmtest == NULL,"returned documentmgr should be null\n");
1521
1522     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1523     ok(SUCCEEDED(hr),"GetTop Failed\n");
1524     ok(cxtTest == cxt, "Wrong context on top\n");
1525     ITfContext_Release(cxtTest);
1526
1527     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1528     ok(SUCCEEDED(hr),"GetBase Failed\n");
1529     ok(cxtTest == cxt, "Wrong context on base\n");
1530     ITfContext_Release(cxtTest);
1531
1532     hr = ITfDocumentMgr_Pop(g_dm, 0);
1533     ok(FAILED(hr),"Pop Succeeded\n");
1534
1535     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1536     ok(SUCCEEDED(hr),"GetTop Failed\n");
1537     ok(cxtTest == cxt, "Wrong context on top\n");
1538     ITfContext_Release(cxtTest);
1539
1540     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1541     ok(SUCCEEDED(hr),"GetBase Failed\n");
1542     ok(cxtTest == cxt, "Wrong context on base\n");
1543     ITfContext_Release(cxtTest);
1544
1545     ITfContext_Release(cxt);
1546     ITfContext_Release(cxt2);
1547     ITfContext_Release(cxt3);
1548 }
1549
1550 static void test_endSession(void)
1551 {
1552     HRESULT hr;
1553     test_ShouldDeactivate = TRUE;
1554     test_CurrentFocus = NULL;
1555     test_PrevFocus = g_dm;
1556     test_OnSetFocus  = SINK_EXPECTED;
1557     hr = ITfThreadMgr_Deactivate(g_tm);
1558     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1559     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1560     test_OnSetFocus  = SINK_UNEXPECTED;
1561 }
1562
1563 static void test_TfGuidAtom(void)
1564 {
1565     GUID gtest,g1;
1566     HRESULT hr;
1567     TfGuidAtom atom1,atom2;
1568     BOOL equal;
1569
1570     CoCreateGuid(&gtest);
1571
1572     /* msdn reports this should return E_INVALIDARG.  However my test show it crashing (winxp)*/
1573     /*
1574     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1575     ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1576     */
1577     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
1578     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1579     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
1580     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1581     ok(atom1 == atom2,"atoms do not match\n");
1582     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
1583     ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
1584     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
1585     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1586     ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
1587     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
1588     ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
1589     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
1590     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1591     ok(equal == TRUE,"Equal value invalid\n");
1592
1593     /* show that cid and tid TfClientIds are also TfGuidAtoms */
1594     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
1595     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1596     ok(equal == TRUE,"Equal value invalid\n");
1597     hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
1598     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1599     ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
1600 }
1601
1602 static void test_ClientId(void)
1603 {
1604     ITfClientId *pcid;
1605     TfClientId id1,id2;
1606     HRESULT hr;
1607     GUID g2;
1608
1609     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1610     ok(SUCCEEDED(hr),"Unable to acquire ITfClientId interface\n");
1611
1612     CoCreateGuid(&g2);
1613
1614     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
1615     ok(SUCCEEDED(hr),"GetClientId failed\n");
1616     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
1617     ok(SUCCEEDED(hr),"GetClientId failed\n");
1618     ok(id1==id2,"Id's for GUID_NULL do not match\n");
1619     hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
1620     ok(SUCCEEDED(hr),"GetClientId failed\n");
1621     ok(id2!=id1,"Id matches GUID_NULL\n");
1622     ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
1623     ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
1624     hr = ITfClientId_GetClientId(pcid,&g2,&id2);
1625     ok(SUCCEEDED(hr),"GetClientId failed\n");
1626     ok(id2!=id1,"Id matches GUID_NULL\n");
1627     ok(id2!=tid,"Id for random guid matching tid\n");
1628     ok(id2!=cid,"Id for random guid matching cid\n");
1629     ITfClientId_Release(pcid);
1630 }
1631
1632 /**********************************************************************
1633  * ITfEditSession
1634  **********************************************************************/
1635 typedef struct tagEditSession
1636 {
1637     ITfEditSession ITfEditSession_iface;
1638     LONG refCount;
1639 } EditSession;
1640
1641 static inline EditSession *impl_from_ITfEditSession(ITfEditSession *iface)
1642 {
1643     return CONTAINING_RECORD(iface, EditSession, ITfEditSession_iface);
1644 }
1645
1646 static void EditSession_Destructor(EditSession *This)
1647 {
1648     HeapFree(GetProcessHeap(),0,This);
1649 }
1650
1651 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1652 {
1653     EditSession *This = impl_from_ITfEditSession(iface);
1654     *ppvOut = NULL;
1655
1656     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1657     {
1658         *ppvOut = This;
1659     }
1660
1661     if (*ppvOut)
1662     {
1663         IUnknown_AddRef(iface);
1664         return S_OK;
1665     }
1666
1667     return E_NOINTERFACE;
1668 }
1669
1670 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1671 {
1672     EditSession *This = impl_from_ITfEditSession(iface);
1673     return InterlockedIncrement(&This->refCount);
1674 }
1675
1676 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1677 {
1678     EditSession *This = impl_from_ITfEditSession(iface);
1679     ULONG ret;
1680
1681     ret = InterlockedDecrement(&This->refCount);
1682     if (ret == 0)
1683         EditSession_Destructor(This);
1684     return ret;
1685 }
1686
1687 static void test_InsertAtSelection(TfEditCookie ec, ITfContext *cxt)
1688 {
1689     HRESULT hr;
1690     ITfInsertAtSelection *iis;
1691     ITfRange *range=NULL;
1692     static const WCHAR txt[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
1693
1694     hr = ITfContext_QueryInterface(cxt, &IID_ITfInsertAtSelection , (LPVOID*)&iis);
1695     ok(SUCCEEDED(hr),"Failed to get ITfInsertAtSelection interface\n");
1696     test_ACP_InsertTextAtSelection = SINK_EXPECTED;
1697     hr = ITfInsertAtSelection_InsertTextAtSelection(iis, ec, 0, txt, 11, &range);
1698     ok(SUCCEEDED(hr),"ITfInsertAtSelection_InsertTextAtSelection failed %x\n",hr);
1699     sink_check_ok(&test_ACP_InsertTextAtSelection,"InsertTextAtSelection");
1700     ok(range != NULL,"No range returned\n");
1701     ITfRange_Release(range);
1702     ITfInsertAtSelection_Release(iis);
1703 }
1704
1705 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1706 TfEditCookie ec)
1707 {
1708     ITfContext *cxt;
1709     ITfDocumentMgr *dm;
1710     ITfRange *range;
1711     TF_SELECTION selection;
1712     ULONG fetched;
1713     HRESULT hr;
1714
1715     sink_fire_ok(&test_DoEditSession,"EditSession_DoEditSession");
1716     sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1717
1718     ITfThreadMgr_GetFocus(g_tm, &dm);
1719     ITfDocumentMgr_GetTop(dm,&cxt);
1720
1721     hr = ITfContext_GetStart(cxt,ec,NULL);
1722     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1723
1724     range = (ITfRange*)0xdeaddead;
1725     hr = ITfContext_GetStart(cxt,0xdeadcafe,&range);
1726     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1727     ok(range == NULL,"Range not set to NULL\n");
1728
1729     hr = ITfContext_GetStart(cxt,ec,&range);
1730     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1731     ok(range != NULL,"Range set to NULL\n");
1732
1733     ITfRange_Release(range);
1734
1735     hr = ITfContext_GetEnd(cxt,ec,NULL);
1736     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1737
1738     range = (ITfRange*)0xdeaddead;
1739     hr = ITfContext_GetEnd(cxt,0xdeadcafe,&range);
1740     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1741     ok(range == NULL,"Range not set to NULL\n");
1742
1743     test_ACP_GetEndACP = SINK_EXPECTED;
1744     hr = ITfContext_GetEnd(cxt,ec,&range);
1745     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1746     ok(range != NULL,"Range set to NULL\n");
1747     sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1748
1749     ITfRange_Release(range);
1750
1751     selection.range = NULL;
1752     test_ACP_GetSelection = SINK_EXPECTED;
1753     hr = ITfContext_GetSelection(cxt, ec, TF_DEFAULT_SELECTION, 1, &selection, &fetched);
1754     ok(SUCCEEDED(hr),"ITfContext_GetSelection failed\n");
1755     ok(fetched == 1,"fetched incorrect\n");
1756     ok(selection.range != NULL,"NULL range\n");
1757     sink_check_ok(&test_ACP_GetSelection,"ACP_GetSepection");
1758     ITfRange_Release(selection.range);
1759
1760     test_InsertAtSelection(ec, cxt);
1761
1762     test_ACP_GetEndACP = SINK_EXPECTED;
1763     hr = ITfContext_GetEnd(cxt,ec,&range);
1764     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1765     ok(range != NULL,"Range set to NULL\n");
1766     sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1767
1768     selection.range = range;
1769     selection.style.ase = TF_AE_NONE;
1770     selection.style.fInterimChar = FALSE;
1771     test_ACP_SetSelection = SINK_EXPECTED;
1772     hr = ITfContext_SetSelection(cxt, ec, 1, &selection);
1773     sink_check_ok(&test_ACP_SetSelection,"SetSelection");
1774     ITfRange_Release(range);
1775
1776     ITfContext_Release(cxt);
1777     ITfDocumentMgr_Release(dm);
1778     return 0xdeadcafe;
1779 }
1780
1781 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1782 {
1783     EditSession_QueryInterface,
1784     EditSession_AddRef,
1785     EditSession_Release,
1786
1787     EditSession_DoEditSession
1788 };
1789
1790 static HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1791 {
1792     EditSession *This;
1793
1794     *ppOut = NULL;
1795     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1796     if (This == NULL)
1797         return E_OUTOFMEMORY;
1798
1799     This->ITfEditSession_iface.lpVtbl = &EditSession_EditSessionVtbl;
1800     This->refCount = 1;
1801
1802     *ppOut = &This->ITfEditSession_iface;
1803     return S_OK;
1804 }
1805
1806 static void test_TStoApplicationText(void)
1807 {
1808     HRESULT hr, hrSession;
1809     ITfEditSession *es;
1810     ITfContext *cxt;
1811     ITfDocumentMgr *dm;
1812     ITfTextEditSink *sink;
1813     ITfSource *source = NULL;
1814     DWORD editSinkCookie = -1;
1815
1816     ITfThreadMgr_GetFocus(g_tm, &dm);
1817     EditSession_Constructor(&es);
1818     ITfDocumentMgr_GetTop(dm,&cxt);
1819
1820     TextEditSink_Constructor(&sink);
1821     hr = ITfContext_QueryInterface(cxt,&IID_ITfSource,(LPVOID*)&source);
1822     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for Context\n");
1823     if (source)
1824     {
1825         hr = ITfSource_AdviseSink(source, &IID_ITfTextEditSink, (LPVOID)sink, &editSinkCookie);
1826         ok(SUCCEEDED(hr),"Failed to advise Sink\n");
1827         ok(editSinkCookie != -1,"Failed to get sink cookie\n");
1828     }
1829
1830     hrSession = 0xfeedface;
1831     /* Test no premissions flags */
1832     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC, &hrSession);
1833     ok(hr == E_INVALIDARG,"RequestEditSession should have failed with %x not %x\n",E_INVALIDARG,hr);
1834     ok(hrSession == E_FAIL,"hrSession should be %x not %x\n",E_FAIL,hrSession);
1835
1836     documentStatus = TS_SD_READONLY;
1837     hrSession = 0xfeedface;
1838     test_ACP_GetStatus = SINK_EXPECTED;
1839     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1840     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1841     ok(hrSession == TS_E_READONLY,"Unexpected hrSession (%x)\n",hrSession);
1842     sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1843
1844     /* signal a change to allow readwrite sessions */
1845     documentStatus = 0;
1846     test_ACP_RequestLock = SINK_EXPECTED;
1847     ITextStoreACPSink_OnStatusChange(ACPSink,documentStatus);
1848     sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1849
1850     test_ACP_GetStatus = SINK_EXPECTED;
1851     test_ACP_RequestLock = SINK_EXPECTED;
1852     test_DoEditSession = SINK_EXPECTED;
1853     hrSession = 0xfeedface;
1854     test_OnEndEdit = SINK_EXPECTED;
1855     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1856     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1857     sink_check_ok(&test_OnEndEdit,"OnEndEdit");
1858     sink_check_ok(&test_DoEditSession,"DoEditSession");
1859     sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1860     ok(hrSession == 0xdeadcafe,"Unexpected hrSession (%x)\n",hrSession);
1861
1862     if (source)
1863     {
1864         hr = ITfSource_UnadviseSink(source, editSinkCookie);
1865         ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1866         ITfTextEditSink_Release(sink);
1867         ITfSource_Release(source);
1868     }
1869
1870     ITfContext_Release(cxt);
1871     ITfDocumentMgr_Release(dm);
1872     ITfEditSession_Release(es);
1873 }
1874
1875 static void enum_compartments(ITfCompartmentMgr *cmpmgr, REFGUID present, REFGUID absent)
1876 {
1877     BOOL found,found2;
1878     IEnumGUID *ppEnum;
1879     found = FALSE;
1880     found2 = FALSE;
1881     if (SUCCEEDED(ITfCompartmentMgr_EnumCompartments(cmpmgr, &ppEnum)))
1882     {
1883         ULONG fetched;
1884         GUID g;
1885         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
1886         {
1887             WCHAR str[50];
1888             CHAR strA[50];
1889             StringFromGUID2(&g,str,sizeof(str)/sizeof(str[0]));
1890             WideCharToMultiByte(CP_ACP,0,str,-1,strA,sizeof(strA),0,0);
1891             trace("found %s\n",strA);
1892             if (present && IsEqualGUID(present,&g))
1893                 found = TRUE;
1894             if (absent && IsEqualGUID(absent, &g))
1895                 found2 = TRUE;
1896         }
1897         IEnumGUID_Release(ppEnum);
1898     }
1899     if (present)
1900         ok(found,"Did not find compartment\n");
1901     if (absent)
1902         ok(!found2,"Found compartment that should be absent\n");
1903 }
1904
1905 static void test_Compartments(void)
1906 {
1907     ITfContext *cxt;
1908     ITfDocumentMgr *dm;
1909     ITfCompartmentMgr *cmpmgr;
1910     ITfCompartment *cmp;
1911     HRESULT hr;
1912
1913     ITfThreadMgr_GetFocus(g_tm, &dm);
1914     ITfDocumentMgr_GetTop(dm,&cxt);
1915
1916     /* Global */
1917     hr = ITfThreadMgr_GetGlobalCompartment(g_tm, &cmpmgr);
1918     ok(SUCCEEDED(hr),"GetGlobalCompartment failed\n");
1919     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_SPEECH_OPENCLOSE, &cmp);
1920     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1921     ITfCompartment_Release(cmp);
1922     enum_compartments(cmpmgr,&GUID_COMPARTMENT_SPEECH_OPENCLOSE,NULL);
1923     ITfCompartmentMgr_Release(cmpmgr);
1924
1925     /* Thread */
1926     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1927     ok(SUCCEEDED(hr),"ThreadMgr QI for IID_ITfCompartmentMgr failed\n");
1928     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &CLSID_FakeService, &cmp);
1929     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1930     enum_compartments(cmpmgr,&CLSID_FakeService,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1931     ITfCompartmentMgr_ClearCompartment(cmpmgr,tid,&CLSID_FakeService);
1932     enum_compartments(cmpmgr,NULL,&CLSID_FakeService);
1933     ITfCompartmentMgr_Release(cmpmgr);
1934     ITfCompartment_Release(cmp);
1935
1936     /* DocumentMgr */
1937     hr = ITfDocumentMgr_QueryInterface(dm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1938     ok(SUCCEEDED(hr),"DocumentMgr QI for IID_ITfCompartmentMgr failed\n");
1939
1940     hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_PERSISTMENUENABLED, &cmp);
1941     ok(SUCCEEDED(hr),"GetCompartment failed\n");
1942     enum_compartments(cmpmgr,&GUID_COMPARTMENT_PERSISTMENUENABLED,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
1943     ITfCompartmentMgr_Release(cmpmgr);
1944
1945     /* Context */
1946     hr = ITfContext_QueryInterface(cxt, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
1947     ok(SUCCEEDED(hr),"Context QI for IID_ITfCompartmentMgr failed\n");
1948     enum_compartments(cmpmgr,NULL,&GUID_COMPARTMENT_PERSISTMENUENABLED);
1949     ITfCompartmentMgr_Release(cmpmgr);
1950
1951     ITfContext_Release(cxt);
1952     ITfDocumentMgr_Release(dm);
1953 }
1954
1955 static void processPendingMessages(void)
1956 {
1957     MSG msg;
1958     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
1959         TranslateMessage(&msg);
1960         DispatchMessage(&msg);
1961     }
1962 }
1963
1964 static void test_AssociateFocus(void)
1965 {
1966     ITfDocumentMgr *dm1, *dm2, *olddm, *dmcheck, *dmorig;
1967     HWND wnd1, wnd2, wnd3;
1968     HRESULT hr;
1969
1970     ITfThreadMgr_GetFocus(g_tm, &dmorig);
1971     test_CurrentFocus = NULL;
1972     test_PrevFocus = dmorig;
1973     test_OnSetFocus  = SINK_EXPECTED;
1974     hr = ITfThreadMgr_SetFocus(g_tm,NULL);
1975     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1976     ITfDocumentMgr_Release(dmorig);
1977
1978     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm1);
1979     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1980
1981     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm2);
1982     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1983
1984     wnd1 = CreateWindow("edit",NULL,WS_POPUP,0,0,200,60,NULL,NULL,NULL,NULL);
1985     ok(wnd1!=NULL,"Unable to create window 1\n");
1986     wnd2 = CreateWindow("edit",NULL,WS_POPUP,0,65,200,60,NULL,NULL,NULL,NULL);
1987     ok(wnd2!=NULL,"Unable to create window 2\n");
1988     wnd3 = CreateWindow("edit",NULL,WS_POPUP,0,130,200,60,NULL,NULL,NULL,NULL);
1989     ok(wnd3!=NULL,"Unable to create window 3\n");
1990
1991     processPendingMessages();
1992
1993     test_OnInitDocumentMgr = SINK_OPTIONAL; /* Vista and greater */
1994     test_OnPushContext = SINK_OPTIONAL; /* Vista and greater */
1995
1996     ShowWindow(wnd1,SW_SHOWNORMAL);
1997     SetFocus(wnd1);
1998     sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
1999     sink_check_ok(&test_OnPushContext,"OnPushContext");
2000
2001     test_OnSetFocus  = SINK_OPTIONAL; /* Vista and greater */
2002     ITfThreadMgr_GetFocus(g_tm, &test_PrevFocus);
2003     test_CurrentFocus = FOCUS_IGNORE; /* This is a default system context */
2004     processPendingMessages();
2005     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2006
2007     test_CurrentFocus = dm1;
2008     test_PrevFocus = FOCUS_IGNORE;
2009     test_OnSetFocus  = SINK_EXPECTED;
2010     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,dm1,&olddm);
2011     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2012     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2013
2014     processPendingMessages();
2015
2016     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2017     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
2018     ITfDocumentMgr_Release(dmcheck);
2019
2020     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,dm2,&olddm);
2021     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2022     processPendingMessages();
2023     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2024     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
2025     ITfDocumentMgr_Release(dmcheck);
2026
2027     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,dm2,&olddm);
2028     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2029     processPendingMessages();
2030     ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2031     ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
2032     ITfDocumentMgr_Release(dmcheck);
2033
2034     test_CurrentFocus = dm2;
2035     test_PrevFocus = dm1;
2036     test_OnSetFocus  = SINK_OPTIONAL; /* wine and Winxp */
2037     ShowWindow(wnd2,SW_SHOWNORMAL);
2038     SetFocus(wnd2);
2039     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2040     test_CurrentFocus = FOCUS_IGNORE; /* occasional wine race */
2041     test_PrevFocus = FOCUS_IGNORE; /* occasional wine race */
2042     test_OnSetFocus = SINK_IGNORE; /* occasional wine race */
2043     processPendingMessages();
2044
2045     ShowWindow(wnd3,SW_SHOWNORMAL);
2046     SetFocus(wnd3);
2047     processPendingMessages();
2048
2049     test_CurrentFocus = dm1;
2050     test_PrevFocus = dm2;
2051     test_OnSetFocus = SINK_EXPECTED;
2052     SetFocus(wnd1);
2053     processPendingMessages();
2054     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2055
2056     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,NULL,&olddm);
2057     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2058     ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2059     ITfDocumentMgr_Release(olddm);
2060
2061     test_CurrentFocus = dmorig;
2062     test_PrevFocus = dm1;
2063     test_OnSetFocus  = SINK_EXPECTED;
2064     test_ACP_GetStatus = SINK_EXPECTED;
2065     ITfThreadMgr_SetFocus(g_tm,dmorig);
2066     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2067
2068     test_CurrentFocus = FOCUS_IGNORE; /* NULL on XP, system default on Vista */
2069     test_PrevFocus = dmorig;
2070     test_OnSetFocus  = SINK_EXPECTED;
2071     SetFocus(wnd3);
2072     processPendingMessages();
2073     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2074
2075     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,NULL,&olddm);
2076     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2077     ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2078     ITfDocumentMgr_Release(olddm);
2079     hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,NULL,&olddm);
2080     ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2081     ok(olddm == dm1, "incorrect old DocumentMgr returned\n");
2082     ITfDocumentMgr_Release(olddm);
2083
2084     SetFocus(wnd2);
2085     processPendingMessages();
2086     SetFocus(wnd1);
2087     processPendingMessages();
2088
2089     ITfDocumentMgr_Release(dm1);
2090     ITfDocumentMgr_Release(dm2);
2091
2092     test_CurrentFocus = dmorig;
2093     test_PrevFocus = FOCUS_IGNORE;
2094     test_OnSetFocus  = SINK_EXPECTED;
2095     test_ACP_GetStatus = SINK_IGNORE;
2096     ITfThreadMgr_SetFocus(g_tm,dmorig);
2097     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2098
2099     DestroyWindow(wnd1);
2100     DestroyWindow(wnd2);
2101     test_OnPopContext = SINK_OPTIONAL; /* Vista and greater */
2102     test_OnSetFocus = SINK_OPTIONAL; /* Vista and greater */
2103     ITfThreadMgr_GetFocus(g_tm, &test_PrevFocus);
2104     test_CurrentFocus = NULL;
2105     DestroyWindow(wnd3);
2106     sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2107     sink_check_ok(&test_OnPopContext,"OnPopContext");
2108 }
2109
2110 START_TEST(inputprocessor)
2111 {
2112     if (SUCCEEDED(initialize()))
2113     {
2114         test_Register();
2115         test_RegisterCategory();
2116         Sleep(2000); /* Win7 needs some time before the registrations become active */
2117         processPendingMessages();
2118         test_EnumLanguageProfiles();
2119         test_EnumInputProcessorInfo();
2120         test_Enable();
2121         test_ThreadMgrAdviseSinks();
2122         test_Activate();
2123         test_startSession();
2124         test_TfGuidAtom();
2125         test_ClientId();
2126         test_KeystrokeMgr();
2127         test_TStoApplicationText();
2128         test_Compartments();
2129         test_AssociateFocus();
2130         test_endSession();
2131         test_FindClosestCategory();
2132         test_Disable();
2133         test_ThreadMgrUnadviseSinks();
2134         test_UnregisterCategory();
2135         test_Unregister();
2136     }
2137     else
2138         skip("Unable to create InputProcessor\n");
2139     cleanup();
2140 }