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