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 testCursorInfo(HANDLE hCon)
136 CONSOLE_CURSOR_INFO info;
138 SetLastError(0xdeadbeef);
139 ret = GetConsoleCursorInfo(NULL, NULL);
140 ok(!ret, "Expected failure\n");
141 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
142 ERROR_INVALID_HANDLE, GetLastError());
144 SetLastError(0xdeadbeef);
146 ret = GetConsoleCursorInfo(NULL, &info);
147 ok(!ret, "Expected failure\n");
148 ok(info.dwSize == -1, "Expected no change for dwSize\n");
149 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
150 ERROR_INVALID_HANDLE, GetLastError());
152 /* Test the correct call first to distinguish between win9x and the rest */
153 SetLastError(0xdeadbeef);
154 ret = GetConsoleCursorInfo(hCon, &info);
155 ok(ret, "Expected success\n");
156 ok(info.dwSize == 25 ||
157 info.dwSize == 12 /* win9x */,
158 "Expected 12 or 25, got %d\n", info.dwSize);
159 ok(info.bVisible, "Expected the cursor to be visible\n");
160 ok(GetLastError() == 0xdeadbeef, "GetLastError: expecting %u got %u\n",
161 0xdeadbeef, GetLastError());
163 if (info.dwSize == 12)
165 skip("NULL CONSOLE_CURSOR_INFO will crash on win9x\n");
169 SetLastError(0xdeadbeef);
170 ret = GetConsoleCursorInfo(hCon, NULL);
171 ok(!ret, "Expected failure\n");
172 ok(GetLastError() == ERROR_INVALID_ACCESS, "GetLastError: expecting %u got %u\n",
173 ERROR_INVALID_ACCESS, GetLastError());
176 static void testWriteSimple(HANDLE hCon, COORD sbSize)
180 const char* mytest = "abcdefg";
181 const int mylen = strlen(mytest);
183 /* single line write */
185 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
187 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
189 for (c.X = 0; c.X < mylen; c.X++)
191 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
195 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
198 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
202 const char* mytest = "123";
203 const int mylen = strlen(mytest);
207 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
208 "clearing wrap at EOL & processed output\n");
210 /* write line, wrapping disabled, buffer exceeds sb width */
211 c.X = sbSize.X - 3; c.Y = 0;
212 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
214 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
215 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
217 for (p = mylen - 3; p < mylen; p++)
219 c.X = sbSize.X - 3 + p % 3;
220 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
224 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
226 p = sbSize.X - 3 + mylen % 3;
229 /* write line, wrapping disabled, strings end on end of line */
230 c.X = sbSize.X - mylen; c.Y = 0;
231 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
233 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
236 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
240 const char* mytest = "abcd\nf\tg";
241 const int mylen = strlen(mytest);
242 const int mylen2 = strchr(mytest, '\n') - mytest;
245 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
246 "clearing wrap at EOL & setting processed output\n");
248 /* write line, wrapping disabled, buffer exceeds sb width */
249 c.X = sbSize.X - 5; c.Y = 0;
250 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
252 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
254 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
256 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
258 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
261 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
262 for (c.X = 1; c.X < 8; c.X++)
263 okCHAR(hCon, c, ' ', TEST_ATTRIB);
264 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
266 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
270 /* write line, wrapping disabled, strings end on end of line */
271 c.X = sbSize.X - 4; c.Y = 0;
272 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
274 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
276 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
278 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
281 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
282 for (c.X = 1; c.X < 8; c.X++)
283 okCHAR(hCon, c, ' ', TEST_ATTRIB);
284 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
286 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
290 /* write line, wrapping disabled, strings end after end of line */
291 c.X = sbSize.X - 3; c.Y = 0;
292 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
294 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
296 for (p = mylen2 - 3; p < mylen2; p++)
298 c.X = sbSize.X - 3 + p % 3;
299 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
302 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
303 for (c.X = 1; c.X < 8; c.X++)
304 okCHAR(hCon, c, ' ', TEST_ATTRIB);
305 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
307 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
312 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
316 const char* mytest = "abcd\nf\tg";
317 const int mylen = strlen(mytest);
320 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
321 "setting wrap at EOL & clearing processed output\n");
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\n");
327 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
329 for (p = 0; p < mylen; p++)
331 c.X = sbSize.X - 9 + p;
332 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
334 c.X = sbSize.X - 9 + mylen;
335 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
337 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
339 /* write line, wrapping enabled, buffer does exceed sb width */
340 c.X = sbSize.X - 3; c.Y = 0;
341 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
345 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
348 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
352 const char* mytest = "abcd\nf\tg";
353 const int mylen = strlen(mytest);
356 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
357 "setting wrap at EOL & processed output\n");
359 /* write line, wrapping enabled, buffer doesn't exceed sb width */
360 c.X = sbSize.X - 9; c.Y = 0;
361 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
363 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
364 for (p = 0; p < 4; p++)
366 c.X = sbSize.X - 9 + p;
367 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
369 c.X = sbSize.X - 9 + p;
370 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
372 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
373 for (c.X = 1; c.X < 8; c.X++)
374 okCHAR(hCon, c, ' ', TEST_ATTRIB);
375 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
377 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
380 /* write line, wrapping enabled, buffer does exceed sb width */
381 c.X = sbSize.X - 3; c.Y = 2;
382 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
384 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
385 for (p = 0; p < 3; p++)
387 c.X = sbSize.X - 3 + p;
388 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
391 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
393 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
396 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
397 for (c.X = 1; c.X < 8; c.X++)
398 okCHAR(hCon, c, ' ', TEST_ATTRIB);
399 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
401 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
405 static void testWrite(HANDLE hCon, COORD sbSize)
407 /* FIXME: should in fact insure that the sb is at least 10 character wide */
408 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
409 resetContent(hCon, sbSize, FALSE);
410 testWriteSimple(hCon, sbSize);
411 resetContent(hCon, sbSize, FALSE);
412 testWriteNotWrappedNotProcessed(hCon, sbSize);
413 resetContent(hCon, sbSize, FALSE);
414 testWriteNotWrappedProcessed(hCon, sbSize);
415 resetContent(hCon, sbSize, FALSE);
416 testWriteWrappedNotProcessed(hCon, sbSize);
417 resetContent(hCon, sbSize, FALSE);
418 testWriteWrappedProcessed(hCon, sbSize);
421 static void testScroll(HANDLE hCon, COORD sbSize)
423 SMALL_RECT scroll, clip;
431 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
432 #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)
434 /* no clipping, src & dst rect don't overlap */
435 resetContent(hCon, sbSize, TRUE);
438 scroll.Right = W - 1;
440 scroll.Bottom = H - 1;
443 ci.Char.UnicodeChar = '#';
444 ci.Attributes = TEST_ATTRIB;
447 clip.Right = sbSize.X - 1;
449 clip.Bottom = sbSize.Y - 1;
451 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
453 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
455 for (c.X = 0; c.X < sbSize.X; c.X++)
457 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
461 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
463 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
464 okCHAR(hCon, c, '#', TEST_ATTRIB);
465 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
469 /* no clipping, src & dst rect do overlap */
470 resetContent(hCon, sbSize, TRUE);
473 scroll.Right = W - 1;
475 scroll.Bottom = H - 1;
478 ci.Char.UnicodeChar = '#';
479 ci.Attributes = TEST_ATTRIB;
482 clip.Right = sbSize.X - 1;
484 clip.Bottom = sbSize.Y - 1;
486 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
488 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
490 for (c.X = 0; c.X < sbSize.X; c.X++)
492 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
496 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
498 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
499 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
503 /* clipping, src & dst rect don't overlap */
504 resetContent(hCon, sbSize, TRUE);
507 scroll.Right = W - 1;
509 scroll.Bottom = H - 1;
512 ci.Char.UnicodeChar = '#';
513 ci.Attributes = TEST_ATTRIB;
516 clip.Right = min(W + W / 2, sbSize.X - 1);
518 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
520 SetLastError(0xdeadbeef);
521 ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
524 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
526 for (c.X = 0; c.X < sbSize.X; c.X++)
528 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
532 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
534 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
535 okCHAR(hCon, c, '#', TEST_ATTRIB);
536 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
542 /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
543 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
544 "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
547 /* clipping, src & dst rect do overlap */
548 resetContent(hCon, sbSize, TRUE);
551 scroll.Right = W - 1;
553 scroll.Bottom = H - 1;
556 ci.Char.UnicodeChar = '#';
557 ci.Attributes = TEST_ATTRIB;
560 clip.Right = min(W + W / 2, sbSize.X - 1);
562 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
564 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
566 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
568 for (c.X = 0; c.X < sbSize.X; c.X++)
570 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
574 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
576 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
577 okCHAR(hCon, c, '#', TEST_ATTRIB);
578 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
583 static int mch_count;
584 /* we need the event as Wine console event generation isn't synchronous
585 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
586 * processes have been called).
588 static HANDLE mch_event;
589 static BOOL WINAPI mch(DWORD event)
596 static void testCtrlHandler(void)
598 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
599 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
600 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
601 /* wine requires the event for the test, as we cannot insure, so far, that event
602 * are processed synchronously in GenerateConsoleCtrlEvent()
604 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
606 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
607 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
608 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
609 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
610 CloseHandle(mch_event);
612 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
613 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
614 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
616 if(!(GetVersion() & 0x80000000))
617 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
618 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
619 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
620 CloseHandle(mch_event);
621 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
622 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
623 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
627 * Test console screen buffer:
628 * 1) Try to set invalid handle.
629 * 2) Try to set non-console handles.
630 * 3) Use CONOUT$ file as active SB.
632 * 5) Test output codepage to show it is not a property of SB.
633 * 6) Test switching to old SB if we close all handles to current SB - works
634 * in Windows, TODO in wine.
636 * What is not tested but should be:
637 * 1) ScreenBufferInfo
639 static void testScreenBuffer(HANDLE hConOut)
641 HANDLE hConOutRW, hConOutRO, hConOutWT;
642 HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
644 char test_str1[] = "Test for SB1";
645 char test_str2[] = "Test for SB2";
646 char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
647 char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
648 WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
656 if (!IsValidCodePage(866))
658 skip("Codepage 866 not available\n");
662 /* In the beginning set output codepage to 866 */
663 oldcp = GetConsoleOutputCP();
664 SetLastError(0xdeadbeef);
665 ret = SetConsoleOutputCP(866);
666 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
668 skip("SetConsoleOutputCP is not implemented\n");
671 ok(ret, "Cannot set output codepage to 866\n");
673 hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
674 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
675 CONSOLE_TEXTMODE_BUFFER, NULL);
676 ok(hConOutRW != INVALID_HANDLE_VALUE,
677 "Cannot create a new screen buffer for ReadWrite\n");
678 hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
679 FILE_SHARE_READ, NULL,
680 CONSOLE_TEXTMODE_BUFFER, NULL);
681 ok(hConOutRO != INVALID_HANDLE_VALUE,
682 "Cannot create a new screen buffer for ReadOnly\n");
683 hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
684 FILE_SHARE_WRITE, NULL,
685 CONSOLE_TEXTMODE_BUFFER, NULL);
686 ok(hConOutWT != INVALID_HANDLE_VALUE,
687 "Cannot create a new screen buffer for WriteOnly\n");
689 hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
690 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
691 OPEN_EXISTING, 0, NULL);
692 ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
693 hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
694 NULL, OPEN_EXISTING, 0, NULL);
695 ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
696 hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
697 NULL, OPEN_EXISTING, 0, NULL);
698 ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
700 /* Trying to set invalid handle */
702 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
703 "Shouldn't succeed\n");
704 ok(GetLastError() == ERROR_INVALID_HANDLE,
705 "GetLastError: expecting %u got %u\n",
706 ERROR_INVALID_HANDLE, GetLastError());
708 /* Trying to set non-console handles */
710 ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
711 ok(GetLastError() == ERROR_INVALID_HANDLE,
712 "GetLastError: expecting %u got %u\n",
713 ERROR_INVALID_HANDLE, GetLastError());
716 ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
717 ok(GetLastError() == ERROR_INVALID_HANDLE,
718 "GetLastError: expecting %u got %u\n",
719 ERROR_INVALID_HANDLE, GetLastError());
722 ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
723 ok(GetLastError() == ERROR_INVALID_HANDLE,
724 "GetLastError: expecting %u got %u\n",
725 ERROR_INVALID_HANDLE, GetLastError());
727 CloseHandle(hFileOutRW);
728 CloseHandle(hFileOutRO);
729 CloseHandle(hFileOutWT);
731 /* Trying to set SB handles with various access modes */
733 ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
734 ok(GetLastError() == ERROR_INVALID_HANDLE,
735 "GetLastError: expecting %u got %u\n",
736 ERROR_INVALID_HANDLE, GetLastError());
738 ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
740 ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
742 CloseHandle(hConOutWT);
743 CloseHandle(hConOutRO);
745 /* Now we have two ReadWrite SB, active must be hConOutRW */
746 /* Open current SB via CONOUT$ */
747 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
748 NULL, OPEN_EXISTING, 0, 0);
749 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
754 SetConsoleCursorPosition(hConOut, c);
756 SetConsoleCursorPosition(hConOutRW, c);
757 okCURSOR(hConOutNew, c);
759 okCURSOR(hConOut, c);
764 /* Write using hConOutNew... */
765 SetConsoleCursorPosition(hConOutNew, c);
766 ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
767 ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
768 /* ... and read it back via hConOutRW */
769 ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
770 ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
771 str_buf[lstrlenA(test_str2)] = 0;
772 ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
775 /* Now test output codepage handling. Current is 866 as we set earlier. */
776 SetConsoleCursorPosition(hConOutRW, c);
777 ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
778 ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
779 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
780 ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
781 str_wbuf[lstrlenA(test_cp866)] = 0;
782 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
785 * cp866 is OK, let's switch to cp1251.
786 * We expect that this codepage will be used in every SB - active and not.
788 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
789 SetConsoleCursorPosition(hConOutRW, c);
790 ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
791 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
792 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
793 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
794 str_wbuf[lstrlenA(test_cp1251)] = 0;
795 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
797 /* Check what has happened to hConOut. */
798 SetConsoleCursorPosition(hConOut, c);
799 ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
800 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
801 ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
802 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
803 str_wbuf[lstrlenA(test_cp1251)] = 0;
804 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
806 /* Close all handles of current console SB */
807 CloseHandle(hConOutNew);
808 CloseHandle(hConOutRW);
810 /* Now active SB should be hConOut */
811 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
812 NULL, OPEN_EXISTING, 0, 0);
813 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
815 /* Write using hConOutNew... */
816 SetConsoleCursorPosition(hConOutNew, c);
817 ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
818 ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
819 /* ... and read it back via hConOut */
820 ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
821 ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
822 str_buf[lstrlenA(test_str1)] = 0;
823 todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
824 CloseHandle(hConOutNew);
826 /* This is not really needed under Windows */
827 SetConsoleActiveScreenBuffer(hConOut);
829 /* restore codepage */
830 SetConsoleOutputCP(oldcp);
833 static void test_GetSetConsoleInputExeName(void)
837 char buffer[MAX_PATH], module[MAX_PATH], *p;
838 static char input_exe[MAX_PATH] = "winetest.exe";
840 SetLastError(0xdeadbeef);
841 ret = pGetConsoleInputExeNameA(0, NULL);
842 error = GetLastError();
843 ok(ret, "GetConsoleInputExeNameA failed\n");
844 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
846 SetLastError(0xdeadbeef);
847 ret = pGetConsoleInputExeNameA(0, buffer);
848 error = GetLastError();
849 ok(ret, "GetConsoleInputExeNameA failed\n");
850 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
852 GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
853 p = strrchr(module, '\\') + 1;
855 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
856 ok(ret, "GetConsoleInputExeNameA failed\n");
857 todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
859 SetLastError(0xdeadbeef);
860 ret = pSetConsoleInputExeNameA(NULL);
861 error = GetLastError();
862 ok(!ret, "SetConsoleInputExeNameA failed\n");
863 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
865 SetLastError(0xdeadbeef);
866 ret = pSetConsoleInputExeNameA("");
867 error = GetLastError();
868 ok(!ret, "SetConsoleInputExeNameA failed\n");
869 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
871 ret = pSetConsoleInputExeNameA(input_exe);
872 ok(ret, "SetConsoleInputExeNameA failed\n");
874 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
875 ok(ret, "GetConsoleInputExeNameA failed\n");
876 ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
881 HANDLE hConIn, hConOut;
883 CONSOLE_SCREEN_BUFFER_INFO sbi;
885 init_function_pointers();
887 /* be sure we have a clean console (and that's our own)
888 * FIXME: this will make the test fail (currently) if we don't run
890 * Another solution would be to rerun the test under wineconsole with
894 /* first, we detach and open a fresh console to play with */
896 ok(AllocConsole(), "Couldn't alloc console\n");
897 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
898 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
900 /* now verify everything's ok */
901 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
902 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
904 ok(ret = GetConsoleScreenBufferInfo(hConOut, &sbi), "Getting sb info\n");
907 /* Non interactive tests */
908 testCursor(hConOut, sbi.dwSize);
909 /* test parameters (FIXME: test functionality) */
910 testCursorInfo(hConOut);
911 /* will test wrapped (on/off) & processed (on/off) strings output */
912 testWrite(hConOut, sbi.dwSize);
913 /* will test line scrolling at the bottom of the screen */
914 /* testBottomScroll(); */
915 /* will test all the scrolling operations */
916 testScroll(hConOut, sbi.dwSize);
917 /* will test sb creation / modification / codepage handling */
918 testScreenBuffer(hConOut);
920 /* still to be done: access rights & access on objects */
922 if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
924 skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
928 test_GetSetConsoleInputExeName();