kernel32: Add a stubbed GetConsoleProcessList().
[wine/multimedia.git] / dlls / kernel32 / tests / console.c
blobace4744067da600994ac636a35ab18ecec2d003b
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 DWORD (WINAPI *pGetConsoleProcessList)(LPDWORD, DWORD);
28 static BOOL (WINAPI *pSetConsoleInputExeNameA)(LPCSTR);
30 /* DEFAULT_ATTRIB is used for all initial filling of the console.
31 * all modifications are made with TEST_ATTRIB so that we could check
32 * what has to be modified or not
34 #define TEST_ATTRIB (BACKGROUND_BLUE | FOREGROUND_GREEN)
35 #define DEFAULT_ATTRIB (FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED)
36 /* when filling the screen with non-blank chars, this macro defines
37 * what character should be at position 'c'
39 #define CONTENT(c) ('A' + (((c).Y * 17 + (c).X) % 23))
41 #define okCURSOR(hCon, c) do { \
42 CONSOLE_SCREEN_BUFFER_INFO __sbi; \
43 BOOL expect = GetConsoleScreenBufferInfo((hCon), &__sbi) && \
44 __sbi.dwCursorPosition.X == (c).X && __sbi.dwCursorPosition.Y == (c).Y; \
45 ok(expect, "Expected cursor at (%d,%d), got (%d,%d)\n", \
46 (c).X, (c).Y, __sbi.dwCursorPosition.X, __sbi.dwCursorPosition.Y); \
47 } while (0)
49 #define okCHAR(hCon, c, ch, attr) do { \
50 char __ch; WORD __attr; DWORD __len; BOOL expect; \
51 expect = ReadConsoleOutputCharacter((hCon), &__ch, 1, (c), &__len) == 1 && __len == 1 && __ch == (ch); \
52 ok(expect, "At (%d,%d): expecting char '%c'/%02x got '%c'/%02x\n", (c).X, (c).Y, (ch), (ch), __ch, __ch); \
53 expect = ReadConsoleOutputAttribute((hCon), &__attr, 1, (c), &__len) == 1 && __len == 1 && __attr == (attr); \
54 ok(expect, "At (%d,%d): expecting attr %04x got %04x\n", (c).X, (c).Y, (attr), __attr); \
55 } while (0)
57 static void init_function_pointers(void)
59 HMODULE hKernel32;
61 #define KERNEL32_GET_PROC(func) \
62 p##func = (void *)GetProcAddress(hKernel32, #func); \
63 if(!p##func) trace("GetProcAddress(hKernel32, '%s') failed\n", #func);
65 hKernel32 = GetModuleHandleA("kernel32.dll");
66 KERNEL32_GET_PROC(GetConsoleInputExeNameA);
67 KERNEL32_GET_PROC(GetConsoleProcessList);
68 KERNEL32_GET_PROC(SetConsoleInputExeNameA);
70 #undef KERNEL32_GET_PROC
73 /* FIXME: this could be optimized on a speed point of view */
74 static void resetContent(HANDLE hCon, COORD sbSize, BOOL content)
76 COORD c;
77 WORD attr = DEFAULT_ATTRIB;
78 char ch;
79 DWORD len;
81 for (c.X = 0; c.X < sbSize.X; c.X++)
83 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
85 ch = (content) ? CONTENT(c) : ' ';
86 WriteConsoleOutputAttribute(hCon, &attr, 1, c, &len);
87 WriteConsoleOutputCharacterA(hCon, &ch, 1, c, &len);
92 static void testCursor(HANDLE hCon, COORD sbSize)
94 COORD c;
96 c.X = c.Y = 0;
97 ok(SetConsoleCursorPosition(0, c) == 0, "No handle\n");
98 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
99 ERROR_INVALID_HANDLE, GetLastError());
101 c.X = c.Y = 0;
102 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
103 okCURSOR(hCon, c);
105 c.X = sbSize.X - 1;
106 c.Y = sbSize.Y - 1;
107 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in lower-right\n");
108 okCURSOR(hCon, c);
110 c.X = sbSize.X;
111 c.Y = sbSize.Y - 1;
112 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
113 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
114 ERROR_INVALID_PARAMETER, GetLastError());
116 c.X = sbSize.X - 1;
117 c.Y = sbSize.Y;
118 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
119 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
120 ERROR_INVALID_PARAMETER, GetLastError());
122 c.X = -1;
123 c.Y = 0;
124 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
125 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
126 ERROR_INVALID_PARAMETER, GetLastError());
128 c.X = 0;
129 c.Y = -1;
130 ok(SetConsoleCursorPosition(hCon, c) == 0, "Cursor is outside\n");
131 ok(GetLastError() == ERROR_INVALID_PARAMETER, "GetLastError: expecting %u got %u\n",
132 ERROR_INVALID_PARAMETER, GetLastError());
135 static void testCursorInfo(HANDLE hCon)
137 BOOL ret;
138 CONSOLE_CURSOR_INFO info;
140 SetLastError(0xdeadbeef);
141 ret = GetConsoleCursorInfo(NULL, NULL);
142 ok(!ret, "Expected failure\n");
143 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
144 ERROR_INVALID_HANDLE, GetLastError());
146 SetLastError(0xdeadbeef);
147 info.dwSize = -1;
148 ret = GetConsoleCursorInfo(NULL, &info);
149 ok(!ret, "Expected failure\n");
150 ok(info.dwSize == -1, "Expected no change for dwSize\n");
151 ok(GetLastError() == ERROR_INVALID_HANDLE, "GetLastError: expecting %u got %u\n",
152 ERROR_INVALID_HANDLE, GetLastError());
154 /* Test the correct call first to distinguish between win9x and the rest */
155 SetLastError(0xdeadbeef);
156 ret = GetConsoleCursorInfo(hCon, &info);
157 ok(ret, "Expected success\n");
158 ok(info.dwSize == 25 ||
159 info.dwSize == 12 /* win9x */,
160 "Expected 12 or 25, got %d\n", info.dwSize);
161 ok(info.bVisible, "Expected the cursor to be visible\n");
162 ok(GetLastError() == 0xdeadbeef, "GetLastError: expecting %u got %u\n",
163 0xdeadbeef, GetLastError());
165 /* Don't test NULL CONSOLE_CURSOR_INFO, it crashes on win9x and win7 */
168 static void testEmptyWrite(HANDLE hCon)
170 COORD c;
171 DWORD len;
172 const char* mytest = "";
174 c.X = c.Y = 0;
175 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
177 len = -1;
178 ok(WriteConsole(hCon, NULL, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
179 okCURSOR(hCon, c);
181 /* Passing a NULL lpBuffer with sufficiently large non-zero length succeeds
182 * on native Windows and result in memory-like contents being written to
183 * the console. Calling WriteConsoleW like this will crash on Wine. */
184 if (0)
186 len = -1;
187 ok(!WriteConsole(hCon, NULL, 16, &len, NULL) && len == -1, "WriteConsole\n");
188 okCURSOR(hCon, c);
190 /* Cursor advances for this call. */
191 len = -1;
192 ok(WriteConsole(hCon, NULL, 128, &len, NULL) != 0 && len == 128, "WriteConsole\n");
195 len = -1;
196 ok(WriteConsole(hCon, mytest, 0, &len, NULL) != 0 && len == 0, "WriteConsole\n");
197 okCURSOR(hCon, c);
199 /* WriteConsole does not halt on a null terminator and is happy to write
200 * memory contents beyond the actual size of the buffer. */
201 len = -1;
202 ok(WriteConsole(hCon, mytest, 16, &len, NULL) != 0 && len == 16, "WriteConsole\n");
203 c.X += 16;
204 okCURSOR(hCon, c);
207 static void testWriteSimple(HANDLE hCon)
209 COORD c;
210 DWORD len;
211 const char* mytest = "abcdefg";
212 const int mylen = strlen(mytest);
214 /* single line write */
215 c.X = c.Y = 0;
216 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
218 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
219 c.Y = 0;
220 for (c.X = 0; c.X < mylen; c.X++)
222 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
225 okCURSOR(hCon, c);
226 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
229 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
231 COORD c;
232 DWORD len, mode;
233 const char* mytest = "123";
234 const int mylen = strlen(mytest);
235 int ret;
236 int p;
238 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
239 "clearing wrap at EOL & processed output\n");
241 /* write line, wrapping disabled, buffer exceeds sb width */
242 c.X = sbSize.X - 3; c.Y = 0;
243 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
245 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
246 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
247 c.Y = 0;
248 for (p = mylen - 3; p < mylen; p++)
250 c.X = sbSize.X - 3 + p % 3;
251 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
254 c.X = 0; c.Y = 1;
255 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
257 p = sbSize.X - 3 + mylen % 3;
258 c.X = p; c.Y = 0;
260 /* write line, wrapping disabled, strings end on end of line */
261 c.X = sbSize.X - mylen; c.Y = 0;
262 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
264 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
267 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
269 COORD c;
270 DWORD len, mode;
271 const char* mytest = "abcd\nf\tg";
272 const int mylen = strlen(mytest);
273 const int mylen2 = strchr(mytest, '\n') - mytest;
274 int p;
275 WORD attr;
277 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
278 "clearing wrap at EOL & setting processed output\n");
280 /* write line, wrapping disabled, buffer exceeds sb width */
281 c.X = sbSize.X - 5; c.Y = 0;
282 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
284 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
285 c.Y = 0;
286 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
288 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
291 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
292 /* Win9x and WinMe change the attribs for '\n' up to 'f' */
293 if (attr == TEST_ATTRIB)
295 win_skip("Win9x/WinMe don't respect ~ENABLE_WRAP_AT_EOL_OUTPUT\n");
296 return;
299 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
301 c.X = 0; c.Y++;
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);
306 c.X++;
307 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
309 okCURSOR(hCon, c);
311 /* write line, wrapping disabled, strings end on end of line */
312 c.X = sbSize.X - 4; c.Y = 0;
313 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
315 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
316 c.Y = 0;
317 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
319 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
321 c.X = 0; c.Y++;
322 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
323 for (c.X = 1; c.X < 8; c.X++)
324 okCHAR(hCon, c, ' ', TEST_ATTRIB);
325 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
326 c.X++;
327 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
329 okCURSOR(hCon, c);
331 /* write line, wrapping disabled, strings end after end of line */
332 c.X = sbSize.X - 3; c.Y = 0;
333 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
335 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
336 c.Y = 0;
337 for (p = mylen2 - 3; p < mylen2; p++)
339 c.X = sbSize.X - 3 + p % 3;
340 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
342 c.X = 0; c.Y = 1;
343 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
344 for (c.X = 1; c.X < 8; c.X++)
345 okCHAR(hCon, c, ' ', TEST_ATTRIB);
346 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
347 c.X++;
348 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
350 okCURSOR(hCon, c);
353 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
355 COORD c;
356 DWORD len, mode;
357 const char* mytest = "abcd\nf\tg";
358 const int mylen = strlen(mytest);
359 int p;
361 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
362 "setting wrap at EOL & clearing processed output\n");
364 /* write line, wrapping enabled, buffer doesn't exceed sb width */
365 c.X = sbSize.X - 9; c.Y = 0;
366 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
368 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
369 c.Y = 0;
370 for (p = 0; p < mylen; p++)
372 c.X = sbSize.X - 9 + p;
373 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
375 c.X = sbSize.X - 9 + mylen;
376 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
377 c.X = 0; c.Y = 1;
378 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
380 /* write line, wrapping enabled, buffer does exceed sb width */
381 c.X = sbSize.X - 3; c.Y = 0;
382 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
384 c.Y = 1;
385 c.X = mylen - 3;
386 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
389 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
391 COORD c;
392 DWORD len, mode;
393 const char* mytest = "abcd\nf\tg";
394 const int mylen = strlen(mytest);
395 int p;
396 WORD attr;
398 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
399 "setting wrap at EOL & processed output\n");
401 /* write line, wrapping enabled, buffer doesn't exceed sb width */
402 c.X = sbSize.X - 9; c.Y = 0;
403 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
405 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
406 for (p = 0; p < 4; p++)
408 c.X = sbSize.X - 9 + p;
409 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
411 c.X = sbSize.X - 9 + p;
412 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
413 if (attr == TEST_ATTRIB)
414 win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
415 else
416 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
417 c.X = 0; c.Y++;
418 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
419 for (c.X = 1; c.X < 8; c.X++)
420 okCHAR(hCon, c, ' ', TEST_ATTRIB);
421 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
422 c.X++;
423 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
424 okCURSOR(hCon, c);
426 /* write line, wrapping enabled, buffer does exceed sb width */
427 c.X = sbSize.X - 3; c.Y = 2;
428 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
430 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
431 for (p = 0; p < 3; p++)
433 c.X = sbSize.X - 3 + p;
434 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
436 c.X = 0; c.Y++;
437 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
438 c.X++;
439 ReadConsoleOutputAttribute(hCon, &attr, 1, c, &len);
440 if (attr == TEST_ATTRIB)
441 win_skip("Win9x/WinMe changes attribs for '\\n' up to 'f'\n");
442 else
443 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
445 c.X = 0; c.Y++;
446 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
447 for (c.X = 1; c.X < 8; c.X++)
448 okCHAR(hCon, c, ' ', TEST_ATTRIB);
449 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
450 c.X++;
451 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
452 okCURSOR(hCon, c);
455 static void testWrite(HANDLE hCon, COORD sbSize)
457 /* FIXME: should in fact insure that the sb is at least 10 character wide */
458 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
459 resetContent(hCon, sbSize, FALSE);
460 testEmptyWrite(hCon);
461 resetContent(hCon, sbSize, FALSE);
462 testWriteSimple(hCon);
463 resetContent(hCon, sbSize, FALSE);
464 testWriteNotWrappedNotProcessed(hCon, sbSize);
465 resetContent(hCon, sbSize, FALSE);
466 testWriteNotWrappedProcessed(hCon, sbSize);
467 resetContent(hCon, sbSize, FALSE);
468 testWriteWrappedNotProcessed(hCon, sbSize);
469 resetContent(hCon, sbSize, FALSE);
470 testWriteWrappedProcessed(hCon, sbSize);
473 static void testScroll(HANDLE hCon, COORD sbSize)
475 SMALL_RECT scroll, clip;
476 COORD dst, c, tc;
477 CHAR_INFO ci;
478 BOOL ret;
480 #define W 11
481 #define H 7
483 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
484 #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)
486 /* no clipping, src & dst rect don't overlap */
487 resetContent(hCon, sbSize, TRUE);
489 scroll.Left = 0;
490 scroll.Right = W - 1;
491 scroll.Top = 0;
492 scroll.Bottom = H - 1;
493 dst.X = W + 3;
494 dst.Y = H + 3;
495 ci.Char.UnicodeChar = '#';
496 ci.Attributes = TEST_ATTRIB;
498 clip.Left = 0;
499 clip.Right = sbSize.X - 1;
500 clip.Top = 0;
501 clip.Bottom = sbSize.Y - 1;
503 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
505 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
507 for (c.X = 0; c.X < sbSize.X; c.X++)
509 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
511 tc.X = c.X - dst.X;
512 tc.Y = c.Y - dst.Y;
513 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
515 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
516 okCHAR(hCon, c, '#', TEST_ATTRIB);
517 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
521 /* no clipping, src & dst rect do overlap */
522 resetContent(hCon, sbSize, TRUE);
524 scroll.Left = 0;
525 scroll.Right = W - 1;
526 scroll.Top = 0;
527 scroll.Bottom = H - 1;
528 dst.X = W /2;
529 dst.Y = H / 2;
530 ci.Char.UnicodeChar = '#';
531 ci.Attributes = TEST_ATTRIB;
533 clip.Left = 0;
534 clip.Right = sbSize.X - 1;
535 clip.Top = 0;
536 clip.Bottom = sbSize.Y - 1;
538 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
540 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
542 for (c.X = 0; c.X < sbSize.X; c.X++)
544 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
546 tc.X = c.X - dst.X;
547 tc.Y = c.Y - dst.Y;
548 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
550 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
551 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
555 /* clipping, src & dst rect don't overlap */
556 resetContent(hCon, sbSize, TRUE);
558 scroll.Left = 0;
559 scroll.Right = W - 1;
560 scroll.Top = 0;
561 scroll.Bottom = H - 1;
562 dst.X = W + 3;
563 dst.Y = H + 3;
564 ci.Char.UnicodeChar = '#';
565 ci.Attributes = TEST_ATTRIB;
567 clip.Left = W / 2;
568 clip.Right = min(W + W / 2, sbSize.X - 1);
569 clip.Top = H / 2;
570 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
572 SetLastError(0xdeadbeef);
573 ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
574 if (ret)
576 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
578 for (c.X = 0; c.X < sbSize.X; c.X++)
580 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
582 tc.X = c.X - dst.X;
583 tc.Y = c.Y - dst.Y;
584 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
586 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
587 okCHAR(hCon, c, '#', TEST_ATTRIB);
588 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
592 else
594 /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
595 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
596 "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
599 /* clipping, src & dst rect do overlap */
600 resetContent(hCon, sbSize, TRUE);
602 scroll.Left = 0;
603 scroll.Right = W - 1;
604 scroll.Top = 0;
605 scroll.Bottom = H - 1;
606 dst.X = W / 2 - 3;
607 dst.Y = H / 2 - 3;
608 ci.Char.UnicodeChar = '#';
609 ci.Attributes = TEST_ATTRIB;
611 clip.Left = W / 2;
612 clip.Right = min(W + W / 2, sbSize.X - 1);
613 clip.Top = H / 2;
614 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
616 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
618 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
620 for (c.X = 0; c.X < sbSize.X; c.X++)
622 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
624 tc.X = c.X - dst.X;
625 tc.Y = c.Y - dst.Y;
626 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
628 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
629 okCHAR(hCon, c, '#', TEST_ATTRIB);
630 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
635 static int mch_count;
636 /* we need the event as Wine console event generation isn't synchronous
637 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
638 * processes have been called).
640 static HANDLE mch_event;
641 static BOOL WINAPI mch(DWORD event)
643 mch_count++;
644 SetEvent(mch_event);
645 return TRUE;
648 static void testCtrlHandler(void)
650 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
651 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
652 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
653 /* wine requires the event for the test, as we cannot insure, so far, that event
654 * are processed synchronously in GenerateConsoleCtrlEvent()
656 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
657 mch_count = 0;
658 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
659 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
660 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
661 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
662 CloseHandle(mch_event);
664 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
665 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
666 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
667 mch_count = 0;
668 if(!(GetVersion() & 0x80000000))
669 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
670 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
671 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
672 CloseHandle(mch_event);
673 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
674 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
675 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
679 * Test console screen buffer:
680 * 1) Try to set invalid handle.
681 * 2) Try to set non-console handles.
682 * 3) Use CONOUT$ file as active SB.
683 * 4) Test cursor.
684 * 5) Test output codepage to show it is not a property of SB.
685 * 6) Test switching to old SB if we close all handles to current SB - works
686 * in Windows, TODO in wine.
688 * What is not tested but should be:
689 * 1) ScreenBufferInfo
691 static void testScreenBuffer(HANDLE hConOut)
693 HANDLE hConOutRW, hConOutRO, hConOutWT;
694 HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
695 HANDLE hConOutNew;
696 char test_str1[] = "Test for SB1";
697 char test_str2[] = "Test for SB2";
698 char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
699 char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
700 WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
701 WCHAR str_wbuf[20];
702 char str_buf[20];
703 DWORD len;
704 COORD c;
705 BOOL ret;
706 DWORD oldcp;
708 if (!IsValidCodePage(866))
710 skip("Codepage 866 not available\n");
711 return;
714 /* In the beginning set output codepage to 866 */
715 oldcp = GetConsoleOutputCP();
716 SetLastError(0xdeadbeef);
717 ret = SetConsoleOutputCP(866);
718 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
720 win_skip("SetConsoleOutputCP is not implemented\n");
721 return;
723 ok(ret, "Cannot set output codepage to 866\n");
725 hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
726 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
727 CONSOLE_TEXTMODE_BUFFER, NULL);
728 ok(hConOutRW != INVALID_HANDLE_VALUE,
729 "Cannot create a new screen buffer for ReadWrite\n");
730 hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
731 FILE_SHARE_READ, NULL,
732 CONSOLE_TEXTMODE_BUFFER, NULL);
733 ok(hConOutRO != INVALID_HANDLE_VALUE,
734 "Cannot create a new screen buffer for ReadOnly\n");
735 hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
736 FILE_SHARE_WRITE, NULL,
737 CONSOLE_TEXTMODE_BUFFER, NULL);
738 ok(hConOutWT != INVALID_HANDLE_VALUE,
739 "Cannot create a new screen buffer for WriteOnly\n");
741 hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
742 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
743 OPEN_EXISTING, 0, NULL);
744 ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
745 hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
746 NULL, OPEN_EXISTING, 0, NULL);
747 ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
748 hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
749 NULL, OPEN_EXISTING, 0, NULL);
750 ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
752 /* Trying to set invalid handle */
753 SetLastError(0);
754 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
755 "Shouldn't succeed\n");
756 ok(GetLastError() == ERROR_INVALID_HANDLE,
757 "GetLastError: expecting %u got %u\n",
758 ERROR_INVALID_HANDLE, GetLastError());
760 /* Trying to set non-console handles */
761 SetLastError(0);
762 ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
763 ok(GetLastError() == ERROR_INVALID_HANDLE,
764 "GetLastError: expecting %u got %u\n",
765 ERROR_INVALID_HANDLE, GetLastError());
767 SetLastError(0);
768 ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
769 ok(GetLastError() == ERROR_INVALID_HANDLE,
770 "GetLastError: expecting %u got %u\n",
771 ERROR_INVALID_HANDLE, GetLastError());
773 SetLastError(0);
774 ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
775 ok(GetLastError() == ERROR_INVALID_HANDLE,
776 "GetLastError: expecting %u got %u\n",
777 ERROR_INVALID_HANDLE, GetLastError());
779 CloseHandle(hFileOutRW);
780 CloseHandle(hFileOutRO);
781 CloseHandle(hFileOutWT);
783 /* Trying to set SB handles with various access modes */
784 SetLastError(0);
785 ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
786 ok(GetLastError() == ERROR_INVALID_HANDLE,
787 "GetLastError: expecting %u got %u\n",
788 ERROR_INVALID_HANDLE, GetLastError());
790 ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
792 ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
794 CloseHandle(hConOutWT);
795 CloseHandle(hConOutRO);
797 /* Now we have two ReadWrite SB, active must be hConOutRW */
798 /* Open current SB via CONOUT$ */
799 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
800 NULL, OPEN_EXISTING, 0, 0);
801 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
804 /* test cursor */
805 c.X = c.Y = 10;
806 SetConsoleCursorPosition(hConOut, c);
807 c.X = c.Y = 5;
808 SetConsoleCursorPosition(hConOutRW, c);
809 okCURSOR(hConOutNew, c);
810 c.X = c.Y = 10;
811 okCURSOR(hConOut, c);
814 c.X = c.Y = 0;
816 /* Write using hConOutNew... */
817 SetConsoleCursorPosition(hConOutNew, c);
818 ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
819 ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
820 /* ... and read it back via hConOutRW */
821 ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
822 ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
823 str_buf[lstrlenA(test_str2)] = 0;
824 ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
827 /* Now test output codepage handling. Current is 866 as we set earlier. */
828 SetConsoleCursorPosition(hConOutRW, c);
829 ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
830 ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
831 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
832 ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
833 str_wbuf[lstrlenA(test_cp866)] = 0;
834 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
837 * cp866 is OK, let's switch to cp1251.
838 * We expect that this codepage will be used in every SB - active and not.
840 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
841 SetConsoleCursorPosition(hConOutRW, c);
842 ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
843 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
844 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
845 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
846 str_wbuf[lstrlenA(test_cp1251)] = 0;
847 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
849 /* Check what has happened to hConOut. */
850 SetConsoleCursorPosition(hConOut, c);
851 ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
852 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
853 ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
854 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
855 str_wbuf[lstrlenA(test_cp1251)] = 0;
856 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
858 /* Close all handles of current console SB */
859 CloseHandle(hConOutNew);
860 CloseHandle(hConOutRW);
862 /* Now active SB should be hConOut */
863 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
864 NULL, OPEN_EXISTING, 0, 0);
865 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
867 /* Write using hConOutNew... */
868 SetConsoleCursorPosition(hConOutNew, c);
869 ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
870 ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
871 /* ... and read it back via hConOut */
872 ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
873 ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
874 str_buf[lstrlenA(test_str1)] = 0;
875 todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
876 CloseHandle(hConOutNew);
878 /* This is not really needed under Windows */
879 SetConsoleActiveScreenBuffer(hConOut);
881 /* restore codepage */
882 SetConsoleOutputCP(oldcp);
885 static void test_GetSetConsoleInputExeName(void)
887 BOOL ret;
888 DWORD error;
889 char buffer[MAX_PATH], module[MAX_PATH], *p;
890 static char input_exe[MAX_PATH] = "winetest.exe";
892 SetLastError(0xdeadbeef);
893 ret = pGetConsoleInputExeNameA(0, NULL);
894 error = GetLastError();
895 ok(ret, "GetConsoleInputExeNameA failed\n");
896 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
898 SetLastError(0xdeadbeef);
899 ret = pGetConsoleInputExeNameA(0, buffer);
900 error = GetLastError();
901 ok(ret, "GetConsoleInputExeNameA failed\n");
902 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
904 GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
905 p = strrchr(module, '\\') + 1;
907 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
908 ok(ret, "GetConsoleInputExeNameA failed\n");
909 todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
911 SetLastError(0xdeadbeef);
912 ret = pSetConsoleInputExeNameA(NULL);
913 error = GetLastError();
914 ok(!ret, "SetConsoleInputExeNameA failed\n");
915 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
917 SetLastError(0xdeadbeef);
918 ret = pSetConsoleInputExeNameA("");
919 error = GetLastError();
920 ok(!ret, "SetConsoleInputExeNameA failed\n");
921 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
923 ret = pSetConsoleInputExeNameA(input_exe);
924 ok(ret, "SetConsoleInputExeNameA failed\n");
926 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
927 ok(ret, "GetConsoleInputExeNameA failed\n");
928 ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
931 static void test_GetConsoleProcessList(void)
933 DWORD ret, *list = NULL;
935 if (!pGetConsoleProcessList)
937 win_skip("GetConsoleProcessList is not available\n");
938 return;
941 SetLastError(0xdeadbeef);
942 ret = pGetConsoleProcessList(NULL, 0);
943 ok(ret == 0, "Expected failure\n");
944 ok(GetLastError() == ERROR_INVALID_PARAMETER,
945 "Expected ERROR_INVALID_PARAMETER, got %d\n",
946 GetLastError());
948 SetLastError(0xdeadbeef);
949 ret = pGetConsoleProcessList(NULL, 1);
950 ok(ret == 0, "Expected failure\n");
951 ok(GetLastError() == ERROR_INVALID_PARAMETER,
952 "Expected ERROR_INVALID_PARAMETER, got %d\n",
953 GetLastError());
955 /* We should only have 1 process but only for these specific unit tests as
956 * we created our own console. An AttachConsole(ATTACH_PARENT_PROCESS) would
957 * give us two processes for example.
959 list = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD));
961 SetLastError(0xdeadbeef);
962 ret = pGetConsoleProcessList(list, 0);
963 ok(ret == 0, "Expected failure\n");
964 ok(GetLastError() == ERROR_INVALID_PARAMETER,
965 "Expected ERROR_INVALID_PARAMETER, got %d\n",
966 GetLastError());
968 SetLastError(0xdeadbeef);
969 ret = pGetConsoleProcessList(list, 1);
970 todo_wine
971 ok(ret == 1, "Expected 1, got %d\n", ret);
973 HeapFree(GetProcessHeap(), 0, list);
975 list = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(DWORD));
977 SetLastError(0xdeadbeef);
978 ret = pGetConsoleProcessList(list, ret);
979 todo_wine
980 ok(ret == 1, "Expected 1, got %d\n", ret);
982 if (ret == 1)
984 DWORD pid = GetCurrentProcessId();
985 ok(list[0] == pid, "Expected %d, got %d\n", pid, list[0]);
988 HeapFree(GetProcessHeap(), 0, list);
991 START_TEST(console)
993 HANDLE hConIn, hConOut;
994 BOOL ret;
995 CONSOLE_SCREEN_BUFFER_INFO sbi;
997 init_function_pointers();
999 /* be sure we have a clean console (and that's our own)
1000 * FIXME: this will make the test fail (currently) if we don't run
1001 * under X11
1002 * Another solution would be to rerun the test under wineconsole with
1003 * the curses backend
1006 /* first, we detach and open a fresh console to play with */
1007 FreeConsole();
1008 ok(AllocConsole(), "Couldn't alloc console\n");
1009 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1010 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
1012 /* now verify everything's ok */
1013 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
1014 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
1016 ret = GetConsoleScreenBufferInfo(hConOut, &sbi);
1017 ok(ret, "Getting sb info\n");
1018 if (!ret) return;
1020 /* Non interactive tests */
1021 testCursor(hConOut, sbi.dwSize);
1022 /* test parameters (FIXME: test functionality) */
1023 testCursorInfo(hConOut);
1024 /* will test wrapped (on/off) & processed (on/off) strings output */
1025 testWrite(hConOut, sbi.dwSize);
1026 /* will test line scrolling at the bottom of the screen */
1027 /* testBottomScroll(); */
1028 /* will test all the scrolling operations */
1029 testScroll(hConOut, sbi.dwSize);
1030 /* will test sb creation / modification / codepage handling */
1031 testScreenBuffer(hConOut);
1032 testCtrlHandler();
1033 /* still to be done: access rights & access on objects */
1035 if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
1036 win_skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
1037 else
1038 test_GetSetConsoleInputExeName();
1040 test_GetConsoleProcessList();