2 * Unit tests for console API
4 * Copyright (c) 2003,2004 Eric Pouech
5 * Copyright (c) 2007 Kirill K. Smirnov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/test.h"
26 static BOOL (WINAPI
*pGetConsoleInputExeNameA
)(DWORD
, LPSTR
);
27 static BOOL (WINAPI
*pSetConsoleInputExeNameA
)(LPCSTR
);
29 /* DEFAULT_ATTRIB is used for all initial filling of the console.
30 * all modifications are made with TEST_ATTRIB so that we could check
31 * what has to be modified or not
33 #define TEST_ATTRIB (BACKGROUND_BLUE | FOREGROUND_GREEN)
34 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
35 /* when filling the screen with non-blank chars, this macro defines
36 * what character should be at position 'c'
38 #define CONTENT(c) ('A' + (((c).Y * 17 + (c).X) % 23))
40 #define okCURSOR(hCon, c) do { \
41 CONSOLE_SCREEN_BUFFER_INFO __sbi; \
42 BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
43 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
44 ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
45 (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
48 #define okCHAR(hCon, c, ch, attr) do { \
49 char __ch; WORD __attr; DWORD __len; BOOL expect; \
50 expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
51 ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
52 expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
53 ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
56 static void init_function_pointers(void)
60 #define KERNEL32_GET_PROC(func) \
61 p##func = (void *)GetProcAddress(hKernel32, #func); \
62 if(!p##func) trace("GetProcAddress(hKernel32, '%s') failed\n", #func);
64 hKernel32
= GetModuleHandleA("kernel32.dll");
65 KERNEL32_GET_PROC(GetConsoleInputExeNameA
);
66 KERNEL32_GET_PROC(SetConsoleInputExeNameA
);
68 #undef KERNEL32_GET_PROC
71 /* FIXME: this could be optimized on a speed point of view */
72 static void resetContent(HANDLE hCon
, COORD sbSize
, BOOL content
)
75 WORD attr
= DEFAULT_ATTRIB
;
79 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
81 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
83 ch
= (content
) ? CONTENT(c
) : ' ';
84 WriteConsoleOutputAttribute(hCon
, &attr
, 1, c
, &len
);
85 WriteConsoleOutputCharacterA(hCon
, &ch
, 1, c
, &len
);
90 static void testCursor(HANDLE hCon
, COORD sbSize
)
95 ok(SetConsoleCursorPosition(0, c
) == 0, "No handle\n");
96 ok(GetLastError() == ERROR_INVALID_HANDLE
, "GetLastError: expecting %u got %u\n",
97 ERROR_INVALID_HANDLE
, GetLastError());
100 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left\n");
105 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in lower-right\n");
110 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
111 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
112 ERROR_INVALID_PARAMETER
, GetLastError());
116 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
117 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
118 ERROR_INVALID_PARAMETER
, GetLastError());
122 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
123 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
124 ERROR_INVALID_PARAMETER
, GetLastError());
128 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
129 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
130 ERROR_INVALID_PARAMETER
, GetLastError());
133 static void testWriteSimple(HANDLE hCon
, COORD sbSize
)
137 const char* mytest
= "abcdefg";
138 const int mylen
= strlen(mytest
);
140 /* single line write */
142 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left\n");
144 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
146 for (c
.X
= 0; c
.X
< mylen
; c
.X
++)
148 okCHAR(hCon
, c
, mytest
[c
.X
], TEST_ATTRIB
);
152 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
155 static void testWriteNotWrappedNotProcessed(HANDLE hCon
, COORD sbSize
)
159 const char* mytest
= "123";
160 const int mylen
= strlen(mytest
);
164 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, mode
& ~(ENABLE_PROCESSED_OUTPUT
|ENABLE_WRAP_AT_EOL_OUTPUT
)),
165 "clearing wrap at EOL & processed output\n");
167 /* write line, wrapping disabled, buffer exceeds sb width */
168 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
169 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
171 ret
= WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
);
172 ok(ret
!= 0 && len
== mylen
, "Couldn't write, ret = %d, len = %d\n", ret
, len
);
174 for (p
= mylen
- 3; p
< mylen
; p
++)
176 c
.X
= sbSize
.X
- 3 + p
% 3;
177 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
181 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
183 p
= sbSize
.X
- 3 + mylen
% 3;
186 /* write line, wrapping disabled, strings end on end of line */
187 c
.X
= sbSize
.X
- mylen
; c
.Y
= 0;
188 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
190 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
193 static void testWriteNotWrappedProcessed(HANDLE hCon
, COORD sbSize
)
197 const char* mytest
= "abcd\nf\tg";
198 const int mylen
= strlen(mytest
);
199 const int mylen2
= strchr(mytest
, '\n') - mytest
;
202 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, (mode
| ENABLE_PROCESSED_OUTPUT
) & ~ENABLE_WRAP_AT_EOL_OUTPUT
),
203 "clearing wrap at EOL & setting processed output\n");
205 /* write line, wrapping disabled, buffer exceeds sb width */
206 c
.X
= sbSize
.X
- 5; c
.Y
= 0;
207 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-5\n");
209 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
211 for (c
.X
= sbSize
.X
- 5; c
.X
< sbSize
.X
- 1; c
.X
++)
213 okCHAR(hCon
, c
, mytest
[c
.X
- sbSize
.X
+ 5], TEST_ATTRIB
);
215 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
218 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
219 for (c
.X
= 1; c
.X
< 8; c
.X
++)
220 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
221 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
223 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
227 /* write line, wrapping disabled, strings end on end of line */
228 c
.X
= sbSize
.X
- 4; c
.Y
= 0;
229 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-4\n");
231 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
233 for (c
.X
= sbSize
.X
- 4; c
.X
< sbSize
.X
; c
.X
++)
235 okCHAR(hCon
, c
, mytest
[c
.X
- sbSize
.X
+ 4], TEST_ATTRIB
);
238 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
239 for (c
.X
= 1; c
.X
< 8; c
.X
++)
240 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
241 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
243 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
247 /* write line, wrapping disabled, strings end after end of line */
248 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
249 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-4\n");
251 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
253 for (p
= mylen2
- 3; p
< mylen2
; p
++)
255 c
.X
= sbSize
.X
- 3 + p
% 3;
256 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
259 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
260 for (c
.X
= 1; c
.X
< 8; c
.X
++)
261 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
262 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
264 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
269 static void testWriteWrappedNotProcessed(HANDLE hCon
, COORD sbSize
)
273 const char* mytest
= "abcd\nf\tg";
274 const int mylen
= strlen(mytest
);
277 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
,(mode
| ENABLE_WRAP_AT_EOL_OUTPUT
) & ~(ENABLE_PROCESSED_OUTPUT
)),
278 "setting wrap at EOL & clearing processed output\n");
280 /* write line, wrapping enabled, buffer doesn't exceed sb width */
281 c
.X
= sbSize
.X
- 9; c
.Y
= 0;
282 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-9\n");
284 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
286 for (p
= 0; p
< mylen
; p
++)
288 c
.X
= sbSize
.X
- 9 + p
;
289 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
291 c
.X
= sbSize
.X
- 9 + mylen
;
292 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
294 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
296 /* write line, wrapping enabled, buffer does exceed sb width */
297 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
298 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
302 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
305 static void testWriteWrappedProcessed(HANDLE hCon
, COORD sbSize
)
309 const char* mytest
= "abcd\nf\tg";
310 const int mylen
= strlen(mytest
);
313 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, mode
| (ENABLE_WRAP_AT_EOL_OUTPUT
|ENABLE_PROCESSED_OUTPUT
)),
314 "setting wrap at EOL & processed output\n");
316 /* write line, wrapping enabled, buffer doesn't exceed sb width */
317 c
.X
= sbSize
.X
- 9; c
.Y
= 0;
318 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-9\n");
320 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
321 for (p
= 0; p
< 4; p
++)
323 c
.X
= sbSize
.X
- 9 + p
;
324 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
326 c
.X
= sbSize
.X
- 9 + p
;
327 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
329 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
330 for (c
.X
= 1; c
.X
< 8; c
.X
++)
331 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
332 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
334 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
337 /* write line, wrapping enabled, buffer does exceed sb width */
338 c
.X
= sbSize
.X
- 3; c
.Y
= 2;
339 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
341 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
342 for (p
= 0; p
< 3; p
++)
344 c
.X
= sbSize
.X
- 3 + p
;
345 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
348 okCHAR(hCon
, c
, mytest
[3], TEST_ATTRIB
);
350 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
353 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
354 for (c
.X
= 1; c
.X
< 8; c
.X
++)
355 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
356 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
358 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
362 static void testWrite(HANDLE hCon
, COORD sbSize
)
364 /* FIXME: should in fact insure that the sb is at least 10 character wide */
365 ok(SetConsoleTextAttribute(hCon
, TEST_ATTRIB
), "Setting default text color\n");
366 resetContent(hCon
, sbSize
, FALSE
);
367 testWriteSimple(hCon
, sbSize
);
368 resetContent(hCon
, sbSize
, FALSE
);
369 testWriteNotWrappedNotProcessed(hCon
, sbSize
);
370 resetContent(hCon
, sbSize
, FALSE
);
371 testWriteNotWrappedProcessed(hCon
, sbSize
);
372 resetContent(hCon
, sbSize
, FALSE
);
373 testWriteWrappedNotProcessed(hCon
, sbSize
);
374 resetContent(hCon
, sbSize
, FALSE
);
375 testWriteWrappedProcessed(hCon
, sbSize
);
378 static void testScroll(HANDLE hCon
, COORD sbSize
)
380 SMALL_RECT scroll
, clip
;
387 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
388 #define IN_SRECT2(r,d,c) ((d).X <= (c).X && (c).X <= (d).X + (r).Right - (r).Left && (d).Y <= (c).Y && (c).Y <= (d).Y + (r).Bottom - (r).Top)
390 /* no clipping, src & dst rect don't overlap */
391 resetContent(hCon
, sbSize
, TRUE
);
394 scroll
.Right
= W
- 1;
396 scroll
.Bottom
= H
- 1;
399 ci
.Char
.UnicodeChar
= '#';
400 ci
.Attributes
= TEST_ATTRIB
;
403 clip
.Right
= sbSize
.X
- 1;
405 clip
.Bottom
= sbSize
.Y
- 1;
407 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, NULL
, dst
, &ci
), "Scrolling SB\n");
409 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
411 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
413 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
417 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
419 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
420 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
421 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
425 /* no clipping, src & dst rect do overlap */
426 resetContent(hCon
, sbSize
, TRUE
);
429 scroll
.Right
= W
- 1;
431 scroll
.Bottom
= H
- 1;
434 ci
.Char
.UnicodeChar
= '#';
435 ci
.Attributes
= TEST_ATTRIB
;
438 clip
.Right
= sbSize
.X
- 1;
440 clip
.Bottom
= sbSize
.Y
- 1;
442 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, NULL
, dst
, &ci
), "Scrolling SB\n");
444 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
446 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
448 if (dst
.X
<= c
.X
&& c
.X
< dst
.X
+ W
&& dst
.Y
<= c
.Y
&& c
.Y
< dst
.Y
+ H
)
452 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
454 else if (c
.X
< W
&& c
.Y
< H
) okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
455 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
459 /* clipping, src & dst rect don't overlap */
460 resetContent(hCon
, sbSize
, TRUE
);
463 scroll
.Right
= W
- 1;
465 scroll
.Bottom
= H
- 1;
468 ci
.Char
.UnicodeChar
= '#';
469 ci
.Attributes
= TEST_ATTRIB
;
472 clip
.Right
= min(W
+ W
/ 2, sbSize
.X
- 1);
474 clip
.Bottom
= min(H
+ H
/ 2, sbSize
.Y
- 1);
476 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, &clip
, dst
, &ci
), "Scrolling SB\n");
478 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
480 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
482 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
486 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
488 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
489 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
490 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
494 /* clipping, src & dst rect do overlap */
495 resetContent(hCon
, sbSize
, TRUE
);
498 scroll
.Right
= W
- 1;
500 scroll
.Bottom
= H
- 1;
503 ci
.Char
.UnicodeChar
= '#';
504 ci
.Attributes
= TEST_ATTRIB
;
507 clip
.Right
= min(W
+ W
/ 2, sbSize
.X
- 1);
509 clip
.Bottom
= min(H
+ H
/ 2, sbSize
.Y
- 1);
511 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, &clip
, dst
, &ci
), "Scrolling SB\n");
513 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
515 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
517 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
521 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
523 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
524 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
525 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
530 static int mch_count
;
531 /* we need the event as Wine console event generation isn't synchronous
532 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
533 * processes have been called).
535 static HANDLE mch_event
;
536 static BOOL WINAPI
mch(DWORD event
)
543 static void testCtrlHandler(void)
545 ok(!SetConsoleCtrlHandler(mch
, FALSE
), "Shouldn't succeed\n");
546 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Bad error %u\n", GetLastError());
547 ok(SetConsoleCtrlHandler(mch
, TRUE
), "Couldn't set handler\n");
548 /* wine requires the event for the test, as we cannot insure, so far, that event
549 * are processed synchronously in GenerateConsoleCtrlEvent()
551 mch_event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
553 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT
, 0), "Couldn't send ctrl-c event\n");
554 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
555 if (0) ok(mch_count
== 1, "Event isn't synchronous\n");
556 ok(WaitForSingleObject(mch_event
, 3000) == WAIT_OBJECT_0
, "event sending didn't work\n");
557 CloseHandle(mch_event
);
559 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
560 ok(SetConsoleCtrlHandler(NULL
, TRUE
), "Couldn't turn off ctrl-c handling\n");
561 mch_event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
563 if(!(GetVersion() & 0x80000000))
564 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
565 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT
, 0), "Couldn't send ctrl-c event\n");
566 ok(WaitForSingleObject(mch_event
, 3000) == WAIT_TIMEOUT
&& mch_count
== 0, "Event shouldn't have been sent\n");
567 CloseHandle(mch_event
);
568 ok(SetConsoleCtrlHandler(mch
, FALSE
), "Couldn't remove handler\n");
569 ok(!SetConsoleCtrlHandler(mch
, FALSE
), "Shouldn't succeed\n");
570 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Bad error %u\n", GetLastError());
574 * Test console screen buffer:
575 * 1) Try to set invalid handle.
576 * 2) Try to set non-console handles.
577 * 3) Use CONOUT$ file as active SB.
579 * 5) Test output codepage to show it is not a property of SB.
580 * 6) Test switching to old SB if we close all handles to current SB - works
581 * in Windows, TODO in wine.
583 * What is not tested but should be:
584 * 1) ScreenBufferInfo
586 static void testScreenBuffer(HANDLE hConOut
)
588 HANDLE hConOutRW
, hConOutRO
, hConOutWT
;
589 HANDLE hFileOutRW
, hFileOutRO
, hFileOutWT
;
591 char test_str1
[] = "Test for SB1";
592 char test_str2
[] = "Test for SB2";
593 char test_cp866
[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
594 char test_cp1251
[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
595 WCHAR test_unicode
[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
603 /* In the beginning set output codepage to 866 */
604 oldcp
= GetConsoleOutputCP();
605 ok(SetConsoleOutputCP(866), "Cannot set output codepage to 866\n");
607 hConOutRW
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
608 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
609 CONSOLE_TEXTMODE_BUFFER
, NULL
);
610 ok(hConOutRW
!= INVALID_HANDLE_VALUE
,
611 "Cannot create a new screen buffer for ReadWrite\n");
612 hConOutRO
= CreateConsoleScreenBuffer(GENERIC_READ
,
613 FILE_SHARE_READ
, NULL
,
614 CONSOLE_TEXTMODE_BUFFER
, NULL
);
615 ok(hConOutRO
!= INVALID_HANDLE_VALUE
,
616 "Cannot create a new screen buffer for ReadOnly\n");
617 hConOutWT
= CreateConsoleScreenBuffer(GENERIC_WRITE
,
618 FILE_SHARE_WRITE
, NULL
,
619 CONSOLE_TEXTMODE_BUFFER
, NULL
);
620 ok(hConOutWT
!= INVALID_HANDLE_VALUE
,
621 "Cannot create a new screen buffer for WriteOnly\n");
623 hFileOutRW
= CreateFileA("NUL", GENERIC_READ
| GENERIC_WRITE
,
624 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
625 OPEN_EXISTING
, 0, NULL
);
626 ok(hFileOutRW
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for ReadWrite\n");
627 hFileOutRO
= CreateFileA("NUL", GENERIC_READ
, FILE_SHARE_READ
,
628 NULL
, OPEN_EXISTING
, 0, NULL
);
629 ok(hFileOutRO
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for ReadOnly\n");
630 hFileOutWT
= CreateFileA("NUL", GENERIC_WRITE
, FILE_SHARE_WRITE
,
631 NULL
, OPEN_EXISTING
, 0, NULL
);
632 ok(hFileOutWT
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for WriteOnly\n");
634 /* Trying to set invalid handle */
636 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE
),
637 "Shouldn't succeed\n");
638 ok(GetLastError() == ERROR_INVALID_HANDLE
,
639 "GetLastError: expecting %u got %u\n",
640 ERROR_INVALID_HANDLE
, GetLastError());
642 /* Trying to set non-console handles */
644 ok(!SetConsoleActiveScreenBuffer(hFileOutRW
), "Shouldn't succeed\n");
645 ok(GetLastError() == ERROR_INVALID_HANDLE
,
646 "GetLastError: expecting %u got %u\n",
647 ERROR_INVALID_HANDLE
, GetLastError());
650 ok(!SetConsoleActiveScreenBuffer(hFileOutRO
), "Shouldn't succeed\n");
651 ok(GetLastError() == ERROR_INVALID_HANDLE
,
652 "GetLastError: expecting %u got %u\n",
653 ERROR_INVALID_HANDLE
, GetLastError());
656 ok(!SetConsoleActiveScreenBuffer(hFileOutWT
), "Shouldn't succeed\n");
657 ok(GetLastError() == ERROR_INVALID_HANDLE
,
658 "GetLastError: expecting %u got %u\n",
659 ERROR_INVALID_HANDLE
, GetLastError());
661 CloseHandle(hFileOutRW
);
662 CloseHandle(hFileOutRO
);
663 CloseHandle(hFileOutWT
);
665 /* Trying to set SB handles with various access modes */
667 ok(!SetConsoleActiveScreenBuffer(hConOutRO
), "Shouldn't succeed\n");
668 ok(GetLastError() == ERROR_INVALID_HANDLE
,
669 "GetLastError: expecting %u got %u\n",
670 ERROR_INVALID_HANDLE
, GetLastError());
672 ok(SetConsoleActiveScreenBuffer(hConOutWT
), "Couldn't set new WriteOnly SB\n");
674 ok(SetConsoleActiveScreenBuffer(hConOutRW
), "Couldn't set new ReadWrite SB\n");
676 CloseHandle(hConOutWT
);
677 CloseHandle(hConOutRO
);
679 /* Now we have two ReadWrite SB, active must be hConOutRW */
680 /* Open current SB via CONOUT$ */
681 hConOutNew
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0,
682 NULL
, OPEN_EXISTING
, 0, 0);
683 ok(hConOutNew
!= INVALID_HANDLE_VALUE
, "CONOUT$ is not opened\n");
688 SetConsoleCursorPosition(hConOut
, c
);
690 SetConsoleCursorPosition(hConOutRW
, c
);
691 okCURSOR(hConOutNew
, c
);
693 okCURSOR(hConOut
, c
);
698 /* Write using hConOutNew... */
699 SetConsoleCursorPosition(hConOutNew
, c
);
700 ret
= WriteConsoleA(hConOutNew
, test_str2
, lstrlenA(test_str2
), &len
, NULL
);
701 ok (ret
&& len
== lstrlenA(test_str2
), "WriteConsoleA failed\n");
702 /* ... and read it back via hConOutRW */
703 ret
= ReadConsoleOutputCharacterA(hConOutRW
, str_buf
, lstrlenA(test_str2
), c
, &len
);
704 ok(ret
&& len
== lstrlenA(test_str2
), "ReadConsoleOutputCharacterA failed\n");
705 str_buf
[lstrlenA(test_str2
)] = 0;
706 ok(!lstrcmpA(str_buf
, test_str2
), "got '%s' expected '%s'\n", str_buf
, test_str2
);
709 /* Now test output codepage handling. Current is 866 as we set earlier. */
710 SetConsoleCursorPosition(hConOutRW
, c
);
711 ret
= WriteConsoleA(hConOutRW
, test_cp866
, lstrlenA(test_cp866
), &len
, NULL
);
712 ok(ret
&& len
== lstrlenA(test_cp866
), "WriteConsoleA failed\n");
713 ret
= ReadConsoleOutputCharacterW(hConOutRW
, str_wbuf
, lstrlenA(test_cp866
), c
, &len
);
714 ok(ret
&& len
== lstrlenA(test_cp866
), "ReadConsoleOutputCharacterW failed\n");
715 str_wbuf
[lstrlenA(test_cp866
)] = 0;
716 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
719 * cp866 is OK, let's switch to cp1251.
720 * We expect that this codepage will be used in every SB - active and not.
722 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
723 SetConsoleCursorPosition(hConOutRW
, c
);
724 ret
= WriteConsoleA(hConOutRW
, test_cp1251
, lstrlenA(test_cp1251
), &len
, NULL
);
725 ok(ret
&& len
== lstrlenA(test_cp1251
), "WriteConsoleA failed\n");
726 ret
= ReadConsoleOutputCharacterW(hConOutRW
, str_wbuf
, lstrlenA(test_cp1251
), c
, &len
);
727 ok(ret
&& len
== lstrlenA(test_cp1251
), "ReadConsoleOutputCharacterW failed\n");
728 str_wbuf
[lstrlenA(test_cp1251
)] = 0;
729 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
731 /* Check what has happened to hConOut. */
732 SetConsoleCursorPosition(hConOut
, c
);
733 ret
= WriteConsoleA(hConOut
, test_cp1251
, lstrlenA(test_cp1251
), &len
, NULL
);
734 ok(ret
&& len
== lstrlenA(test_cp1251
), "WriteConsoleA failed\n");
735 ret
= ReadConsoleOutputCharacterW(hConOut
, str_wbuf
, lstrlenA(test_cp1251
), c
, &len
);
736 ok(ret
&& len
== lstrlenA(test_cp1251
), "ReadConsoleOutputCharacterW failed\n");
737 str_wbuf
[lstrlenA(test_cp1251
)] = 0;
738 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
740 /* Close all handles of current console SB */
741 CloseHandle(hConOutNew
);
742 CloseHandle(hConOutRW
);
744 /* Now active SB should be hConOut */
745 hConOutNew
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0,
746 NULL
, OPEN_EXISTING
, 0, 0);
747 ok(hConOutNew
!= INVALID_HANDLE_VALUE
, "CONOUT$ is not opened\n");
749 /* Write using hConOutNew... */
750 SetConsoleCursorPosition(hConOutNew
, c
);
751 ret
= WriteConsoleA(hConOutNew
, test_str1
, lstrlenA(test_str1
), &len
, NULL
);
752 ok (ret
&& len
== lstrlenA(test_str1
), "WriteConsoleA failed\n");
753 /* ... and read it back via hConOut */
754 ret
= ReadConsoleOutputCharacterA(hConOut
, str_buf
, lstrlenA(test_str1
), c
, &len
);
755 ok(ret
&& len
== lstrlenA(test_str1
), "ReadConsoleOutputCharacterA failed\n");
756 str_buf
[lstrlenA(test_str1
)] = 0;
757 todo_wine
ok(!lstrcmpA(str_buf
, test_str1
), "got '%s' expected '%s'\n", str_buf
, test_str1
);
758 CloseHandle(hConOutNew
);
760 /* This is not really needed under Windows */
761 SetConsoleActiveScreenBuffer(hConOut
);
763 /* restore codepage */
764 SetConsoleOutputCP(oldcp
);
767 static void test_GetSetConsoleInputExeName(void)
771 char buffer
[MAX_PATH
], module
[MAX_PATH
], *p
;
772 static char input_exe
[MAX_PATH
] = "winetest.exe";
774 SetLastError(0xdeadbeef);
775 ret
= pGetConsoleInputExeNameA(0, NULL
);
776 error
= GetLastError();
777 ok(ret
, "GetConsoleInputExeNameA failed\n");
778 ok(error
== ERROR_BUFFER_OVERFLOW
, "got %u expected ERROR_BUFFER_OVERFLOW\n", error
);
780 SetLastError(0xdeadbeef);
781 ret
= pGetConsoleInputExeNameA(0, buffer
);
782 error
= GetLastError();
783 ok(ret
, "GetConsoleInputExeNameA failed\n");
784 ok(error
== ERROR_BUFFER_OVERFLOW
, "got %u expected ERROR_BUFFER_OVERFLOW\n", error
);
786 GetModuleFileNameA(GetModuleHandle(NULL
), module
, sizeof(module
));
787 p
= strrchr(module
, '\\') + 1;
789 ret
= pGetConsoleInputExeNameA(sizeof(buffer
)/sizeof(buffer
[0]), buffer
);
790 ok(ret
, "GetConsoleInputExeNameA failed\n");
791 todo_wine
ok(!lstrcmpA(buffer
, p
), "got %s expected %s\n", buffer
, p
);
793 SetLastError(0xdeadbeef);
794 ret
= pSetConsoleInputExeNameA(NULL
);
795 error
= GetLastError();
796 ok(!ret
, "SetConsoleInputExeNameA failed\n");
797 ok(error
== ERROR_INVALID_PARAMETER
, "got %u expected ERROR_INVALID_PARAMETER\n", error
);
799 SetLastError(0xdeadbeef);
800 ret
= pSetConsoleInputExeNameA("");
801 error
= GetLastError();
802 ok(!ret
, "SetConsoleInputExeNameA failed\n");
803 ok(error
== ERROR_INVALID_PARAMETER
, "got %u expected ERROR_INVALID_PARAMETER\n", error
);
805 ret
= pSetConsoleInputExeNameA(input_exe
);
806 ok(ret
, "SetConsoleInputExeNameA failed\n");
808 ret
= pGetConsoleInputExeNameA(sizeof(buffer
)/sizeof(buffer
[0]), buffer
);
809 ok(ret
, "GetConsoleInputExeNameA failed\n");
810 ok(!lstrcmpA(buffer
, input_exe
), "got %s expected %s\n", buffer
, input_exe
);
815 HANDLE hConIn
, hConOut
;
817 CONSOLE_SCREEN_BUFFER_INFO sbi
;
819 init_function_pointers();
821 /* be sure we have a clean console (and that's our own)
822 * FIXME: this will make the test fail (currently) if we don't run
824 * Another solution would be to rerun the test under wineconsole with
828 /* first, we detach and open a fresh console to play with */
830 ok(AllocConsole(), "Couldn't alloc console\n");
831 hConIn
= CreateFileA("CONIN$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
832 hConOut
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
834 /* now verify everything's ok */
835 ok(hConIn
!= INVALID_HANDLE_VALUE
, "Opening ConIn\n");
836 ok(hConOut
!= INVALID_HANDLE_VALUE
, "Opening ConOut\n");
838 ok(ret
= GetConsoleScreenBufferInfo(hConOut
, &sbi
), "Getting sb info\n");
841 /* Non interactive tests */
842 testCursor(hConOut
, sbi
.dwSize
);
843 /* will test wrapped (on/off) & processed (on/off) strings output */
844 testWrite(hConOut
, sbi
.dwSize
);
845 /* will test line scrolling at the bottom of the screen */
846 /* testBottomScroll(); */
847 /* will test all the scrolling operations */
848 testScroll(hConOut
, sbi
.dwSize
);
849 /* will test sb creation / modification / codepage handling */
850 testScreenBuffer(hConOut
);
852 /* still to be done: access rights & access on objects */
854 if (!pGetConsoleInputExeNameA
|| !pSetConsoleInputExeNameA
)
856 skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
860 test_GetSetConsoleInputExeName();