msctf: Implement ITfKeystrokeMgr::GetForeground.
[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     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
409     ok(test_KEV_OnSetFocus == SINK_FIRED, "KeyEventSink_OnSetFocus not fired as expected\n");
410     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
411     ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
412     hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
413     ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
414
415     hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
416     ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
417
418     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
419     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
420
421     hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
422     ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey inproperly succeeded\n");
423
424     preserved = FALSE;
425     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
426     ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
427     if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
428
429     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
430     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
431
432     hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
433     ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
434     if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
435
436     hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
437     ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey inproperly succeeded\n");
438
439     hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
440     ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
441
442     ITfKeystrokeMgr_Release(keymgr);
443     ITfKeyEventSink_Release(sink);
444 }
445
446 static void test_Activate(void)
447 {
448     HRESULT hr;
449
450     hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
451     ok(SUCCEEDED(hr),"Failed to Activate text service\n");
452 }
453
454 static inline int check_context_refcount(ITfContext *iface)
455 {
456     IUnknown_AddRef(iface);
457     return IUnknown_Release(iface);
458 }
459
460 static void test_startSession(void)
461 {
462     HRESULT hr;
463     DWORD cnt;
464     DWORD editCookie;
465     ITfDocumentMgr *dmtest;
466     ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
467     ITextStoreACP *ts;
468     TfClientId cid2 = 0;
469
470     hr = ITfThreadMgr_Deactivate(g_tm);
471     ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
472
473     test_ShouldActivate = TRUE;
474     hr  = ITfThreadMgr_Activate(g_tm,&cid);
475     ok(SUCCEEDED(hr),"Failed to Activate\n");
476     ok(cid != tid,"TextService id mistakenly matches Client id\n");
477
478     test_ShouldActivate = FALSE;
479     hr = ITfThreadMgr_Activate(g_tm,&cid2);
480     ok(SUCCEEDED(hr),"Failed to Activate\n");
481     ok (cid == cid2, "Second activate client ID does not match\n");
482
483     hr = ITfThreadMgr_Deactivate(g_tm);
484     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
485
486     hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
487     ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
488
489     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
490     ok(SUCCEEDED(hr),"GetFocus Failed\n");
491     ok(dmtest == NULL,"Initial focus not null\n");
492
493     test_CurrentFocus = g_dm;
494     test_PrevFocus = NULL;
495     test_OnSetFocus  = SINK_EXPECTED;
496     hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
497     ok(SUCCEEDED(hr),"SetFocus Failed\n");
498     ok(test_OnSetFocus == SINK_FIRED, "OnSetFocus sink not called\n");
499     test_OnSetFocus  = SINK_UNEXPECTED;
500
501     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
502     ok(SUCCEEDED(hr),"GetFocus Failed\n");
503     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
504
505     cnt = ITfDocumentMgr_Release(g_dm);
506     ok(cnt == 2,"DocumentMgr refcount not expected (2 vs %i)\n",cnt);
507
508     hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
509     ok(SUCCEEDED(hr),"GetFocus Failed\n");
510     ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
511
512     TextStoreACP_Constructor((IUnknown**)&ts);
513
514     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
515     ok(SUCCEEDED(hr),"CreateContext Failed\n");
516
517     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
518     ok(SUCCEEDED(hr),"CreateContext Failed\n");
519
520     hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
521     ok(SUCCEEDED(hr),"CreateContext Failed\n");
522
523     cnt = check_context_refcount(cxt);
524     test_OnPushContext = SINK_EXPECTED;
525     test_OnInitDocumentMgr = SINK_EXPECTED;
526     hr = ITfDocumentMgr_Push(g_dm, cxt);
527     ok(SUCCEEDED(hr),"Push Failed\n");
528     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
529     ok(test_OnPushContext == SINK_FIRED, "OnPushContext sink not fired\n");
530     ok(test_OnInitDocumentMgr == SINK_FIRED, "OnInitDocumentMgr sink not fired\n");
531
532     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
533     ok(SUCCEEDED(hr),"GetTop Failed\n");
534     ok(cxtTest == cxt, "Wrong context on top\n");
535     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
536     cnt = ITfContext_Release(cxtTest);
537
538     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
539     ok(SUCCEEDED(hr),"GetBase Failed\n");
540     ok(cxtTest == cxt, "Wrong context on Base\n");
541     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
542     ITfContext_Release(cxtTest);
543
544     check_context_refcount(cxt2);
545     test_OnPushContext = SINK_EXPECTED;
546     hr = ITfDocumentMgr_Push(g_dm, cxt2);
547     ok(SUCCEEDED(hr),"Push Failed\n");
548     ok(test_OnPushContext == SINK_FIRED, "OnPushContext sink not fired\n");
549
550     cnt = check_context_refcount(cxt2);
551     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
552     ok(SUCCEEDED(hr),"GetTop Failed\n");
553     ok(cxtTest == cxt2, "Wrong context on top\n");
554     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
555     ITfContext_Release(cxtTest);
556
557     cnt = check_context_refcount(cxt);
558     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
559     ok(SUCCEEDED(hr),"GetBase Failed\n");
560     ok(cxtTest == cxt, "Wrong context on Base\n");
561     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
562     ITfContext_Release(cxtTest);
563
564     cnt = check_context_refcount(cxt3);
565     hr = ITfDocumentMgr_Push(g_dm, cxt3);
566     ok(!SUCCEEDED(hr),"Push Succeeded\n");
567     ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
568
569     cnt = check_context_refcount(cxt2);
570     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
571     ok(SUCCEEDED(hr),"GetTop Failed\n");
572     ok(cxtTest == cxt2, "Wrong context on top\n");
573     ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
574     ITfContext_Release(cxtTest);
575
576     cnt = check_context_refcount(cxt);
577     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
578     ok(SUCCEEDED(hr),"GetBase Failed\n");
579     ok(cxtTest == cxt, "Wrong context on Base\n");
580     ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
581     ITfContext_Release(cxtTest);
582
583     cnt = check_context_refcount(cxt2);
584     test_OnPopContext = SINK_EXPECTED;
585     hr = ITfDocumentMgr_Pop(g_dm, 0);
586     ok(SUCCEEDED(hr),"Pop Failed\n");
587     ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
588     ok(test_OnPopContext == SINK_FIRED, "OnPopContext sink not fired\n");
589
590     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
591     ok(SUCCEEDED(hr),"GetTop Failed\n");
592     ok(cxtTest == cxt, "Wrong context on top\n");
593     ITfContext_Release(cxtTest);
594
595     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
596     ok(SUCCEEDED(hr),"GetBase Failed\n");
597     ok(cxtTest == cxt, "Wrong context on base\n");
598     ITfContext_Release(cxtTest);
599
600     hr = ITfDocumentMgr_Pop(g_dm, 0);
601     ok(!SUCCEEDED(hr),"Pop Succeeded\n");
602
603     hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
604     ok(SUCCEEDED(hr),"GetTop Failed\n");
605     ok(cxtTest == cxt, "Wrong context on top\n");
606     ITfContext_Release(cxtTest);
607
608     hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
609     ok(SUCCEEDED(hr),"GetBase Failed\n");
610     ok(cxtTest == cxt, "Wrong context on base\n");
611     ITfContext_Release(cxtTest);
612
613     ITfContext_Release(cxt);
614     ITfContext_Release(cxt2);
615     ITfContext_Release(cxt3);
616 }
617
618 static void test_endSession(void)
619 {
620     HRESULT hr;
621     test_ShouldDeactivate = TRUE;
622     test_CurrentFocus = NULL;
623     test_PrevFocus = g_dm;
624     test_OnSetFocus  = SINK_EXPECTED;
625     hr = ITfThreadMgr_Deactivate(g_tm);
626     ok(SUCCEEDED(hr),"Failed to Deactivate\n");
627     ok(test_OnSetFocus == SINK_FIRED, "OnSetFocus sink not called\n");
628     test_OnSetFocus  = SINK_UNEXPECTED;
629 }
630
631 static void test_TfGuidAtom(void)
632 {
633     GUID gtest,g1;
634     HRESULT hr;
635     TfGuidAtom atom1,atom2;
636     BOOL equal;
637
638     CoCreateGuid(&gtest);
639
640     /* msdn reports this should return E_INVALIDARG.  However my test show it crashing (winxp)*/
641     /*
642     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
643     ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
644     */
645     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
646     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
647     hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
648     ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
649     ok(atom1 == atom2,"atoms do not match\n");
650     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
651     ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
652     hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
653     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
654     ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
655     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
656     ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
657     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
658     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
659     ok(equal == TRUE,"Equal value invalid\n");
660
661     /* show that cid and tid TfClientIds are also TfGuidAtoms */
662     hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
663     ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
664     ok(equal == TRUE,"Equal value invalid\n");
665     hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
666     ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
667     ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
668 }
669
670 static void test_ClientId(void)
671 {
672     ITfClientId *pcid;
673     TfClientId id1,id2;
674     HRESULT hr;
675     GUID g2;
676
677     hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
678     ok(SUCCEEDED(hr),"Unable to aquire ITfClientId interface\n");
679
680     CoCreateGuid(&g2);
681
682     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
683     ok(SUCCEEDED(hr),"GetClientId failed\n");
684     hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
685     ok(SUCCEEDED(hr),"GetClientId failed\n");
686     ok(id1==id2,"Id's for GUID_NULL do not match\n");
687     hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
688     ok(SUCCEEDED(hr),"GetClientId failed\n");
689     ok(id2!=id1,"Id matches GUID_NULL\n");
690     ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
691     ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
692     hr = ITfClientId_GetClientId(pcid,&g2,&id2);
693     ok(SUCCEEDED(hr),"GetClientId failed\n");
694     ok(id2!=id1,"Id matches GUID_NULL\n");
695     ok(id2!=tid,"Id for random guid matching tid\n");
696     ok(id2!=cid,"Id for random guid matching cid\n");
697     ITfClientId_Release(pcid);
698 }
699
700 START_TEST(inputprocessor)
701 {
702     if (SUCCEEDED(initialize()))
703     {
704         test_Register();
705         test_RegisterCategory();
706         test_EnumInputProcessorInfo();
707         test_Enable();
708         test_ThreadMgrAdviseSinks();
709         test_Activate();
710         test_startSession();
711         test_TfGuidAtom();
712         test_ClientId();
713         test_KeystrokeMgr();
714         test_endSession();
715         test_EnumLanguageProfiles();
716         test_FindClosestCategory();
717         test_Disable();
718         test_ThreadMgrUnadviseSinks();
719         test_UnregisterCategory();
720         test_Unregister();
721     }
722     else
723         skip("Unable to create InputProcessor\n");
724     cleanup();
725 }
726
727 /**********************************************************************
728  * ITextStoreACP
729  **********************************************************************/
730 typedef struct tagTextStoreACP
731 {
732     const ITextStoreACPVtbl *TextStoreACPVtbl;
733     LONG refCount;
734 } TextStoreACP;
735
736 static void TextStoreACP_Destructor(TextStoreACP *This)
737 {
738     HeapFree(GetProcessHeap(),0,This);
739 }
740
741 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
742 {
743     TextStoreACP *This = (TextStoreACP *)iface;
744     *ppvOut = NULL;
745
746     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
747     {
748         *ppvOut = This;
749     }
750
751     if (*ppvOut)
752     {
753         IUnknown_AddRef(iface);
754         return S_OK;
755     }
756
757     return E_NOINTERFACE;
758 }
759
760 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
761 {
762     TextStoreACP *This = (TextStoreACP *)iface;
763     return InterlockedIncrement(&This->refCount);
764 }
765
766 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
767 {
768     TextStoreACP *This = (TextStoreACP *)iface;
769     ULONG ret;
770
771     ret = InterlockedDecrement(&This->refCount);
772     if (ret == 0)
773         TextStoreACP_Destructor(This);
774     return ret;
775 }
776
777 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
778     REFIID riid, IUnknown *punk, DWORD dwMask)
779 {
780     trace("\n");
781     return S_OK;
782 }
783
784 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
785     IUnknown *punk)
786 {
787     trace("\n");
788     return S_OK;
789 }
790 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
791     DWORD dwLockFlags, HRESULT *phrSession)
792 {
793     trace("\n");
794     return S_OK;
795 }
796 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
797     TS_STATUS *pdcs)
798 {
799     trace("\n");
800     return S_OK;
801 }
802 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
803     LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
804     LONG *pacpResultEnd)
805 {
806     trace("\n");
807     return S_OK;
808 }
809 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
810     ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
811 {
812     trace("\n");
813     return S_OK;
814 }
815 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
816     ULONG ulCount, const TS_SELECTION_ACP *pSelection)
817 {
818     trace("\n");
819     return S_OK;
820 }
821 static HRESULT WINAPI TextStoreACP_GetText(ITextStoreACP *iface,
822     LONG acpStart, LONG acpEnd, WCHAR *pchPlain, ULONG cchPlainReq,
823     ULONG *pcchPlainRet, TS_RUNINFO *prgRunInfo, ULONG cRunInfoReq,
824     ULONG *pcRunInfoRet, LONG *pacpNext)
825 {
826     trace("\n");
827     return S_OK;
828 }
829 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
830     DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
831     ULONG cch, TS_TEXTCHANGE *pChange)
832 {
833     trace("\n");
834     return S_OK;
835 }
836 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
837     LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
838 {
839     trace("\n");
840     return S_OK;
841 }
842 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
843     LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
844 {
845     trace("\n");
846     return S_OK;
847 }
848 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
849     const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
850 {
851     trace("\n");
852     return S_OK;
853 }
854 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
855     DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
856     TS_TEXTCHANGE *pChange)
857 {
858     trace("\n");
859     return S_OK;
860 }
861 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
862     DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
863     LONG *pacpEnd, TS_TEXTCHANGE *pChange)
864 {
865     trace("\n");
866     return S_OK;
867 }
868 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
869     DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
870     TS_TEXTCHANGE *pChange)
871 {
872     trace("\n");
873     return S_OK;
874 }
875 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
876     DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
877 {
878     trace("\n");
879     return S_OK;
880 }
881 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
882     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
883     DWORD dwFlags)
884 {
885     trace("\n");
886     return S_OK;
887 }
888 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
889     LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
890     DWORD dwFlags)
891 {
892     trace("\n");
893     return S_OK;
894 }
895 static HRESULT WINAPI TextStoreACP_FindNextAttrTransition(ITextStoreACP *iface,
896     LONG acpStart, LONG acpHalt, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
897     DWORD dwFlags, LONG *pacpNext, BOOL *pfFound, LONG *plFoundOffset)
898 {
899     trace("\n");
900     return S_OK;
901 }
902 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
903     ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
904 {
905     trace("\n");
906     return S_OK;
907 }
908 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
909     LONG *pacp)
910 {
911     trace("\n");
912     return S_OK;
913 }
914 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
915     TsViewCookie *pvcView)
916 {
917     trace("\n");
918     return S_OK;
919 }
920 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
921     TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
922     LONG *pacp)
923 {
924     trace("\n");
925     return S_OK;
926 }
927 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
928     TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
929     BOOL *pfClipped)
930 {
931     trace("\n");
932     return S_OK;
933 }
934 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
935     TsViewCookie vcView, RECT *prc)
936 {
937     trace("\n");
938     return S_OK;
939 }
940 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
941     TsViewCookie vcView, HWND *phwnd)
942 {
943     trace("\n");
944     return S_OK;
945 }
946
947 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
948 {
949     TextStoreACP_QueryInterface,
950     TextStoreACP_AddRef,
951     TextStoreACP_Release,
952
953     TextStoreACP_AdviseSink,
954     TextStoreACP_UnadviseSink,
955     TextStoreACP_RequestLock,
956     TextStoreACP_GetStatus,
957     TextStoreACP_QueryInsert,
958     TextStoreACP_GetSelection,
959     TextStoreACP_SetSelection,
960     TextStoreACP_GetText,
961     TextStoreACP_SetText,
962     TextStoreACP_GetFormattedText,
963     TextStoreACP_GetEmbedded,
964     TextStoreACP_QueryInsertEmbedded,
965     TextStoreACP_InsertEmbedded,
966     TextStoreACP_InsertTextAtSelection,
967     TextStoreACP_InsertEmbeddedAtSelection,
968     TextStoreACP_RequestSupportedAttrs,
969     TextStoreACP_RequestAttrsAtPosition,
970     TextStoreACP_RequestAttrsTransitioningAtPosition,
971     TextStoreACP_FindNextAttrTransition,
972     TextStoreACP_RetrieveRequestedAttrs,
973     TextStoreACP_GetEndACP,
974     TextStoreACP_GetActiveView,
975     TextStoreACP_GetACPFromPoint,
976     TextStoreACP_GetTextExt,
977     TextStoreACP_GetScreenExt,
978     TextStoreACP_GetWnd
979 };
980
981 HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
982 {
983     TextStoreACP *This;
984
985     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
986     if (This == NULL)
987         return E_OUTOFMEMORY;
988
989     This->TextStoreACPVtbl = &TextStoreACP_TextStoreACPVtbl;
990     This->refCount = 1;
991
992     *ppOut = (IUnknown *)This;
993     return S_OK;
994 }
995
996 /**********************************************************************
997  * ITfThreadMgrEventSink
998  **********************************************************************/
999 typedef struct tagThreadMgrEventSink
1000 {
1001     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl;
1002     LONG refCount;
1003 } ThreadMgrEventSink;
1004
1005 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
1006 {
1007     HeapFree(GetProcessHeap(),0,This);
1008 }
1009
1010 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
1011 {
1012     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
1013     *ppvOut = NULL;
1014
1015     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
1016     {
1017         *ppvOut = This;
1018     }
1019
1020     if (*ppvOut)
1021     {
1022         IUnknown_AddRef(iface);
1023         return S_OK;
1024     }
1025
1026     return E_NOINTERFACE;
1027 }
1028
1029 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
1030 {
1031     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
1032     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
1033     return InterlockedIncrement(&This->refCount);
1034 }
1035
1036 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
1037 {
1038     ThreadMgrEventSink *This = (ThreadMgrEventSink *)iface;
1039     ULONG ret;
1040
1041     ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
1042     ret = InterlockedDecrement(&This->refCount);
1043     if (ret == 0)
1044         ThreadMgrEventSink_Destructor(This);
1045     return ret;
1046 }
1047
1048 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
1049 ITfDocumentMgr *pdim)
1050 {
1051     ok(test_OnInitDocumentMgr == SINK_EXPECTED, "Unexpected OnInitDocumentMgr sink\n");
1052     test_OnInitDocumentMgr = SINK_FIRED;
1053     return S_OK;
1054 }
1055
1056 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
1057 ITfDocumentMgr *pdim)
1058 {
1059     trace("\n");
1060     return S_OK;
1061 }
1062
1063 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
1064 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
1065 {
1066     ok(test_OnSetFocus == SINK_EXPECTED, "Unexpected OnSetFocus sink\n");
1067     ok(pdimFocus == test_CurrentFocus,"Sink reports wrong focus\n");
1068     ok(pdimPrevFocus == test_PrevFocus,"Sink reports wrong previous focus\n");
1069     test_OnSetFocus = SINK_FIRED;
1070     return S_OK;
1071 }
1072
1073 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(ITfThreadMgrEventSink *iface,
1074 ITfContext *pic)
1075 {
1076     ok(test_OnPushContext == SINK_EXPECTED, "Unexpected OnPushContext sink\n");
1077     test_OnPushContext = SINK_FIRED;
1078     return S_OK;
1079 }
1080
1081 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
1082 ITfContext *pic)
1083 {
1084     ok(test_OnPopContext == SINK_EXPECTED, "Unexpected OnPopContext sink\n");
1085     test_OnPopContext = SINK_FIRED;
1086     return S_OK;
1087 }
1088
1089 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
1090 {
1091     ThreadMgrEventSink_QueryInterface,
1092     ThreadMgrEventSink_AddRef,
1093     ThreadMgrEventSink_Release,
1094
1095     ThreadMgrEventSink_OnInitDocumentMgr,
1096     ThreadMgrEventSink_OnUninitDocumentMgr,
1097     ThreadMgrEventSink_OnSetFocus,
1098     ThreadMgrEventSink_OnPushContext,
1099     ThreadMgrEventSink_OnPopContext
1100 };
1101
1102 HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
1103 {
1104     ThreadMgrEventSink *This;
1105
1106     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
1107     if (This == NULL)
1108         return E_OUTOFMEMORY;
1109
1110     This->ThreadMgrEventSinkVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
1111     This->refCount = 1;
1112
1113     *ppOut = (IUnknown *)This;
1114     return S_OK;
1115 }
1116
1117
1118 /********************************************************************************************
1119  * Stub text service for testing
1120  ********************************************************************************************/
1121
1122 static LONG TS_refCount;
1123 static IClassFactory *cf;
1124 static DWORD regid;
1125
1126 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
1127
1128 typedef struct tagClassFactory
1129 {
1130     const IClassFactoryVtbl *vtbl;
1131     LONG   ref;
1132     LPFNCONSTRUCTOR ctor;
1133 } ClassFactory;
1134
1135 typedef struct tagTextService
1136 {
1137     const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
1138     LONG refCount;
1139 } TextService;
1140
1141 static void ClassFactory_Destructor(ClassFactory *This)
1142 {
1143     HeapFree(GetProcessHeap(),0,This);
1144     TS_refCount--;
1145 }
1146
1147 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
1148 {
1149     *ppvOut = NULL;
1150     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
1151     {
1152         IClassFactory_AddRef(iface);
1153         *ppvOut = iface;
1154         return S_OK;
1155     }
1156
1157     return E_NOINTERFACE;
1158 }
1159
1160 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
1161 {
1162     ClassFactory *This = (ClassFactory *)iface;
1163     return InterlockedIncrement(&This->ref);
1164 }
1165
1166 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
1167 {
1168     ClassFactory *This = (ClassFactory *)iface;
1169     ULONG ret = InterlockedDecrement(&This->ref);
1170
1171     if (ret == 0)
1172         ClassFactory_Destructor(This);
1173     return ret;
1174 }
1175
1176 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
1177 {
1178     ClassFactory *This = (ClassFactory *)iface;
1179     HRESULT ret;
1180     IUnknown *obj;
1181
1182     ret = This->ctor(punkOuter, &obj);
1183     if (FAILED(ret))
1184         return ret;
1185     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
1186     IUnknown_Release(obj);
1187     return ret;
1188 }
1189
1190 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
1191 {
1192     if(fLock)
1193         InterlockedIncrement(&TS_refCount);
1194     else
1195         InterlockedDecrement(&TS_refCount);
1196
1197     return S_OK;
1198 }
1199
1200 static const IClassFactoryVtbl ClassFactoryVtbl = {
1201     /* IUnknown */
1202     ClassFactory_QueryInterface,
1203     ClassFactory_AddRef,
1204     ClassFactory_Release,
1205
1206     /* IClassFactory*/
1207     ClassFactory_CreateInstance,
1208     ClassFactory_LockServer
1209 };
1210
1211 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
1212 {
1213     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
1214     This->vtbl = &ClassFactoryVtbl;
1215     This->ref = 1;
1216     This->ctor = ctor;
1217     *ppvOut = (LPVOID)This;
1218     TS_refCount++;
1219     return S_OK;
1220 }
1221
1222 static void TextService_Destructor(TextService *This)
1223 {
1224     HeapFree(GetProcessHeap(),0,This);
1225 }
1226
1227 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
1228 {
1229     TextService *This = (TextService *)iface;
1230     *ppvOut = NULL;
1231
1232     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
1233     {
1234         *ppvOut = This;
1235     }
1236
1237     if (*ppvOut)
1238     {
1239         IUnknown_AddRef(iface);
1240         return S_OK;
1241     }
1242
1243     return E_NOINTERFACE;
1244 }
1245
1246 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
1247 {
1248     TextService *This = (TextService *)iface;
1249     return InterlockedIncrement(&This->refCount);
1250 }
1251
1252 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
1253 {
1254     TextService *This = (TextService *)iface;
1255     ULONG ret;
1256
1257     ret = InterlockedDecrement(&This->refCount);
1258     if (ret == 0)
1259         TextService_Destructor(This);
1260     return ret;
1261 }
1262
1263 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
1264         ITfThreadMgr *ptim, TfClientId id)
1265 {
1266     trace("TextService_Activate\n");
1267     ok(test_ShouldActivate,"Activation came unexpectedly\n");
1268     tid = id;
1269     return S_OK;
1270 }
1271
1272 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
1273 {
1274     trace("TextService_Deactivate\n");
1275     ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
1276     return S_OK;
1277 }
1278
1279 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
1280 {
1281     TextService_QueryInterface,
1282     TextService_AddRef,
1283     TextService_Release,
1284
1285     TextService_Activate,
1286     TextService_Deactivate
1287 };
1288
1289 HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
1290 {
1291     TextService *This;
1292     if (pUnkOuter)
1293         return CLASS_E_NOAGGREGATION;
1294
1295     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
1296     if (This == NULL)
1297         return E_OUTOFMEMORY;
1298
1299     This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
1300     This->refCount = 1;
1301
1302     *ppOut = (IUnknown *)This;
1303     return S_OK;
1304 }
1305
1306 HRESULT RegisterTextService(REFCLSID rclsid)
1307 {
1308     ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
1309     return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
1310 }
1311
1312 HRESULT UnregisterTextService()
1313 {
1314     return CoRevokeClassObject(regid);
1315 }