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