2 * Unit tests for console API
4 * Copyright (c) 2003,2004 Eric Pouech
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.
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.
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
21 #include "wine/test.h"
25 /* DEFAULT_ATTRIB is used for all initial filling of the console.
26 * all modifications are made with TEST_ATTRIB so that we could check
27 * what has to be modified or not
29 #define TEST_ATTRIB (BACKGROUND_BLUE | FOREGROUND_GREEN)
30 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
31 /* when filling the screen with non-blank chars, this macro defines
32 * what character should be at position 'c'
34 #define CONTENT(c) ('A' + (((c).Y * 17 + (c).X) % 23))
36 #define okCURSOR(hCon, c) do { \
37 CONSOLE_SCREEN_BUFFER_INFO __sbi; \
38 BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
39 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
40 ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
41 (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
44 #define okCHAR(hCon, c, ch, attr) do { \
45 char __ch; WORD __attr; DWORD __len; BOOL expect; \
46 expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
47 ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
48 expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
49 ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
52 /* FIXME: this could be optimized on a speed point of view */
53 static void resetContent(HANDLE hCon, COORD sbSize, BOOL content)
56 WORD attr = DEFAULT_ATTRIB;
60 for (c.X = 0; c.X < sbSize.X; c.X++)
62 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
64 ch = (content) ? CONTENT(c) : ' ';
65 WriteConsoleOutputAttribute(hCon, &attr, 1, c, &len);
66 WriteConsoleOutputCharacterA(hCon, &ch, 1, c, &len);
71 static void testCursor(HANDLE hCon, COORD sbSize)
76 ok(SetConsoleCursorPosition(0, c) == 0, "No handle\n");
77 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
78 ERROR_INVALID_HANDLE, GetLastError());
81 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
86 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right\n");
91 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
92 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
93 ERROR_INVALID_PARAMETER, GetLastError());
97 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
98 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
99 ERROR_INVALID_PARAMETER, GetLastError());
103 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
104 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
105 ERROR_INVALID_PARAMETER, GetLastError());
109 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
110 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
111 ERROR_INVALID_PARAMETER, GetLastError());
114 static void testWriteSimple(HANDLE hCon, COORD sbSize)
118 const char* mytest = "abcdefg";
119 const int mylen = strlen(mytest);
121 /* single line write */
123 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
125 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
127 for (c.X = 0; c.X < mylen; c.X++)
129 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
133 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
136 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
140 const char* mytest = "123";
141 const int mylen = strlen(mytest);
145 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
146 "clearing wrap at EOL & processed output\n");
148 /* write line, wrapping disabled, buffer exceeds sb width */
149 c.X = sbSize.X - 3; c.Y = 0;
150 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
152 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
153 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
155 for (p = mylen - 3; p < mylen; p++)
157 c.X = sbSize.X - 3 + p % 3;
158 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
162 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
164 p = sbSize.X - 3 + mylen % 3;
167 /* write line, wrapping disabled, strings end on end of line */
168 c.X = sbSize.X - mylen; c.Y = 0;
169 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
171 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
174 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
178 const char* mytest = "abcd\nf\tg";
179 const int mylen = strlen(mytest);
180 const int mylen2 = strchr(mytest, '\n') - mytest;
183 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
184 "clearing wrap at EOL & setting processed output\n");
186 /* write line, wrapping disabled, buffer exceeds sb width */
187 c.X = sbSize.X - 5; c.Y = 0;
188 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
190 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
192 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
194 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
196 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
199 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
200 for (c.X = 1; c.X < 8; c.X++)
201 okCHAR(hCon, c, ' ', TEST_ATTRIB);
202 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
204 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
208 /* write line, wrapping disabled, strings end on end of line */
209 c.X = sbSize.X - 4; c.Y = 0;
210 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
212 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
214 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
216 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
219 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
220 for (c.X = 1; c.X < 8; c.X++)
221 okCHAR(hCon, c, ' ', TEST_ATTRIB);
222 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
224 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
228 /* write line, wrapping disabled, strings end after end of line */
229 c.X = sbSize.X - 3; c.Y = 0;
230 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
232 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
234 for (p = mylen2 - 3; p < mylen2; p++)
236 c.X = sbSize.X - 3 + p % 3;
237 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
240 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
241 for (c.X = 1; c.X < 8; c.X++)
242 okCHAR(hCon, c, ' ', TEST_ATTRIB);
243 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
245 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
250 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
254 const char* mytest = "abcd\nf\tg";
255 const int mylen = strlen(mytest);
258 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
259 "setting wrap at EOL & clearing processed output\n");
261 /* write line, wrapping enabled, buffer doesn't exceed sb width */
262 c.X = sbSize.X - 9; c.Y = 0;
263 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
265 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
267 for (p = 0; p < mylen; p++)
269 c.X = sbSize.X - 9 + p;
270 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
272 c.X = sbSize.X - 9 + mylen;
273 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
275 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
277 /* write line, wrapping enabled, buffer does exceed sb width */
278 c.X = sbSize.X - 3; c.Y = 0;
279 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
283 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
286 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
290 const char* mytest = "abcd\nf\tg";
291 const int mylen = strlen(mytest);
294 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
295 "setting wrap at EOL & processed output\n");
297 /* write line, wrapping enabled, buffer doesn't exceed sb width */
298 c.X = sbSize.X - 9; c.Y = 0;
299 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
301 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
302 for (p = 0; p < 4; p++)
304 c.X = sbSize.X - 9 + p;
305 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
307 c.X = sbSize.X - 9 + p;
308 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
310 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
311 for (c.X = 1; c.X < 8; c.X++)
312 okCHAR(hCon, c, ' ', TEST_ATTRIB);
313 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
315 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
318 /* write line, wrapping enabled, buffer does exceed sb width */
319 c.X = sbSize.X - 3; c.Y = 2;
320 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
322 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
323 for (p = 0; p < 3; p++)
325 c.X = sbSize.X - 3 + p;
326 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
329 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
331 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
334 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
335 for (c.X = 1; c.X < 8; c.X++)
336 okCHAR(hCon, c, ' ', TEST_ATTRIB);
337 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
339 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
343 static void testWrite(HANDLE hCon, COORD sbSize)
345 /* FIXME: should in fact insure that the sb is at least 10 character wide */
346 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
347 resetContent(hCon, sbSize, FALSE);
348 testWriteSimple(hCon, sbSize);
349 resetContent(hCon, sbSize, FALSE);
350 testWriteNotWrappedNotProcessed(hCon, sbSize);
351 resetContent(hCon, sbSize, FALSE);
352 testWriteNotWrappedProcessed(hCon, sbSize);
353 resetContent(hCon, sbSize, FALSE);
354 testWriteWrappedNotProcessed(hCon, sbSize);
355 resetContent(hCon, sbSize, FALSE);
356 testWriteWrappedProcessed(hCon, sbSize);
359 static void testScroll(HANDLE hCon, COORD sbSize)
361 SMALL_RECT scroll, clip;
368 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
369 #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)
371 /* no clipping, src & dst rect don't overlap */
372 resetContent(hCon, sbSize, TRUE);
375 scroll.Right = W - 1;
377 scroll.Bottom = H - 1;
380 ci.Char.UnicodeChar = '#';
381 ci.Attributes = TEST_ATTRIB;
384 clip.Right = sbSize.X - 1;
386 clip.Bottom = sbSize.Y - 1;
388 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
390 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
392 for (c.X = 0; c.X < sbSize.X; c.X++)
394 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
398 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
400 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
401 okCHAR(hCon, c, '#', TEST_ATTRIB);
402 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
406 /* no clipping, src & dst rect do overlap */
407 resetContent(hCon, sbSize, TRUE);
410 scroll.Right = W - 1;
412 scroll.Bottom = H - 1;
415 ci.Char.UnicodeChar = '#';
416 ci.Attributes = TEST_ATTRIB;
419 clip.Right = sbSize.X - 1;
421 clip.Bottom = sbSize.Y - 1;
423 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
425 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
427 for (c.X = 0; c.X < sbSize.X; c.X++)
429 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
433 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
435 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
436 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
440 /* clipping, src & dst rect don't overlap */
441 resetContent(hCon, sbSize, TRUE);
444 scroll.Right = W - 1;
446 scroll.Bottom = H - 1;
449 ci.Char.UnicodeChar = '#';
450 ci.Attributes = TEST_ATTRIB;
453 clip.Right = min(W + W / 2, sbSize.X - 1);
455 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
457 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
459 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
461 for (c.X = 0; c.X < sbSize.X; c.X++)
463 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
467 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
469 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
470 okCHAR(hCon, c, '#', TEST_ATTRIB);
471 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
475 /* clipping, src & dst rect do overlap */
476 resetContent(hCon, sbSize, TRUE);
479 scroll.Right = W - 1;
481 scroll.Bottom = H - 1;
484 ci.Char.UnicodeChar = '#';
485 ci.Attributes = TEST_ATTRIB;
488 clip.Right = min(W + W / 2, sbSize.X - 1);
490 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
492 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
494 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
496 for (c.X = 0; c.X < sbSize.X; c.X++)
498 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
502 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
504 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
505 okCHAR(hCon, c, '#', TEST_ATTRIB);
506 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
511 static int mch_count;
512 /* we need the event as Wine console event generation isn't synchronous
513 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
514 * processes have been called).
516 static HANDLE mch_event;
517 static BOOL WINAPI mch(DWORD event)
524 static void testCtrlHandler(void)
526 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
527 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
528 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
529 /* wine requires the event for the test, as we cannot insure, so far, that event
530 * are processed synchronously in GenerateConsoleCtrlEvent()
532 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
534 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
535 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
536 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
537 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
538 CloseHandle(mch_event);
540 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
541 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
542 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
544 if(!(GetVersion() & 0x80000000))
545 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
546 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
547 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
548 CloseHandle(mch_event);
549 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
550 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
551 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
556 HANDLE hConIn, hConOut;
558 CONSOLE_SCREEN_BUFFER_INFO sbi;
560 /* be sure we have a clean console (and that's our own)
561 * FIXME: this will make the test fail (currently) if we don't run
563 * Another solution would be to rerun the test under wineconsole with
567 /* first, we detach and open a fresh console to play with */
569 ok(AllocConsole(), "Couldn't alloc console\n");
570 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
571 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
573 /* now verify everything's ok */
574 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
575 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
577 ok(ret = GetConsoleScreenBufferInfo(hConOut, &sbi), "Getting sb info\n");
580 /* Non interactive tests */
581 testCursor(hConOut, sbi.dwSize);
582 /* will test wrapped (on/off) & processed (on/off) strings output */
583 testWrite(hConOut, sbi.dwSize);
584 /* will test line scrolling at the bottom of the screen */
585 /* testBottomScroll(); */
586 /* will test all the scrolling operations */
587 testScroll(hConOut, sbi.dwSize);
588 /* will test sb creation / modification... */
589 /* testScreenBuffer() */
591 /* still to be done: access rights & access on objects */