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