msctf: Beginning implementation of ITfContext::GetSelection.
[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 #define SINK_UNEXPECTED 0
42 #define SINK_EXPECTED 1
43 #define SINK_FIRED 2
44
45 static BOOL test_ShouldActivate = FALSE;
46 static BOOL test_ShouldDeactivate = FALSE;
47
48 static DWORD tmSinkCookie;
49 static DWORD tmSinkRefCount;
50 static ITfDocumentMgr *test_CurrentFocus = NULL;
51 static ITfDocumentMgr *test_PrevFocus = NULL;
52 static INT  test_OnSetFocus = SINK_UNEXPECTED;
53 static INT  test_OnInitDocumentMgr = SINK_UNEXPECTED;
54 static INT  test_OnPushContext = SINK_UNEXPECTED;
55 static INT  test_OnPopContext = SINK_UNEXPECTED;
56 static INT  test_KEV_OnSetFocus = SINK_UNEXPECTED;
57 static INT  test_ACP_AdviseSink = SINK_UNEXPECTED;
58 static INT  test_ACP_GetStatus = SINK_UNEXPECTED;
59 static INT  test_ACP_RequestLock = SINK_UNEXPECTED;
60 static INT  test_ACP_GetEndACP = SINK_UNEXPECTED;
61 static INT  test_ACP_GetSelection = SINK_UNEXPECTED;
62 static INT  test_DoEditSession = SINK_UNEXPECTED;
63
64
65 /**********************************************************************
66  * ITextStoreACP
67  **********************************************************************/
68 typedef struct tagTextStoreACP
69 {
70     const ITextStoreACPVtbl *TextStoreACPVtbl;
71     LONG refCount;
72
73     ITextStoreACPSink *sink;
74 } TextStoreACP;
75
76 static void TextStoreACP_Destructor(TextStoreACP *This)
77 {
78     HeapFree(GetProcessHeap(),0,This);
79 }
80
81 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
82 {
83     TextStoreACP *This = (TextStoreACP *)iface;
84     *ppvOut = NULL;
85
86     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
87     {
88         *ppvOut = This;
89     }
90
91     if (*ppvOut)
92     {
93         IUnknown_AddRef(iface);
94         return S_OK;
95     }
96
97     return E_NOINTERFACE;
98 }
99
100 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
101 {
102     TextStoreACP *This = (TextStoreACP *)iface;
103     return InterlockedIncrement(&This->refCount);
104 }
105
106 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
107 {
108     TextStoreACP *This = (TextStoreACP *)iface;
109     ULONG ret;
110
111     ret = InterlockedDecrement(&This->refCount);
112     if (ret == 0)
113         TextStoreACP_Destructor(This);
114     return ret;
115 }
116
117 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
118     REFIID riid, IUnknown *punk, DWORD dwMask)
119 {
120     TextStoreACP *This = (TextStoreACP *)iface;
121     HRESULT hr;
122
123     ok(test_ACP_AdviseSink == SINK_EXPECTED, "Unexpected TextStoreACP_AdviseSink sink\n");
124     test_ACP_AdviseSink = SINK_FIRED;
125
126     hr = IUnknown_QueryInterface(punk, &IID_ITextStoreACPSink,(LPVOID*)(&This->sink));
127     ok(SUCCEEDED(hr),"Unable to QueryInterface on sink\n");
128     return S_OK;
129 }
130
131 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
132     IUnknown *punk)
133 {
134     trace("\n");
135     return S_OK;
136 }
137
138 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
139     DWORD dwLockFlags, HRESULT *phrSession)
140 {
141     TextStoreACP *This = (TextStoreACP *)iface;
142
143     ok(test_ACP_RequestLock == SINK_EXPECTED,"Unexpected TextStoreACP_RequestLock\n");
144     test_ACP_RequestLock = SINK_FIRED;
145     test_DoEditSession = SINK_EXPECTED;
146     *phrSession = ITextStoreACPSink_OnLockGranted(This->sink, TS_LF_READWRITE);
147     ok(test_DoEditSession = SINK_FIRED,"expected DoEditSession not fired\n");
148     ok(*phrSession == 0xdeadcafe,"Unexpected return from ITextStoreACPSink_OnLockGranted\n");
149     return S_OK;
150 }
151 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
152     TS_STATUS *pdcs)
153 {
154     static UINT count = 0;
155     count ++;
156
157     if (count == 1)
158         ok(test_ACP_GetStatus  == SINK_EXPECTED, "Unexpected TextStoreACP_GetStatus\n");
159     else
160         todo_wine ok(count == 1,"GetStatus called too many times\n");
161     test_ACP_GetStatus = SINK_FIRED;
162     pdcs->dwDynamicFlags = TS_SD_READONLY;
163     return S_OK;
164 }
165 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
166     LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
167     LONG *pacpResultEnd)
168 {
169     trace("\n");
170     return S_OK;
171 }
172 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
173     ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
174 {
175     ok(test_ACP_GetSelection == SINK_EXPECTED, "Unexpected TextStoreACP_GetSelection\n");
176     test_ACP_GetSelection = SINK_FIRED;
177
178     pSelection->acpStart = 10;
179     pSelection->acpEnd = 20;
180     pSelection->style.fInterimChar = 0;
181     pSelection->style.ase = TS_AE_NONE;
182     *pcFetched = 1;
183
184     return S_OK;
185 }
186 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
187     ULONG ulCount, const TS_SELECTION_ACP *pSelection)
188 {
189     trace("\n");
190     return S_OK;
191 }
192 static HRESULT WINAPI TextStoreACP_GetText(ITextStoreACP *iface,
193     LONG acpStart, LONG acpEnd, WCHAR *pchPlain, ULONG cchPlainReq,
194     ULONG *pcchPlainRet, TS_RUNINFO *prgRunInfo, ULONG cRunInfoReq,
195     ULONG *pcRunInfoRet, LONG *pacpNext)
196 {
197     trace("\n");
198     return S_OK;
199 }
200 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
201     DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
202     ULONG cch, TS_TEXTCHANGE *pChange)
203 {
204     trace("\n");
205     return S_OK;
206 }
207 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
208     LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
209 {
210     trace("\n");
211     return S_OK;
212 }
213 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
214     LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
215 {
216     trace("\n");
217     return S_OK;
218 }
219 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
220     const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
221 {
222     trace("\n");
223     return S_OK;
224 }
225 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
226     DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
227     TS_TEXTCHANGE *pChange)
228 {
229     trace("\n");
230     return S_OK;
231 }
232 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
233     DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
234     LONG *pacpEnd, TS_TEXTCHANGE *pChange)
235 {
236     trace("\n");
237     return S_OK;
238 }
239 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
240     DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
241     TS_TEXTCHANGE *pChange)
242 {
243     trace("\n");
244     return S_OK;
245 }
246 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
247     DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
248 {
249     trace("\n");
250     return S_OK;
251 }
252 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
253     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
254     DWORD dwFlags)
255 {
256     trace("\n");
257     return S_OK;
258 }
259 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
260     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
261     DWORD dwFlags)
262 {
263     trace("\n");
264     return S_OK;
265 }
266 static HRESULT WINAPI TextStoreACP_FindNextAttrTransition(ITextStoreACP *iface,
267     LONG acpStart, LONG acpHalt, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
268     DWORD dwFlags, LONG *pacpNext, BOOL *pfFound, LONG *plFoundOffset)
269 {
270     trace("\n");
271     return S_OK;
272 }
273 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
274     ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
275 {
276     trace("\n");
277     return S_OK;
278 }
279 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
280     LONG *pacp)
281 {
282     ok(test_ACP_GetEndACP == SINK_EXPECTED,"Unexpected TextStoreACP_GetEndACP\n");
283     test_ACP_GetEndACP = SINK_FIRED;
284     return S_OK;
285 }
286 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
287     TsViewCookie *pvcView)
288 {
289     trace("\n");
290     return S_OK;
291 }
292 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
293     TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
294     LONG *pacp)
295 {
296     trace("\n");
297     return S_OK;
298 }
299 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
300     TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
301     BOOL *pfClipped)
302 {
303     trace("\n");
304     return S_OK;
305 }
306 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
307     TsViewCookie vcView, RECT *prc)
308 {
309     trace("\n");
310     return S_OK;
311 }
312 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
313     TsViewCookie vcView, HWND *phwnd)
314 {
315     trace("\n");
316     return S_OK;
317 }
318
319 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
320 {
321     TextStoreACP_QueryInterface,
322     TextStoreACP_AddRef,
323     TextStoreACP_Release,
324
325     TextStoreACP_AdviseSink,
326     TextStoreACP_UnadviseSink,
327     TextStoreACP_RequestLock,
328     TextStoreACP_GetStatus,
329     TextStoreACP_QueryInsert,
330     TextStoreACP_GetSelection,
331     TextStoreACP_SetSelection,
332     TextStoreACP_GetText,
333     TextStoreACP_SetText,
334     TextStoreACP_GetFormattedText,
335     TextStoreACP_GetEmbedded,
336     TextStoreACP_QueryInsertEmbedded,
337     TextStoreACP_InsertEmbedded,
338     TextStoreACP_InsertTextAtSelection,
339     TextStoreACP_InsertEmbeddedAtSelection,
340     TextStoreACP_RequestSupportedAttrs,
341     TextStoreACP_RequestAttrsAtPosition,
342     TextStoreACP_RequestAttrsTransitioningAtPosition,
343     TextStoreACP_FindNextAttrTransition,
344     TextStoreACP_RetrieveRequestedAttrs,
345     TextStoreACP_GetEndACP,
346     TextStoreACP_GetActiveView,
347     TextStoreACP_GetACPFromPoint,
348     TextStoreACP_GetTextExt,
349     TextStoreACP_GetScreenExt,
350     TextStoreACP_GetWnd
351 };
352
353 static HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
354 {
355     TextStoreACP *This;
356
357     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
358     if (This == NULL)
359         return E_OUTOFMEMORY;
360
361     This->TextStoreACPVtbl = &TextStoreACP_TextStoreACPVtbl;
362     This->refCount = 1;
363
364     *ppOut = (IUnknown *)This;
365     return S_OK;
366 }
367
368 /**********************************************************************
369  * ITfThreadMgrEventSink
370  **********************************************************************/
371 typedef struct tagThreadMgrEventSink
372 {
373     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl;
374     LONG refCount;
375 } ThreadMgrEventSink;
376
377 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
378 {
379     HeapFree(GetProcessHeap(),0,This);
380 }
381
382 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
383 {
384     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
385     *ppvOut = NULL;
386
387     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
388     {
389         *ppvOut = This;
390     }
391
392     if (*ppvOut)
393     {
394         IUnknown_AddRef(iface);
395         return S_OK;
396     }
397
398     return E_NOINTERFACE;
399 }
400
401 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
402 {
403     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
404     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
405     return InterlockedIncrement(&This->refCount);
406 }
407
408 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
409 {
410     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
411     ULONG ret;
412
413     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
414     ret = InterlockedDecrement(&This->refCount);
415     if (ret == 0)
416         ThreadMgrEventSink_Destructor(This);
417     return ret;
418 }
419
420 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
421 ITfDocumentMgr *pdim)
422 {
423     ok(test_OnInitDocumentMgr == SINK_EXPECTED, "Unexpected OnInitDocumentMgr sink\n");
424     test_OnInitDocumentMgr = SINK_FIRED;
425     return S_OK;
426 }
427
428 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
429 ITfDocumentMgr *pdim)
430 {
431     trace("\n");
432     return S_OK;
433 }
434
435 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
436 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
437 {
438     ok(test_OnSetFocus == SINK_EXPECTED, "Unexpected OnSetFocus sink\n");
439     ok(pdimFocus == test_CurrentFocus,"Sink reports wrong focus\n");
440     ok(pdimPrevFocus == test_PrevFocus,"Sink reports wrong previous focus\n");
441     test_OnSetFocus = SINK_FIRED;
442     return S_OK;
443 }
444
445 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(ITfThreadMgrEventSink *iface,
446 ITfContext *pic)
447 {
448     ok(test_OnPushContext == SINK_EXPECTED, "Unexpected OnPushContext sink\n");
449     test_OnPushContext = SINK_FIRED;
450     return S_OK;
451 }
452
453 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
454 ITfContext *pic)
455 {
456     ok(test_OnPopContext == SINK_EXPECTED, "Unexpected OnPopContext sink\n");
457     test_OnPopContext = SINK_FIRED;
458     return S_OK;
459 }
460
461 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
462 {
463     ThreadMgrEventSink_QueryInterface,
464     ThreadMgrEventSink_AddRef,
465     ThreadMgrEventSink_Release,
466
467     ThreadMgrEventSink_OnInitDocumentMgr,
468     ThreadMgrEventSink_OnUninitDocumentMgr,
469     ThreadMgrEventSink_OnSetFocus,
470     ThreadMgrEventSink_OnPushContext,
471     ThreadMgrEventSink_OnPopContext
472 };
473
474 static HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
475 {
476     ThreadMgrEventSink *This;
477
478     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
479     if (This == NULL)
480         return E_OUTOFMEMORY;
481
482     This->ThreadMgrEventSinkVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
483     This->refCount = 1;
484
485     *ppOut = (IUnknown *)This;
486     return S_OK;
487 }
488
489
490 /********************************************************************************************
491  * Stub text service for testing
492  ********************************************************************************************/
493
494 static LONG TS_refCount;
495 static IClassFactory *cf;
496 static DWORD regid;
497
498 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
499
500 typedef struct tagClassFactory
501 {
502     const IClassFactoryVtbl *vtbl;
503     LONG   ref;
504     LPFNCONSTRUCTOR ctor;
505 } ClassFactory;
506
507 typedef struct tagTextService
508 {
509     const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
510     LONG refCount;
511 } TextService;
512
513 static void ClassFactory_Destructor(ClassFactory *This)
514 {
515     HeapFree(GetProcessHeap(),0,This);
516     TS_refCount--;
517 }
518
519 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
520 {
521     *ppvOut = NULL;
522     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
523     {
524         IClassFactory_AddRef(iface);
525         *ppvOut = iface;
526         return S_OK;
527     }
528
529     return E_NOINTERFACE;
530 }
531
532 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
533 {
534     ClassFactory *This = (ClassFactory *)iface;
535     return InterlockedIncrement(&This->ref);
536 }
537
538 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
539 {
540     ClassFactory *This = (ClassFactory *)iface;
541     ULONG ret = InterlockedDecrement(&This->ref);
542
543     if (ret == 0)
544         ClassFactory_Destructor(This);
545     return ret;
546 }
547
548 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
549 {
550     ClassFactory *This = (ClassFactory *)iface;
551     HRESULT ret;
552     IUnknown *obj;
553
554     ret = This->ctor(punkOuter, &obj);
555     if (FAILED(ret))
556         return ret;
557     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
558     IUnknown_Release(obj);
559     return ret;
560 }
561
562 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
563 {
564     if(fLock)
565         InterlockedIncrement(&TS_refCount);
566     else
567         InterlockedDecrement(&TS_refCount);
568
569     return S_OK;
570 }
571
572 static const IClassFactoryVtbl ClassFactoryVtbl = {
573     /* IUnknown */
574     ClassFactory_QueryInterface,
575     ClassFactory_AddRef,
576     ClassFactory_Release,
577
578     /* IClassFactory*/
579     ClassFactory_CreateInstance,
580     ClassFactory_LockServer
581 };
582
583 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
584 {
585     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
586     This->vtbl = &ClassFactoryVtbl;
587     This->ref = 1;
588     This->ctor = ctor;
589     *ppvOut = (LPVOID)This;
590     TS_refCount++;
591     return S_OK;
592 }
593
594 static void TextService_Destructor(TextService *This)
595 {
596     HeapFree(GetProcessHeap(),0,This);
597 }
598
599 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
600 {
601     TextService *This = (TextService *)iface;
602     *ppvOut = NULL;
603
604     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
605     {
606         *ppvOut = This;
607     }
608
609     if (*ppvOut)
610     {
611         IUnknown_AddRef(iface);
612         return S_OK;
613     }
614
615     return E_NOINTERFACE;
616 }
617
618 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
619 {
620     TextService *This = (TextService *)iface;
621     return InterlockedIncrement(&This->refCount);
622 }
623
624 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
625 {
626     TextService *This = (TextService *)iface;
627     ULONG ret;
628
629     ret = InterlockedDecrement(&This->refCount);
630     if (ret == 0)
631         TextService_Destructor(This);
632     return ret;
633 }
634
635 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
636         ITfThreadMgr *ptim, TfClientId id)
637 {
638     trace("TextService_Activate\n");
639     ok(test_ShouldActivate,"Activation came unexpectedly\n");
640     tid = id;
641     return S_OK;
642 }
643
644 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
645 {
646     trace("TextService_Deactivate\n");
647     ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
648     return S_OK;
649 }
650
651 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
652 {
653     TextService_QueryInterface,
654     TextService_AddRef,
655     TextService_Release,
656
657     TextService_Activate,
658     TextService_Deactivate
659 };
660
661 static HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
662 {
663     TextService *This;
664     if (pUnkOuter)
665         return CLASS_E_NOAGGREGATION;
666
667     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
668     if (This == NULL)
669         return E_OUTOFMEMORY;
670
671     This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
672     This->refCount = 1;
673
674     *ppOut = (IUnknown *)This;
675     return S_OK;
676 }
677
678 static HRESULT RegisterTextService(REFCLSID rclsid)
679 {
680     ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
681     return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
682 }
683
684 static HRESULT UnregisterTextService()
685 {
686     return CoRevokeClassObject(regid);
687 }
688
689 /*
690  * The tests
691  */
692
693 DEFINE_GUID(CLSID_FakeService, 0xEDE1A7AD,0x66DE,0x47E0,0xB6,0x20,0x3E,0x92,0xF8,0x24,0x6B,0xF3);
694 DEFINE_GUID(CLSID_TF_InputProcessorProfiles, 0x33c53a50,0xf456,0x4884,0xb0,0x49,0x85,0xfd,0x64,0x3e,0xcf,0xed);
695 DEFINE_GUID(CLSID_TF_CategoryMgr,         0xA4B544A1,0x438D,0x4B41,0x93,0x25,0x86,0x95,0x23,0xE2,0xD6,0xC7);
696 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD,     0x34745c63,0xb2f0,0x4784,0x8b,0x67,0x5e,0x12,0xc8,0x70,0x1a,0x31);
697 DEFINE_GUID(GUID_TFCAT_TIP_SPEECH,       0xB5A73CD1,0x8355,0x426B,0xA1,0x61,0x25,0x98,0x08,0xF2,0x6B,0x14);
698 DEFINE_GUID(GUID_TFCAT_TIP_HANDWRITING,  0x246ecb87,0xc2f2,0x4abe,0x90,0x5b,0xc8,0xb3,0x8a,0xdd,0x2c,0x43);
699 DEFINE_GUID (GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER,  0x046B8C80,0x1647,0x40F7,0x9B,0x21,0xB9,0x3B,0x81,0xAA,0xBC,0x1B);
700 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
701 DEFINE_GUID(CLSID_TF_ThreadMgr,           0x529a9e6b,0x6587,0x4f23,0xab,0x9e,0x9c,0x7d,0x68,0x3e,0x3c,0x50);
702 DEFINE_GUID(CLSID_PreservedKey,           0xA0ED8E55,0xCD3B,0x4274,0xB2,0x95,0xF6,0xC9,0xBA,0x2B,0x84,0x72);
703
704
705 static HRESULT initialize(void)
706 {
707     HRESULT hr;
708     CoInitialize(NULL);
709     hr = CoCreateInstance (&CLSID_TF_InputProcessorProfiles, NULL,
710           CLSCTX_INPROC_SERVER, &IID_ITfInputProcessorProfiles, (void**)&g_ipp);
711     if (SUCCEEDED(hr))
712         hr = CoCreateInstance (&CLSID_TF_CategoryMgr, NULL,
713           CLSCTX_INPROC_SERVER, &IID_ITfCategoryMgr, (void**)&g_cm);
714     if (SUCCEEDED(hr))
715         hr = CoCreateInstance (&CLSID_TF_ThreadMgr, NULL,
716           CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (void**)&g_tm);
717     return hr;
718 }
719
720 static void cleanup(void)
721 {
722     if (g_ipp)
723         ITfInputProcessorProfiles_Release(g_ipp);
724     if (g_cm)
725         ITfCategoryMgr_Release(g_cm);
726     if (g_tm)
727         ITfThreadMgr_Release(g_tm);
728     CoUninitialize();
729 }
730
731 static void test_Register(void)
732 {
733     HRESULT hr;
734
735     static const WCHAR szDesc[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',0};
736     static const WCHAR szFile[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',' ','F','i','l','e',0};
737
738     hr = ITfInputProcessorProfiles_GetCurrentLanguage(g_ipp,&gLangid);
739     ok(SUCCEEDED(hr),"Unable to get current language id\n");
740     trace("Current Language %x\n",gLangid);
741
742     hr = RegisterTextService(&CLSID_FakeService);
743     ok(SUCCEEDED(hr),"Unable to register COM for TextService\n");
744     hr = ITfInputProcessorProfiles_Register(g_ipp, &CLSID_FakeService);
745     ok(SUCCEEDED(hr),"Unable to register text service(%x)\n",hr);
746     hr = ITfInputProcessorProfiles_AddLanguageProfile(g_ipp, &CLSID_FakeService, gLangid, &CLSID_FakeService, szDesc, sizeof(szDesc)/sizeof(WCHAR), szFile, sizeof(szFile)/sizeof(WCHAR), 1);
747     ok(SUCCEEDED(hr),"Unable to add Language Profile (%x)\n",hr);
748 }
749
750 static void test_Unregister(void)
751 {
752     HRESULT hr;
753     hr = ITfInputProcessorProfiles_Unregister(g_ipp, &CLSID_FakeService);
754     ok(SUCCEEDED(hr),"Unable to unregister text service(%x)\n",hr);
755     UnregisterTextService();
756 }
757
758 static void test_EnumInputProcessorInfo(void)
759 {
760     IEnumGUID *ppEnum;
761     BOOL found = FALSE;
762
763     if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
764     {
765         ULONG fetched;
766         GUID g;
767         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
768         {
769             if(IsEqualGUID(&g,&CLSID_FakeService))
770                 found = TRUE;
771         }
772     }
773     ok(found,"Did not find registered text service\n");
774 }
775
776 static void test_EnumLanguageProfiles(void)
777 {
778     BOOL found = FALSE;
779     IEnumTfLanguageProfiles *ppEnum;
780     if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
781     {
782         TF_LANGUAGEPROFILE profile;
783         while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
784         {
785             if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
786             {
787                 found = TRUE;
788                 ok(profile.langid == gLangid, "LangId Incorrect\n");
789                 ok(IsEqualGUID(&profile.catid,&GUID_TFCAT_TIP_KEYBOARD), "CatId Incorrect\n");
790                 ok(IsEqualGUID(&profile.guidProfile,&CLSID_FakeService), "guidProfile Incorrect\n");
791             }
792         }
793     }
794     ok(found,"Registered text service not found\n");
795 }
796
797 static void test_RegisterCategory(void)
798 {
799     HRESULT hr;
800     hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
801     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
802     hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
803     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
804 }
805
806 static void test_UnregisterCategory(void)
807 {
808     HRESULT hr;
809     hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
810     ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
811     hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
812     ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
813 }
814
815 static void test_FindClosestCategory(void)
816 {
817     GUID output;
818     HRESULT hr;
819     const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
820
821     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, NULL, 0);
822     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
823     ok(IsEqualGUID(&output,&GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER),"Wrong GUID\n");
824
825     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 1);
826     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
827     ok(IsEqualGUID(&output,&GUID_NULL),"Wrong GUID\n");
828
829     hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 3);
830     ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
831     ok(IsEqualGUID(&output,&GUID_TFCAT_TIP_KEYBOARD),"Wrong GUID\n");
832 }
833
834 static void test_Enable(void)
835 {
836     HRESULT hr;
837     BOOL enabled = FALSE;
838
839     hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, TRUE);
840     ok(SUCCEEDED(hr),"Failed to enable text service\n");
841     hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, &enabled);
842     ok(SUCCEEDED(hr),"Failed to get enabled state\n");
843     ok(enabled == TRUE,"enabled state incorrect\n");
844 }
845
846 static void test_Disable(void)
847 {
848     HRESULT hr;
849
850     trace("Disabling\n");
851     hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, FALSE);
852     ok(SUCCEEDED(hr),"Failed to disable text service\n");
853 }
854
855 static void test_ThreadMgrAdviseSinks(void)
856 {
857     ITfSource *source = NULL;
858     HRESULT hr;
859     IUnknown *sink;
860
861     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
862     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
863     if (!source)
864         return;
865
866     hr = ThreadMgrEventSink_Constructor(&sink);
867     ok(hr == S_OK, "got %08x\n", hr);
868     if(FAILED(hr)) return;
869
870     tmSinkRefCount = 1;
871     tmSinkCookie = 0;
872     hr = ITfSource_AdviseSink(source,&IID_ITfThreadMgrEventSink, sink, &tmSinkCookie);
873     ok(SUCCEEDED(hr),"Failed to Advise Sink\n");
874     ok(tmSinkCookie!=0,"Failed to get sink cookie\n");
875
876     /* Advising the sink adds a ref, Relesing here lets the object be deleted
877        when unadvised */
878     tmSinkRefCount = 2;
879     IUnknown_Release(sink);
880     ITfSource_Release(source);
881 }
882
883 static void test_ThreadMgrUnadviseSinks(void)
884 {
885     ITfSource *source = NULL;
886     HRESULT hr;
887
888     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
889     ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
890     if (!source)
891         return;
892
893     tmSinkRefCount = 1;
894     hr = ITfSource_UnadviseSink(source, tmSinkCookie);
895     ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
896     ITfSource_Release(source);
897 }
898
899 /**********************************************************************
900  * ITfKeyEventSink
901  **********************************************************************/
902 typedef struct tagKeyEventSink
903 {
904     const ITfKeyEventSinkVtbl *KeyEventSinkVtbl;
905     LONG refCount;
906 } KeyEventSink;
907
908 static void KeyEventSink_Destructor(KeyEventSink *This)
909 {
910     HeapFree(GetProcessHeap(),0,This);
911 }
912
913 static HRESULT WINAPI KeyEventSink_QueryInterface(ITfKeyEventSink *iface, REFIID iid, LPVOID *ppvOut)
914 {
915     KeyEventSink *This = (KeyEventSink *)iface;
916     *ppvOut = NULL;
917
918     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfKeyEventSink))
919     {
920         *ppvOut = This;
921     }
922
923     if (*ppvOut)
924     {
925         IUnknown_AddRef(iface);
926         return S_OK;
927     }
928
929     return E_NOINTERFACE;
930 }
931
932 static ULONG WINAPI KeyEventSink_AddRef(ITfKeyEventSink *iface)
933 {
934     KeyEventSink *This = (KeyEventSink *)iface;
935     return InterlockedIncrement(&This->refCount);
936 }
937
938 static ULONG WINAPI KeyEventSink_Release(ITfKeyEventSink *iface)
939 {
940     KeyEventSink *This = (KeyEventSink *)iface;
941     ULONG ret;
942
943     ret = InterlockedDecrement(&This->refCount);
944     if (ret == 0)
945         KeyEventSink_Destructor(This);
946     return ret;
947 }
948
949 static HRESULT WINAPI KeyEventSink_OnSetFocus(ITfKeyEventSink *iface,
950         BOOL fForeground)
951 {
952     ok(test_KEV_OnSetFocus == SINK_EXPECTED,"Unexpected KeyEventSink_OnSetFocus\n");
953     test_KEV_OnSetFocus = SINK_FIRED;
954     return S_OK;
955 }
956
957 static HRESULT WINAPI KeyEventSink_OnTestKeyDown(ITfKeyEventSink *iface,
958         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
959 {
960     trace("\n");
961     return S_OK;
962 }
963
964 static HRESULT WINAPI KeyEventSink_OnTestKeyUp(ITfKeyEventSink *iface,
965         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
966 {
967     trace("\n");
968     return S_OK;
969 }
970
971 static HRESULT WINAPI KeyEventSink_OnKeyDown(ITfKeyEventSink *iface,
972         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
973 {
974     trace("\n");
975     return S_OK;
976 }
977
978 static HRESULT WINAPI KeyEventSink_OnKeyUp(ITfKeyEventSink *iface,
979         ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
980 {
981     trace("\n");
982     return S_OK;
983 }
984
985 static HRESULT WINAPI KeyEventSink_OnPreservedKey(ITfKeyEventSink *iface,
986     ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
987 {
988     trace("\n");
989     return S_OK;
990 }
991
992 static const ITfKeyEventSinkVtbl KeyEventSink_KeyEventSinkVtbl =
993 {
994     KeyEventSink_QueryInterface,
995     KeyEventSink_AddRef,
996     KeyEventSink_Release,
997
998     KeyEventSink_OnSetFocus,
999     KeyEventSink_OnTestKeyDown,
1000     KeyEventSink_OnTestKeyUp,
1001     KeyEventSink_OnKeyDown,
1002     KeyEventSink_OnKeyUp,
1003     KeyEventSink_OnPreservedKey
1004 };
1005
1006 static HRESULT KeyEventSink_Constructor(ITfKeyEventSink **ppOut)
1007 {
1008     KeyEventSink *This;
1009
1010     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(KeyEventSink));
1011     if (This == NULL)
1012         return E_OUTOFMEMORY;
1013
1014     This->KeyEventSinkVtbl = &KeyEventSink_KeyEventSinkVtbl;
1015     This->refCount = 1;
1016
1017     *ppOut = (ITfKeyEventSink*)This;
1018     return S_OK;
1019 }
1020
1021
1022 static void test_KeystrokeMgr(void)
1023 {
1024     ITfKeystrokeMgr *keymgr= NULL;
1025     HRESULT hr;
1026     TF_PRESERVEDKEY tfpk;
1027     BOOL preserved;
1028     ITfKeyEventSink *sink = NULL;
1029
1030     KeyEventSink_Constructor(&sink);
1031
1032     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfKeystrokeMgr, (LPVOID*)&keymgr);
1033     ok(SUCCEEDED(hr),"Failed to get IID_ITfKeystrokeMgr for ThreadMgr\n");
1034
1035     tfpk.uVKey = 'A';
1036     tfpk.uModifiers = TF_MOD_SHIFT;
1037
1038     test_KEV_OnSetFocus = SINK_EXPECTED;
1039     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1040     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
1041     ok(test_KEV_OnSetFocus == SINK_FIRED, "KeyEventSink_OnSetFocus not fired as expected\n");
1042     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1043     ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
1044     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
1045     ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
1046
1047     hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1048     ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1049
1050     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1051     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1052
1053     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1054     ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
1055
1056     preserved = FALSE;
1057     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1058     ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1059     if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
1060
1061     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1062     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1063
1064     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1065     ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1066     if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
1067
1068     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1069     ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
1070
1071     hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1072     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1073
1074     ITfKeystrokeMgr_Release(keymgr);
1075     ITfKeyEventSink_Release(sink);
1076 }
1077
1078 static void test_Activate(void)
1079 {
1080     HRESULT hr;
1081
1082     hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1083     ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1084 }
1085
1086 static inline int check_context_refcount(ITfContext *iface)
1087 {
1088     IUnknown_AddRef(iface);
1089     return IUnknown_Release(iface);
1090 }
1091
1092 static void test_startSession(void)
1093 {
1094     HRESULT hr;
1095     DWORD cnt;
1096     DWORD editCookie;
1097     ITfDocumentMgr *dmtest;
1098     ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
1099     ITextStoreACP *ts;
1100     TfClientId cid2 = 0;
1101
1102     hr = ITfThreadMgr_Deactivate(g_tm);
1103     ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1104
1105     test_ShouldActivate = TRUE;
1106     hr  = ITfThreadMgr_Activate(g_tm,&cid);
1107     ok(SUCCEEDED(hr),"Failed to Activate\n");
1108     ok(cid != tid,"TextService id mistakenly matches Client id\n");
1109
1110     test_ShouldActivate = FALSE;
1111     hr = ITfThreadMgr_Activate(g_tm,&cid2);
1112     ok(SUCCEEDED(hr),"Failed to Activate\n");
1113     ok (cid == cid2, "Second activate client ID does not match\n");
1114
1115     hr = ITfThreadMgr_Deactivate(g_tm);
1116     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1117
1118     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1119     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1120
1121     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1122     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1123     ok(dmtest == NULL,"Initial focus not null\n");
1124
1125     test_CurrentFocus = g_dm;
1126     test_PrevFocus = NULL;
1127     test_OnSetFocus  = SINK_EXPECTED;
1128     hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
1129     ok(SUCCEEDED(hr),"SetFocus Failed\n");
1130     ok(test_OnSetFocus == SINK_FIRED, "OnSetFocus sink not called\n");
1131     test_OnSetFocus  = SINK_UNEXPECTED;
1132
1133     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1134     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1135     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1136
1137     cnt = ITfDocumentMgr_Release(g_dm);
1138     ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
1139
1140     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1141     ok(SUCCEEDED(hr),"GetFocus Failed\n");
1142     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1143
1144     TextStoreACP_Constructor((IUnknown**)&ts);
1145
1146     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1147     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1148
1149     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1150     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1151
1152     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1153     ok(SUCCEEDED(hr),"CreateContext Failed\n");
1154
1155     cnt = check_context_refcount(cxt);
1156     test_OnPushContext = SINK_EXPECTED;
1157     test_ACP_AdviseSink = SINK_EXPECTED;
1158     test_OnInitDocumentMgr = SINK_EXPECTED;
1159     hr = ITfDocumentMgr_Push(g_dm, cxt);
1160     ok(SUCCEEDED(hr),"Push Failed\n");
1161     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1162     ok(test_OnPushContext == SINK_FIRED, "OnPushContext sink not fired\n");
1163     ok(test_OnInitDocumentMgr == SINK_FIRED, "OnInitDocumentMgr sink not fired\n");
1164     ok(test_ACP_AdviseSink == SINK_FIRED,"TextStoreACP_AdviseSink not fired\n");
1165
1166     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1167     ok(SUCCEEDED(hr),"GetTop Failed\n");
1168     ok(cxtTest == cxt, "Wrong context on top\n");
1169     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1170     cnt = ITfContext_Release(cxtTest);
1171
1172     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1173     ok(SUCCEEDED(hr),"GetBase Failed\n");
1174     ok(cxtTest == cxt, "Wrong context on Base\n");
1175     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1176     ITfContext_Release(cxtTest);
1177
1178     check_context_refcount(cxt2);
1179     test_OnPushContext = SINK_EXPECTED;
1180     hr = ITfDocumentMgr_Push(g_dm, cxt2);
1181     ok(SUCCEEDED(hr),"Push Failed\n");
1182     ok(test_OnPushContext == SINK_FIRED, "OnPushContext sink not fired\n");
1183
1184     cnt = check_context_refcount(cxt2);
1185     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1186     ok(SUCCEEDED(hr),"GetTop Failed\n");
1187     ok(cxtTest == cxt2, "Wrong context on top\n");
1188     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1189     ITfContext_Release(cxtTest);
1190
1191     cnt = check_context_refcount(cxt);
1192     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1193     ok(SUCCEEDED(hr),"GetBase Failed\n");
1194     ok(cxtTest == cxt, "Wrong context on Base\n");
1195     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1196     ITfContext_Release(cxtTest);
1197
1198     cnt = check_context_refcount(cxt3);
1199     hr = ITfDocumentMgr_Push(g_dm, cxt3);
1200     ok(FAILED(hr),"Push Succeeded\n");
1201     ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
1202
1203     cnt = check_context_refcount(cxt2);
1204     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1205     ok(SUCCEEDED(hr),"GetTop Failed\n");
1206     ok(cxtTest == cxt2, "Wrong context on top\n");
1207     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1208     ITfContext_Release(cxtTest);
1209
1210     cnt = check_context_refcount(cxt);
1211     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1212     ok(SUCCEEDED(hr),"GetBase Failed\n");
1213     ok(cxtTest == cxt, "Wrong context on Base\n");
1214     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1215     ITfContext_Release(cxtTest);
1216
1217     cnt = check_context_refcount(cxt2);
1218     test_OnPopContext = SINK_EXPECTED;
1219     hr = ITfDocumentMgr_Pop(g_dm, 0);
1220     ok(SUCCEEDED(hr),"Pop Failed\n");
1221     ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1222     ok(test_OnPopContext == SINK_FIRED, "OnPopContext sink not fired\n");
1223
1224     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1225     ok(SUCCEEDED(hr),"GetTop Failed\n");
1226     ok(cxtTest == cxt, "Wrong context on top\n");
1227     ITfContext_Release(cxtTest);
1228
1229     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1230     ok(SUCCEEDED(hr),"GetBase Failed\n");
1231     ok(cxtTest == cxt, "Wrong context on base\n");
1232     ITfContext_Release(cxtTest);
1233
1234     hr = ITfDocumentMgr_Pop(g_dm, 0);
1235     ok(FAILED(hr),"Pop Succeeded\n");
1236
1237     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1238     ok(SUCCEEDED(hr),"GetTop Failed\n");
1239     ok(cxtTest == cxt, "Wrong context on top\n");
1240     ITfContext_Release(cxtTest);
1241
1242     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1243     ok(SUCCEEDED(hr),"GetBase Failed\n");
1244     ok(cxtTest == cxt, "Wrong context on base\n");
1245     ITfContext_Release(cxtTest);
1246
1247     ITfContext_Release(cxt);
1248     ITfContext_Release(cxt2);
1249     ITfContext_Release(cxt3);
1250 }
1251
1252 static void test_endSession(void)
1253 {
1254     HRESULT hr;
1255     test_ShouldDeactivate = TRUE;
1256     test_CurrentFocus = NULL;
1257     test_PrevFocus = g_dm;
1258     test_OnSetFocus  = SINK_EXPECTED;
1259     hr = ITfThreadMgr_Deactivate(g_tm);
1260     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1261     ok(test_OnSetFocus == SINK_FIRED, "OnSetFocus sink not called\n");
1262     test_OnSetFocus  = SINK_UNEXPECTED;
1263 }
1264
1265 static void test_TfGuidAtom(void)
1266 {
1267     GUID gtest,g1;
1268     HRESULT hr;
1269     TfGuidAtom atom1,atom2;
1270     BOOL equal;
1271
1272     CoCreateGuid(&gtest);
1273
1274     /* msdn reports this should return E_INVALIDARG.  However my test show it crashing (winxp)*/
1275     /*
1276     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1277     ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1278     */
1279     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
1280     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1281     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
1282     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1283     ok(atom1 == atom2,"atoms do not match\n");
1284     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
1285     ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
1286     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
1287     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1288     ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
1289     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
1290     ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
1291     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
1292     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1293     ok(equal == TRUE,"Equal value invalid\n");
1294
1295     /* show that cid and tid TfClientIds are also TfGuidAtoms */
1296     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
1297     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1298     ok(equal == TRUE,"Equal value invalid\n");
1299     hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
1300     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1301     ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
1302 }
1303
1304 static void test_ClientId(void)
1305 {
1306     ITfClientId *pcid;
1307     TfClientId id1,id2;
1308     HRESULT hr;
1309     GUID g2;
1310
1311     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1312     ok(SUCCEEDED(hr),"Unable to aquire ITfClientId interface\n");
1313
1314     CoCreateGuid(&g2);
1315
1316     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
1317     ok(SUCCEEDED(hr),"GetClientId failed\n");
1318     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
1319     ok(SUCCEEDED(hr),"GetClientId failed\n");
1320     ok(id1==id2,"Id's for GUID_NULL do not match\n");
1321     hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
1322     ok(SUCCEEDED(hr),"GetClientId failed\n");
1323     ok(id2!=id1,"Id matches GUID_NULL\n");
1324     ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
1325     ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
1326     hr = ITfClientId_GetClientId(pcid,&g2,&id2);
1327     ok(SUCCEEDED(hr),"GetClientId failed\n");
1328     ok(id2!=id1,"Id matches GUID_NULL\n");
1329     ok(id2!=tid,"Id for random guid matching tid\n");
1330     ok(id2!=cid,"Id for random guid matching cid\n");
1331     ITfClientId_Release(pcid);
1332 }
1333
1334 /**********************************************************************
1335  * ITfEditSession
1336  **********************************************************************/
1337 typedef struct tagEditSession
1338 {
1339     const ITfEditSessionVtbl *EditSessionVtbl;
1340     LONG refCount;
1341 } EditSession;
1342
1343 static void EditSession_Destructor(EditSession *This)
1344 {
1345     HeapFree(GetProcessHeap(),0,This);
1346 }
1347
1348 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1349 {
1350     EditSession *This = (EditSession *)iface;
1351     *ppvOut = NULL;
1352
1353     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1354     {
1355         *ppvOut = This;
1356     }
1357
1358     if (*ppvOut)
1359     {
1360         IUnknown_AddRef(iface);
1361         return S_OK;
1362     }
1363
1364     return E_NOINTERFACE;
1365 }
1366
1367 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1368 {
1369     EditSession *This = (EditSession *)iface;
1370     return InterlockedIncrement(&This->refCount);
1371 }
1372
1373 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1374 {
1375     EditSession *This = (EditSession *)iface;
1376     ULONG ret;
1377
1378     ret = InterlockedDecrement(&This->refCount);
1379     if (ret == 0)
1380         EditSession_Destructor(This);
1381     return ret;
1382 }
1383
1384 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1385 TfEditCookie ec)
1386 {
1387     ITfContext *cxt;
1388     ITfDocumentMgr *dm;
1389     ITfRange *range;
1390     TF_SELECTION selection;
1391     ULONG fetched;
1392     HRESULT hr;
1393
1394     ok(test_DoEditSession == SINK_EXPECTED, "Unexpected DoEditSession\n");
1395     ok(test_ACP_RequestLock == SINK_FIRED,"Expected RequestLock not fired\n");
1396     test_DoEditSession = SINK_FIRED;
1397
1398     ITfThreadMgr_GetFocus(g_tm, &dm);
1399     ITfDocumentMgr_GetTop(dm,&cxt);
1400
1401     hr = ITfContext_GetStart(cxt,ec,NULL);
1402     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1403
1404     range = (ITfRange*)0xdeaddead;
1405     hr = ITfContext_GetStart(cxt,0xdeadcafe,&range);
1406     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1407     ok(range == NULL,"Range not set to NULL\n");
1408
1409     hr = ITfContext_GetStart(cxt,ec,&range);
1410     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1411     ok(range != NULL,"Range set to NULL\n");
1412
1413     ITfRange_Release(range);
1414
1415     hr = ITfContext_GetEnd(cxt,ec,NULL);
1416     ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1417
1418     range = (ITfRange*)0xdeaddead;
1419     hr = ITfContext_GetEnd(cxt,0xdeadcafe,&range);
1420     ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1421     ok(range == NULL,"Range not set to NULL\n");
1422
1423     test_ACP_GetEndACP = SINK_EXPECTED;
1424     hr = ITfContext_GetEnd(cxt,ec,&range);
1425     ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1426     ok(range != NULL,"Range set to NULL\n");
1427     ok(test_ACP_GetEndACP == SINK_FIRED, "GetEndACP not fired as expected\n");
1428
1429     ITfRange_Release(range);
1430
1431     selection.range = NULL;
1432     test_ACP_GetSelection = SINK_EXPECTED;
1433     hr = ITfContext_GetSelection(cxt, ec, TF_DEFAULT_SELECTION, 1, &selection, &fetched);
1434     ok(SUCCEEDED(hr),"ITfContext_GetSelection failed\n");
1435     ok(fetched == 1,"fetched incorrect\n");
1436     ok(selection.range != NULL,"NULL range\n");
1437     ok(test_ACP_GetSelection == SINK_FIRED," expected ACP_GetSepection not fired\n");
1438     ITfRange_Release(selection.range);
1439
1440     ITfContext_Release(cxt);
1441     ITfDocumentMgr_Release(dm);
1442     return 0xdeadcafe;
1443 }
1444
1445 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1446 {
1447     EditSession_QueryInterface,
1448     EditSession_AddRef,
1449     EditSession_Release,
1450
1451     EditSession_DoEditSession
1452 };
1453
1454 HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1455 {
1456     EditSession *This;
1457
1458     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1459     if (This == NULL)
1460         return E_OUTOFMEMORY;
1461
1462     This->EditSessionVtbl = &EditSession_EditSessionVtbl;
1463     This->refCount = 1;
1464
1465     *ppOut = (ITfEditSession*)This;
1466     return S_OK;
1467 }
1468
1469 static void test_TStoApplicationText(void)
1470 {
1471     HRESULT hr, hrSession;
1472     ITfEditSession *es;
1473     ITfContext *cxt;
1474     ITfDocumentMgr *dm;
1475
1476     ITfThreadMgr_GetFocus(g_tm, &dm);
1477     EditSession_Constructor(&es);
1478     ITfDocumentMgr_GetTop(dm,&cxt);
1479
1480     hrSession = 0xfeedface;
1481     /* Test no premissions flags */
1482     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC, &hrSession);
1483     ok(hr == E_INVALIDARG,"RequestEditSession should have failed with %x not %x\n",E_INVALIDARG,hr);
1484     ok(hrSession == E_FAIL,"hrSession should be %x not %x\n",E_FAIL,hrSession);
1485
1486     hrSession = 0xfeedface;
1487     test_ACP_GetStatus = SINK_EXPECTED;
1488     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1489     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1490     ok(hrSession == TS_E_READONLY,"Unexpected hrSession (%x)\n",hrSession);
1491     ok(test_ACP_GetStatus == SINK_FIRED," expected GetStatus not fired\n");
1492
1493     test_ACP_GetStatus = SINK_UNEXPECTED;
1494     test_ACP_RequestLock = SINK_EXPECTED;
1495     hrSession = 0xfeedface;
1496     hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READ, &hrSession);
1497     ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1498     ok(test_ACP_RequestLock == SINK_FIRED," expected RequestLock not fired\n");
1499     ok(test_DoEditSession == SINK_FIRED," expected DoEditSession not fired\n");
1500     ok(hrSession == 0xdeadcafe,"Unexpected hrSession (%x)\n",hrSession);
1501
1502     ITfContext_Release(cxt);
1503     ITfDocumentMgr_Release(dm);
1504     ITfEditSession_Release(es);
1505 }
1506
1507 START_TEST(inputprocessor)
1508 {
1509     if (SUCCEEDED(initialize()))
1510     {
1511         test_Register();
1512         test_RegisterCategory();
1513         test_EnumInputProcessorInfo();
1514         test_Enable();
1515         test_ThreadMgrAdviseSinks();
1516         test_Activate();
1517         test_startSession();
1518         test_TfGuidAtom();
1519         test_ClientId();
1520         test_KeystrokeMgr();
1521         test_TStoApplicationText();
1522         test_endSession();
1523         test_EnumLanguageProfiles();
1524         test_FindClosestCategory();
1525         test_Disable();
1526         test_ThreadMgrUnadviseSinks();
1527         test_UnregisterCategory();
1528         test_Unregister();
1529     }
1530     else
1531         skip("Unable to create InputProcessor\n");
1532     cleanup();
1533 }