2 * Unit tests for console API
4 * Copyright (c) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 %lu\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 %lu\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 %lu\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 %lu\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 %lu\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)
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");
154 mylen = strlen(mytest);
156 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
157 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %ld\n", ret, len);
159 for (p = mylen - 3; p < mylen; p++)
161 c.X = sbSize.X - 3 + p % 3;
162 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
166 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
168 p = sbSize.X - 3 + mylen % 3;
171 /* write line, wrapping disabled, strings end on end of line */
172 c.X = sbSize.X - mylen; c.Y = 0;
173 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
175 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
178 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
182 const char* mytest = "abcd\nf\tg";
183 const int mylen = strlen(mytest);
184 const int mylen2 = strchr(mytest, '\n') - mytest;
187 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
188 "clearing wrap at EOL & setting processed output\n");
190 /* write line, wrapping disabled, buffer exceeds sb width */
191 c.X = sbSize.X - 5; c.Y = 0;
192 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
194 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
196 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
198 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
200 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
203 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
204 for (c.X = 1; c.X < 8; c.X++)
205 okCHAR(hCon, c, ' ', TEST_ATTRIB);
206 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
208 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
212 /* write line, wrapping disabled, strings end on end of line */
213 c.X = sbSize.X - 4; c.Y = 0;
214 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
216 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
218 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
220 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
223 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
224 for (c.X = 1; c.X < 8; c.X++)
225 okCHAR(hCon, c, ' ', TEST_ATTRIB);
226 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
228 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
232 /* write line, wrapping disabled, strings end after end of line */
233 c.X = sbSize.X - 3; c.Y = 0;
234 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
236 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
238 for (p = mylen2 - 3; p < mylen2; p++)
240 c.X = sbSize.X - 3 + p % 3;
241 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
244 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
245 for (c.X = 1; c.X < 8; c.X++)
246 okCHAR(hCon, c, ' ', TEST_ATTRIB);
247 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
249 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
254 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
258 const char* mytest = "abcd\nf\tg";
259 const int mylen = strlen(mytest);
262 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
263 "setting wrap at EOL & clearing processed output\n");
265 /* write line, wrapping enabled, buffer doesn't exceed sb width */
266 c.X = sbSize.X - 9; c.Y = 0;
267 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
269 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
271 for (p = 0; p < mylen; p++)
273 c.X = sbSize.X - 9 + p;
274 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
276 c.X = sbSize.X - 9 + mylen;
277 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
279 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
281 /* write line, wrapping enabled, buffer does exceed sb width */
282 c.X = sbSize.X - 3; c.Y = 0;
283 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
287 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
290 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
294 const char* mytest = "abcd\nf\tg";
295 const int mylen = strlen(mytest);
298 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
299 "setting wrap at EOL & processed output\n");
301 /* write line, wrapping enabled, buffer doesn't exceed sb width */
302 c.X = sbSize.X - 9; c.Y = 0;
303 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
305 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
306 for (p = 0; p < 4; p++)
308 c.X = sbSize.X - 9 + p;
309 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
311 c.X = sbSize.X - 9 + p;
312 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
314 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
315 for (c.X = 1; c.X < 8; c.X++)
316 okCHAR(hCon, c, ' ', TEST_ATTRIB);
317 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
319 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
322 /* write line, wrapping enabled, buffer does exceed sb width */
323 c.X = sbSize.X - 3; c.Y = 2;
324 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
326 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
327 for (p = 0; p < 3; p++)
329 c.X = sbSize.X - 3 + p;
330 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
333 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
335 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
338 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
339 for (c.X = 1; c.X < 8; c.X++)
340 okCHAR(hCon, c, ' ', TEST_ATTRIB);
341 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
343 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
347 static void testWrite(HANDLE hCon, COORD sbSize)
349 /* FIXME: should in fact insure that the sb is at least 10 character wide */
350 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
351 resetContent(hCon, sbSize, FALSE);
352 testWriteSimple(hCon, sbSize);
353 resetContent(hCon, sbSize, FALSE);
354 testWriteNotWrappedNotProcessed(hCon, sbSize);
355 resetContent(hCon, sbSize, FALSE);
356 testWriteNotWrappedProcessed(hCon, sbSize);
357 resetContent(hCon, sbSize, FALSE);
358 testWriteWrappedNotProcessed(hCon, sbSize);
359 resetContent(hCon, sbSize, FALSE);
360 testWriteWrappedProcessed(hCon, sbSize);
364 static void testScroll(HANDLE hCon, COORD sbSize)
366 SMALL_RECT scroll, clip;
373 /* no clipping, src & dst rect don't overlap */
374 resetContent(hCon, sbSize, TRUE);
376 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
377 #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)
380 scroll.Right = W - 1;
382 scroll.Bottom = H - 1;
385 ci.Char.UnicodeChar = '#';
386 ci.Attributes = TEST_ATTRIB;
389 clip.Right = sbSize.X - 1;
391 clip.Bottom = sbSize.Y - 1;
393 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
395 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
397 for (c.X = 0; c.X < sbSize.X; c.X++)
399 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
403 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
405 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
406 okCHAR(hCon, c, '#', TEST_ATTRIB);
407 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
411 /* no clipping, src & dst rect do overlap */
412 resetContent(hCon, sbSize, TRUE);
415 scroll.Right = W - 1;
417 scroll.Bottom = H - 1;
420 ci.Char.UnicodeChar = '#';
421 ci.Attributes = TEST_ATTRIB;
424 clip.Right = sbSize.X - 1;
426 clip.Bottom = sbSize.Y - 1;
428 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
430 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
432 for (c.X = 0; c.X < sbSize.X; c.X++)
434 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
438 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
440 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
441 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
445 /* clipping, src & dst rect don't overlap */
446 resetContent(hCon, sbSize, TRUE);
449 scroll.Right = W - 1;
451 scroll.Bottom = H - 1;
454 ci.Char.UnicodeChar = '#';
455 ci.Attributes = TEST_ATTRIB;
458 clip.Right = min(W + W / 2, sbSize.X - 1);
460 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
462 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
464 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
466 for (c.X = 0; c.X < sbSize.X; c.X++)
468 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
472 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
474 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
475 okCHAR(hCon, c, '#', TEST_ATTRIB);
476 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
480 /* clipping, src & dst rect do overlap */
481 resetContent(hCon, sbSize, TRUE);
484 scroll.Right = W - 1;
486 scroll.Bottom = H - 1;
489 ci.Char.UnicodeChar = '#';
490 ci.Attributes = TEST_ATTRIB;
493 clip.Right = min(W + W / 2, sbSize.X - 1);
495 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
497 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
499 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
501 for (c.X = 0; c.X < sbSize.X; c.X++)
503 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
507 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
509 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
510 okCHAR(hCon, c, '#', TEST_ATTRIB);
511 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
519 HANDLE hConIn, hConOut;
521 CONSOLE_SCREEN_BUFFER_INFO sbi;
523 /* be sure we have a clean console (and that's our own)
524 * FIXME: this will make the test fail (currently) if we don't run
526 * Another solution would be to rerun the test under wineconsole with
530 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
531 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
533 /* first, we need to be sure we're attached to a console */
534 if (hConIn == INVALID_HANDLE_VALUE || hConOut == INVALID_HANDLE_VALUE)
536 /* we're not attached to a console, let's do it */
538 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
539 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
541 /* now verify everything's ok */
542 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
543 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
545 ok(ret = GetConsoleScreenBufferInfo(hConOut, &sbi), "Getting sb info\n");
548 /* Non interactive tests */
549 testCursor(hConOut, sbi.dwSize);
550 /* will test wrapped (on/off) & processed (on/off) strings output */
551 testWrite(hConOut, sbi.dwSize);
552 /* will test line scrolling at the bottom of the screen */
553 /* testBottomScroll(); */
554 /* will test all the scrolling operations */
555 /* this one is disabled for now, Wine's result are way too bad */
556 /* testScroll(hCon, sbi.dwSize); */
557 /* will test sb creation / modification... */
558 /* testScreenBuffer() */
560 /* still to be done: events generation, access rights & access on objects */