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