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