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