push e4fb87785f42653a03d84664d7db014c69dd1260
[wine/hacks.git] / dlls / msctf / tests / inputprocessor.c
blob13b81c7858535be97b0c30ae11b0bc10bfd8e84c
1 /*
2 * Unit tests for ITfInputProcessor
4 * Copyright 2009 Aric Stewart, CodeWeavers
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.
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.
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
21 #include <stdio.h>
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"
32 static ITfInputProcessorProfiles* g_ipp;
33 static LANGID gLangid;
34 static ITfCategoryMgr * g_cm = NULL;
35 static ITfThreadMgr* g_tm = NULL;
37 HRESULT RegisterTextService(REFCLSID rclsid);
38 HRESULT UnregisterTextService();
40 DEFINE_GUID(CLSID_FakeService, 0xEDE1A7AD,0x66DE,0x47E0,0xB6,0x20,0x3E,0x92,0xF8,0x24,0x6B,0xF3);
41 DEFINE_GUID(CLSID_TF_InputProcessorProfiles, 0x33c53a50,0xf456,0x4884,0xb0,0x49,0x85,0xfd,0x64,0x3e,0xcf,0xed);
42 DEFINE_GUID(CLSID_TF_CategoryMgr, 0xA4B544A1,0x438D,0x4B41,0x93,0x25,0x86,0x95,0x23,0xE2,0xD6,0xC7);
43 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD, 0x34745c63,0xb2f0,0x4784,0x8b,0x67,0x5e,0x12,0xc8,0x70,0x1a,0x31);
44 DEFINE_GUID(GUID_TFCAT_TIP_SPEECH, 0xB5A73CD1,0x8355,0x426B,0xA1,0x61,0x25,0x98,0x08,0xF2,0x6B,0x14);
45 DEFINE_GUID(GUID_TFCAT_TIP_HANDWRITING, 0x246ecb87,0xc2f2,0x4abe,0x90,0x5b,0xc8,0xb3,0x8a,0xdd,0x2c,0x43);
46 DEFINE_GUID (GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, 0x046B8C80,0x1647,0x40F7,0x9B,0x21,0xB9,0x3B,0x81,0xAA,0xBC,0x1B);
47 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
48 DEFINE_GUID(CLSID_TF_ThreadMgr, 0x529a9e6b,0x6587,0x4f23,0xab,0x9e,0x9c,0x7d,0x68,0x3e,0x3c,0x50);
51 static HRESULT initialize(void)
53 HRESULT hr;
54 CoInitialize(NULL);
55 hr = CoCreateInstance (&CLSID_TF_InputProcessorProfiles, NULL,
56 CLSCTX_INPROC_SERVER, &IID_ITfInputProcessorProfiles, (void**)&g_ipp);
57 if (SUCCEEDED(hr))
58 hr = CoCreateInstance (&CLSID_TF_CategoryMgr, NULL,
59 CLSCTX_INPROC_SERVER, &IID_ITfCategoryMgr, (void**)&g_cm);
60 if (SUCCEEDED(hr))
61 hr = CoCreateInstance (&CLSID_TF_ThreadMgr, NULL,
62 CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (void**)&g_tm);
63 return hr;
66 static void cleanup(void)
68 if (g_ipp)
69 ITfInputProcessorProfiles_Release(g_ipp);
70 if (g_cm)
71 ITfCategoryMgr_Release(g_cm);
72 if (g_tm)
73 ITfThreadMgr_Release(g_tm);
74 CoUninitialize();
77 static void test_Register(void)
79 HRESULT hr;
81 static const WCHAR szDesc[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',0};
82 static const WCHAR szFile[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',' ','F','i','l','e',0};
84 hr = RegisterTextService(&CLSID_FakeService);
85 ok(SUCCEEDED(hr),"Unable to register COM for TextService\n");
86 hr = ITfInputProcessorProfiles_Register(g_ipp, &CLSID_FakeService);
87 ok(SUCCEEDED(hr),"Unable to register text service(%x)\n",hr);
88 hr = ITfInputProcessorProfiles_AddLanguageProfile(g_ipp, &CLSID_FakeService, gLangid, &CLSID_FakeService, szDesc, sizeof(szDesc)/sizeof(WCHAR), szFile, sizeof(szFile)/sizeof(WCHAR), 1);
89 ok(SUCCEEDED(hr),"Unable to add Language Profile (%x)\n",hr);
92 static void test_Unregister(void)
94 HRESULT hr;
95 hr = ITfInputProcessorProfiles_Unregister(g_ipp, &CLSID_FakeService);
96 ok(SUCCEEDED(hr),"Unable to unregister text service(%x)\n",hr);
97 UnregisterTextService();
100 static void test_EnumInputProcessorInfo(void)
102 IEnumGUID *ppEnum;
103 BOOL found = FALSE;
105 if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
107 ULONG fetched;
108 GUID g;
109 while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
111 if(IsEqualGUID(&g,&CLSID_FakeService))
112 found = TRUE;
115 ok(found,"Did not find registered text service\n");
118 static void test_EnumLanguageProfiles(void)
120 BOOL found = FALSE;
121 IEnumTfLanguageProfiles *ppEnum;
122 if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
124 TF_LANGUAGEPROFILE profile;
125 while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
127 if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
129 found = TRUE;
130 ok(profile.langid == gLangid, "LangId Incorrect\n");
131 ok(IsEqualGUID(&profile.catid,&GUID_TFCAT_TIP_KEYBOARD), "CatId Incorrect\n");
132 ok(IsEqualGUID(&profile.guidProfile,&CLSID_FakeService), "guidProfile Incorrect\n");
136 ok(found,"Registered text service not found\n");
139 static void test_RegisterCategory(void)
141 HRESULT hr;
142 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
143 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
144 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
145 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
148 static void test_UnregisterCategory(void)
150 HRESULT hr;
151 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
152 todo_wine ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
153 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
154 todo_wine ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
157 static void test_FindClosestCategory(void)
159 GUID output;
160 HRESULT hr;
161 const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
163 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, NULL, 0);
164 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
165 ok(IsEqualGUID(&output,&GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER),"Wrong GUID\n");
167 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 1);
168 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
169 ok(IsEqualGUID(&output,&GUID_NULL),"Wrong GUID\n");
171 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 3);
172 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
173 ok(IsEqualGUID(&output,&GUID_TFCAT_TIP_KEYBOARD),"Wrong GUID\n");
176 static void test_Enable(void)
178 HRESULT hr;
179 BOOL enabled = FALSE;
181 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, TRUE);
182 ok(SUCCEEDED(hr),"Failed to enable text service\n");
183 hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, &enabled);
184 ok(SUCCEEDED(hr),"Failed to get enabled state\n");
185 ok(enabled == TRUE,"enabled state incorrect\n");
188 static void test_Disable(void)
190 HRESULT hr;
192 trace("Disabling\n");
193 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, FALSE);
194 ok(SUCCEEDED(hr),"Failed to disable text service\n");
197 START_TEST(inputprocessor)
199 if (SUCCEEDED(initialize()))
201 gLangid = GetUserDefaultLCID();
202 test_Register();
203 test_RegisterCategory();
204 test_EnumInputProcessorInfo();
205 test_Enable();
206 test_EnumLanguageProfiles();
207 test_FindClosestCategory();
208 test_Disable();
209 test_UnregisterCategory();
210 test_Unregister();
212 else
213 skip("Unable to create InputProcessor\n");
214 cleanup();
219 /********************************************************************************************
220 * Stub text service for testing
221 ********************************************************************************************/
223 static LONG TS_refCount;
224 static IClassFactory *cf;
225 static DWORD regid;
227 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
229 typedef struct tagClassFactory
231 const IClassFactoryVtbl *vtbl;
232 LONG ref;
233 LPFNCONSTRUCTOR ctor;
234 } ClassFactory;
236 typedef struct tagTextService
238 const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
239 LONG refCount;
240 } TextService;
242 static void ClassFactory_Destructor(ClassFactory *This)
244 HeapFree(GetProcessHeap(),0,This);
245 TS_refCount--;
248 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
250 *ppvOut = NULL;
251 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
253 IClassFactory_AddRef(iface);
254 *ppvOut = iface;
255 return S_OK;
258 return E_NOINTERFACE;
261 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
263 ClassFactory *This = (ClassFactory *)iface;
264 return InterlockedIncrement(&This->ref);
267 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
269 ClassFactory *This = (ClassFactory *)iface;
270 ULONG ret = InterlockedDecrement(&This->ref);
272 if (ret == 0)
273 ClassFactory_Destructor(This);
274 return ret;
277 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
279 ClassFactory *This = (ClassFactory *)iface;
280 HRESULT ret;
281 IUnknown *obj;
283 ret = This->ctor(punkOuter, &obj);
284 if (FAILED(ret))
285 return ret;
286 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
287 IUnknown_Release(obj);
288 return ret;
291 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
293 if(fLock)
294 InterlockedIncrement(&TS_refCount);
295 else
296 InterlockedDecrement(&TS_refCount);
298 return S_OK;
301 static const IClassFactoryVtbl ClassFactoryVtbl = {
302 /* IUnknown */
303 ClassFactory_QueryInterface,
304 ClassFactory_AddRef,
305 ClassFactory_Release,
307 /* IClassFactory*/
308 ClassFactory_CreateInstance,
309 ClassFactory_LockServer
312 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
314 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
315 This->vtbl = &ClassFactoryVtbl;
316 This->ref = 1;
317 This->ctor = ctor;
318 *ppvOut = (LPVOID)This;
319 TS_refCount++;
320 return S_OK;
323 static void TextService_Destructor(TextService *This)
325 HeapFree(GetProcessHeap(),0,This);
328 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
330 TextService *This = (TextService *)iface;
331 *ppvOut = NULL;
333 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
335 *ppvOut = This;
338 if (*ppvOut)
340 IUnknown_AddRef(iface);
341 return S_OK;
344 return E_NOINTERFACE;
347 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
349 TextService *This = (TextService *)iface;
350 return InterlockedIncrement(&This->refCount);
353 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
355 TextService *This = (TextService *)iface;
356 ULONG ret;
358 ret = InterlockedDecrement(&This->refCount);
359 if (ret == 0)
360 TextService_Destructor(This);
361 return ret;
364 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
365 ITfThreadMgr *ptim, TfClientId id)
367 trace("TextService_Activate\n");
368 return S_OK;
371 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
373 trace("TextService_Deactivate\n");
374 return S_OK;
377 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
379 TextService_QueryInterface,
380 TextService_AddRef,
381 TextService_Release,
383 TextService_Activate,
384 TextService_Deactivate
387 HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
389 TextService *This;
390 if (pUnkOuter)
391 return CLASS_E_NOAGGREGATION;
393 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
394 if (This == NULL)
395 return E_OUTOFMEMORY;
397 This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
398 This->refCount = 1;
400 *ppOut = (IUnknown *)This;
401 return S_OK;
404 HRESULT RegisterTextService(REFCLSID rclsid)
406 ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
407 return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
410 HRESULT UnregisterTextService()
412 return CoRevokeClassObject(regid);