msctf/tests: Add framework for a TextService for testing.
[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
32 static ITfInputProcessorProfiles* g_ipp;
33 static LANGID gLangid;
34 static ITfCategoryMgr * g_cm = NULL;
35 static ITfThreadMgr* g_tm = NULL;
36
37 HRESULT RegisterTextService(REFCLSID rclsid);
38 HRESULT UnregisterTextService();
39
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);
49
50
51 static HRESULT initialize(void)
52 {
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;
64 }
65
66 static void cleanup(void)
67 {
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();
75 }
76
77 static void test_Register(void)
78 {
79     HRESULT hr;
80
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};
83
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);
90 }
91
92 static void test_Unregister(void)
93 {
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();
98 }
99
100 static void test_EnumInputProcessorInfo(void)
101 {
102     IEnumGUID *ppEnum;
103     BOOL found = FALSE;
104
105     if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
106     {
107         ULONG fetched;
108         GUID g;
109         while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
110         {
111             if(IsEqualGUID(&g,&CLSID_FakeService))
112                 found = TRUE;
113         }
114     }
115     ok(found,"Did not find registered text service\n");
116 }
117
118 static void test_EnumLanguageProfiles(void)
119 {
120     BOOL found = FALSE;
121     IEnumTfLanguageProfiles *ppEnum;
122     if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
123     {
124         TF_LANGUAGEPROFILE profile;
125         while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
126         {
127             if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
128             {
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");
133             }
134         }
135     }
136     ok(found,"Registered text service not found\n");
137 }
138
139 static void test_RegisterCategory(void)
140 {
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");
146 }
147
148 static void test_UnregisterCategory(void)
149 {
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");
155 }
156
157 static void test_FindClosestCategory(void)
158 {
159     GUID output;
160     HRESULT hr;
161     const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
162
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");
166
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");
170
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");
174 }
175
176 static void test_Enable(void)
177 {
178     HRESULT hr;
179     BOOL enabled = FALSE;
180
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");
186 }
187
188 static void test_Disable(void)
189 {
190     HRESULT hr;
191
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");
195 }
196
197 START_TEST(inputprocessor)
198 {
199     if (SUCCEEDED(initialize()))
200     {
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();
211     }
212     else
213         skip("Unable to create InputProcessor\n");
214     cleanup();
215 }
216
217
218
219 /********************************************************************************************
220  * Stub text service for testing
221  ********************************************************************************************/
222
223 static LONG TS_refCount;
224 static IClassFactory *cf;
225 static DWORD regid;
226
227 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
228
229 typedef struct tagClassFactory
230 {
231     const IClassFactoryVtbl *vtbl;
232     LONG   ref;
233     LPFNCONSTRUCTOR ctor;
234 } ClassFactory;
235
236 typedef struct tagTextService
237 {
238     const ITfTextInputProcessorVtbl *TextInputProcessorVtbl;
239     LONG refCount;
240 } TextService;
241
242 static void ClassFactory_Destructor(ClassFactory *This)
243 {
244     HeapFree(GetProcessHeap(),0,This);
245     TS_refCount--;
246 }
247
248 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
249 {
250     *ppvOut = NULL;
251     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
252     {
253         IClassFactory_AddRef(iface);
254         *ppvOut = iface;
255         return S_OK;
256     }
257
258     return E_NOINTERFACE;
259 }
260
261 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
262 {
263     ClassFactory *This = (ClassFactory *)iface;
264     return InterlockedIncrement(&This->ref);
265 }
266
267 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
268 {
269     ClassFactory *This = (ClassFactory *)iface;
270     ULONG ret = InterlockedDecrement(&This->ref);
271
272     if (ret == 0)
273         ClassFactory_Destructor(This);
274     return ret;
275 }
276
277 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
278 {
279     ClassFactory *This = (ClassFactory *)iface;
280     HRESULT ret;
281     IUnknown *obj;
282
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;
289 }
290
291 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
292 {
293     if(fLock)
294         InterlockedIncrement(&TS_refCount);
295     else
296         InterlockedDecrement(&TS_refCount);
297
298     return S_OK;
299 }
300
301 static const IClassFactoryVtbl ClassFactoryVtbl = {
302     /* IUnknown */
303     ClassFactory_QueryInterface,
304     ClassFactory_AddRef,
305     ClassFactory_Release,
306
307     /* IClassFactory*/
308     ClassFactory_CreateInstance,
309     ClassFactory_LockServer
310 };
311
312 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
313 {
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;
321 }
322
323 static void TextService_Destructor(TextService *This)
324 {
325     HeapFree(GetProcessHeap(),0,This);
326 }
327
328 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
329 {
330     TextService *This = (TextService *)iface;
331     *ppvOut = NULL;
332
333     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
334     {
335         *ppvOut = This;
336     }
337
338     if (*ppvOut)
339     {
340         IUnknown_AddRef(iface);
341         return S_OK;
342     }
343
344     return E_NOINTERFACE;
345 }
346
347 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
348 {
349     TextService *This = (TextService *)iface;
350     return InterlockedIncrement(&This->refCount);
351 }
352
353 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
354 {
355     TextService *This = (TextService *)iface;
356     ULONG ret;
357
358     ret = InterlockedDecrement(&This->refCount);
359     if (ret == 0)
360         TextService_Destructor(This);
361     return ret;
362 }
363
364 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
365         ITfThreadMgr *ptim, TfClientId id)
366 {
367     trace("TextService_Activate\n");
368     return S_OK;
369 }
370
371 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
372 {
373     trace("TextService_Deactivate\n");
374     return S_OK;
375 }
376
377 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
378 {
379     TextService_QueryInterface,
380     TextService_AddRef,
381     TextService_Release,
382
383     TextService_Activate,
384     TextService_Deactivate
385 };
386
387 HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
388 {
389     TextService *This;
390     if (pUnkOuter)
391         return CLASS_E_NOAGGREGATION;
392
393     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
394     if (This == NULL)
395         return E_OUTOFMEMORY;
396
397     This->TextInputProcessorVtbl= &TextService_TextInputProcessorVtbl;
398     This->refCount = 1;
399
400     *ppOut = (IUnknown *)This;
401     return S_OK;
402 }
403
404 HRESULT RegisterTextService(REFCLSID rclsid)
405 {
406     ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
407     return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
408 }
409
410 HRESULT UnregisterTextService()
411 {
412     return CoRevokeClassObject(regid);
413 }