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