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