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