2096bff539f8b4a4ae22200db4e5390ba956d430
[reactos.git] / rostests / winetests / msctf / 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 #define CONST_VTABLE
25 #include "wine/test.h"
26 #include "winuser.h"
27 #include "initguid.h"
28 #include "shlwapi.h"
29 #include "shlguid.h"
30 #include "comcat.h"
31 #include "msctf.h"
32 #include "olectl.h"
33
34 static ITfInputProcessorProfiles* g_ipp;
35 static LANGID gLangid;
36 static ITfCategoryMgr * g_cm = NULL;
37 static ITfThreadMgr* g_tm = NULL;
38 static ITfDocumentMgr *g_dm = NULL;
39 static TfClientId cid = 0;
40 static TfClientId tid = 0;
41
42 static ITextStoreACPSink *ACPSink;
43
44 #define SINK_UNEXPECTED 0
45 #define SINK_EXPECTED 1
46 #define SINK_FIRED 2
47 #define SINK_IGNORE 3
48 #define SINK_OPTIONAL 4
49 #define SINK_SAVE 5
50
51 #define SINK_ACTION_MASK 0xff
52 #define SINK_OPTION_MASK 0xff00
53 #define SINK_EXPECTED_COUNT_MASK 0xff0000
54
55 #define SINK_OPTION_TODO 0x0100
56
57 #define FOCUS_IGNORE (ITfDocumentMgr*)0xffffffff
58 #define FOCUS_SAVE (ITfDocumentMgr*)0xfffffffe
59
60 static BOOL test_ShouldActivate = FALSE;
61 static BOOL test_ShouldDeactivate = FALSE;
62
63 static DWORD tmSinkCookie;
64 static DWORD tmSinkRefCount;
65 static DWORD dmSinkCookie;
66 static DWORD documentStatus;
67 static ITfDocumentMgr *test_CurrentFocus = NULL;
68 static ITfDocumentMgr *test_PrevFocus = NULL;
69 static ITfDocumentMgr *test_LastCurrentFocus = FOCUS_SAVE;
70 static ITfDocumentMgr *test_FirstPrevFocus = FOCUS_SAVE;
71 static INT test_OnSetFocus = SINK_UNEXPECTED;
72 static INT test_OnInitDocumentMgr = SINK_UNEXPECTED;
73 static INT test_OnPushContext = SINK_UNEXPECTED;
74 static INT test_OnPopContext = SINK_UNEXPECTED;
75 static INT test_KEV_OnSetFocus = SINK_UNEXPECTED;
76 static INT test_ACP_AdviseSink = SINK_UNEXPECTED;
77 static INT test_ACP_UnadviseSink = SINK_UNEXPECTED;
78 static INT test_ACP_GetStatus = SINK_UNEXPECTED;
79 static INT test_ACP_RequestLock = SINK_UNEXPECTED;
80 static INT test_ACP_GetEndACP = SINK_UNEXPECTED;
81 static INT test_ACP_GetSelection = SINK_UNEXPECTED;
82 static INT test_DoEditSession = SINK_UNEXPECTED;
83 static INT test_ACP_InsertTextAtSelection = SINK_UNEXPECTED;
84 static INT test_ACP_SetSelection = SINK_UNEXPECTED;
85 static INT test_OnEndEdit = SINK_UNEXPECTED;
86
87
88 static inline int expected_count(int *sink)
89 {
90 return (*sink & SINK_EXPECTED_COUNT_MASK)>>16;
91 }
92
93 static inline void _sink_fire_ok(INT *sink, const CHAR* name)
94 {
95 int count;
96 int todo = *sink & SINK_OPTION_TODO;
97 int action = *sink & SINK_ACTION_MASK;
98
99 if (winetest_interactive)
100 winetest_trace("firing %s\n",name);
101
102 switch (action)
103 {
104 case SINK_OPTIONAL:
105 case SINK_EXPECTED:
106 count = expected_count(sink);
107 if (count > 1)
108 {
109 count --;
110 *sink = (*sink & ~SINK_EXPECTED_COUNT_MASK) + (count << 16);
111 return;
112 }
113 break;
114 case SINK_IGNORE:
115 winetest_trace("Ignoring %s\n",name);
116 return;
117 case SINK_SAVE:
118 count = expected_count(sink) + 1;
119 *sink = (*sink & ~SINK_EXPECTED_COUNT_MASK) + (count << 16);
120 return;
121 default:
122 todo_wine_if (todo)
123 winetest_ok(0, "Unexpected %s sink\n",name);
124 }
125 *sink = SINK_FIRED;
126 }
127
128 #define sink_fire_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_fire_ok(a,b)
129
130 static inline void _sink_check_ok(INT *sink, const CHAR* name)
131 {
132 int action = *sink & SINK_ACTION_MASK;
133 int todo = *sink & SINK_OPTION_TODO;
134 int count = expected_count(sink);
135
136 switch (action)
137 {
138 case SINK_OPTIONAL:
139 if (winetest_interactive)
140 winetest_trace("optional sink %s not fired\n",name);
141 case SINK_FIRED:
142 break;
143 case SINK_IGNORE:
144 return;
145 case SINK_SAVE:
146 if (count == 0 && winetest_interactive)
147 winetest_trace("optional sink %s not fired\n",name);
148 break;
149 default:
150 todo_wine_if (todo)
151 winetest_ok(0, "%s not fired as expected, in state %x\n",name,*sink);
152 }
153 *sink = SINK_UNEXPECTED;
154 }
155
156 #define sink_check_ok(a,b) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_check_ok(a,b)
157
158 static inline void _sink_check_saved(INT *sink, ITfDocumentMgr *PrevFocus, ITfDocumentMgr *CurrentFocus, const CHAR* name)
159 {
160 int count = expected_count(sink);
161 _sink_check_ok(sink, name);
162 if (PrevFocus != FOCUS_IGNORE && count != 0)
163 winetest_ok(PrevFocus == test_FirstPrevFocus, "%s expected prev focus %p got %p\n", name, PrevFocus, test_FirstPrevFocus);
164 if (CurrentFocus != FOCUS_IGNORE && count != 0)
165 winetest_ok(CurrentFocus == test_LastCurrentFocus, "%s expected current focus %p got %p\n", name, CurrentFocus, test_LastCurrentFocus);
166 test_FirstPrevFocus = FOCUS_SAVE;
167 test_LastCurrentFocus = FOCUS_SAVE;
168 }
169
170 #define sink_check_saved(s,p,c,n) (winetest_set_location(__FILE__,__LINE__), 0) ? 0 : _sink_check_saved(s,p,c,n)
171
172 /**********************************************************************
173 * ITextStoreACP
174 **********************************************************************/
175 typedef struct tagTextStoreACP
176 {
177 ITextStoreACP ITextStoreACP_iface;
178 LONG refCount;
179
180 } TextStoreACP;
181
182 static inline TextStoreACP *impl_from_ITextStoreACP(ITextStoreACP *iface)
183 {
184 return CONTAINING_RECORD(iface, TextStoreACP, ITextStoreACP_iface);
185 }
186
187 static void TextStoreACP_Destructor(TextStoreACP *This)
188 {
189 HeapFree(GetProcessHeap(),0,This);
190 }
191
192 static HRESULT WINAPI TextStoreACP_QueryInterface(ITextStoreACP *iface, REFIID iid, LPVOID *ppvOut)
193 {
194 TextStoreACP *This = impl_from_ITextStoreACP(iface);
195 *ppvOut = NULL;
196
197 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACP))
198 {
199 *ppvOut = This;
200 }
201
202 if (*ppvOut)
203 {
204 ITextStoreACP_AddRef(iface);
205 return S_OK;
206 }
207
208 return E_NOINTERFACE;
209 }
210
211 static ULONG WINAPI TextStoreACP_AddRef(ITextStoreACP *iface)
212 {
213 TextStoreACP *This = impl_from_ITextStoreACP(iface);
214 return InterlockedIncrement(&This->refCount);
215 }
216
217 static ULONG WINAPI TextStoreACP_Release(ITextStoreACP *iface)
218 {
219 TextStoreACP *This = impl_from_ITextStoreACP(iface);
220 ULONG ret;
221
222 ret = InterlockedDecrement(&This->refCount);
223 if (ret == 0)
224 TextStoreACP_Destructor(This);
225 return ret;
226 }
227
228 static HRESULT WINAPI TextStoreACP_AdviseSink(ITextStoreACP *iface,
229 REFIID riid, IUnknown *punk, DWORD dwMask)
230 {
231 ITextStoreACPServices *services;
232 HRESULT hr;
233
234 sink_fire_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
235
236 if(ACPSink)
237 return S_OK;
238
239 hr = IUnknown_QueryInterface(punk, &IID_ITextStoreACPSink, (void**)&ACPSink);
240 ok(SUCCEEDED(hr),"Unable to QueryInterface on sink\n");
241
242 hr = ITextStoreACPSink_QueryInterface(ACPSink, &IID_ITextStoreACPServices, (void**)&services);
243 ok(hr == S_OK, "got 0x%08x\n", hr);
244 ITextStoreACPServices_Release(services);
245
246 return S_OK;
247 }
248
249 static HRESULT WINAPI TextStoreACP_UnadviseSink(ITextStoreACP *iface,
250 IUnknown *punk)
251 {
252 sink_fire_ok(&test_ACP_UnadviseSink,"TextStoreACP_UnadviseSink");
253 return S_OK;
254 }
255
256 static HRESULT WINAPI TextStoreACP_RequestLock(ITextStoreACP *iface,
257 DWORD dwLockFlags, HRESULT *phrSession)
258 {
259 sink_fire_ok(&test_ACP_RequestLock,"TextStoreACP_RequestLock");
260 *phrSession = ITextStoreACPSink_OnLockGranted(ACPSink, dwLockFlags);
261 return S_OK;
262 }
263 static HRESULT WINAPI TextStoreACP_GetStatus(ITextStoreACP *iface,
264 TS_STATUS *pdcs)
265 {
266 sink_fire_ok(&test_ACP_GetStatus,"TextStoreACP_GetStatus");
267 pdcs->dwDynamicFlags = documentStatus;
268 return S_OK;
269 }
270 static HRESULT WINAPI TextStoreACP_QueryInsert(ITextStoreACP *iface,
271 LONG acpTestStart, LONG acpTestEnd, ULONG cch, LONG *pacpResultStart,
272 LONG *pacpResultEnd)
273 {
274 trace("\n");
275 return S_OK;
276 }
277 static HRESULT WINAPI TextStoreACP_GetSelection(ITextStoreACP *iface,
278 ULONG ulIndex, ULONG ulCount, TS_SELECTION_ACP *pSelection, ULONG *pcFetched)
279 {
280 sink_fire_ok(&test_ACP_GetSelection,"TextStoreACP_GetSelection");
281
282 pSelection->acpStart = 10;
283 pSelection->acpEnd = 20;
284 pSelection->style.fInterimChar = 0;
285 pSelection->style.ase = TS_AE_NONE;
286 *pcFetched = 1;
287
288 return S_OK;
289 }
290 static HRESULT WINAPI TextStoreACP_SetSelection(ITextStoreACP *iface,
291 ULONG ulCount, const TS_SELECTION_ACP *pSelection)
292 {
293 sink_fire_ok(&test_ACP_SetSelection,"TextStoreACP_SetSelection");
294 return S_OK;
295 }
296 static HRESULT WINAPI TextStoreACP_GetText(ITextStoreACP *iface,
297 LONG acpStart, LONG acpEnd, WCHAR *pchPlain, ULONG cchPlainReq,
298 ULONG *pcchPlainRet, TS_RUNINFO *prgRunInfo, ULONG cRunInfoReq,
299 ULONG *pcRunInfoRet, LONG *pacpNext)
300 {
301 trace("\n");
302 return S_OK;
303 }
304 static HRESULT WINAPI TextStoreACP_SetText(ITextStoreACP *iface,
305 DWORD dwFlags, LONG acpStart, LONG acpEnd, const WCHAR *pchText,
306 ULONG cch, TS_TEXTCHANGE *pChange)
307 {
308 trace("\n");
309 return S_OK;
310 }
311 static HRESULT WINAPI TextStoreACP_GetFormattedText(ITextStoreACP *iface,
312 LONG acpStart, LONG acpEnd, IDataObject **ppDataObject)
313 {
314 trace("\n");
315 return S_OK;
316 }
317 static HRESULT WINAPI TextStoreACP_GetEmbedded(ITextStoreACP *iface,
318 LONG acpPos, REFGUID rguidService, REFIID riid, IUnknown **ppunk)
319 {
320 trace("\n");
321 return S_OK;
322 }
323 static HRESULT WINAPI TextStoreACP_QueryInsertEmbedded(ITextStoreACP *iface,
324 const GUID *pguidService, const FORMATETC *pFormatEtc, BOOL *pfInsertable)
325 {
326 trace("\n");
327 return S_OK;
328 }
329 static HRESULT WINAPI TextStoreACP_InsertEmbedded(ITextStoreACP *iface,
330 DWORD dwFlags, LONG acpStart, LONG acpEnd, IDataObject *pDataObject,
331 TS_TEXTCHANGE *pChange)
332 {
333 trace("\n");
334 return S_OK;
335 }
336 static HRESULT WINAPI TextStoreACP_InsertTextAtSelection(ITextStoreACP *iface,
337 DWORD dwFlags, const WCHAR *pchText, ULONG cch, LONG *pacpStart,
338 LONG *pacpEnd, TS_TEXTCHANGE *pChange)
339 {
340 sink_fire_ok(&test_ACP_InsertTextAtSelection,"TextStoreACP_InsertTextAtSelection");
341 return S_OK;
342 }
343 static HRESULT WINAPI TextStoreACP_InsertEmbeddedAtSelection(ITextStoreACP *iface,
344 DWORD dwFlags, IDataObject *pDataObject, LONG *pacpStart, LONG *pacpEnd,
345 TS_TEXTCHANGE *pChange)
346 {
347 trace("\n");
348 return S_OK;
349 }
350 static HRESULT WINAPI TextStoreACP_RequestSupportedAttrs(ITextStoreACP *iface,
351 DWORD dwFlags, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs)
352 {
353 trace("\n");
354 return S_OK;
355 }
356 static HRESULT WINAPI TextStoreACP_RequestAttrsAtPosition(ITextStoreACP *iface,
357 LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
358 DWORD dwFlags)
359 {
360 trace("\n");
361 return S_OK;
362 }
363 static HRESULT WINAPI TextStoreACP_RequestAttrsTransitioningAtPosition(ITextStoreACP *iface,
364 LONG acpPos, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
365 DWORD dwFlags)
366 {
367 trace("\n");
368 return S_OK;
369 }
370 static HRESULT WINAPI TextStoreACP_FindNextAttrTransition(ITextStoreACP *iface,
371 LONG acpStart, LONG acpHalt, ULONG cFilterAttrs, const TS_ATTRID *paFilterAttrs,
372 DWORD dwFlags, LONG *pacpNext, BOOL *pfFound, LONG *plFoundOffset)
373 {
374 trace("\n");
375 return S_OK;
376 }
377 static HRESULT WINAPI TextStoreACP_RetrieveRequestedAttrs(ITextStoreACP *iface,
378 ULONG ulCount, TS_ATTRVAL *paAttrVals, ULONG *pcFetched)
379 {
380 trace("\n");
381 return S_OK;
382 }
383 static HRESULT WINAPI TextStoreACP_GetEndACP(ITextStoreACP *iface,
384 LONG *pacp)
385 {
386 sink_fire_ok(&test_ACP_GetEndACP,"TextStoreACP_GetEndACP");
387 return S_OK;
388 }
389 static HRESULT WINAPI TextStoreACP_GetActiveView(ITextStoreACP *iface,
390 TsViewCookie *pvcView)
391 {
392 trace("\n");
393 return S_OK;
394 }
395 static HRESULT WINAPI TextStoreACP_GetACPFromPoint(ITextStoreACP *iface,
396 TsViewCookie vcView, const POINT *ptScreen, DWORD dwFlags,
397 LONG *pacp)
398 {
399 trace("\n");
400 return S_OK;
401 }
402 static HRESULT WINAPI TextStoreACP_GetTextExt(ITextStoreACP *iface,
403 TsViewCookie vcView, LONG acpStart, LONG acpEnd, RECT *prc,
404 BOOL *pfClipped)
405 {
406 trace("\n");
407 return S_OK;
408 }
409 static HRESULT WINAPI TextStoreACP_GetScreenExt(ITextStoreACP *iface,
410 TsViewCookie vcView, RECT *prc)
411 {
412 trace("\n");
413 return S_OK;
414 }
415 static HRESULT WINAPI TextStoreACP_GetWnd(ITextStoreACP *iface,
416 TsViewCookie vcView, HWND *phwnd)
417 {
418 trace("\n");
419 return S_OK;
420 }
421
422 static const ITextStoreACPVtbl TextStoreACP_TextStoreACPVtbl =
423 {
424 TextStoreACP_QueryInterface,
425 TextStoreACP_AddRef,
426 TextStoreACP_Release,
427
428 TextStoreACP_AdviseSink,
429 TextStoreACP_UnadviseSink,
430 TextStoreACP_RequestLock,
431 TextStoreACP_GetStatus,
432 TextStoreACP_QueryInsert,
433 TextStoreACP_GetSelection,
434 TextStoreACP_SetSelection,
435 TextStoreACP_GetText,
436 TextStoreACP_SetText,
437 TextStoreACP_GetFormattedText,
438 TextStoreACP_GetEmbedded,
439 TextStoreACP_QueryInsertEmbedded,
440 TextStoreACP_InsertEmbedded,
441 TextStoreACP_InsertTextAtSelection,
442 TextStoreACP_InsertEmbeddedAtSelection,
443 TextStoreACP_RequestSupportedAttrs,
444 TextStoreACP_RequestAttrsAtPosition,
445 TextStoreACP_RequestAttrsTransitioningAtPosition,
446 TextStoreACP_FindNextAttrTransition,
447 TextStoreACP_RetrieveRequestedAttrs,
448 TextStoreACP_GetEndACP,
449 TextStoreACP_GetActiveView,
450 TextStoreACP_GetACPFromPoint,
451 TextStoreACP_GetTextExt,
452 TextStoreACP_GetScreenExt,
453 TextStoreACP_GetWnd
454 };
455
456 static HRESULT TextStoreACP_Constructor(IUnknown **ppOut)
457 {
458 TextStoreACP *This;
459
460 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACP));
461 if (This == NULL)
462 return E_OUTOFMEMORY;
463
464 This->ITextStoreACP_iface.lpVtbl = &TextStoreACP_TextStoreACPVtbl;
465 This->refCount = 1;
466
467 *ppOut = (IUnknown *)This;
468 return S_OK;
469 }
470
471 /**********************************************************************
472 * ITfThreadMgrEventSink
473 **********************************************************************/
474 typedef struct tagThreadMgrEventSink
475 {
476 ITfThreadMgrEventSink ITfThreadMgrEventSink_iface;
477 LONG refCount;
478 } ThreadMgrEventSink;
479
480 static inline ThreadMgrEventSink *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
481 {
482 return CONTAINING_RECORD(iface, ThreadMgrEventSink, ITfThreadMgrEventSink_iface);
483 }
484
485 static void ThreadMgrEventSink_Destructor(ThreadMgrEventSink *This)
486 {
487 HeapFree(GetProcessHeap(),0,This);
488 }
489
490 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
491 {
492 ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
493 *ppvOut = NULL;
494
495 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgrEventSink))
496 {
497 *ppvOut = This;
498 }
499
500 if (*ppvOut)
501 {
502 ITfThreadMgrEventSink_AddRef(iface);
503 return S_OK;
504 }
505
506 return E_NOINTERFACE;
507 }
508
509 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
510 {
511 ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
512 ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
513 return InterlockedIncrement(&This->refCount);
514 }
515
516 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
517 {
518 ThreadMgrEventSink *This = impl_from_ITfThreadMgrEventSink(iface);
519 ULONG ret;
520
521 ok (tmSinkRefCount == This->refCount,"ThreadMgrEventSink refcount off %i vs %i\n",This->refCount,tmSinkRefCount);
522 ret = InterlockedDecrement(&This->refCount);
523 if (ret == 0)
524 ThreadMgrEventSink_Destructor(This);
525 return ret;
526 }
527
528 static HRESULT WINAPI ThreadMgrEventSink_OnInitDocumentMgr(ITfThreadMgrEventSink *iface,
529 ITfDocumentMgr *pdim)
530 {
531 sink_fire_ok(&test_OnInitDocumentMgr,"ThreadMgrEventSink_OnInitDocumentMgr");
532 return S_OK;
533 }
534
535 static HRESULT WINAPI ThreadMgrEventSink_OnUninitDocumentMgr(ITfThreadMgrEventSink *iface,
536 ITfDocumentMgr *pdim)
537 {
538 trace("\n");
539 return S_OK;
540 }
541
542 static HRESULT WINAPI ThreadMgrEventSink_OnSetFocus(ITfThreadMgrEventSink *iface,
543 ITfDocumentMgr *pdimFocus, ITfDocumentMgr *pdimPrevFocus)
544 {
545 sink_fire_ok(&test_OnSetFocus,"ThreadMgrEventSink_OnSetFocus");
546 if (test_CurrentFocus == FOCUS_SAVE)
547 test_LastCurrentFocus = pdimFocus;
548 else if (test_CurrentFocus != FOCUS_IGNORE)
549 ok(pdimFocus == test_CurrentFocus,"Sink reports wrong focus\n");
550 if (test_PrevFocus == FOCUS_SAVE)
551 {
552 if (test_FirstPrevFocus == FOCUS_SAVE)
553 test_FirstPrevFocus = pdimPrevFocus;
554 }
555 else if (test_PrevFocus != FOCUS_IGNORE)
556 ok(pdimPrevFocus == test_PrevFocus,"Sink reports wrong previous focus\n");
557 return S_OK;
558 }
559
560 static HRESULT WINAPI ThreadMgrEventSink_OnPushContext(ITfThreadMgrEventSink *iface,
561 ITfContext *pic)
562 {
563 HRESULT hr;
564 ITfDocumentMgr *docmgr;
565 ITfContext *test;
566
567 hr = ITfContext_GetDocumentMgr(pic,&docmgr);
568 ok(SUCCEEDED(hr),"GetDocumentMgr failed\n");
569 test = (ITfContext*)0xdeadbeef;
570 hr = ITfDocumentMgr_GetTop(docmgr,&test);
571 ok(SUCCEEDED(hr),"GetTop failed\n");
572 ITfDocumentMgr_Release(docmgr);
573 ok(test == pic, "Wrong context is on top\n");
574 if (test)
575 ITfContext_Release(test);
576
577 sink_fire_ok(&test_OnPushContext,"ThreadMgrEventSink_OnPushContext");
578 return S_OK;
579 }
580
581 static HRESULT WINAPI ThreadMgrEventSink_OnPopContext(ITfThreadMgrEventSink *iface,
582 ITfContext *pic)
583 {
584 HRESULT hr;
585 ITfDocumentMgr *docmgr;
586 ITfContext *test;
587
588 hr = ITfContext_GetDocumentMgr(pic,&docmgr);
589 ok(SUCCEEDED(hr),"GetDocumentMgr failed\n");
590 ITfDocumentMgr_Release(docmgr);
591 test = (ITfContext*)0xdeadbeef;
592 hr = ITfDocumentMgr_GetTop(docmgr,&test);
593 ok(SUCCEEDED(hr),"GetTop failed\n");
594 ok(test == pic, "Wrong context is on top\n");
595 if (test)
596 ITfContext_Release(test);
597
598 sink_fire_ok(&test_OnPopContext,"ThreadMgrEventSink_OnPopContext");
599 return S_OK;
600 }
601
602 static const ITfThreadMgrEventSinkVtbl ThreadMgrEventSink_ThreadMgrEventSinkVtbl =
603 {
604 ThreadMgrEventSink_QueryInterface,
605 ThreadMgrEventSink_AddRef,
606 ThreadMgrEventSink_Release,
607
608 ThreadMgrEventSink_OnInitDocumentMgr,
609 ThreadMgrEventSink_OnUninitDocumentMgr,
610 ThreadMgrEventSink_OnSetFocus,
611 ThreadMgrEventSink_OnPushContext,
612 ThreadMgrEventSink_OnPopContext
613 };
614
615 static HRESULT ThreadMgrEventSink_Constructor(IUnknown **ppOut)
616 {
617 ThreadMgrEventSink *This;
618
619 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgrEventSink));
620 if (This == NULL)
621 return E_OUTOFMEMORY;
622
623 This->ITfThreadMgrEventSink_iface.lpVtbl = &ThreadMgrEventSink_ThreadMgrEventSinkVtbl;
624 This->refCount = 1;
625
626 *ppOut = (IUnknown *)This;
627 return S_OK;
628 }
629
630 static HRESULT WINAPI TfTransitoryExtensionSink_QueryInterface(ITfTransitoryExtensionSink *iface, REFIID riid, void **ppv)
631 {
632 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_ITfTransitoryExtensionSink, riid)) {
633 *ppv = iface;
634 return S_OK;
635 }
636
637 *ppv = NULL;
638 return E_NOINTERFACE;
639 }
640
641 static ULONG WINAPI TfTransitoryExtensionSink_AddRef(ITfTransitoryExtensionSink *iface)
642 {
643 return 2;
644 }
645
646 static ULONG WINAPI TfTransitoryExtensionSink_Release(ITfTransitoryExtensionSink *iface)
647 {
648 return 1;
649 }
650
651 static HRESULT WINAPI TfTransitoryExtensionSink_OnTransitoryExtensionUpdated(ITfTransitoryExtensionSink *iface, ITfContext *pic,
652 TfEditCookie ecReadOnly, ITfRange *pResultRange, ITfRange *pCompositionRange, BOOL *pfDeleteResultRange)
653 {
654 ok(0, "unexpected call\n");
655 return E_NOTIMPL;
656 }
657
658 static const ITfTransitoryExtensionSinkVtbl TfTransitoryExtensionSinkVtbl = {
659 TfTransitoryExtensionSink_QueryInterface,
660 TfTransitoryExtensionSink_AddRef,
661 TfTransitoryExtensionSink_Release,
662 TfTransitoryExtensionSink_OnTransitoryExtensionUpdated
663 };
664
665 static ITfTransitoryExtensionSink TfTransitoryExtensionSink = { &TfTransitoryExtensionSinkVtbl };
666
667 /********************************************************************************************
668 * Stub text service for testing
669 ********************************************************************************************/
670
671 static LONG TS_refCount;
672 static IClassFactory *cf;
673 static DWORD regid;
674
675 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
676
677 typedef struct tagClassFactory
678 {
679 IClassFactory IClassFactory_iface;
680 LONG ref;
681 LPFNCONSTRUCTOR ctor;
682 } ClassFactory;
683
684 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
685 {
686 return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
687 }
688
689 typedef struct tagTextService
690 {
691 ITfTextInputProcessor ITfTextInputProcessor_iface;
692 LONG refCount;
693 } TextService;
694
695 static inline TextService *impl_from_ITfTextInputProcessor(ITfTextInputProcessor *iface)
696 {
697 return CONTAINING_RECORD(iface, TextService, ITfTextInputProcessor_iface);
698 }
699
700 static void ClassFactory_Destructor(ClassFactory *This)
701 {
702 HeapFree(GetProcessHeap(),0,This);
703 TS_refCount--;
704 }
705
706 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
707 {
708 *ppvOut = NULL;
709 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown))
710 {
711 IClassFactory_AddRef(iface);
712 *ppvOut = iface;
713 return S_OK;
714 }
715
716 return E_NOINTERFACE;
717 }
718
719 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
720 {
721 ClassFactory *This = impl_from_IClassFactory(iface);
722 return InterlockedIncrement(&This->ref);
723 }
724
725 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
726 {
727 ClassFactory *This = impl_from_IClassFactory(iface);
728 ULONG ret = InterlockedDecrement(&This->ref);
729
730 if (ret == 0)
731 ClassFactory_Destructor(This);
732 return ret;
733 }
734
735 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
736 {
737 ClassFactory *This = impl_from_IClassFactory(iface);
738 HRESULT ret;
739 IUnknown *obj;
740
741 ret = This->ctor(punkOuter, &obj);
742 if (FAILED(ret))
743 return ret;
744 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
745 IUnknown_Release(obj);
746 return ret;
747 }
748
749 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
750 {
751 if(fLock)
752 InterlockedIncrement(&TS_refCount);
753 else
754 InterlockedDecrement(&TS_refCount);
755
756 return S_OK;
757 }
758
759 static const IClassFactoryVtbl ClassFactoryVtbl = {
760 /* IUnknown */
761 ClassFactory_QueryInterface,
762 ClassFactory_AddRef,
763 ClassFactory_Release,
764
765 /* IClassFactory*/
766 ClassFactory_CreateInstance,
767 ClassFactory_LockServer
768 };
769
770 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
771 {
772 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
773 This->IClassFactory_iface.lpVtbl = &ClassFactoryVtbl;
774 This->ref = 1;
775 This->ctor = ctor;
776 *ppvOut = (LPVOID)This;
777 TS_refCount++;
778 return S_OK;
779 }
780
781 static void TextService_Destructor(TextService *This)
782 {
783 HeapFree(GetProcessHeap(),0,This);
784 }
785
786 static HRESULT WINAPI TextService_QueryInterface(ITfTextInputProcessor *iface, REFIID iid, LPVOID *ppvOut)
787 {
788 TextService *This = impl_from_ITfTextInputProcessor(iface);
789 *ppvOut = NULL;
790
791 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextInputProcessor))
792 {
793 *ppvOut = This;
794 }
795
796 if (*ppvOut)
797 {
798 ITfTextInputProcessor_AddRef(iface);
799 return S_OK;
800 }
801
802 return E_NOINTERFACE;
803 }
804
805 static ULONG WINAPI TextService_AddRef(ITfTextInputProcessor *iface)
806 {
807 TextService *This = impl_from_ITfTextInputProcessor(iface);
808 return InterlockedIncrement(&This->refCount);
809 }
810
811 static ULONG WINAPI TextService_Release(ITfTextInputProcessor *iface)
812 {
813 TextService *This = impl_from_ITfTextInputProcessor(iface);
814 ULONG ret;
815
816 ret = InterlockedDecrement(&This->refCount);
817 if (ret == 0)
818 TextService_Destructor(This);
819 return ret;
820 }
821
822 static HRESULT WINAPI TextService_Activate(ITfTextInputProcessor *iface,
823 ITfThreadMgr *ptim, TfClientId id)
824 {
825 trace("TextService_Activate\n");
826 ok(test_ShouldActivate,"Activation came unexpectedly\n");
827 tid = id;
828 return S_OK;
829 }
830
831 static HRESULT WINAPI TextService_Deactivate(ITfTextInputProcessor *iface)
832 {
833 trace("TextService_Deactivate\n");
834 ok(test_ShouldDeactivate,"Deactivation came unexpectedly\n");
835 return S_OK;
836 }
837
838 static const ITfTextInputProcessorVtbl TextService_TextInputProcessorVtbl=
839 {
840 TextService_QueryInterface,
841 TextService_AddRef,
842 TextService_Release,
843
844 TextService_Activate,
845 TextService_Deactivate
846 };
847
848 static HRESULT TextService_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
849 {
850 TextService *This;
851 if (pUnkOuter)
852 return CLASS_E_NOAGGREGATION;
853
854 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextService));
855 if (This == NULL)
856 return E_OUTOFMEMORY;
857
858 This->ITfTextInputProcessor_iface.lpVtbl = &TextService_TextInputProcessorVtbl;
859 This->refCount = 1;
860
861 *ppOut = (IUnknown *)This;
862 return S_OK;
863 }
864
865 static HRESULT RegisterTextService(REFCLSID rclsid)
866 {
867 ClassFactory_Constructor( TextService_Constructor ,(LPVOID*)&cf);
868 return CoRegisterClassObject(rclsid, (IUnknown*) cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regid);
869 }
870
871 static HRESULT UnregisterTextService(void)
872 {
873 return CoRevokeClassObject(regid);
874 }
875
876 /*
877 * The tests
878 */
879
880 DEFINE_GUID(CLSID_FakeService, 0xEDE1A7AD,0x66DE,0x47E0,0xB6,0x20,0x3E,0x92,0xF8,0x24,0x6B,0xF3);
881 DEFINE_GUID(CLSID_TF_InputProcessorProfiles, 0x33c53a50,0xf456,0x4884,0xb0,0x49,0x85,0xfd,0x64,0x3e,0xcf,0xed);
882 DEFINE_GUID(CLSID_TF_CategoryMgr, 0xA4B544A1,0x438D,0x4B41,0x93,0x25,0x86,0x95,0x23,0xE2,0xD6,0xC7);
883 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD, 0x34745c63,0xb2f0,0x4784,0x8b,0x67,0x5e,0x12,0xc8,0x70,0x1a,0x31);
884 DEFINE_GUID(GUID_TFCAT_TIP_SPEECH, 0xB5A73CD1,0x8355,0x426B,0xA1,0x61,0x25,0x98,0x08,0xF2,0x6B,0x14);
885 DEFINE_GUID(GUID_TFCAT_TIP_HANDWRITING, 0x246ecb87,0xc2f2,0x4abe,0x90,0x5b,0xc8,0xb3,0x8a,0xdd,0x2c,0x43);
886 DEFINE_GUID (GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, 0x046B8C80,0x1647,0x40F7,0x9B,0x21,0xB9,0x3B,0x81,0xAA,0xBC,0x1B);
887 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
888 DEFINE_GUID(CLSID_TF_ThreadMgr, 0x529a9e6b,0x6587,0x4f23,0xab,0x9e,0x9c,0x7d,0x68,0x3e,0x3c,0x50);
889 DEFINE_GUID(CLSID_PreservedKey, 0xA0ED8E55,0xCD3B,0x4274,0xB2,0x95,0xF6,0xC9,0xBA,0x2B,0x84,0x72);
890 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_DISABLED, 0x71a5b253,0x1951,0x466b,0x9f,0xbc,0x9c,0x88,0x08,0xfa,0x84,0xf2);
891 DEFINE_GUID(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, 0x58273aad,0x01bb,0x4164,0x95,0xc6,0x75,0x5b,0xa0,0xb5,0x16,0x2d);
892 DEFINE_GUID(GUID_COMPARTMENT_HANDWRITING_OPENCLOSE, 0xf9ae2c6b,0x1866,0x4361,0xaf,0x72,0x7a,0xa3,0x09,0x48,0x89,0x0e);
893 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_DISABLED, 0x56c5c607,0x0703,0x4e59,0x8e,0x52,0xcb,0xc8,0x4e,0x8b,0xbe,0x35);
894 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_OPENCLOSE, 0x544d6a63,0xe2e8,0x4752,0xbb,0xd1,0x00,0x09,0x60,0xbc,0xa0,0x83);
895 DEFINE_GUID(GUID_COMPARTMENT_SPEECH_GLOBALSTATE, 0x2a54fe8e,0x0d08,0x460c,0xa7,0x5d,0x87,0x03,0x5f,0xf4,0x36,0xc5);
896 DEFINE_GUID(GUID_COMPARTMENT_PERSISTMENUENABLED, 0x575f3783,0x70c8,0x47c8,0xae,0x5d,0x91,0xa0,0x1a,0x1f,0x75,0x92);
897 DEFINE_GUID(GUID_COMPARTMENT_EMPTYCONTEXT, 0xd7487dbf,0x804e,0x41c5,0x89,0x4d,0xad,0x96,0xfd,0x4e,0xea,0x13);
898 DEFINE_GUID(GUID_COMPARTMENT_TIPUISTATUS, 0x148ca3ec,0x0366,0x401c,0x8d,0x75,0xed,0x97,0x8d,0x85,0xfb,0xc9);
899
900 static HRESULT initialize(void)
901 {
902 HRESULT hr;
903 CoInitialize(NULL);
904 hr = CoCreateInstance (&CLSID_TF_InputProcessorProfiles, NULL,
905 CLSCTX_INPROC_SERVER, &IID_ITfInputProcessorProfiles, (void**)&g_ipp);
906 if (SUCCEEDED(hr))
907 hr = CoCreateInstance (&CLSID_TF_CategoryMgr, NULL,
908 CLSCTX_INPROC_SERVER, &IID_ITfCategoryMgr, (void**)&g_cm);
909 if (SUCCEEDED(hr))
910 hr = CoCreateInstance (&CLSID_TF_ThreadMgr, NULL,
911 CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (void**)&g_tm);
912 return hr;
913 }
914
915 static void cleanup(void)
916 {
917 if (g_ipp)
918 ITfInputProcessorProfiles_Release(g_ipp);
919 if (g_cm)
920 ITfCategoryMgr_Release(g_cm);
921 if (g_tm)
922 ITfThreadMgr_Release(g_tm);
923 CoUninitialize();
924 }
925
926 static void test_Register(void)
927 {
928 HRESULT hr;
929
930 static const WCHAR szDesc[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',0};
931 static const WCHAR szFile[] = {'F','a','k','e',' ','W','i','n','e',' ','S','e','r','v','i','c','e',' ','F','i','l','e',0};
932
933 hr = ITfInputProcessorProfiles_GetCurrentLanguage(g_ipp,&gLangid);
934 ok(SUCCEEDED(hr),"Unable to get current language id\n");
935 trace("Current Language %x\n",gLangid);
936
937 hr = RegisterTextService(&CLSID_FakeService);
938 ok(SUCCEEDED(hr),"Unable to register COM for TextService\n");
939 hr = ITfInputProcessorProfiles_Register(g_ipp, &CLSID_FakeService);
940 ok(SUCCEEDED(hr),"Unable to register text service(%x)\n",hr);
941 hr = ITfInputProcessorProfiles_AddLanguageProfile(g_ipp, &CLSID_FakeService, gLangid, &CLSID_FakeService, szDesc, sizeof(szDesc)/sizeof(WCHAR), szFile, sizeof(szFile)/sizeof(WCHAR), 1);
942 ok(SUCCEEDED(hr),"Unable to add Language Profile (%x)\n",hr);
943 }
944
945 static void test_Unregister(void)
946 {
947 HRESULT hr;
948 hr = ITfInputProcessorProfiles_Unregister(g_ipp, &CLSID_FakeService);
949 ok(SUCCEEDED(hr),"Unable to unregister text service(%x)\n",hr);
950 UnregisterTextService();
951 }
952
953 static void test_EnumInputProcessorInfo(void)
954 {
955 IEnumGUID *ppEnum;
956 BOOL found = FALSE;
957
958 if (SUCCEEDED(ITfInputProcessorProfiles_EnumInputProcessorInfo(g_ipp, &ppEnum)))
959 {
960 ULONG fetched;
961 GUID g;
962 while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
963 {
964 if(IsEqualGUID(&g,&CLSID_FakeService))
965 found = TRUE;
966 }
967 IEnumGUID_Release(ppEnum);
968 }
969 ok(found,"Did not find registered text service\n");
970 }
971
972 static void test_EnumLanguageProfiles(void)
973 {
974 BOOL found = FALSE;
975 IEnumTfLanguageProfiles *ppEnum;
976 HRESULT hr;
977
978 hr = ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp, gLangid, NULL);
979 ok(hr == E_INVALIDARG, "EnumLanguageProfiles failed: %x\n", hr);
980
981 if (SUCCEEDED(ITfInputProcessorProfiles_EnumLanguageProfiles(g_ipp,gLangid,&ppEnum)))
982 {
983 TF_LANGUAGEPROFILE profile;
984 while (IEnumTfLanguageProfiles_Next(ppEnum,1,&profile,NULL)==S_OK)
985 {
986 if (IsEqualGUID(&profile.clsid,&CLSID_FakeService))
987 {
988 found = TRUE;
989 ok(profile.langid == gLangid, "LangId Incorrect\n");
990 ok(IsEqualGUID(&profile.catid,&GUID_TFCAT_TIP_KEYBOARD) ||
991 broken(IsEqualGUID(&profile.catid,&GUID_NULL) /* Win8 */), "CatId Incorrect\n");
992 ok(IsEqualGUID(&profile.guidProfile,&CLSID_FakeService), "guidProfile Incorrect\n");
993 }
994 }
995 IEnumTfLanguageProfiles_Release(ppEnum);
996 }
997 ok(found,"Registered text service not found\n");
998 }
999
1000 static void test_RegisterCategory(void)
1001 {
1002 HRESULT hr;
1003 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
1004 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
1005 hr = ITfCategoryMgr_RegisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
1006 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterCategory failed\n");
1007 }
1008
1009 static void test_UnregisterCategory(void)
1010 {
1011 HRESULT hr;
1012 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_TIP_KEYBOARD, &CLSID_FakeService);
1013 ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
1014 hr = ITfCategoryMgr_UnregisterCategory(g_cm, &CLSID_FakeService, &GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER, &CLSID_FakeService);
1015 ok(SUCCEEDED(hr),"ITfCategoryMgr_UnregisterCategory failed\n");
1016 }
1017
1018 static void test_FindClosestCategory(void)
1019 {
1020 GUID output;
1021 HRESULT hr;
1022 const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
1023
1024 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, NULL, 0);
1025 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
1026 ok(IsEqualGUID(&output,&GUID_TFCAT_DISPLAYATTRIBUTEPROVIDER),"Wrong GUID\n");
1027
1028 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 1);
1029 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
1030 ok(IsEqualGUID(&output,&GUID_NULL),"Wrong GUID\n");
1031
1032 hr = ITfCategoryMgr_FindClosestCategory(g_cm, &CLSID_FakeService, &output, list, 3);
1033 ok(SUCCEEDED(hr),"ITfCategoryMgr_FindClosestCategory failed (%x)\n",hr);
1034 ok(IsEqualGUID(&output,&GUID_TFCAT_TIP_KEYBOARD),"Wrong GUID\n");
1035 }
1036
1037 static void test_Enable(void)
1038 {
1039 HRESULT hr;
1040 BOOL enabled = FALSE;
1041
1042 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, TRUE);
1043 ok(SUCCEEDED(hr),"Failed to enable text service\n");
1044 hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, &enabled);
1045 ok(SUCCEEDED(hr),"Failed to get enabled state\n");
1046 ok(enabled == TRUE,"enabled state incorrect\n");
1047 }
1048
1049 static void test_Disable(void)
1050 {
1051 HRESULT hr;
1052
1053 trace("Disabling\n");
1054 hr = ITfInputProcessorProfiles_EnableLanguageProfile(g_ipp,&CLSID_FakeService, gLangid, &CLSID_FakeService, FALSE);
1055 ok(SUCCEEDED(hr),"Failed to disable text service\n");
1056 }
1057
1058 static void test_ThreadMgrAdviseSinks(void)
1059 {
1060 ITfSource *source = NULL;
1061 HRESULT hr;
1062 IUnknown *sink;
1063
1064 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
1065 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
1066 if (!source)
1067 return;
1068
1069 hr = ThreadMgrEventSink_Constructor(&sink);
1070 ok(hr == S_OK, "got %08x\n", hr);
1071 if(FAILED(hr)) return;
1072
1073 tmSinkRefCount = 1;
1074 tmSinkCookie = 0;
1075 hr = ITfSource_AdviseSink(source,&IID_ITfThreadMgrEventSink, sink, &tmSinkCookie);
1076 ok(SUCCEEDED(hr),"Failed to Advise Sink\n");
1077 ok(tmSinkCookie!=0,"Failed to get sink cookie\n");
1078
1079 /* Advising the sink adds a ref, Releasing here lets the object be deleted
1080 when unadvised */
1081 tmSinkRefCount = 2;
1082 IUnknown_Release(sink);
1083 ITfSource_Release(source);
1084 }
1085
1086 static void test_ThreadMgrUnadviseSinks(void)
1087 {
1088 ITfSource *source = NULL;
1089 HRESULT hr;
1090
1091 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfSource, (LPVOID*)&source);
1092 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for ThreadMgr\n");
1093 if (!source)
1094 return;
1095
1096 tmSinkRefCount = 1;
1097 hr = ITfSource_UnadviseSink(source, tmSinkCookie);
1098 ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
1099 ITfSource_Release(source);
1100 }
1101
1102 static void test_DocumentMgrAdviseSinks(void)
1103 {
1104 ITfSource *source;
1105 HRESULT hr;
1106
1107 hr = ITfDocumentMgr_QueryInterface(g_dm, &IID_ITfSource, (void**)&source);
1108 ok(hr == S_OK,"Failed to get IID_ITfSource for DocumentMgr\n");
1109
1110 dmSinkCookie = 0;
1111 hr = ITfSource_AdviseSink(source, &IID_ITfTransitoryExtensionSink, (IUnknown*)&TfTransitoryExtensionSink, &dmSinkCookie);
1112 ok(hr == S_OK,"Failed to Advise Sink\n");
1113
1114 ITfSource_Release(source);
1115 }
1116
1117 static void test_DocumentMgrUnadviseSinks(void)
1118 {
1119 ITfSource *source = NULL;
1120 HRESULT hr;
1121
1122 hr = ITfDocumentMgr_QueryInterface(g_dm, &IID_ITfSource, (void**)&source);
1123 ok(hr == S_OK,"Failed to get IID_ITfSource for DocumentMgr\n");
1124
1125 hr = ITfSource_UnadviseSink(source, dmSinkCookie);
1126 ok(hr == S_OK,"Failed to unadvise Sink\n");
1127 ITfSource_Release(source);
1128 }
1129
1130 /**********************************************************************
1131 * ITfKeyEventSink
1132 **********************************************************************/
1133 typedef struct tagKeyEventSink
1134 {
1135 ITfKeyEventSink ITfKeyEventSink_iface;
1136 LONG refCount;
1137 } KeyEventSink;
1138
1139 static inline KeyEventSink *impl_from_ITfKeyEventSink(ITfKeyEventSink *iface)
1140 {
1141 return CONTAINING_RECORD(iface, KeyEventSink, ITfKeyEventSink_iface);
1142 }
1143
1144 static void KeyEventSink_Destructor(KeyEventSink *This)
1145 {
1146 HeapFree(GetProcessHeap(),0,This);
1147 }
1148
1149 static HRESULT WINAPI KeyEventSink_QueryInterface(ITfKeyEventSink *iface, REFIID iid, LPVOID *ppvOut)
1150 {
1151 KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1152 *ppvOut = NULL;
1153
1154 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfKeyEventSink))
1155 {
1156 *ppvOut = This;
1157 }
1158
1159 if (*ppvOut)
1160 {
1161 ITfKeyEventSink_AddRef(iface);
1162 return S_OK;
1163 }
1164
1165 return E_NOINTERFACE;
1166 }
1167
1168 static ULONG WINAPI KeyEventSink_AddRef(ITfKeyEventSink *iface)
1169 {
1170 KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1171 return InterlockedIncrement(&This->refCount);
1172 }
1173
1174 static ULONG WINAPI KeyEventSink_Release(ITfKeyEventSink *iface)
1175 {
1176 KeyEventSink *This = impl_from_ITfKeyEventSink(iface);
1177 ULONG ret;
1178
1179 ret = InterlockedDecrement(&This->refCount);
1180 if (ret == 0)
1181 KeyEventSink_Destructor(This);
1182 return ret;
1183 }
1184
1185 static HRESULT WINAPI KeyEventSink_OnSetFocus(ITfKeyEventSink *iface,
1186 BOOL fForeground)
1187 {
1188 sink_fire_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1189 return S_OK;
1190 }
1191
1192 static HRESULT WINAPI KeyEventSink_OnTestKeyDown(ITfKeyEventSink *iface,
1193 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1194 {
1195 trace("\n");
1196 return S_OK;
1197 }
1198
1199 static HRESULT WINAPI KeyEventSink_OnTestKeyUp(ITfKeyEventSink *iface,
1200 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1201 {
1202 trace("\n");
1203 return S_OK;
1204 }
1205
1206 static HRESULT WINAPI KeyEventSink_OnKeyDown(ITfKeyEventSink *iface,
1207 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1208 {
1209 trace("\n");
1210 return S_OK;
1211 }
1212
1213 static HRESULT WINAPI KeyEventSink_OnKeyUp(ITfKeyEventSink *iface,
1214 ITfContext *pic, WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
1215 {
1216 trace("\n");
1217 return S_OK;
1218 }
1219
1220 static HRESULT WINAPI KeyEventSink_OnPreservedKey(ITfKeyEventSink *iface,
1221 ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
1222 {
1223 trace("\n");
1224 return S_OK;
1225 }
1226
1227 static const ITfKeyEventSinkVtbl KeyEventSink_KeyEventSinkVtbl =
1228 {
1229 KeyEventSink_QueryInterface,
1230 KeyEventSink_AddRef,
1231 KeyEventSink_Release,
1232
1233 KeyEventSink_OnSetFocus,
1234 KeyEventSink_OnTestKeyDown,
1235 KeyEventSink_OnTestKeyUp,
1236 KeyEventSink_OnKeyDown,
1237 KeyEventSink_OnKeyUp,
1238 KeyEventSink_OnPreservedKey
1239 };
1240
1241 static HRESULT KeyEventSink_Constructor(ITfKeyEventSink **ppOut)
1242 {
1243 KeyEventSink *This;
1244
1245 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(KeyEventSink));
1246 if (This == NULL)
1247 return E_OUTOFMEMORY;
1248
1249 This->ITfKeyEventSink_iface.lpVtbl = &KeyEventSink_KeyEventSinkVtbl;
1250 This->refCount = 1;
1251
1252 *ppOut = &This->ITfKeyEventSink_iface;
1253 return S_OK;
1254 }
1255
1256
1257 static void test_KeystrokeMgr(void)
1258 {
1259 ITfKeystrokeMgr *keymgr= NULL;
1260 HRESULT hr;
1261 TF_PRESERVEDKEY tfpk;
1262 BOOL preserved;
1263 ITfKeyEventSink *sink = NULL;
1264
1265 KeyEventSink_Constructor(&sink);
1266
1267 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfKeystrokeMgr, (LPVOID*)&keymgr);
1268 ok(SUCCEEDED(hr),"Failed to get IID_ITfKeystrokeMgr for ThreadMgr\n");
1269
1270 tfpk.uVKey = 'A';
1271 tfpk.uModifiers = TF_MOD_SHIFT;
1272
1273 test_KEV_OnSetFocus = SINK_EXPECTED;
1274 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1275 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_AdviseKeyEventSink failed\n");
1276 sink_check_ok(&test_KEV_OnSetFocus,"KeyEventSink_OnSetFocus");
1277 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,tid,sink,TRUE);
1278 ok(hr == CONNECT_E_ADVISELIMIT,"Wrong return, expected CONNECT_E_ADVISELIMIT\n");
1279 hr = ITfKeystrokeMgr_AdviseKeyEventSink(keymgr,cid,sink,TRUE);
1280 ok(hr == E_INVALIDARG,"Wrong return, expected E_INVALIDARG\n");
1281
1282 hr =ITfKeystrokeMgr_PreserveKey(keymgr, 0, &CLSID_PreservedKey, &tfpk, NULL, 0);
1283 ok(hr==E_INVALIDARG,"ITfKeystrokeMgr_PreserveKey improperly succeeded\n");
1284
1285 hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1286 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_PreserveKey failed\n");
1287
1288 hr =ITfKeystrokeMgr_PreserveKey(keymgr, tid, &CLSID_PreservedKey, &tfpk, NULL, 0);
1289 ok(hr == TF_E_ALREADY_EXISTS,"ITfKeystrokeMgr_PreserveKey improperly succeeded\n");
1290
1291 preserved = FALSE;
1292 hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1293 ok(hr == S_OK, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1294 if (hr == S_OK) ok(preserved == TRUE,"misreporting preserved key\n");
1295
1296 hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1297 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnpreserveKey failed\n");
1298
1299 hr = ITfKeystrokeMgr_IsPreservedKey(keymgr, &CLSID_PreservedKey, &tfpk, &preserved);
1300 ok(hr == S_FALSE, "ITfKeystrokeMgr_IsPreservedKey failed\n");
1301 if (hr == S_FALSE) ok(preserved == FALSE,"misreporting preserved key\n");
1302
1303 hr = ITfKeystrokeMgr_UnpreserveKey(keymgr, &CLSID_PreservedKey,&tfpk);
1304 ok(hr==CONNECT_E_NOCONNECTION,"ITfKeystrokeMgr_UnpreserveKey improperly succeeded\n");
1305
1306 hr = ITfKeystrokeMgr_UnadviseKeyEventSink(keymgr,tid);
1307 ok(SUCCEEDED(hr),"ITfKeystrokeMgr_UnadviseKeyEventSink failed\n");
1308
1309 ITfKeystrokeMgr_Release(keymgr);
1310 ITfKeyEventSink_Release(sink);
1311 }
1312
1313 static void test_Activate(void)
1314 {
1315 HRESULT hr;
1316
1317 test_ShouldActivate = TRUE; /* Win7 */
1318 hr = ITfInputProcessorProfiles_ActivateLanguageProfile(g_ipp,&CLSID_FakeService,gLangid,&CLSID_FakeService);
1319 ok(SUCCEEDED(hr),"Failed to Activate text service\n");
1320 test_ShouldActivate = FALSE;
1321 }
1322
1323
1324 static void test_EnumContexts(ITfDocumentMgr *dm, ITfContext *search)
1325 {
1326 HRESULT hr;
1327 IEnumTfContexts* pEnum;
1328 BOOL found = FALSE;
1329
1330 hr = ITfDocumentMgr_EnumContexts(dm,&pEnum);
1331 ok(SUCCEEDED(hr),"EnumContexts failed\n");
1332 if (SUCCEEDED(hr))
1333 {
1334 ULONG fetched;
1335 ITfContext *cxt;
1336 while (IEnumTfContexts_Next(pEnum, 1, &cxt, &fetched) == S_OK)
1337 {
1338 if (!search)
1339 found = TRUE;
1340 else if (search == cxt)
1341 found = TRUE;
1342 ITfContext_Release(cxt);
1343 }
1344 IEnumTfContexts_Release(pEnum);
1345 }
1346 if (search)
1347 ok(found,"Did not find proper ITfContext\n");
1348 else
1349 ok(!found,"Found an ITfContext we should should not have\n");
1350 }
1351
1352 static void test_EnumDocumentMgr(ITfThreadMgr *tm, ITfDocumentMgr *search, ITfDocumentMgr *absent)
1353 {
1354 HRESULT hr;
1355 IEnumTfDocumentMgrs* pEnum;
1356 BOOL found = FALSE;
1357 BOOL notfound = TRUE;
1358
1359 hr = ITfThreadMgr_EnumDocumentMgrs(tm,&pEnum);
1360 ok(SUCCEEDED(hr),"EnumDocumentMgrs failed\n");
1361 if (SUCCEEDED(hr))
1362 {
1363 ULONG fetched;
1364 ITfDocumentMgr *dm;
1365 while (IEnumTfDocumentMgrs_Next(pEnum, 1, &dm, &fetched) == S_OK)
1366 {
1367 if (!search)
1368 found = TRUE;
1369 else if (search == dm)
1370 found = TRUE;
1371 if (absent && dm == absent)
1372 notfound = FALSE;
1373 ITfDocumentMgr_Release(dm);
1374 }
1375 IEnumTfDocumentMgrs_Release(pEnum);
1376 }
1377 if (search)
1378 ok(found,"Did not find proper ITfDocumentMgr\n");
1379 else
1380 ok(!found,"Found an ITfDocumentMgr we should should not have\n");
1381 if (absent)
1382 ok(notfound,"Found an ITfDocumentMgr we believe should be absent\n");
1383 }
1384
1385 static inline int check_context_refcount(ITfContext *iface)
1386 {
1387 ITfContext_AddRef(iface);
1388 return ITfContext_Release(iface);
1389 }
1390
1391
1392 /**********************************************************************
1393 * ITfTextEditSink
1394 **********************************************************************/
1395 typedef struct tagTextEditSink
1396 {
1397 ITfTextEditSink ITfTextEditSink_iface;
1398 LONG refCount;
1399 } TextEditSink;
1400
1401 static inline TextEditSink *impl_from_ITfTextEditSink(ITfTextEditSink *iface)
1402 {
1403 return CONTAINING_RECORD(iface, TextEditSink, ITfTextEditSink_iface);
1404 }
1405
1406 static void TextEditSink_Destructor(TextEditSink *This)
1407 {
1408 HeapFree(GetProcessHeap(),0,This);
1409 }
1410
1411 static HRESULT WINAPI TextEditSink_QueryInterface(ITfTextEditSink *iface, REFIID iid, LPVOID *ppvOut)
1412 {
1413 TextEditSink *This = impl_from_ITfTextEditSink(iface);
1414 *ppvOut = NULL;
1415
1416 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfTextEditSink))
1417 {
1418 *ppvOut = This;
1419 }
1420
1421 if (*ppvOut)
1422 {
1423 ITfTextEditSink_AddRef(iface);
1424 return S_OK;
1425 }
1426
1427 return E_NOINTERFACE;
1428 }
1429
1430 static ULONG WINAPI TextEditSink_AddRef(ITfTextEditSink *iface)
1431 {
1432 TextEditSink *This = impl_from_ITfTextEditSink(iface);
1433 return InterlockedIncrement(&This->refCount);
1434 }
1435
1436 static ULONG WINAPI TextEditSink_Release(ITfTextEditSink *iface)
1437 {
1438 TextEditSink *This = impl_from_ITfTextEditSink(iface);
1439 ULONG ret;
1440
1441 ret = InterlockedDecrement(&This->refCount);
1442 if (ret == 0)
1443 TextEditSink_Destructor(This);
1444 return ret;
1445 }
1446
1447 static HRESULT WINAPI TextEditSink_OnEndEdit(ITfTextEditSink *iface,
1448 ITfContext *pic, TfEditCookie ecReadOnly, ITfEditRecord *pEditRecord)
1449 {
1450 sink_fire_ok(&test_OnEndEdit,"TextEditSink_OnEndEdit");
1451 return S_OK;
1452 }
1453
1454 static const ITfTextEditSinkVtbl TextEditSink_TextEditSinkVtbl =
1455 {
1456 TextEditSink_QueryInterface,
1457 TextEditSink_AddRef,
1458 TextEditSink_Release,
1459
1460 TextEditSink_OnEndEdit
1461 };
1462
1463 static HRESULT TextEditSink_Constructor(ITfTextEditSink **ppOut)
1464 {
1465 TextEditSink *This;
1466
1467 *ppOut = NULL;
1468 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextEditSink));
1469 if (This == NULL)
1470 return E_OUTOFMEMORY;
1471
1472 This->ITfTextEditSink_iface.lpVtbl = &TextEditSink_TextEditSinkVtbl;
1473 This->refCount = 1;
1474
1475 *ppOut = &This->ITfTextEditSink_iface;
1476 return S_OK;
1477 }
1478
1479 static void test_startSession(void)
1480 {
1481 HRESULT hr;
1482 DWORD cnt;
1483 DWORD editCookie;
1484 ITfDocumentMgr *dmtest;
1485 ITfContext *cxt,*cxt2,*cxt3,*cxtTest;
1486 ITextStoreACP *ts = NULL;
1487 TfClientId cid2 = 0;
1488 ITfThreadMgrEx *tmex;
1489
1490 hr = ITfThreadMgr_Deactivate(g_tm);
1491 ok(hr == E_UNEXPECTED,"Deactivate should have failed with E_UNEXPECTED\n");
1492
1493 test_ShouldActivate = TRUE;
1494 hr = ITfThreadMgr_Activate(g_tm,&cid);
1495 ok(SUCCEEDED(hr),"Failed to Activate\n");
1496 ok(cid != tid,"TextService id mistakenly matches Client id\n");
1497
1498 test_ShouldActivate = FALSE;
1499 hr = ITfThreadMgr_Activate(g_tm,&cid2);
1500 ok(SUCCEEDED(hr),"Failed to Activate\n");
1501 ok(cid == cid2, "Second activate client ID does not match\n");
1502
1503 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfThreadMgrEx, (void **)&tmex);
1504 if (hr == S_OK)
1505 {
1506 hr = ITfThreadMgrEx_ActivateEx(tmex, &cid2, 0);
1507 ok(SUCCEEDED(hr), "Failed to Activate\n");
1508 ok(cid == cid2, "ActivateEx client ID does not match\n");
1509
1510 ITfThreadMgrEx_Release(tmex);
1511 }
1512 else
1513 win_skip("ITfThreadMgrEx is not supported\n");
1514
1515 hr = ITfThreadMgr_Deactivate(g_tm);
1516 ok(SUCCEEDED(hr), "Failed to Deactivate\n");
1517 hr = ITfThreadMgr_Deactivate(g_tm);
1518 ok(SUCCEEDED(hr), "Failed to Deactivate\n");
1519
1520 test_EnumDocumentMgr(g_tm,NULL,NULL);
1521
1522 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&g_dm);
1523 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1524
1525 test_EnumDocumentMgr(g_tm,g_dm,NULL);
1526
1527 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dmtest);
1528 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
1529
1530 test_EnumDocumentMgr(g_tm,dmtest,NULL);
1531
1532 ITfDocumentMgr_Release(dmtest);
1533 test_EnumDocumentMgr(g_tm,g_dm,dmtest);
1534
1535 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1536 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1537 ok(dmtest == NULL,"Initial focus not null\n");
1538
1539 test_CurrentFocus = g_dm;
1540 test_PrevFocus = NULL;
1541 test_OnSetFocus = SINK_OPTIONAL; /* Doesn't always fire on Win7 */
1542 hr = ITfThreadMgr_SetFocus(g_tm,g_dm);
1543 ok(SUCCEEDED(hr),"SetFocus Failed\n");
1544 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1545
1546 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1547 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1548 ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1549
1550 ITfDocumentMgr_Release(g_dm);
1551
1552 hr = ITfThreadMgr_GetFocus(g_tm,&dmtest);
1553 ok(SUCCEEDED(hr),"GetFocus Failed\n");
1554 ok(g_dm == dmtest,"Expected DocumentMgr not focused\n");
1555 ITfDocumentMgr_Release(dmtest);
1556
1557 hr = TextStoreACP_Constructor((IUnknown**)&ts);
1558 if (SUCCEEDED(hr))
1559 {
1560 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt, &editCookie);
1561 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1562 }
1563
1564 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt2, &editCookie);
1565 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1566
1567 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, NULL, &cxt3, &editCookie);
1568 ok(SUCCEEDED(hr),"CreateContext Failed\n");
1569
1570 test_EnumContexts(g_dm, NULL);
1571
1572 hr = ITfContext_GetDocumentMgr(cxt,&dmtest);
1573 ok(hr == S_OK, "ITfContext_GetDocumentMgr failed with %x\n",hr);
1574 ok(dmtest == g_dm, "Wrong documentmgr\n");
1575 ITfDocumentMgr_Release(dmtest);
1576
1577 cnt = check_context_refcount(cxt);
1578 test_OnPushContext = SINK_EXPECTED;
1579 test_ACP_AdviseSink = SINK_EXPECTED;
1580 test_OnInitDocumentMgr = SINK_EXPECTED;
1581 hr = ITfDocumentMgr_Push(g_dm, cxt);
1582 ok(SUCCEEDED(hr),"Push Failed\n");
1583 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1584 sink_check_ok(&test_OnPushContext,"OnPushContext");
1585 sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
1586 sink_check_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
1587
1588 test_EnumContexts(g_dm, cxt);
1589
1590 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1591 ok(SUCCEEDED(hr),"GetTop Failed\n");
1592 ok(cxtTest == cxt, "Wrong context on top\n");
1593 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1594 cnt = ITfContext_Release(cxtTest);
1595
1596 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1597 ok(SUCCEEDED(hr),"GetBase Failed\n");
1598 ok(cxtTest == cxt, "Wrong context on Base\n");
1599 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1600 ITfContext_Release(cxtTest);
1601
1602 check_context_refcount(cxt2);
1603 test_OnPushContext = SINK_EXPECTED;
1604 hr = ITfDocumentMgr_Push(g_dm, cxt2);
1605 ok(SUCCEEDED(hr),"Push Failed\n");
1606 sink_check_ok(&test_OnPushContext,"OnPushContext");
1607
1608 cnt = check_context_refcount(cxt2);
1609 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1610 ok(SUCCEEDED(hr),"GetTop Failed\n");
1611 ok(cxtTest == cxt2, "Wrong context on top\n");
1612 ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1613 ITfContext_Release(cxtTest);
1614
1615 cnt = check_context_refcount(cxt);
1616 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1617 ok(SUCCEEDED(hr),"GetBase Failed\n");
1618 ok(cxtTest == cxt, "Wrong context on Base\n");
1619 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1620 ITfContext_Release(cxtTest);
1621
1622 cnt = check_context_refcount(cxt3);
1623 hr = ITfDocumentMgr_Push(g_dm, cxt3);
1624 ok(FAILED(hr),"Push Succeeded\n");
1625 ok(check_context_refcount(cxt3) == cnt, "Ref changed\n");
1626
1627 cnt = check_context_refcount(cxt2);
1628 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1629 ok(SUCCEEDED(hr),"GetTop Failed\n");
1630 ok(cxtTest == cxt2, "Wrong context on top\n");
1631 ok(check_context_refcount(cxt2) > cnt, "Ref count did not increase\n");
1632 ITfContext_Release(cxtTest);
1633
1634 cnt = check_context_refcount(cxt);
1635 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1636 ok(SUCCEEDED(hr),"GetBase Failed\n");
1637 ok(cxtTest == cxt, "Wrong context on Base\n");
1638 ok(check_context_refcount(cxt) > cnt, "Ref count did not increase\n");
1639 ITfContext_Release(cxtTest);
1640
1641 cnt = check_context_refcount(cxt2);
1642 test_OnPopContext = SINK_EXPECTED;
1643 hr = ITfDocumentMgr_Pop(g_dm, 0);
1644 ok(SUCCEEDED(hr),"Pop Failed\n");
1645 ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1646 sink_check_ok(&test_OnPopContext,"OnPopContext");
1647
1648 dmtest = (void *)0xfeedface;
1649 hr = ITfContext_GetDocumentMgr(cxt2,&dmtest);
1650 ok(hr == S_FALSE, "ITfContext_GetDocumentMgr wrong rc %x\n",hr);
1651 ok(dmtest == NULL,"returned documentmgr should be null\n");
1652
1653 ITfContext_Release(cxt2);
1654
1655 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1656 ok(SUCCEEDED(hr),"GetTop Failed\n");
1657 ok(cxtTest == cxt, "Wrong context on top\n");
1658 ITfContext_Release(cxtTest);
1659
1660 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1661 ok(SUCCEEDED(hr),"GetBase Failed\n");
1662 ok(cxtTest == cxt, "Wrong context on base\n");
1663 ITfContext_Release(cxtTest);
1664
1665 hr = ITfDocumentMgr_CreateContext(g_dm, cid, 0, (IUnknown*)ts, &cxt2, &editCookie);
1666 ok(hr == S_OK,"CreateContext Failed\n");
1667
1668 test_OnPushContext = SINK_EXPECTED;
1669 test_ACP_AdviseSink = SINK_EXPECTED;
1670 hr = ITfDocumentMgr_Push(g_dm, cxt2);
1671 ok(hr == S_OK,"Push Failed\n");
1672 sink_check_ok(&test_OnPushContext,"OnPushContext");
1673 sink_check_ok(&test_ACP_AdviseSink,"TextStoreACP_AdviseSink");
1674
1675 test_ACP_UnadviseSink = SINK_EXPECTED;
1676 cnt = check_context_refcount(cxt2);
1677 test_OnPopContext = SINK_EXPECTED;
1678 hr = ITfDocumentMgr_Pop(g_dm, 0);
1679 ok(hr == S_OK,"Pop Failed\n");
1680 ok(check_context_refcount(cxt2) < cnt, "Ref count did not decrease\n");
1681 sink_check_ok(&test_OnPopContext,"OnPopContext");
1682 sink_check_ok(&test_ACP_UnadviseSink,"TextStoreACP_AdviseSink");
1683
1684 hr = ITfDocumentMgr_Pop(g_dm, 0);
1685 ok(FAILED(hr),"Pop Succeeded\n");
1686
1687 hr = ITfDocumentMgr_GetTop(g_dm, &cxtTest);
1688 ok(SUCCEEDED(hr),"GetTop Failed\n");
1689 ok(cxtTest == cxt, "Wrong context on top\n");
1690 ITfContext_Release(cxtTest);
1691
1692 hr = ITfDocumentMgr_GetBase(g_dm, &cxtTest);
1693 ok(SUCCEEDED(hr),"GetBase Failed\n");
1694 ok(cxtTest == cxt, "Wrong context on base\n");
1695 ITfContext_Release(cxtTest);
1696
1697 ITfContext_Release(cxt);
1698 ITfContext_Release(cxt2);
1699 ITfContext_Release(cxt3);
1700 }
1701
1702 static void test_endSession(void)
1703 {
1704 HRESULT hr;
1705 test_ShouldDeactivate = TRUE;
1706 test_CurrentFocus = NULL;
1707 test_PrevFocus = g_dm;
1708 test_OnSetFocus = SINK_OPTIONAL; /* Doesn't fire on Win7 */
1709 hr = ITfThreadMgr_Deactivate(g_tm);
1710 ok(SUCCEEDED(hr),"Failed to Deactivate\n");
1711 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
1712 test_OnSetFocus = SINK_UNEXPECTED;
1713 }
1714
1715 static void test_TfGuidAtom(void)
1716 {
1717 GUID gtest,g1;
1718 HRESULT hr;
1719 TfGuidAtom atom1,atom2;
1720 BOOL equal;
1721
1722 CoCreateGuid(&gtest);
1723
1724 /* msdn reports this should return E_INVALIDARG. However my test show it crashing (winxp)*/
1725 /*
1726 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,NULL);
1727 ok(hr==E_INVALIDARG,"ITfCategoryMgr_RegisterGUID should have failed\n");
1728 */
1729 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom1);
1730 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1731 hr = ITfCategoryMgr_RegisterGUID(g_cm,&gtest,&atom2);
1732 ok(SUCCEEDED(hr),"ITfCategoryMgr_RegisterGUID failed\n");
1733 ok(atom1 == atom2,"atoms do not match\n");
1734 hr = ITfCategoryMgr_GetGUID(g_cm,atom2,NULL);
1735 ok(hr==E_INVALIDARG,"ITfCategoryMgr_GetGUID should have failed\n");
1736 hr = ITfCategoryMgr_GetGUID(g_cm,atom2,&g1);
1737 ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1738 ok(IsEqualGUID(&g1,&gtest),"guids do not match\n");
1739 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,NULL);
1740 ok(hr==E_INVALIDARG,"ITfCategoryMgr_IsEqualTfGuidAtom should have failed\n");
1741 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,atom1,&gtest,&equal);
1742 ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1743 ok(equal == TRUE,"Equal value invalid\n");
1744
1745 /* show that cid and tid TfClientIds are also TfGuidAtoms */
1746 hr = ITfCategoryMgr_IsEqualTfGuidAtom(g_cm,tid,&CLSID_FakeService,&equal);
1747 ok(SUCCEEDED(hr),"ITfCategoryMgr_IsEqualTfGuidAtom failed\n");
1748 ok(equal == TRUE,"Equal value invalid\n");
1749 hr = ITfCategoryMgr_GetGUID(g_cm,cid,&g1);
1750 ok(SUCCEEDED(hr),"ITfCategoryMgr_GetGUID failed\n");
1751 ok(!IsEqualGUID(&g1,&GUID_NULL),"guid should not be NULL\n");
1752 }
1753
1754 static void test_ClientId(void)
1755 {
1756 ITfClientId *pcid;
1757 TfClientId id1,id2;
1758 HRESULT hr;
1759 GUID g2;
1760
1761 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfClientId, (LPVOID*)&pcid);
1762 ok(SUCCEEDED(hr),"Unable to acquire ITfClientId interface\n");
1763
1764 CoCreateGuid(&g2);
1765
1766 hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id1);
1767 ok(SUCCEEDED(hr),"GetClientId failed\n");
1768 hr = ITfClientId_GetClientId(pcid,&GUID_NULL,&id2);
1769 ok(SUCCEEDED(hr),"GetClientId failed\n");
1770 ok(id1==id2,"Id's for GUID_NULL do not match\n");
1771 hr = ITfClientId_GetClientId(pcid,&CLSID_FakeService,&id2);
1772 ok(SUCCEEDED(hr),"GetClientId failed\n");
1773 ok(id2!=id1,"Id matches GUID_NULL\n");
1774 ok(id2==tid,"Id for CLSID_FakeService not matching tid\n");
1775 ok(id2!=cid,"Id for CLSID_FakeService matching cid\n");
1776 hr = ITfClientId_GetClientId(pcid,&g2,&id2);
1777 ok(SUCCEEDED(hr),"GetClientId failed\n");
1778 ok(id2!=id1,"Id matches GUID_NULL\n");
1779 ok(id2!=tid,"Id for random guid matching tid\n");
1780 ok(id2!=cid,"Id for random guid matching cid\n");
1781 ITfClientId_Release(pcid);
1782 }
1783
1784 /**********************************************************************
1785 * ITfEditSession
1786 **********************************************************************/
1787 typedef struct tagEditSession
1788 {
1789 ITfEditSession ITfEditSession_iface;
1790 LONG refCount;
1791 } EditSession;
1792
1793 static inline EditSession *impl_from_ITfEditSession(ITfEditSession *iface)
1794 {
1795 return CONTAINING_RECORD(iface, EditSession, ITfEditSession_iface);
1796 }
1797
1798 static void EditSession_Destructor(EditSession *This)
1799 {
1800 HeapFree(GetProcessHeap(),0,This);
1801 }
1802
1803 static HRESULT WINAPI EditSession_QueryInterface(ITfEditSession *iface, REFIID iid, LPVOID *ppvOut)
1804 {
1805 EditSession *This = impl_from_ITfEditSession(iface);
1806 *ppvOut = NULL;
1807
1808 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfEditSession))
1809 {
1810 *ppvOut = This;
1811 }
1812
1813 if (*ppvOut)
1814 {
1815 ITfEditSession_AddRef(iface);
1816 return S_OK;
1817 }
1818
1819 return E_NOINTERFACE;
1820 }
1821
1822 static ULONG WINAPI EditSession_AddRef(ITfEditSession *iface)
1823 {
1824 EditSession *This = impl_from_ITfEditSession(iface);
1825 return InterlockedIncrement(&This->refCount);
1826 }
1827
1828 static ULONG WINAPI EditSession_Release(ITfEditSession *iface)
1829 {
1830 EditSession *This = impl_from_ITfEditSession(iface);
1831 ULONG ret;
1832
1833 ret = InterlockedDecrement(&This->refCount);
1834 if (ret == 0)
1835 EditSession_Destructor(This);
1836 return ret;
1837 }
1838
1839 static void test_InsertAtSelection(TfEditCookie ec, ITfContext *cxt)
1840 {
1841 HRESULT hr;
1842 ITfInsertAtSelection *iis;
1843 ITfRange *range=NULL;
1844 static const WCHAR txt[] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
1845
1846 hr = ITfContext_QueryInterface(cxt, &IID_ITfInsertAtSelection , (LPVOID*)&iis);
1847 ok(SUCCEEDED(hr),"Failed to get ITfInsertAtSelection interface\n");
1848 test_ACP_InsertTextAtSelection = SINK_EXPECTED;
1849 hr = ITfInsertAtSelection_InsertTextAtSelection(iis, ec, 0, txt, 11, &range);
1850 ok(SUCCEEDED(hr),"ITfInsertAtSelection_InsertTextAtSelection failed %x\n",hr);
1851 sink_check_ok(&test_ACP_InsertTextAtSelection,"InsertTextAtSelection");
1852 ok(range != NULL,"No range returned\n");
1853 ITfRange_Release(range);
1854 ITfInsertAtSelection_Release(iis);
1855 }
1856
1857 static HRESULT WINAPI EditSession_DoEditSession(ITfEditSession *iface,
1858 TfEditCookie ec)
1859 {
1860 ITfContext *cxt;
1861 ITfDocumentMgr *dm;
1862 ITfRange *range;
1863 TF_SELECTION selection;
1864 ULONG fetched;
1865 HRESULT hr;
1866
1867 sink_fire_ok(&test_DoEditSession,"EditSession_DoEditSession");
1868 sink_check_ok(&test_ACP_RequestLock,"RequestLock");
1869
1870 ITfThreadMgr_GetFocus(g_tm, &dm);
1871 ITfDocumentMgr_GetTop(dm,&cxt);
1872
1873 hr = ITfContext_GetStart(cxt,ec,NULL);
1874 ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1875
1876 range = (ITfRange*)0xdeaddead;
1877 hr = ITfContext_GetStart(cxt,0xdeadcafe,&range);
1878 ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1879 ok(range == NULL,"Range not set to NULL\n");
1880
1881 hr = ITfContext_GetStart(cxt,ec,&range);
1882 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1883 ok(range != NULL,"Range set to NULL\n");
1884
1885 ITfRange_Release(range);
1886
1887 hr = ITfContext_GetEnd(cxt,ec,NULL);
1888 ok(hr == E_INVALIDARG,"Unexpected return code %x\n",hr);
1889
1890 range = (ITfRange*)0xdeaddead;
1891 hr = ITfContext_GetEnd(cxt,0xdeadcafe,&range);
1892 ok(hr == TF_E_NOLOCK,"Unexpected return code %x\n",hr);
1893 ok(range == NULL,"Range not set to NULL\n");
1894
1895 test_ACP_GetEndACP = SINK_EXPECTED;
1896 hr = ITfContext_GetEnd(cxt,ec,&range);
1897 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1898 ok(range != NULL,"Range set to NULL\n");
1899 sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1900
1901 ITfRange_Release(range);
1902
1903 selection.range = NULL;
1904 test_ACP_GetSelection = SINK_EXPECTED;
1905 hr = ITfContext_GetSelection(cxt, ec, TF_DEFAULT_SELECTION, 1, &selection, &fetched);
1906 ok(SUCCEEDED(hr),"ITfContext_GetSelection failed\n");
1907 ok(fetched == 1,"fetched incorrect\n");
1908 ok(selection.range != NULL,"NULL range\n");
1909 sink_check_ok(&test_ACP_GetSelection,"ACP_GetSepection");
1910 ITfRange_Release(selection.range);
1911
1912 test_InsertAtSelection(ec, cxt);
1913
1914 test_ACP_GetEndACP = SINK_EXPECTED;
1915 hr = ITfContext_GetEnd(cxt,ec,&range);
1916 ok(SUCCEEDED(hr),"Unexpected return code %x\n",hr);
1917 ok(range != NULL,"Range set to NULL\n");
1918 sink_check_ok(&test_ACP_GetEndACP,"GetEndACP");
1919
1920 selection.range = range;
1921 selection.style.ase = TF_AE_NONE;
1922 selection.style.fInterimChar = FALSE;
1923 test_ACP_SetSelection = SINK_EXPECTED;
1924 hr = ITfContext_SetSelection(cxt, ec, 1, &selection);
1925 ok(SUCCEEDED(hr),"ITfContext_SetSelection failed\n");
1926 sink_check_ok(&test_ACP_SetSelection,"SetSelection");
1927 ITfRange_Release(range);
1928
1929 ITfContext_Release(cxt);
1930 ITfDocumentMgr_Release(dm);
1931 return 0xdeadcafe;
1932 }
1933
1934 static const ITfEditSessionVtbl EditSession_EditSessionVtbl =
1935 {
1936 EditSession_QueryInterface,
1937 EditSession_AddRef,
1938 EditSession_Release,
1939
1940 EditSession_DoEditSession
1941 };
1942
1943 static HRESULT EditSession_Constructor(ITfEditSession **ppOut)
1944 {
1945 EditSession *This;
1946
1947 *ppOut = NULL;
1948 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EditSession));
1949 if (This == NULL)
1950 return E_OUTOFMEMORY;
1951
1952 This->ITfEditSession_iface.lpVtbl = &EditSession_EditSessionVtbl;
1953 This->refCount = 1;
1954
1955 *ppOut = &This->ITfEditSession_iface;
1956 return S_OK;
1957 }
1958
1959 static void test_TStoApplicationText(void)
1960 {
1961 HRESULT hr, hrSession;
1962 ITfEditSession *es;
1963 ITfContext *cxt;
1964 ITfDocumentMgr *dm;
1965 ITfTextEditSink *sink;
1966 ITfSource *source = NULL;
1967 DWORD editSinkCookie = -1;
1968
1969 ITfThreadMgr_GetFocus(g_tm, &dm);
1970 EditSession_Constructor(&es);
1971 ITfDocumentMgr_GetTop(dm,&cxt);
1972
1973 TextEditSink_Constructor(&sink);
1974 hr = ITfContext_QueryInterface(cxt,&IID_ITfSource,(LPVOID*)&source);
1975 ok(SUCCEEDED(hr),"Failed to get IID_ITfSource for Context\n");
1976 if (source)
1977 {
1978 hr = ITfSource_AdviseSink(source, &IID_ITfTextEditSink, (LPVOID)sink, &editSinkCookie);
1979 ok(SUCCEEDED(hr),"Failed to advise Sink\n");
1980 ok(editSinkCookie != -1,"Failed to get sink cookie\n");
1981 }
1982
1983 hrSession = 0xfeedface;
1984 /* Test no permissions flags */
1985 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC, &hrSession);
1986 ok(hr == E_INVALIDARG,"RequestEditSession should have failed with %x not %x\n",E_INVALIDARG,hr);
1987 ok(hrSession == E_FAIL,"hrSession should be %x not %x\n",E_FAIL,hrSession);
1988
1989 documentStatus = TS_SD_READONLY;
1990 hrSession = 0xfeedface;
1991 test_ACP_GetStatus = SINK_EXPECTED;
1992 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
1993 ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
1994 ok(hrSession == TS_E_READONLY,"Unexpected hrSession (%x)\n",hrSession);
1995 sink_check_ok(&test_ACP_GetStatus,"GetStatus");
1996
1997 /* signal a change to allow readwrite sessions */
1998 documentStatus = 0;
1999 test_ACP_RequestLock = SINK_EXPECTED;
2000 ITextStoreACPSink_OnStatusChange(ACPSink,documentStatus);
2001 sink_check_ok(&test_ACP_RequestLock,"RequestLock");
2002
2003 test_ACP_GetStatus = SINK_EXPECTED;
2004 test_ACP_RequestLock = SINK_EXPECTED;
2005 test_DoEditSession = SINK_EXPECTED;
2006 hrSession = 0xfeedface;
2007 test_OnEndEdit = SINK_EXPECTED;
2008 hr = ITfContext_RequestEditSession(cxt, tid, es, TF_ES_SYNC|TF_ES_READWRITE, &hrSession);
2009 ok(SUCCEEDED(hr),"ITfContext_RequestEditSession failed\n");
2010 sink_check_ok(&test_OnEndEdit,"OnEndEdit");
2011 sink_check_ok(&test_DoEditSession,"DoEditSession");
2012 sink_check_ok(&test_ACP_GetStatus,"GetStatus");
2013 ok(hrSession == 0xdeadcafe,"Unexpected hrSession (%x)\n",hrSession);
2014
2015 if (source)
2016 {
2017 hr = ITfSource_UnadviseSink(source, editSinkCookie);
2018 ok(SUCCEEDED(hr),"Failed to unadvise Sink\n");
2019 ITfSource_Release(source);
2020 }
2021 ITfTextEditSink_Release(sink);
2022 ITfContext_Release(cxt);
2023 ITfDocumentMgr_Release(dm);
2024 ITfEditSession_Release(es);
2025 }
2026
2027 static void enum_compartments(ITfCompartmentMgr *cmpmgr, REFGUID present, REFGUID absent)
2028 {
2029 BOOL found,found2;
2030 IEnumGUID *ppEnum;
2031 found = FALSE;
2032 found2 = FALSE;
2033 if (SUCCEEDED(ITfCompartmentMgr_EnumCompartments(cmpmgr, &ppEnum)))
2034 {
2035 ULONG fetched;
2036 GUID g;
2037 while (IEnumGUID_Next(ppEnum, 1, &g, &fetched) == S_OK)
2038 {
2039 WCHAR str[50];
2040 CHAR strA[50];
2041 StringFromGUID2(&g,str,sizeof(str)/sizeof(str[0]));
2042 WideCharToMultiByte(CP_ACP,0,str,-1,strA,sizeof(strA),0,0);
2043 trace("found %s\n",strA);
2044 if (present && IsEqualGUID(present,&g))
2045 found = TRUE;
2046 if (absent && IsEqualGUID(absent, &g))
2047 found2 = TRUE;
2048 }
2049 IEnumGUID_Release(ppEnum);
2050 }
2051 if (present)
2052 ok(found,"Did not find compartment\n");
2053 if (absent)
2054 ok(!found2,"Found compartment that should be absent\n");
2055 }
2056
2057 static void test_Compartments(void)
2058 {
2059 ITfContext *cxt;
2060 ITfDocumentMgr *dm;
2061 ITfCompartmentMgr *cmpmgr;
2062 ITfCompartment *cmp;
2063 HRESULT hr;
2064
2065 ITfThreadMgr_GetFocus(g_tm, &dm);
2066 ITfDocumentMgr_GetTop(dm,&cxt);
2067
2068 /* Global */
2069 hr = ITfThreadMgr_GetGlobalCompartment(g_tm, &cmpmgr);
2070 ok(SUCCEEDED(hr),"GetGlobalCompartment failed\n");
2071 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_SPEECH_OPENCLOSE, &cmp);
2072 ok(SUCCEEDED(hr),"GetCompartment failed\n");
2073 ITfCompartment_Release(cmp);
2074 enum_compartments(cmpmgr,&GUID_COMPARTMENT_SPEECH_OPENCLOSE,NULL);
2075 ITfCompartmentMgr_Release(cmpmgr);
2076
2077 /* Thread */
2078 hr = ITfThreadMgr_QueryInterface(g_tm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
2079 ok(SUCCEEDED(hr),"ThreadMgr QI for IID_ITfCompartmentMgr failed\n");
2080 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &CLSID_FakeService, &cmp);
2081 ok(SUCCEEDED(hr),"GetCompartment failed\n");
2082 enum_compartments(cmpmgr,&CLSID_FakeService,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
2083 ITfCompartmentMgr_ClearCompartment(cmpmgr,tid,&CLSID_FakeService);
2084 enum_compartments(cmpmgr,NULL,&CLSID_FakeService);
2085 ITfCompartmentMgr_Release(cmpmgr);
2086 ITfCompartment_Release(cmp);
2087
2088 /* DocumentMgr */
2089 hr = ITfDocumentMgr_QueryInterface(dm, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
2090 ok(SUCCEEDED(hr),"DocumentMgr QI for IID_ITfCompartmentMgr failed\n");
2091
2092 hr = ITfCompartmentMgr_GetCompartment(cmpmgr, &GUID_COMPARTMENT_PERSISTMENUENABLED, &cmp);
2093 ok(SUCCEEDED(hr),"GetCompartment failed\n");
2094 enum_compartments(cmpmgr,&GUID_COMPARTMENT_PERSISTMENUENABLED,&GUID_COMPARTMENT_SPEECH_OPENCLOSE);
2095 ITfCompartmentMgr_Release(cmpmgr);
2096
2097 /* Context */
2098 hr = ITfContext_QueryInterface(cxt, &IID_ITfCompartmentMgr, (LPVOID*)&cmpmgr);
2099 ok(SUCCEEDED(hr),"Context QI for IID_ITfCompartmentMgr failed\n");
2100 enum_compartments(cmpmgr,NULL,&GUID_COMPARTMENT_PERSISTMENUENABLED);
2101 ITfCompartmentMgr_Release(cmpmgr);
2102
2103 ITfContext_Release(cxt);
2104 ITfDocumentMgr_Release(dm);
2105 }
2106
2107 static void processPendingMessages(void)
2108 {
2109 MSG msg;
2110 int diff = 200;
2111 int min_timeout = 100;
2112 DWORD time = GetTickCount() + diff;
2113
2114 while (diff > 0)
2115 {
2116 if (MsgWaitForMultipleObjects(0, NULL, FALSE, min_timeout, QS_ALLINPUT) == WAIT_TIMEOUT)
2117 break;
2118 while (PeekMessageW(&msg, 0, 0, 0, PM_REMOVE))
2119 {
2120 TranslateMessage(&msg);
2121 DispatchMessageW(&msg);
2122 }
2123 diff = time - GetTickCount();
2124 }
2125 }
2126
2127 static void test_AssociateFocus(void)
2128 {
2129 ITfDocumentMgr *dm1, *dm2, *olddm, *dmcheck, *dmorig;
2130 HWND wnd1, wnd2, wnd3;
2131 HRESULT hr;
2132
2133 ITfThreadMgr_GetFocus(g_tm, &dmorig);
2134 test_CurrentFocus = NULL;
2135 test_PrevFocus = dmorig;
2136 test_OnSetFocus = SINK_OPTIONAL; /* Doesn't always fire on Win7 */
2137 test_ACP_GetStatus = SINK_OPTIONAL;
2138 hr = ITfThreadMgr_SetFocus(g_tm,NULL);
2139 ok(SUCCEEDED(hr),"ITfThreadMgr_SetFocus failed\n");
2140 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2141 test_ACP_GetStatus = SINK_UNEXPECTED;
2142 ITfDocumentMgr_Release(dmorig);
2143
2144 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm1);
2145 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
2146
2147 hr = ITfThreadMgr_CreateDocumentMgr(g_tm,&dm2);
2148 ok(SUCCEEDED(hr),"CreateDocumentMgr failed\n");
2149
2150 wnd1 = CreateWindowA("edit",NULL,WS_POPUP,0,0,200,60,NULL,NULL,NULL,NULL);
2151 ok(wnd1!=NULL,"Unable to create window 1\n");
2152 wnd2 = CreateWindowA("edit",NULL,WS_POPUP,0,65,200,60,NULL,NULL,NULL,NULL);
2153 ok(wnd2!=NULL,"Unable to create window 2\n");
2154 wnd3 = CreateWindowA("edit",NULL,WS_POPUP,0,130,200,60,NULL,NULL,NULL,NULL);
2155 ok(wnd3!=NULL,"Unable to create window 3\n");
2156
2157 processPendingMessages();
2158
2159 test_OnInitDocumentMgr = SINK_OPTIONAL; /* Vista and greater */
2160 test_OnPushContext = SINK_OPTIONAL; /* Vista and greater */
2161 test_OnSetFocus = SINK_OPTIONAL; /* Win7 */
2162 test_PrevFocus = NULL;
2163 test_CurrentFocus = FOCUS_IGNORE;
2164
2165 ShowWindow(wnd1,SW_SHOWNORMAL);
2166 test_OnSetFocus = SINK_UNEXPECTED;
2167 SetFocus(wnd1);
2168 sink_check_ok(&test_OnInitDocumentMgr,"OnInitDocumentMgr");
2169 sink_check_ok(&test_OnPushContext,"OnPushContext");
2170
2171 test_OnSetFocus = SINK_OPTIONAL; /* Vista and greater */
2172 test_ACP_RequestLock = SINK_OPTIONAL; /* Win7 x64 */
2173 test_ACP_GetSelection = SINK_OPTIONAL; /* Win7 x64 */
2174 ITfThreadMgr_GetFocus(g_tm, &test_PrevFocus);
2175 test_CurrentFocus = FOCUS_IGNORE; /* This is a default system context */
2176 processPendingMessages();
2177 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2178 test_ACP_RequestLock = SINK_UNEXPECTED;
2179 test_ACP_GetSelection = SINK_UNEXPECTED;
2180
2181 test_CurrentFocus = dm1;
2182 test_PrevFocus = FOCUS_IGNORE;
2183 test_OnSetFocus = SINK_OPTIONAL;
2184 test_ShouldDeactivate = SINK_OPTIONAL;
2185 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,dm1,&olddm);
2186 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2187 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2188 test_ShouldDeactivate = SINK_UNEXPECTED;
2189
2190 processPendingMessages();
2191
2192 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2193 ok(dmcheck == dm1 || broken(dmcheck == dmorig /* Win7+ */), "Expected DocumentMgr not focused\n");
2194 ITfDocumentMgr_Release(dmcheck);
2195
2196 /* We need to explicitly set focus on Win7+ */
2197 test_CurrentFocus = dm1;
2198 test_PrevFocus = FOCUS_IGNORE;
2199 test_OnSetFocus = SINK_OPTIONAL; /* Doesn't always fire on Win7+ */
2200 ITfThreadMgr_SetFocus(g_tm, dm1);
2201 sink_check_ok(&test_OnSetFocus, "OnSetFocus");
2202
2203 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,dm2,&olddm);
2204 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2205 processPendingMessages();
2206 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2207 ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
2208 ITfDocumentMgr_Release(dmcheck);
2209
2210 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,dm2,&olddm);
2211 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2212 processPendingMessages();
2213 ITfThreadMgr_GetFocus(g_tm, &dmcheck);
2214 ok(dmcheck == dm1, "Expected DocumentMgr not focused\n");
2215 ITfDocumentMgr_Release(dmcheck);
2216
2217 test_CurrentFocus = FOCUS_SAVE;
2218 test_PrevFocus = FOCUS_SAVE;
2219 test_OnSetFocus = SINK_SAVE;
2220 ShowWindow(wnd2,SW_SHOWNORMAL);
2221 SetFocus(wnd2);
2222 sink_check_saved(&test_OnSetFocus,dm1,dm2,"OnSetFocus");
2223 test_CurrentFocus = FOCUS_IGNORE; /* occasional wine race */
2224 test_PrevFocus = FOCUS_IGNORE; /* occasional wine race */
2225 test_OnSetFocus = SINK_IGNORE; /* occasional wine race */
2226 processPendingMessages();
2227
2228 ShowWindow(wnd3,SW_SHOWNORMAL);
2229 SetFocus(wnd3);
2230 processPendingMessages();
2231
2232 test_CurrentFocus = FOCUS_SAVE;
2233 test_PrevFocus = FOCUS_SAVE;
2234 test_OnSetFocus = SINK_SAVE;
2235 SetFocus(wnd1);
2236 processPendingMessages();
2237 sink_check_saved(&test_OnSetFocus,dm2,dm1,"OnSetFocus");
2238
2239 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd3,NULL,&olddm);
2240 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2241 ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2242 ITfDocumentMgr_Release(olddm);
2243
2244 test_CurrentFocus = dmorig;
2245 test_PrevFocus = dm1;
2246 test_OnSetFocus = SINK_OPTIONAL; /* Doesn't always fire on Win7+ */
2247 test_ACP_GetStatus = SINK_IGNORE;
2248 ITfThreadMgr_SetFocus(g_tm,dmorig);
2249 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2250
2251 test_CurrentFocus = FOCUS_SAVE;
2252 test_PrevFocus = FOCUS_SAVE;
2253 test_OnSetFocus = SINK_SAVE;
2254 SetFocus(wnd3);
2255 processPendingMessages();
2256 sink_check_saved(&test_OnSetFocus,dmorig,FOCUS_IGNORE,"OnSetFocus"); /* CurrentFocus NULL on XP, system default on Vista */
2257
2258 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd2,NULL,&olddm);
2259 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2260 ok(olddm == dm2, "incorrect old DocumentMgr returned\n");
2261 ITfDocumentMgr_Release(olddm);
2262 hr = ITfThreadMgr_AssociateFocus(g_tm,wnd1,NULL,&olddm);
2263 ok(SUCCEEDED(hr),"AssociateFocus failed\n");
2264 ok(olddm == dm1, "incorrect old DocumentMgr returned\n");
2265 ITfDocumentMgr_Release(olddm);
2266
2267 test_OnSetFocus = SINK_IGNORE; /* OnSetFocus fires a couple of times on Win7 */
2268 test_CurrentFocus = FOCUS_IGNORE;
2269 test_PrevFocus = FOCUS_IGNORE;
2270 SetFocus(wnd2);
2271 processPendingMessages();
2272 SetFocus(wnd1);
2273 processPendingMessages();
2274 test_OnSetFocus = SINK_UNEXPECTED;
2275
2276 ITfDocumentMgr_Release(dm1);
2277 ITfDocumentMgr_Release(dm2);
2278
2279 test_CurrentFocus = dmorig;
2280 test_PrevFocus = FOCUS_IGNORE;
2281 test_OnSetFocus = SINK_OPTIONAL;
2282 test_ACP_GetStatus = SINK_IGNORE;
2283 ITfThreadMgr_SetFocus(g_tm,dmorig);
2284 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2285
2286 test_OnSetFocus = SINK_IGNORE; /* OnSetFocus fires a couple of times on Win7 */
2287 test_CurrentFocus = FOCUS_IGNORE;
2288 test_PrevFocus = FOCUS_IGNORE;
2289 DestroyWindow(wnd1);
2290 DestroyWindow(wnd2);
2291 test_OnPopContext = SINK_OPTIONAL; /* Vista and greater */
2292 test_OnSetFocus = SINK_OPTIONAL; /* Vista and greater */
2293 ITfThreadMgr_GetFocus(g_tm, &test_PrevFocus);
2294 test_CurrentFocus = NULL;
2295 test_ShouldDeactivate = TRUE; /* Win7 */
2296 DestroyWindow(wnd3);
2297 test_ShouldDeactivate = FALSE;
2298 sink_check_ok(&test_OnSetFocus,"OnSetFocus");
2299 sink_check_ok(&test_OnPopContext,"OnPopContext");
2300 }
2301
2302 static void test_profile_mgr(void)
2303 {
2304 IEnumTfInputProcessorProfiles *enum_profiles;
2305 ITfInputProcessorProfileMgr *ipp_mgr;
2306 HRESULT hres;
2307
2308 hres = ITfInputProcessorProfiles_QueryInterface(g_ipp, &IID_ITfInputProcessorProfileMgr, (void**)&ipp_mgr);
2309 if (hres != S_OK)
2310 {
2311 win_skip("ITfInputProcessorProfileMgr is not supported.\n");
2312 return;
2313 }
2314 ok(hres == S_OK, "Could not get ITfInputProcessorProfileMgr iface: %08x\n", hres);
2315
2316 hres = ITfInputProcessorProfileMgr_EnumProfiles(ipp_mgr, 0, &enum_profiles);
2317 ok(hres == S_OK, "EnumProfiles failed: %08x\n", hres);
2318
2319 IEnumTfInputProcessorProfiles_Release(enum_profiles);
2320
2321 ITfInputProcessorProfileMgr_Release(ipp_mgr);
2322 }
2323
2324 START_TEST(inputprocessor)
2325 {
2326 if (SUCCEEDED(initialize()))
2327 {
2328 test_Register();
2329 test_RegisterCategory();
2330 Sleep(2000); /* Win7 needs some time before the registrations become active */
2331 processPendingMessages();
2332 test_EnumLanguageProfiles();
2333 test_EnumInputProcessorInfo();
2334 test_Enable();
2335 test_ThreadMgrAdviseSinks();
2336 test_Activate();
2337 test_startSession();
2338 test_DocumentMgrAdviseSinks();
2339 test_TfGuidAtom();
2340 test_ClientId();
2341 test_KeystrokeMgr();
2342 test_TStoApplicationText();
2343 test_Compartments();
2344 test_AssociateFocus();
2345 test_endSession();
2346 test_FindClosestCategory();
2347 test_Disable();
2348 test_ThreadMgrUnadviseSinks();
2349 test_DocumentMgrUnadviseSinks();
2350 test_UnregisterCategory();
2351 test_Unregister();
2352 test_profile_mgr();
2353
2354 ITextStoreACPSink_Release(ACPSink);
2355 ITfDocumentMgr_Release(g_dm);
2356 }
2357 else
2358 skip("Unable to create InputProcessor\n");
2359 cleanup();
2360 }