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)", \
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", (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", (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)
75 ok(SetConsoleCursorPosition(0, c) == 0, "No handle");
76 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %lu",
77 ERROR_INVALID_HANDLE, GetLastError());
80 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left");
85 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right");
90 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside");
91 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %lu",
92 ERROR_INVALID_PARAMETER, GetLastError());
96 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside");
97 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %lu",
98 ERROR_INVALID_PARAMETER, GetLastError());
102 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside");
103 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %lu",
104 ERROR_INVALID_PARAMETER, GetLastError());
108 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside");
109 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %lu",
110 ERROR_INVALID_PARAMETER, GetLastError());
113 static void testWriteSimple(HANDLE hCon, COORD sbSize)
117 const char* mytest = "abcdefg";
118 const size_t mylen = strlen(mytest);
120 /* single line write */
122 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left");
124 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
126 for (c.X = 0; c.X < mylen; c.X++)
128 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
132 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
135 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
139 const char* mytest = "abcd\nf\tg";
140 const size_t mylen = strlen(mytest);
143 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
144 "clearing wrap at EOL & processed output");
146 /* write line, wrapping disabled, buffer exceeds sb width */
147 c.X = sbSize.X - 3; c.Y = 0;
148 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3");
150 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
152 for (p = mylen - 3; p < mylen; p++)
154 c.X = sbSize.X - 3 + p % 3;
155 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
159 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
161 p = sbSize.X - 3 + mylen % 3;
165 /* write line, wrapping disabled, strings end on end of line */
166 c.X = sbSize.X - mylen; c.Y = 0;
167 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3");
169 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
171 for (p = 0; p < mylen; p++)
173 c.X = sbSize.X - mylen + p;
174 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
178 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
180 p = sbSize.X - mylen;
185 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
189 const char* mytest = "abcd\nf\tg";
190 const size_t mylen = strlen(mytest);
191 const size_t mylen2 = strchr(mytest, '\n') - mytest;
194 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
195 "clearing wrap at EOL & setting processed output");
197 /* write line, wrapping disabled, buffer exceeds sb width */
198 c.X = sbSize.X - 5; c.Y = 0;
199 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5");
201 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
203 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
205 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
207 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
210 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
211 for (c.X = 1; c.X < 8; c.X++)
212 okCHAR(hCon, c, ' ', TEST_ATTRIB);
213 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
215 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
219 /* write line, wrapping disabled, strings end on end of line */
220 c.X = sbSize.X - 4; c.Y = 0;
221 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4");
223 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
225 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
227 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
230 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
231 for (c.X = 1; c.X < 8; c.X++)
232 okCHAR(hCon, c, ' ', TEST_ATTRIB);
233 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
235 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
239 /* write line, wrapping disabled, strings end after end of line */
240 c.X = sbSize.X - 3; c.Y = 0;
241 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4");
243 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
245 for (p = mylen2 - 3; p < mylen2; p++)
247 c.X = sbSize.X - 3 + p % 3;
248 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
251 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
252 for (c.X = 1; c.X < 8; c.X++)
253 okCHAR(hCon, c, ' ', TEST_ATTRIB);
254 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
256 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
261 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
265 const char* mytest = "abcd\nf\tg";
266 const size_t mylen = strlen(mytest);
269 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
270 "setting wrap at EOL & clearing processed output");
272 /* write line, wrapping enabled, buffer doesn't exceed sb width */
273 c.X = sbSize.X - 9; c.Y = 0;
274 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9");
276 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
278 for (p = 0; p < mylen; p++)
280 c.X = sbSize.X - 9 + p;
281 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
283 c.X = sbSize.X - 9 + mylen;
284 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
286 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
288 /* write line, wrapping enabled, buffer does exceed sb width */
289 c.X = sbSize.X - 3; c.Y = 0;
290 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3");
292 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
294 for (p = 0; p < 3; p++)
296 c.X = sbSize.X - 3 + p;
297 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
301 for (p = 0; p < mylen - 3; p++)
304 okCHAR(hCon, c, mytest[p + 3], TEST_ATTRIB);
307 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
312 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
316 const char* mytest = "abcd\nf\tg";
317 const size_t mylen = strlen(mytest);
320 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
321 "setting wrap at EOL & processed output");
323 /* write line, wrapping enabled, buffer doesn't exceed sb width */
324 c.X = sbSize.X - 9; c.Y = 0;
325 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9");
327 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
328 for (p = 0; p < 4; p++)
330 c.X = sbSize.X - 9 + p;
331 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
333 c.X = sbSize.X - 9 + p;
334 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
336 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
337 for (c.X = 1; c.X < 8; c.X++)
338 okCHAR(hCon, c, ' ', TEST_ATTRIB);
339 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
341 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
344 /* write line, wrapping enabled, buffer does exceed sb width */
345 c.X = sbSize.X - 3; c.Y = 2;
346 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3");
348 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole");
349 for (p = 0; p < 3; p++)
351 c.X = sbSize.X - 3 + p;
352 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
355 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
357 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
360 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
361 for (c.X = 1; c.X < 8; c.X++)
362 okCHAR(hCon, c, ' ', TEST_ATTRIB);
363 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
365 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
369 static void testWrite(HANDLE hCon, COORD sbSize)
371 /* FIXME: should in fact insure that the sb is at least 10 character wide */
372 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color");
373 resetContent(hCon, sbSize, FALSE);
374 testWriteSimple(hCon, sbSize);
375 resetContent(hCon, sbSize, FALSE);
376 testWriteNotWrappedNotProcessed(hCon, sbSize);
377 resetContent(hCon, sbSize, FALSE);
378 testWriteNotWrappedProcessed(hCon, sbSize);
379 resetContent(hCon, sbSize, FALSE);
380 testWriteWrappedNotProcessed(hCon, sbSize);
381 resetContent(hCon, sbSize, FALSE);
382 testWriteWrappedProcessed(hCon, sbSize);
386 static void testScroll(HANDLE hCon, COORD sbSize)
388 SMALL_RECT scroll, clip;
395 /* no clipping, src & dst rect don't overlap */
396 resetContent(hCon, sbSize, TRUE);
398 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
399 #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)
402 scroll.Right = W - 1;
404 scroll.Bottom = H - 1;
407 ci.Char.UnicodeChar = '#';
408 ci.Attributes = TEST_ATTRIB;
411 clip.Right = sbSize.X - 1;
413 clip.Bottom = sbSize.Y - 1;
415 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB");
417 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
419 for (c.X = 0; c.X < sbSize.X; c.X++)
421 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
425 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
427 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
428 okCHAR(hCon, c, '#', TEST_ATTRIB);
429 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
433 /* no clipping, src & dst rect do overlap */
434 resetContent(hCon, sbSize, TRUE);
437 scroll.Right = W - 1;
439 scroll.Bottom = H - 1;
442 ci.Char.UnicodeChar = '#';
443 ci.Attributes = TEST_ATTRIB;
446 clip.Right = sbSize.X - 1;
448 clip.Bottom = sbSize.Y - 1;
450 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB");
452 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
454 for (c.X = 0; c.X < sbSize.X; c.X++)
456 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
460 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
462 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
463 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
467 /* clipping, src & dst rect don't overlap */
468 resetContent(hCon, sbSize, TRUE);
471 scroll.Right = W - 1;
473 scroll.Bottom = H - 1;
476 ci.Char.UnicodeChar = '#';
477 ci.Attributes = TEST_ATTRIB;
480 clip.Right = min(W + W / 2, sbSize.X - 1);
482 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
484 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB");
486 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
488 for (c.X = 0; c.X < sbSize.X; c.X++)
490 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
494 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
496 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
497 okCHAR(hCon, c, '#', TEST_ATTRIB);
498 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
502 /* clipping, src & dst rect do overlap */
503 resetContent(hCon, sbSize, TRUE);
506 scroll.Right = W - 1;
508 scroll.Bottom = H - 1;
511 ci.Char.UnicodeChar = '#';
512 ci.Attributes = TEST_ATTRIB;
515 clip.Right = min(W + W / 2, sbSize.X - 1);
517 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
519 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB");
521 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
523 for (c.X = 0; c.X < sbSize.X; c.X++)
525 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
529 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
531 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
532 okCHAR(hCon, c, '#', TEST_ATTRIB);
533 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
543 CONSOLE_SCREEN_BUFFER_INFO sbi;
545 /* be sure we have a clean console (and that's our own)
546 * FIXME: this will make the test fail (currently) if we don't run
548 * Another solution would be to rerun the test under wineconsole with
553 hCon = GetStdHandle(STD_OUTPUT_HANDLE);
554 ok(ret = GetConsoleScreenBufferInfo(hCon, &sbi), "Getting sb info");
557 /* Non interactive tests */
558 testCursor(hCon, sbi.dwSize);
559 /* will test wrapped (on/off) & processed (on/off) strings output */
560 testWrite(hCon, sbi.dwSize);
561 /* will test line scrolling at the bottom of the screen */
562 /* testBottomScroll(); */
563 /* will test all the scrolling operations */
564 /* this one is disabled for now, Wine's result are way too bad */
565 /* testScroll(hCon, sbi.dwSize); */
566 /* will test sb creation / modification... */
567 /* testScreenBuffer() */
569 /* still to be done: events generation, access rights & access on objects */