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 /* DEFAULT_ATTRIB is used for all initial filling of the console.
27 * all modifications are made with TEST_ATTRIB so that we could check
28 * what has to be modified or not
30 #define TEST_ATTRIB (BACKGROUND_BLUE | FOREGROUND_GREEN)
31 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
32 /* when filling the screen with non-blank chars, this macro defines
33 * what character should be at position 'c'
35 #define CONTENT(c) ('A' + (((c).Y * 17 + (c).X) % 23))
37 #define okCURSOR(hCon, c) do { \
38 CONSOLE_SCREEN_BUFFER_INFO __sbi; \
39 BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
40 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
41 ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
42 (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
45 #define okCHAR(hCon, c, ch, attr) do { \
46 char __ch; WORD __attr; DWORD __len; BOOL expect; \
47 expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
48 ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
49 expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
50 ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
53 /* FIXME: this could be optimized on a speed point of view */
54 static void resetContent(HANDLE hCon
, COORD sbSize
, BOOL content
)
57 WORD attr
= DEFAULT_ATTRIB
;
61 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
63 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
65 ch
= (content
) ? CONTENT(c
) : ' ';
66 WriteConsoleOutputAttribute(hCon
, &attr
, 1, c
, &len
);
67 WriteConsoleOutputCharacterA(hCon
, &ch
, 1, c
, &len
);
72 static void testCursor(HANDLE hCon
, COORD sbSize
)
77 ok(SetConsoleCursorPosition(0, c
) == 0, "No handle\n");
78 ok(GetLastError() == ERROR_INVALID_HANDLE
, "GetLastError: expecting %u got %u\n",
79 ERROR_INVALID_HANDLE
, GetLastError());
82 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left\n");
87 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in lower-right\n");
92 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
93 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
94 ERROR_INVALID_PARAMETER
, GetLastError());
98 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
99 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
100 ERROR_INVALID_PARAMETER
, GetLastError());
104 ok(SetConsoleCursorPosition(hCon
, c
) == 0, "Cursor is outside\n");
105 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "GetLastError: expecting %u got %u\n",
106 ERROR_INVALID_PARAMETER
, GetLastError());
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());
115 static void testWriteSimple(HANDLE hCon
, COORD sbSize
)
119 const char* mytest
= "abcdefg";
120 const int mylen
= strlen(mytest
);
122 /* single line write */
124 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left\n");
126 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
128 for (c
.X
= 0; c
.X
< mylen
; c
.X
++)
130 okCHAR(hCon
, c
, mytest
[c
.X
], TEST_ATTRIB
);
134 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
137 static void testWriteNotWrappedNotProcessed(HANDLE hCon
, COORD sbSize
)
141 const char* mytest
= "123";
142 const int mylen
= strlen(mytest
);
146 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, mode
& ~(ENABLE_PROCESSED_OUTPUT
|ENABLE_WRAP_AT_EOL_OUTPUT
)),
147 "clearing wrap at EOL & processed output\n");
149 /* write line, wrapping disabled, buffer exceeds sb width */
150 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
151 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
153 ret
= WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
);
154 ok(ret
!= 0 && len
== mylen
, "Couldn't write, ret = %d, len = %d\n", ret
, len
);
156 for (p
= mylen
- 3; p
< mylen
; p
++)
158 c
.X
= sbSize
.X
- 3 + p
% 3;
159 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
163 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
165 p
= sbSize
.X
- 3 + mylen
% 3;
168 /* write line, wrapping disabled, strings end on end of line */
169 c
.X
= sbSize
.X
- mylen
; c
.Y
= 0;
170 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
172 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
175 static void testWriteNotWrappedProcessed(HANDLE hCon
, COORD sbSize
)
179 const char* mytest
= "abcd\nf\tg";
180 const int mylen
= strlen(mytest
);
181 const int mylen2
= strchr(mytest
, '\n') - mytest
;
184 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, (mode
| ENABLE_PROCESSED_OUTPUT
) & ~ENABLE_WRAP_AT_EOL_OUTPUT
),
185 "clearing wrap at EOL & setting processed output\n");
187 /* write line, wrapping disabled, buffer exceeds sb width */
188 c
.X
= sbSize
.X
- 5; c
.Y
= 0;
189 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-5\n");
191 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
193 for (c
.X
= sbSize
.X
- 5; c
.X
< sbSize
.X
- 1; c
.X
++)
195 okCHAR(hCon
, c
, mytest
[c
.X
- sbSize
.X
+ 5], TEST_ATTRIB
);
197 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
200 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
201 for (c
.X
= 1; c
.X
< 8; c
.X
++)
202 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
203 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
205 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
209 /* write line, wrapping disabled, strings end on end of line */
210 c
.X
= sbSize
.X
- 4; c
.Y
= 0;
211 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-4\n");
213 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
215 for (c
.X
= sbSize
.X
- 4; c
.X
< sbSize
.X
; c
.X
++)
217 okCHAR(hCon
, c
, mytest
[c
.X
- sbSize
.X
+ 4], TEST_ATTRIB
);
220 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
221 for (c
.X
= 1; c
.X
< 8; c
.X
++)
222 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
223 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
225 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
229 /* write line, wrapping disabled, strings end after end of line */
230 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
231 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-4\n");
233 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
235 for (p
= mylen2
- 3; p
< mylen2
; p
++)
237 c
.X
= sbSize
.X
- 3 + p
% 3;
238 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
241 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
242 for (c
.X
= 1; c
.X
< 8; c
.X
++)
243 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
244 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
246 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
251 static void testWriteWrappedNotProcessed(HANDLE hCon
, COORD sbSize
)
255 const char* mytest
= "abcd\nf\tg";
256 const int mylen
= strlen(mytest
);
259 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
,(mode
| ENABLE_WRAP_AT_EOL_OUTPUT
) & ~(ENABLE_PROCESSED_OUTPUT
)),
260 "setting wrap at EOL & clearing processed output\n");
262 /* write line, wrapping enabled, buffer doesn't exceed sb width */
263 c
.X
= sbSize
.X
- 9; c
.Y
= 0;
264 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-9\n");
266 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
268 for (p
= 0; p
< mylen
; p
++)
270 c
.X
= sbSize
.X
- 9 + p
;
271 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
273 c
.X
= sbSize
.X
- 9 + mylen
;
274 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
276 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
278 /* write line, wrapping enabled, buffer does exceed sb width */
279 c
.X
= sbSize
.X
- 3; c
.Y
= 0;
280 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
284 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
287 static void testWriteWrappedProcessed(HANDLE hCon
, COORD sbSize
)
291 const char* mytest
= "abcd\nf\tg";
292 const int mylen
= strlen(mytest
);
295 ok(GetConsoleMode(hCon
, &mode
) && SetConsoleMode(hCon
, mode
| (ENABLE_WRAP_AT_EOL_OUTPUT
|ENABLE_PROCESSED_OUTPUT
)),
296 "setting wrap at EOL & processed output\n");
298 /* write line, wrapping enabled, buffer doesn't exceed sb width */
299 c
.X
= sbSize
.X
- 9; c
.Y
= 0;
300 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-9\n");
302 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
303 for (p
= 0; p
< 4; p
++)
305 c
.X
= sbSize
.X
- 9 + p
;
306 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
308 c
.X
= sbSize
.X
- 9 + p
;
309 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
311 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
312 for (c
.X
= 1; c
.X
< 8; c
.X
++)
313 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
314 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
316 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
319 /* write line, wrapping enabled, buffer does exceed sb width */
320 c
.X
= sbSize
.X
- 3; c
.Y
= 2;
321 ok(SetConsoleCursorPosition(hCon
, c
) != 0, "Cursor in upper-left-3\n");
323 ok(WriteConsole(hCon
, mytest
, mylen
, &len
, NULL
) != 0 && len
== mylen
, "WriteConsole\n");
324 for (p
= 0; p
< 3; p
++)
326 c
.X
= sbSize
.X
- 3 + p
;
327 okCHAR(hCon
, c
, mytest
[p
], TEST_ATTRIB
);
330 okCHAR(hCon
, c
, mytest
[3], TEST_ATTRIB
);
332 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
335 okCHAR(hCon
, c
, mytest
[5], TEST_ATTRIB
);
336 for (c
.X
= 1; c
.X
< 8; c
.X
++)
337 okCHAR(hCon
, c
, ' ', TEST_ATTRIB
);
338 okCHAR(hCon
, c
, mytest
[7], TEST_ATTRIB
);
340 okCHAR(hCon
, c
, ' ', DEFAULT_ATTRIB
);
344 static void testWrite(HANDLE hCon
, COORD sbSize
)
346 /* FIXME: should in fact insure that the sb is at least 10 character wide */
347 ok(SetConsoleTextAttribute(hCon
, TEST_ATTRIB
), "Setting default text color\n");
348 resetContent(hCon
, sbSize
, FALSE
);
349 testWriteSimple(hCon
, sbSize
);
350 resetContent(hCon
, sbSize
, FALSE
);
351 testWriteNotWrappedNotProcessed(hCon
, sbSize
);
352 resetContent(hCon
, sbSize
, FALSE
);
353 testWriteNotWrappedProcessed(hCon
, sbSize
);
354 resetContent(hCon
, sbSize
, FALSE
);
355 testWriteWrappedNotProcessed(hCon
, sbSize
);
356 resetContent(hCon
, sbSize
, FALSE
);
357 testWriteWrappedProcessed(hCon
, sbSize
);
360 static void testScroll(HANDLE hCon
, COORD sbSize
)
362 SMALL_RECT scroll
, clip
;
369 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
370 #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)
372 /* no clipping, src & dst rect don't overlap */
373 resetContent(hCon
, sbSize
, TRUE
);
376 scroll
.Right
= W
- 1;
378 scroll
.Bottom
= H
- 1;
381 ci
.Char
.UnicodeChar
= '#';
382 ci
.Attributes
= TEST_ATTRIB
;
385 clip
.Right
= sbSize
.X
- 1;
387 clip
.Bottom
= sbSize
.Y
- 1;
389 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, NULL
, dst
, &ci
), "Scrolling SB\n");
391 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
393 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
395 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
399 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
401 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
402 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
403 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
407 /* no clipping, src & dst rect do overlap */
408 resetContent(hCon
, sbSize
, TRUE
);
411 scroll
.Right
= W
- 1;
413 scroll
.Bottom
= H
- 1;
416 ci
.Char
.UnicodeChar
= '#';
417 ci
.Attributes
= TEST_ATTRIB
;
420 clip
.Right
= sbSize
.X
- 1;
422 clip
.Bottom
= sbSize
.Y
- 1;
424 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, NULL
, dst
, &ci
), "Scrolling SB\n");
426 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
428 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
430 if (dst
.X
<= c
.X
&& c
.X
< dst
.X
+ W
&& dst
.Y
<= c
.Y
&& c
.Y
< dst
.Y
+ H
)
434 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
436 else if (c
.X
< W
&& c
.Y
< H
) okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
437 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
441 /* clipping, src & dst rect don't overlap */
442 resetContent(hCon
, sbSize
, TRUE
);
445 scroll
.Right
= W
- 1;
447 scroll
.Bottom
= H
- 1;
450 ci
.Char
.UnicodeChar
= '#';
451 ci
.Attributes
= TEST_ATTRIB
;
454 clip
.Right
= min(W
+ W
/ 2, sbSize
.X
- 1);
456 clip
.Bottom
= min(H
+ H
/ 2, sbSize
.Y
- 1);
458 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, &clip
, dst
, &ci
), "Scrolling SB\n");
460 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
462 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
464 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
468 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
470 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
471 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
472 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
476 /* clipping, src & dst rect do overlap */
477 resetContent(hCon
, sbSize
, TRUE
);
480 scroll
.Right
= W
- 1;
482 scroll
.Bottom
= H
- 1;
485 ci
.Char
.UnicodeChar
= '#';
486 ci
.Attributes
= TEST_ATTRIB
;
489 clip
.Right
= min(W
+ W
/ 2, sbSize
.X
- 1);
491 clip
.Bottom
= min(H
+ H
/ 2, sbSize
.Y
- 1);
493 ok(ScrollConsoleScreenBuffer(hCon
, &scroll
, &clip
, dst
, &ci
), "Scrolling SB\n");
495 for (c
.Y
= 0; c
.Y
< sbSize
.Y
; c
.Y
++)
497 for (c
.X
= 0; c
.X
< sbSize
.X
; c
.X
++)
499 if (IN_SRECT2(scroll
, dst
, c
) && IN_SRECT(clip
, c
))
503 okCHAR(hCon
, c
, CONTENT(tc
), DEFAULT_ATTRIB
);
505 else if (IN_SRECT(scroll
, c
) && IN_SRECT(clip
, c
))
506 okCHAR(hCon
, c
, '#', TEST_ATTRIB
);
507 else okCHAR(hCon
, c
, CONTENT(c
), DEFAULT_ATTRIB
);
512 static int mch_count
;
513 /* we need the event as Wine console event generation isn't synchronous
514 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
515 * processes have been called).
517 static HANDLE mch_event
;
518 static BOOL WINAPI
mch(DWORD event
)
525 static void testCtrlHandler(void)
527 ok(!SetConsoleCtrlHandler(mch
, FALSE
), "Shouldn't succeed\n");
528 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Bad error %u\n", GetLastError());
529 ok(SetConsoleCtrlHandler(mch
, TRUE
), "Couldn't set handler\n");
530 /* wine requires the event for the test, as we cannot insure, so far, that event
531 * are processed synchronously in GenerateConsoleCtrlEvent()
533 mch_event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
535 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT
, 0), "Couldn't send ctrl-c event\n");
536 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
537 if (0) ok(mch_count
== 1, "Event isn't synchronous\n");
538 ok(WaitForSingleObject(mch_event
, 3000) == WAIT_OBJECT_0
, "event sending didn't work\n");
539 CloseHandle(mch_event
);
541 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
542 ok(SetConsoleCtrlHandler(NULL
, TRUE
), "Couldn't turn off ctrl-c handling\n");
543 mch_event
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
545 if(!(GetVersion() & 0x80000000))
546 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
547 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT
, 0), "Couldn't send ctrl-c event\n");
548 ok(WaitForSingleObject(mch_event
, 3000) == WAIT_TIMEOUT
&& mch_count
== 0, "Event shouldn't have been sent\n");
549 CloseHandle(mch_event
);
550 ok(SetConsoleCtrlHandler(mch
, FALSE
), "Couldn't remove handler\n");
551 ok(!SetConsoleCtrlHandler(mch
, FALSE
), "Shouldn't succeed\n");
552 ok(GetLastError() == ERROR_INVALID_PARAMETER
, "Bad error %u\n", GetLastError());
556 * Test console screen buffer:
557 * 1) Try to set invalid handle.
558 * 2) Try to set non-console handles.
559 * 3) Use CONOUT$ file as active SB.
561 * 5) Test output codepage to show it is not a property of SB.
562 * 6) Test switching to old SB if we close all handles to current SB - works
563 * in Windows, TODO in wine.
565 * What is not tested but should be:
566 * 1) ScreenBufferInfo
568 static void testScreenBuffer(HANDLE hConOut
)
570 HANDLE hConOutRW
, hConOutRO
, hConOutWT
;
571 HANDLE hFileOutRW
, hFileOutRO
, hFileOutWT
;
573 char test_str1
[] = "Test for SB1";
574 char test_str2
[] = "Test for SB2";
575 char test_cp866
[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
576 char test_cp1251
[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
577 WCHAR test_unicode
[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
585 /* In the beginning set output codepage to 866 */
586 oldcp
= GetConsoleOutputCP();
587 ok(SetConsoleOutputCP(866), "Cannot set output codepage to 866\n");
589 hConOutRW
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
590 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
591 CONSOLE_TEXTMODE_BUFFER
, NULL
);
592 ok(hConOutRW
!= INVALID_HANDLE_VALUE
,
593 "Cannot create a new screen buffer for ReadWrite\n");
594 hConOutRO
= CreateConsoleScreenBuffer(GENERIC_READ
,
595 FILE_SHARE_READ
, NULL
,
596 CONSOLE_TEXTMODE_BUFFER
, NULL
);
597 ok(hConOutRO
!= INVALID_HANDLE_VALUE
,
598 "Cannot create a new screen buffer for ReadOnly\n");
599 hConOutWT
= CreateConsoleScreenBuffer(GENERIC_WRITE
,
600 FILE_SHARE_WRITE
, NULL
,
601 CONSOLE_TEXTMODE_BUFFER
, NULL
);
602 ok(hConOutWT
!= INVALID_HANDLE_VALUE
,
603 "Cannot create a new screen buffer for WriteOnly\n");
605 hFileOutRW
= CreateFileA("NUL", GENERIC_READ
| GENERIC_WRITE
,
606 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
,
607 OPEN_EXISTING
, 0, NULL
);
608 ok(hFileOutRW
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for ReadWrite\n");
609 hFileOutRO
= CreateFileA("NUL", GENERIC_READ
, FILE_SHARE_READ
,
610 NULL
, OPEN_EXISTING
, 0, NULL
);
611 ok(hFileOutRO
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for ReadOnly\n");
612 hFileOutWT
= CreateFileA("NUL", GENERIC_WRITE
, FILE_SHARE_WRITE
,
613 NULL
, OPEN_EXISTING
, 0, NULL
);
614 ok(hFileOutWT
!= INVALID_HANDLE_VALUE
, "Cannot open NUL for WriteOnly\n");
616 /* Trying to set invalid handle */
618 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE
),
619 "Shouldn't succeed\n");
620 ok(GetLastError() == ERROR_INVALID_HANDLE
,
621 "GetLastError: expecting %u got %u\n",
622 ERROR_INVALID_HANDLE
, GetLastError());
624 /* Trying to set non-console handles */
626 ok(!SetConsoleActiveScreenBuffer(hFileOutRW
), "Shouldn't succeed\n");
627 ok(GetLastError() == ERROR_INVALID_HANDLE
,
628 "GetLastError: expecting %u got %u\n",
629 ERROR_INVALID_HANDLE
, GetLastError());
632 ok(!SetConsoleActiveScreenBuffer(hFileOutRO
), "Shouldn't succeed\n");
633 ok(GetLastError() == ERROR_INVALID_HANDLE
,
634 "GetLastError: expecting %u got %u\n",
635 ERROR_INVALID_HANDLE
, GetLastError());
638 ok(!SetConsoleActiveScreenBuffer(hFileOutWT
), "Shouldn't succeed\n");
639 ok(GetLastError() == ERROR_INVALID_HANDLE
,
640 "GetLastError: expecting %u got %u\n",
641 ERROR_INVALID_HANDLE
, GetLastError());
643 CloseHandle(hFileOutRW
);
644 CloseHandle(hFileOutRO
);
645 CloseHandle(hFileOutWT
);
647 /* Trying to set SB handles with various access modes */
649 ok(!SetConsoleActiveScreenBuffer(hConOutRO
), "Shouldn't succeed\n");
650 ok(GetLastError() == ERROR_INVALID_HANDLE
,
651 "GetLastError: expecting %u got %u\n",
652 ERROR_INVALID_HANDLE
, GetLastError());
654 ok(SetConsoleActiveScreenBuffer(hConOutWT
), "Couldn't set new WriteOnly SB\n");
656 ok(SetConsoleActiveScreenBuffer(hConOutRW
), "Couldn't set new ReadWrite SB\n");
658 CloseHandle(hConOutWT
);
659 CloseHandle(hConOutRO
);
661 /* Now we have two ReadWrite SB, active must be hConOutRW */
662 /* Open current SB via CONOUT$ */
663 hConOutNew
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0,
664 NULL
, OPEN_EXISTING
, 0, 0);
665 ok(hConOutNew
!= INVALID_HANDLE_VALUE
, "CONOUT$ is not opened\n");
670 SetConsoleCursorPosition(hConOut
, c
);
672 SetConsoleCursorPosition(hConOutRW
, c
);
673 okCURSOR(hConOutNew
, c
);
675 okCURSOR(hConOut
, c
);
680 /* Write using hConOutNew... */
681 SetConsoleCursorPosition(hConOutNew
, c
);
682 ret
= WriteConsoleA(hConOutNew
, test_str2
, lstrlenA(test_str2
), &len
, NULL
);
683 ok (ret
&& len
== lstrlenA(test_str2
), "WriteConsoleA failed\n");
684 /* ... and read it back via hConOutRW */
685 ret
= ReadConsoleOutputCharacterA(hConOutRW
, str_buf
, lstrlenA(test_str2
), c
, &len
);
686 ok(ret
&& len
== lstrlenA(test_str2
), "ReadConsoleOutputCharacterA failed\n");
687 str_buf
[lstrlenA(test_str2
)] = 0;
688 ok(!lstrcmpA(str_buf
, test_str2
), "got '%s' expected '%s'\n", str_buf
, test_str2
);
691 /* Now test output codepage handling. Current is 866 as we set earlier. */
692 SetConsoleCursorPosition(hConOutRW
, c
);
693 ret
= WriteConsoleA(hConOutRW
, test_cp866
, lstrlenA(test_cp866
), &len
, NULL
);
694 ok(ret
&& len
== lstrlenA(test_cp866
), "WriteConsoleA failed\n");
695 ret
= ReadConsoleOutputCharacterW(hConOutRW
, str_wbuf
, lstrlenA(test_cp866
), c
, &len
);
696 ok(ret
&& len
== lstrlenA(test_cp866
), "ReadConsoleOutputCharacterW failed\n");
697 str_wbuf
[lstrlenA(test_cp866
)] = 0;
698 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
701 * cp866 is OK, let's switch to cp1251.
702 * We expect that this codepage will be used in every SB - active and not.
704 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
705 SetConsoleCursorPosition(hConOutRW
, c
);
706 ret
= WriteConsoleA(hConOutRW
, test_cp1251
, lstrlenA(test_cp1251
), &len
, NULL
);
707 ok(ret
&& len
== lstrlenA(test_cp1251
), "WriteConsoleA failed\n");
708 ret
= ReadConsoleOutputCharacterW(hConOutRW
, str_wbuf
, lstrlenA(test_cp1251
), c
, &len
);
709 ok(ret
&& len
== lstrlenA(test_cp1251
), "ReadConsoleOutputCharacterW failed\n");
710 str_wbuf
[lstrlenA(test_cp1251
)] = 0;
711 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
713 /* Check what has happened to hConOut. */
714 SetConsoleCursorPosition(hConOut
, c
);
715 ret
= WriteConsoleA(hConOut
, test_cp1251
, lstrlenA(test_cp1251
), &len
, NULL
);
716 ok(ret
&& len
== lstrlenA(test_cp1251
), "WriteConsoleA failed\n");
717 ret
= ReadConsoleOutputCharacterW(hConOut
, str_wbuf
, lstrlenA(test_cp1251
), c
, &len
);
718 ok(ret
&& len
== lstrlenA(test_cp1251
), "ReadConsoleOutputCharacterW failed\n");
719 str_wbuf
[lstrlenA(test_cp1251
)] = 0;
720 ok(!lstrcmpW(str_wbuf
, test_unicode
), "string does not match the pattern\n");
722 /* Close all handles of current console SB */
723 CloseHandle(hConOutNew
);
724 CloseHandle(hConOutRW
);
726 /* Now active SB should be hConOut */
727 hConOutNew
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0,
728 NULL
, OPEN_EXISTING
, 0, 0);
729 ok(hConOutNew
!= INVALID_HANDLE_VALUE
, "CONOUT$ is not opened\n");
731 /* Write using hConOutNew... */
732 SetConsoleCursorPosition(hConOutNew
, c
);
733 ret
= WriteConsoleA(hConOutNew
, test_str1
, lstrlenA(test_str1
), &len
, NULL
);
734 ok (ret
&& len
== lstrlenA(test_str1
), "WriteConsoleA failed\n");
735 /* ... and read it back via hConOut */
736 ret
= ReadConsoleOutputCharacterA(hConOut
, str_buf
, lstrlenA(test_str1
), c
, &len
);
737 ok(ret
&& len
== lstrlenA(test_str1
), "ReadConsoleOutputCharacterA failed\n");
738 str_buf
[lstrlenA(test_str1
)] = 0;
739 todo_wine
ok(!lstrcmpA(str_buf
, test_str1
), "got '%s' expected '%s'\n", str_buf
, test_str1
);
740 CloseHandle(hConOutNew
);
742 /* This is not really needed under Windows */
743 SetConsoleActiveScreenBuffer(hConOut
);
745 /* restore codepage */
746 SetConsoleOutputCP(oldcp
);
751 HANDLE hConIn
, hConOut
;
753 CONSOLE_SCREEN_BUFFER_INFO sbi
;
755 /* be sure we have a clean console (and that's our own)
756 * FIXME: this will make the test fail (currently) if we don't run
758 * Another solution would be to rerun the test under wineconsole with
762 /* first, we detach and open a fresh console to play with */
764 ok(AllocConsole(), "Couldn't alloc console\n");
765 hConIn
= CreateFileA("CONIN$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
766 hConOut
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, 0);
768 /* now verify everything's ok */
769 ok(hConIn
!= INVALID_HANDLE_VALUE
, "Opening ConIn\n");
770 ok(hConOut
!= INVALID_HANDLE_VALUE
, "Opening ConOut\n");
772 ok(ret
= GetConsoleScreenBufferInfo(hConOut
, &sbi
), "Getting sb info\n");
775 /* Non interactive tests */
776 testCursor(hConOut
, sbi
.dwSize
);
777 /* will test wrapped (on/off) & processed (on/off) strings output */
778 testWrite(hConOut
, sbi
.dwSize
);
779 /* will test line scrolling at the bottom of the screen */
780 /* testBottomScroll(); */
781 /* will test all the scrolling operations */
782 testScroll(hConOut
, sbi
.dwSize
);
783 /* will test sb creation / modification / codepage handling */
784 testScreenBuffer(hConOut
);
786 /* still to be done: access rights & access on objects */