push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / dlls / kernel32 / tests / console.c
blob33eab268e9c74705974ca314a010e136e3c3e1bb
1 /*
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"
23 #include <windows.h>
24 #include <stdio.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); \
46 } while (0)
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); \
54 } while (0)
56 static void init_function_pointers(void)
58 HMODULE hKernel32;
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)
74 COORD c;
75 WORD attr = DEFAULT_ATTRIB;
76 char ch;
77 DWORD len;
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)
92 COORD c;
94 c.X = c.Y = 0;
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());
99 c.X = c.Y = 0;
100 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
101 okCURSOR(hCon, c);
103 c.X = sbSize.X - 1;
104 c.Y = sbSize.Y - 1;
105 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right\n");
106 okCURSOR(hCon, c);
108 c.X = sbSize.X;
109 c.Y = sbSize.Y - 1;
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());
114 c.X = sbSize.X - 1;
115 c.Y = sbSize.Y;
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());
120 c.X = -1;
121 c.Y = 0;
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());
126 c.X = 0;
127 c.Y = -1;
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)
135 BOOL ret;
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);
145 info.dwSize = -1;
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 /* Don't test NULL CONSOLE_CURSOR_INFO, it crashes on win9x and win7 */
166 static void testEmptyWrite(HANDLE hCon)
168 COORD c;
169 DWORD len;
170 const char* mytest = "";
172 c.X = c.Y = 0;
173 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
175 len = -1;
176 ok(WriteConsole(hCon, NULL, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
177 okCURSOR(hCon, c);
179 /* Passing a NULL lpBuffer with sufficiently large non-zero length succeeds
180 * on native Windows and result in memory-like contents being written to
181 * the console. Calling WriteConsoleW like this will crash on Wine. */
182 if (0)
184 len = -1;
185 ok(!WriteConsole(hCon, NULL, 16, &len, NULL) && len == -1, "WriteConsole\n");
186 okCURSOR(hCon, c);
188 /* Cursor advances for this call. */
189 len = -1;
190 ok(WriteConsole(hCon, NULL, 128, &len, NULL) != 0 && len == 128, "WriteConsole\n");
193 len = -1;
194 ok(WriteConsole(hCon, mytest, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
195 okCURSOR(hCon, c);
197 /* WriteConsole does not halt on a null terminator and is happy to write
198 * memory contents beyond the actual size of the buffer. */
199 len = -1;
200 ok(WriteConsole(hCon, mytest, 16, &len, NULL) != 0 && len == 16, "WriteConsole\n");
201 c.X += 16;
202 okCURSOR(hCon, c);
205 static void testWriteSimple(HANDLE hCon)
207 COORD c;
208 DWORD len;
209 const char* mytest = "abcdefg";
210 const int mylen = strlen(mytest);
212 /* single line write */
213 c.X = c.Y = 0;
214 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
216 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
217 c.Y = 0;
218 for (c.X = 0; c.X < mylen; c.X++)
220 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
223 okCURSOR(hCon, c);
224 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
227 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
229 COORD c;
230 DWORD len, mode;
231 const char* mytest = "123";
232 const int mylen = strlen(mytest);
233 int ret;
234 int p;
236 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
237 "clearing wrap at EOL & processed output\n");
239 /* write line, wrapping disabled, buffer exceeds sb width */
240 c.X = sbSize.X - 3; c.Y = 0;
241 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
243 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
244 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
245 c.Y = 0;
246 for (p = mylen - 3; p < mylen; p++)
248 c.X = sbSize.X - 3 + p % 3;
249 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
252 c.X = 0; c.Y = 1;
253 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
255 p = sbSize.X - 3 + mylen % 3;
256 c.X = p; c.Y = 0;
258 /* write line, wrapping disabled, strings end on end of line */
259 c.X = sbSize.X - mylen; c.Y = 0;
260 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
262 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
265 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
267 COORD c;
268 DWORD len, mode;
269 const char* mytest = "abcd\nf\tg";
270 const int mylen = strlen(mytest);
271 const int mylen2 = strchr(mytest, '\n') - mytest;
272 int p;
273 WORD attr;
275 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
276 "clearing wrap at EOL & setting processed output\n");
278 /* write line, wrapping disabled, buffer exceeds sb width */
279 c.X = sbSize.X - 5; c.Y = 0;
280 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
282 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
283 c.Y = 0;
284 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
286 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
289 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
290 /* Win9x and WinMe change the attribs for '\n' up to 'f' */
291 if (attr == TEST_ATTRIB)
293 win_skip("Win9x/WinMe don't respect ~ENABLE_WRAP_AT_EOL_OUTPUT\n");
294 return;
297 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
299 c.X = 0; c.Y++;
300 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
301 for (c.X = 1; c.X < 8; c.X++)
302 okCHAR(hCon, c, ' ', TEST_ATTRIB);
303 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
304 c.X++;
305 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
307 okCURSOR(hCon, c);
309 /* write line, wrapping disabled, strings end on end of line */
310 c.X = sbSize.X - 4; c.Y = 0;
311 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
313 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
314 c.Y = 0;
315 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
317 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
319 c.X = 0; c.Y++;
320 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
321 for (c.X = 1; c.X < 8; c.X++)
322 okCHAR(hCon, c, ' ', TEST_ATTRIB);
323 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
324 c.X++;
325 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
327 okCURSOR(hCon, c);
329 /* write line, wrapping disabled, strings end after end of line */
330 c.X = sbSize.X - 3; c.Y = 0;
331 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
333 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
334 c.Y = 0;
335 for (p = mylen2 - 3; p < mylen2; p++)
337 c.X = sbSize.X - 3 + p % 3;
338 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
340 c.X = 0; c.Y = 1;
341 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
342 for (c.X = 1; c.X < 8; c.X++)
343 okCHAR(hCon, c, ' ', TEST_ATTRIB);
344 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
345 c.X++;
346 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
348 okCURSOR(hCon, c);
351 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
353 COORD c;
354 DWORD len, mode;
355 const char* mytest = "abcd\nf\tg";
356 const int mylen = strlen(mytest);
357 int p;
359 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
360 "setting wrap at EOL & clearing processed output\n");
362 /* write line, wrapping enabled, buffer doesn't exceed sb width */
363 c.X = sbSize.X - 9; c.Y = 0;
364 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
366 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
367 c.Y = 0;
368 for (p = 0; p < mylen; p++)
370 c.X = sbSize.X - 9 + p;
371 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
373 c.X = sbSize.X - 9 + mylen;
374 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
375 c.X = 0; c.Y = 1;
376 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
378 /* write line, wrapping enabled, buffer does exceed sb width */
379 c.X = sbSize.X - 3; c.Y = 0;
380 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
382 c.Y = 1;
383 c.X = mylen - 3;
384 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
387 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
389 COORD c;
390 DWORD len, mode;
391 const char* mytest = "abcd\nf\tg";
392 const int mylen = strlen(mytest);
393 int p;
394 WORD attr;
396 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
397 "setting wrap at EOL & processed output\n");
399 /* write line, wrapping enabled, buffer doesn't exceed sb width */
400 c.X = sbSize.X - 9; c.Y = 0;
401 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
403 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
404 for (p = 0; p < 4; p++)
406 c.X = sbSize.X - 9 + p;
407 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
409 c.X = sbSize.X - 9 + p;
410 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
411 if (attr == TEST_ATTRIB)
412 win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
413 else
414 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
415 c.X = 0; c.Y++;
416 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
417 for (c.X = 1; c.X < 8; c.X++)
418 okCHAR(hCon, c, ' ', TEST_ATTRIB);
419 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
420 c.X++;
421 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
422 okCURSOR(hCon, c);
424 /* write line, wrapping enabled, buffer does exceed sb width */
425 c.X = sbSize.X - 3; c.Y = 2;
426 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
428 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
429 for (p = 0; p < 3; p++)
431 c.X = sbSize.X - 3 + p;
432 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
434 c.X = 0; c.Y++;
435 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
436 c.X++;
437 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
438 if (attr == TEST_ATTRIB)
439 win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
440 else
441 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
443 c.X = 0; c.Y++;
444 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
445 for (c.X = 1; c.X < 8; c.X++)
446 okCHAR(hCon, c, ' ', TEST_ATTRIB);
447 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
448 c.X++;
449 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
450 okCURSOR(hCon, c);
453 static void testWrite(HANDLE hCon, COORD sbSize)
455 /* FIXME: should in fact insure that the sb is at least 10 character wide */
456 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
457 resetContent(hCon, sbSize, FALSE);
458 testEmptyWrite(hCon);
459 resetContent(hCon, sbSize, FALSE);
460 testWriteSimple(hCon);
461 resetContent(hCon, sbSize, FALSE);
462 testWriteNotWrappedNotProcessed(hCon, sbSize);
463 resetContent(hCon, sbSize, FALSE);
464 testWriteNotWrappedProcessed(hCon, sbSize);
465 resetContent(hCon, sbSize, FALSE);
466 testWriteWrappedNotProcessed(hCon, sbSize);
467 resetContent(hCon, sbSize, FALSE);
468 testWriteWrappedProcessed(hCon, sbSize);
471 static void testScroll(HANDLE hCon, COORD sbSize)
473 SMALL_RECT scroll, clip;
474 COORD dst, c, tc;
475 CHAR_INFO ci;
476 BOOL ret;
478 #define W 11
479 #define H 7
481 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
482 #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)
484 /* no clipping, src & dst rect don't overlap */
485 resetContent(hCon, sbSize, TRUE);
487 scroll.Left = 0;
488 scroll.Right = W - 1;
489 scroll.Top = 0;
490 scroll.Bottom = H - 1;
491 dst.X = W + 3;
492 dst.Y = H + 3;
493 ci.Char.UnicodeChar = '#';
494 ci.Attributes = TEST_ATTRIB;
496 clip.Left = 0;
497 clip.Right = sbSize.X - 1;
498 clip.Top = 0;
499 clip.Bottom = sbSize.Y - 1;
501 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
503 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
505 for (c.X = 0; c.X < sbSize.X; c.X++)
507 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
509 tc.X = c.X - dst.X;
510 tc.Y = c.Y - dst.Y;
511 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
513 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
514 okCHAR(hCon, c, '#', TEST_ATTRIB);
515 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
519 /* no clipping, src & dst rect do overlap */
520 resetContent(hCon, sbSize, TRUE);
522 scroll.Left = 0;
523 scroll.Right = W - 1;
524 scroll.Top = 0;
525 scroll.Bottom = H - 1;
526 dst.X = W /2;
527 dst.Y = H / 2;
528 ci.Char.UnicodeChar = '#';
529 ci.Attributes = TEST_ATTRIB;
531 clip.Left = 0;
532 clip.Right = sbSize.X - 1;
533 clip.Top = 0;
534 clip.Bottom = sbSize.Y - 1;
536 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
538 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
540 for (c.X = 0; c.X < sbSize.X; c.X++)
542 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
544 tc.X = c.X - dst.X;
545 tc.Y = c.Y - dst.Y;
546 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
548 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
549 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
553 /* clipping, src & dst rect don't overlap */
554 resetContent(hCon, sbSize, TRUE);
556 scroll.Left = 0;
557 scroll.Right = W - 1;
558 scroll.Top = 0;
559 scroll.Bottom = H - 1;
560 dst.X = W + 3;
561 dst.Y = H + 3;
562 ci.Char.UnicodeChar = '#';
563 ci.Attributes = TEST_ATTRIB;
565 clip.Left = W / 2;
566 clip.Right = min(W + W / 2, sbSize.X - 1);
567 clip.Top = H / 2;
568 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
570 SetLastError(0xdeadbeef);
571 ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
572 if (ret)
574 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
576 for (c.X = 0; c.X < sbSize.X; c.X++)
578 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
580 tc.X = c.X - dst.X;
581 tc.Y = c.Y - dst.Y;
582 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
584 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
585 okCHAR(hCon, c, '#', TEST_ATTRIB);
586 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
590 else
592 /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
593 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
594 "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
597 /* clipping, src & dst rect do overlap */
598 resetContent(hCon, sbSize, TRUE);
600 scroll.Left = 0;
601 scroll.Right = W - 1;
602 scroll.Top = 0;
603 scroll.Bottom = H - 1;
604 dst.X = W / 2 - 3;
605 dst.Y = H / 2 - 3;
606 ci.Char.UnicodeChar = '#';
607 ci.Attributes = TEST_ATTRIB;
609 clip.Left = W / 2;
610 clip.Right = min(W + W / 2, sbSize.X - 1);
611 clip.Top = H / 2;
612 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
614 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
616 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
618 for (c.X = 0; c.X < sbSize.X; c.X++)
620 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
622 tc.X = c.X - dst.X;
623 tc.Y = c.Y - dst.Y;
624 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
626 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
627 okCHAR(hCon, c, '#', TEST_ATTRIB);
628 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
633 static int mch_count;
634 /* we need the event as Wine console event generation isn't synchronous
635 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
636 * processes have been called).
638 static HANDLE mch_event;
639 static BOOL WINAPI mch(DWORD event)
641 mch_count++;
642 SetEvent(mch_event);
643 return TRUE;
646 static void testCtrlHandler(void)
648 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
649 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
650 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
651 /* wine requires the event for the test, as we cannot insure, so far, that event
652 * are processed synchronously in GenerateConsoleCtrlEvent()
654 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
655 mch_count = 0;
656 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
657 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
658 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
659 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
660 CloseHandle(mch_event);
662 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
663 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
664 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
665 mch_count = 0;
666 if(!(GetVersion() & 0x80000000))
667 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
668 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
669 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
670 CloseHandle(mch_event);
671 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
672 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
673 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
677 * Test console screen buffer:
678 * 1) Try to set invalid handle.
679 * 2) Try to set non-console handles.
680 * 3) Use CONOUT$ file as active SB.
681 * 4) Test cursor.
682 * 5) Test output codepage to show it is not a property of SB.
683 * 6) Test switching to old SB if we close all handles to current SB - works
684 * in Windows, TODO in wine.
686 * What is not tested but should be:
687 * 1) ScreenBufferInfo
689 static void testScreenBuffer(HANDLE hConOut)
691 HANDLE hConOutRW, hConOutRO, hConOutWT;
692 HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
693 HANDLE hConOutNew;
694 char test_str1[] = "Test for SB1";
695 char test_str2[] = "Test for SB2";
696 char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
697 char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
698 WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
699 WCHAR str_wbuf[20];
700 char str_buf[20];
701 DWORD len;
702 COORD c;
703 BOOL ret;
704 DWORD oldcp;
706 if (!IsValidCodePage(866))
708 skip("Codepage 866 not available\n");
709 return;
712 /* In the beginning set output codepage to 866 */
713 oldcp = GetConsoleOutputCP();
714 SetLastError(0xdeadbeef);
715 ret = SetConsoleOutputCP(866);
716 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
718 win_skip("SetConsoleOutputCP is not implemented\n");
719 return;
721 ok(ret, "Cannot set output codepage to 866\n");
723 hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
724 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
725 CONSOLE_TEXTMODE_BUFFER, NULL);
726 ok(hConOutRW != INVALID_HANDLE_VALUE,
727 "Cannot create a new screen buffer for ReadWrite\n");
728 hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
729 FILE_SHARE_READ, NULL,
730 CONSOLE_TEXTMODE_BUFFER, NULL);
731 ok(hConOutRO != INVALID_HANDLE_VALUE,
732 "Cannot create a new screen buffer for ReadOnly\n");
733 hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
734 FILE_SHARE_WRITE, NULL,
735 CONSOLE_TEXTMODE_BUFFER, NULL);
736 ok(hConOutWT != INVALID_HANDLE_VALUE,
737 "Cannot create a new screen buffer for WriteOnly\n");
739 hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
740 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
741 OPEN_EXISTING, 0, NULL);
742 ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
743 hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
744 NULL, OPEN_EXISTING, 0, NULL);
745 ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
746 hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
747 NULL, OPEN_EXISTING, 0, NULL);
748 ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
750 /* Trying to set invalid handle */
751 SetLastError(0);
752 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
753 "Shouldn't succeed\n");
754 ok(GetLastError() == ERROR_INVALID_HANDLE,
755 "GetLastError: expecting %u got %u\n",
756 ERROR_INVALID_HANDLE, GetLastError());
758 /* Trying to set non-console handles */
759 SetLastError(0);
760 ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
761 ok(GetLastError() == ERROR_INVALID_HANDLE,
762 "GetLastError: expecting %u got %u\n",
763 ERROR_INVALID_HANDLE, GetLastError());
765 SetLastError(0);
766 ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
767 ok(GetLastError() == ERROR_INVALID_HANDLE,
768 "GetLastError: expecting %u got %u\n",
769 ERROR_INVALID_HANDLE, GetLastError());
771 SetLastError(0);
772 ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
773 ok(GetLastError() == ERROR_INVALID_HANDLE,
774 "GetLastError: expecting %u got %u\n",
775 ERROR_INVALID_HANDLE, GetLastError());
777 CloseHandle(hFileOutRW);
778 CloseHandle(hFileOutRO);
779 CloseHandle(hFileOutWT);
781 /* Trying to set SB handles with various access modes */
782 SetLastError(0);
783 ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
784 ok(GetLastError() == ERROR_INVALID_HANDLE,
785 "GetLastError: expecting %u got %u\n",
786 ERROR_INVALID_HANDLE, GetLastError());
788 ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
790 ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
792 CloseHandle(hConOutWT);
793 CloseHandle(hConOutRO);
795 /* Now we have two ReadWrite SB, active must be hConOutRW */
796 /* Open current SB via CONOUT$ */
797 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
798 NULL, OPEN_EXISTING, 0, 0);
799 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
802 /* test cursor */
803 c.X = c.Y = 10;
804 SetConsoleCursorPosition(hConOut, c);
805 c.X = c.Y = 5;
806 SetConsoleCursorPosition(hConOutRW, c);
807 okCURSOR(hConOutNew, c);
808 c.X = c.Y = 10;
809 okCURSOR(hConOut, c);
812 c.X = c.Y = 0;
814 /* Write using hConOutNew... */
815 SetConsoleCursorPosition(hConOutNew, c);
816 ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
817 ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
818 /* ... and read it back via hConOutRW */
819 ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
820 ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
821 str_buf[lstrlenA(test_str2)] = 0;
822 ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
825 /* Now test output codepage handling. Current is 866 as we set earlier. */
826 SetConsoleCursorPosition(hConOutRW, c);
827 ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
828 ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
829 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
830 ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
831 str_wbuf[lstrlenA(test_cp866)] = 0;
832 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
835 * cp866 is OK, let's switch to cp1251.
836 * We expect that this codepage will be used in every SB - active and not.
838 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
839 SetConsoleCursorPosition(hConOutRW, c);
840 ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
841 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
842 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
843 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
844 str_wbuf[lstrlenA(test_cp1251)] = 0;
845 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
847 /* Check what has happened to hConOut. */
848 SetConsoleCursorPosition(hConOut, c);
849 ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
850 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
851 ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
852 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
853 str_wbuf[lstrlenA(test_cp1251)] = 0;
854 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
856 /* Close all handles of current console SB */
857 CloseHandle(hConOutNew);
858 CloseHandle(hConOutRW);
860 /* Now active SB should be hConOut */
861 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
862 NULL, OPEN_EXISTING, 0, 0);
863 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
865 /* Write using hConOutNew... */
866 SetConsoleCursorPosition(hConOutNew, c);
867 ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
868 ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
869 /* ... and read it back via hConOut */
870 ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
871 ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
872 str_buf[lstrlenA(test_str1)] = 0;
873 todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
874 CloseHandle(hConOutNew);
876 /* This is not really needed under Windows */
877 SetConsoleActiveScreenBuffer(hConOut);
879 /* restore codepage */
880 SetConsoleOutputCP(oldcp);
883 static void test_GetSetConsoleInputExeName(void)
885 BOOL ret;
886 DWORD error;
887 char buffer[MAX_PATH], module[MAX_PATH], *p;
888 static char input_exe[MAX_PATH] = "winetest.exe";
890 SetLastError(0xdeadbeef);
891 ret = pGetConsoleInputExeNameA(0, NULL);
892 error = GetLastError();
893 ok(ret, "GetConsoleInputExeNameA failed\n");
894 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
896 SetLastError(0xdeadbeef);
897 ret = pGetConsoleInputExeNameA(0, buffer);
898 error = GetLastError();
899 ok(ret, "GetConsoleInputExeNameA failed\n");
900 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
902 GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
903 p = strrchr(module, '\\') + 1;
905 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
906 ok(ret, "GetConsoleInputExeNameA failed\n");
907 todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
909 SetLastError(0xdeadbeef);
910 ret = pSetConsoleInputExeNameA(NULL);
911 error = GetLastError();
912 ok(!ret, "SetConsoleInputExeNameA failed\n");
913 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
915 SetLastError(0xdeadbeef);
916 ret = pSetConsoleInputExeNameA("");
917 error = GetLastError();
918 ok(!ret, "SetConsoleInputExeNameA failed\n");
919 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
921 ret = pSetConsoleInputExeNameA(input_exe);
922 ok(ret, "SetConsoleInputExeNameA failed\n");
924 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
925 ok(ret, "GetConsoleInputExeNameA failed\n");
926 ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
929 START_TEST(console)
931 HANDLE hConIn, hConOut;
932 BOOL ret;
933 CONSOLE_SCREEN_BUFFER_INFO sbi;
935 init_function_pointers();
937 /* be sure we have a clean console (and that's our own)
938 * FIXME: this will make the test fail (currently) if we don't run
939 * under X11
940 * Another solution would be to rerun the test under wineconsole with
941 * the curses backend
944 /* first, we detach and open a fresh console to play with */
945 FreeConsole();
946 ok(AllocConsole(), "Couldn't alloc console\n");
947 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
948 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
950 /* now verify everything's ok */
951 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
952 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
954 ret = GetConsoleScreenBufferInfo(hConOut, &sbi);
955 ok(ret, "Getting sb info\n");
956 if (!ret) return;
958 /* Non interactive tests */
959 testCursor(hConOut, sbi.dwSize);
960 /* test parameters (FIXME: test functionality) */
961 testCursorInfo(hConOut);
962 /* will test wrapped (on/off) & processed (on/off) strings output */
963 testWrite(hConOut, sbi.dwSize);
964 /* will test line scrolling at the bottom of the screen */
965 /* testBottomScroll(); */
966 /* will test all the scrolling operations */
967 testScroll(hConOut, sbi.dwSize);
968 /* will test sb creation / modification / codepage handling */
969 testScreenBuffer(hConOut);
970 testCtrlHandler();
971 /* still to be done: access rights & access on objects */
973 if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
975 win_skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
976 return;
978 else
979 test_GetSetConsoleInputExeName();