kernel32/tests: Add some tests for GetConsoleCursorInfo.
[wine/gsoc_dplay.git] / dlls / kernel32 / tests / console.c
blob2e0a78d0d942cfef0d61bc071c8c16c814594653
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 if (info.dwSize == 12)
165 skip("NULL CONSOLE_CURSOR_INFO will crash on win9x\n");
166 return;
169 SetLastError(0xdeadbeef);
170 ret = GetConsoleCursorInfo(hCon, NULL);
171 todo_wine
173 ok(!ret, "Expected failure\n");
174 ok(GetLastError() == ERROR_INVALID_ACCESS, "GetLastError: expecting %u got %u\n",
175 ERROR_INVALID_ACCESS, GetLastError());
179 static void testWriteSimple(HANDLE hCon, COORD sbSize)
181 COORD c;
182 DWORD len;
183 const char* mytest = "abcdefg";
184 const int mylen = strlen(mytest);
186 /* single line write */
187 c.X = c.Y = 0;
188 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
190 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
191 c.Y = 0;
192 for (c.X = 0; c.X < mylen; c.X++)
194 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
197 okCURSOR(hCon, c);
198 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
201 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
203 COORD c;
204 DWORD len, mode;
205 const char* mytest = "123";
206 const int mylen = strlen(mytest);
207 int ret;
208 int p;
210 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
211 "clearing wrap at EOL & processed output\n");
213 /* write line, wrapping disabled, buffer exceeds sb width */
214 c.X = sbSize.X - 3; c.Y = 0;
215 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
217 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
218 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
219 c.Y = 0;
220 for (p = mylen - 3; p < mylen; p++)
222 c.X = sbSize.X - 3 + p % 3;
223 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
226 c.X = 0; c.Y = 1;
227 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
229 p = sbSize.X - 3 + mylen % 3;
230 c.X = p; c.Y = 0;
232 /* write line, wrapping disabled, strings end on end of line */
233 c.X = sbSize.X - mylen; c.Y = 0;
234 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
236 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
239 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
241 COORD c;
242 DWORD len, mode;
243 const char* mytest = "abcd\nf\tg";
244 const int mylen = strlen(mytest);
245 const int mylen2 = strchr(mytest, '\n') - mytest;
246 int p;
248 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
249 "clearing wrap at EOL & setting processed output\n");
251 /* write line, wrapping disabled, buffer exceeds sb width */
252 c.X = sbSize.X - 5; c.Y = 0;
253 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
255 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
256 c.Y = 0;
257 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
259 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
261 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
263 c.X = 0; c.Y++;
264 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
265 for (c.X = 1; c.X < 8; c.X++)
266 okCHAR(hCon, c, ' ', TEST_ATTRIB);
267 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
268 c.X++;
269 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
271 okCURSOR(hCon, c);
273 /* write line, wrapping disabled, strings end on end of line */
274 c.X = sbSize.X - 4; c.Y = 0;
275 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
277 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
278 c.Y = 0;
279 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
281 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
283 c.X = 0; c.Y++;
284 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
285 for (c.X = 1; c.X < 8; c.X++)
286 okCHAR(hCon, c, ' ', TEST_ATTRIB);
287 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
288 c.X++;
289 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
291 okCURSOR(hCon, c);
293 /* write line, wrapping disabled, strings end after end of line */
294 c.X = sbSize.X - 3; c.Y = 0;
295 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
297 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
298 c.Y = 0;
299 for (p = mylen2 - 3; p < mylen2; p++)
301 c.X = sbSize.X - 3 + p % 3;
302 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
304 c.X = 0; c.Y = 1;
305 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
306 for (c.X = 1; c.X < 8; c.X++)
307 okCHAR(hCon, c, ' ', TEST_ATTRIB);
308 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
309 c.X++;
310 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
312 okCURSOR(hCon, c);
315 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
317 COORD c;
318 DWORD len, mode;
319 const char* mytest = "abcd\nf\tg";
320 const int mylen = strlen(mytest);
321 int p;
323 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
324 "setting wrap at EOL & clearing processed output\n");
326 /* write line, wrapping enabled, buffer doesn't exceed sb width */
327 c.X = sbSize.X - 9; c.Y = 0;
328 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
330 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
331 c.Y = 0;
332 for (p = 0; p < mylen; p++)
334 c.X = sbSize.X - 9 + p;
335 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
337 c.X = sbSize.X - 9 + mylen;
338 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
339 c.X = 0; c.Y = 1;
340 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
342 /* write line, wrapping enabled, buffer does exceed sb width */
343 c.X = sbSize.X - 3; c.Y = 0;
344 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
346 c.Y = 1;
347 c.X = mylen - 3;
348 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
351 static void testWriteWrappedProcessed(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 & 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 for (p = 0; p < 4; p++)
369 c.X = sbSize.X - 9 + p;
370 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
372 c.X = sbSize.X - 9 + p;
373 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
374 c.X = 0; c.Y++;
375 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
376 for (c.X = 1; c.X < 8; c.X++)
377 okCHAR(hCon, c, ' ', TEST_ATTRIB);
378 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
379 c.X++;
380 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
381 okCURSOR(hCon, c);
383 /* write line, wrapping enabled, buffer does exceed sb width */
384 c.X = sbSize.X - 3; c.Y = 2;
385 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
387 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
388 for (p = 0; p < 3; p++)
390 c.X = sbSize.X - 3 + p;
391 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
393 c.X = 0; c.Y++;
394 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
395 c.X++;
396 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
398 c.X = 0; c.Y++;
399 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
400 for (c.X = 1; c.X < 8; c.X++)
401 okCHAR(hCon, c, ' ', TEST_ATTRIB);
402 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
403 c.X++;
404 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
405 okCURSOR(hCon, c);
408 static void testWrite(HANDLE hCon, COORD sbSize)
410 /* FIXME: should in fact insure that the sb is at least 10 character wide */
411 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
412 resetContent(hCon, sbSize, FALSE);
413 testWriteSimple(hCon, sbSize);
414 resetContent(hCon, sbSize, FALSE);
415 testWriteNotWrappedNotProcessed(hCon, sbSize);
416 resetContent(hCon, sbSize, FALSE);
417 testWriteNotWrappedProcessed(hCon, sbSize);
418 resetContent(hCon, sbSize, FALSE);
419 testWriteWrappedNotProcessed(hCon, sbSize);
420 resetContent(hCon, sbSize, FALSE);
421 testWriteWrappedProcessed(hCon, sbSize);
424 static void testScroll(HANDLE hCon, COORD sbSize)
426 SMALL_RECT scroll, clip;
427 COORD dst, c, tc;
428 CHAR_INFO ci;
429 BOOL ret;
431 #define W 11
432 #define H 7
434 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
435 #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)
437 /* no clipping, src & dst rect don't overlap */
438 resetContent(hCon, sbSize, TRUE);
440 scroll.Left = 0;
441 scroll.Right = W - 1;
442 scroll.Top = 0;
443 scroll.Bottom = H - 1;
444 dst.X = W + 3;
445 dst.Y = H + 3;
446 ci.Char.UnicodeChar = '#';
447 ci.Attributes = TEST_ATTRIB;
449 clip.Left = 0;
450 clip.Right = sbSize.X - 1;
451 clip.Top = 0;
452 clip.Bottom = sbSize.Y - 1;
454 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
456 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
458 for (c.X = 0; c.X < sbSize.X; c.X++)
460 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
462 tc.X = c.X - dst.X;
463 tc.Y = c.Y - dst.Y;
464 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
466 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
467 okCHAR(hCon, c, '#', TEST_ATTRIB);
468 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
472 /* no clipping, src & dst rect do overlap */
473 resetContent(hCon, sbSize, TRUE);
475 scroll.Left = 0;
476 scroll.Right = W - 1;
477 scroll.Top = 0;
478 scroll.Bottom = H - 1;
479 dst.X = W /2;
480 dst.Y = H / 2;
481 ci.Char.UnicodeChar = '#';
482 ci.Attributes = TEST_ATTRIB;
484 clip.Left = 0;
485 clip.Right = sbSize.X - 1;
486 clip.Top = 0;
487 clip.Bottom = sbSize.Y - 1;
489 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
491 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
493 for (c.X = 0; c.X < sbSize.X; c.X++)
495 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
497 tc.X = c.X - dst.X;
498 tc.Y = c.Y - dst.Y;
499 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
501 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
502 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
506 /* clipping, src & dst rect don't overlap */
507 resetContent(hCon, sbSize, TRUE);
509 scroll.Left = 0;
510 scroll.Right = W - 1;
511 scroll.Top = 0;
512 scroll.Bottom = H - 1;
513 dst.X = W + 3;
514 dst.Y = H + 3;
515 ci.Char.UnicodeChar = '#';
516 ci.Attributes = TEST_ATTRIB;
518 clip.Left = W / 2;
519 clip.Right = min(W + W / 2, sbSize.X - 1);
520 clip.Top = H / 2;
521 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
523 SetLastError(0xdeadbeef);
524 ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
525 if (ret)
527 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
529 for (c.X = 0; c.X < sbSize.X; c.X++)
531 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
533 tc.X = c.X - dst.X;
534 tc.Y = c.Y - dst.Y;
535 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
537 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
538 okCHAR(hCon, c, '#', TEST_ATTRIB);
539 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
543 else
545 /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
546 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
547 "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
550 /* clipping, src & dst rect do overlap */
551 resetContent(hCon, sbSize, TRUE);
553 scroll.Left = 0;
554 scroll.Right = W - 1;
555 scroll.Top = 0;
556 scroll.Bottom = H - 1;
557 dst.X = W / 2 - 3;
558 dst.Y = H / 2 - 3;
559 ci.Char.UnicodeChar = '#';
560 ci.Attributes = TEST_ATTRIB;
562 clip.Left = W / 2;
563 clip.Right = min(W + W / 2, sbSize.X - 1);
564 clip.Top = H / 2;
565 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
567 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
569 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
571 for (c.X = 0; c.X < sbSize.X; c.X++)
573 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
575 tc.X = c.X - dst.X;
576 tc.Y = c.Y - dst.Y;
577 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
579 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
580 okCHAR(hCon, c, '#', TEST_ATTRIB);
581 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
586 static int mch_count;
587 /* we need the event as Wine console event generation isn't synchronous
588 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
589 * processes have been called).
591 static HANDLE mch_event;
592 static BOOL WINAPI mch(DWORD event)
594 mch_count++;
595 SetEvent(mch_event);
596 return TRUE;
599 static void testCtrlHandler(void)
601 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
602 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
603 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
604 /* wine requires the event for the test, as we cannot insure, so far, that event
605 * are processed synchronously in GenerateConsoleCtrlEvent()
607 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
608 mch_count = 0;
609 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
610 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
611 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
612 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
613 CloseHandle(mch_event);
615 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
616 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
617 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
618 mch_count = 0;
619 if(!(GetVersion() & 0x80000000))
620 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
621 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
622 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
623 CloseHandle(mch_event);
624 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
625 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
626 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
630 * Test console screen buffer:
631 * 1) Try to set invalid handle.
632 * 2) Try to set non-console handles.
633 * 3) Use CONOUT$ file as active SB.
634 * 4) Test cursor.
635 * 5) Test output codepage to show it is not a property of SB.
636 * 6) Test switching to old SB if we close all handles to current SB - works
637 * in Windows, TODO in wine.
639 * What is not tested but should be:
640 * 1) ScreenBufferInfo
642 static void testScreenBuffer(HANDLE hConOut)
644 HANDLE hConOutRW, hConOutRO, hConOutWT;
645 HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
646 HANDLE hConOutNew;
647 char test_str1[] = "Test for SB1";
648 char test_str2[] = "Test for SB2";
649 char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
650 char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
651 WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
652 WCHAR str_wbuf[20];
653 char str_buf[20];
654 DWORD len;
655 COORD c;
656 BOOL ret;
657 DWORD oldcp;
659 if (!IsValidCodePage(866))
661 skip("Codepage 866 not available\n");
662 return;
665 /* In the beginning set output codepage to 866 */
666 oldcp = GetConsoleOutputCP();
667 SetLastError(0xdeadbeef);
668 ret = SetConsoleOutputCP(866);
669 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
671 skip("SetConsoleOutputCP is not implemented\n");
672 return;
674 ok(ret, "Cannot set output codepage to 866\n");
676 hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
677 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
678 CONSOLE_TEXTMODE_BUFFER, NULL);
679 ok(hConOutRW != INVALID_HANDLE_VALUE,
680 "Cannot create a new screen buffer for ReadWrite\n");
681 hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
682 FILE_SHARE_READ, NULL,
683 CONSOLE_TEXTMODE_BUFFER, NULL);
684 ok(hConOutRO != INVALID_HANDLE_VALUE,
685 "Cannot create a new screen buffer for ReadOnly\n");
686 hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
687 FILE_SHARE_WRITE, NULL,
688 CONSOLE_TEXTMODE_BUFFER, NULL);
689 ok(hConOutWT != INVALID_HANDLE_VALUE,
690 "Cannot create a new screen buffer for WriteOnly\n");
692 hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
693 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
694 OPEN_EXISTING, 0, NULL);
695 ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
696 hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
697 NULL, OPEN_EXISTING, 0, NULL);
698 ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
699 hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
700 NULL, OPEN_EXISTING, 0, NULL);
701 ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
703 /* Trying to set invalid handle */
704 SetLastError(0);
705 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
706 "Shouldn't succeed\n");
707 ok(GetLastError() == ERROR_INVALID_HANDLE,
708 "GetLastError: expecting %u got %u\n",
709 ERROR_INVALID_HANDLE, GetLastError());
711 /* Trying to set non-console handles */
712 SetLastError(0);
713 ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
714 ok(GetLastError() == ERROR_INVALID_HANDLE,
715 "GetLastError: expecting %u got %u\n",
716 ERROR_INVALID_HANDLE, GetLastError());
718 SetLastError(0);
719 ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
720 ok(GetLastError() == ERROR_INVALID_HANDLE,
721 "GetLastError: expecting %u got %u\n",
722 ERROR_INVALID_HANDLE, GetLastError());
724 SetLastError(0);
725 ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
726 ok(GetLastError() == ERROR_INVALID_HANDLE,
727 "GetLastError: expecting %u got %u\n",
728 ERROR_INVALID_HANDLE, GetLastError());
730 CloseHandle(hFileOutRW);
731 CloseHandle(hFileOutRO);
732 CloseHandle(hFileOutWT);
734 /* Trying to set SB handles with various access modes */
735 SetLastError(0);
736 ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
737 ok(GetLastError() == ERROR_INVALID_HANDLE,
738 "GetLastError: expecting %u got %u\n",
739 ERROR_INVALID_HANDLE, GetLastError());
741 ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
743 ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
745 CloseHandle(hConOutWT);
746 CloseHandle(hConOutRO);
748 /* Now we have two ReadWrite SB, active must be hConOutRW */
749 /* Open current SB via CONOUT$ */
750 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
751 NULL, OPEN_EXISTING, 0, 0);
752 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
755 /* test cursor */
756 c.X = c.Y = 10;
757 SetConsoleCursorPosition(hConOut, c);
758 c.X = c.Y = 5;
759 SetConsoleCursorPosition(hConOutRW, c);
760 okCURSOR(hConOutNew, c);
761 c.X = c.Y = 10;
762 okCURSOR(hConOut, c);
765 c.X = c.Y = 0;
767 /* Write using hConOutNew... */
768 SetConsoleCursorPosition(hConOutNew, c);
769 ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
770 ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
771 /* ... and read it back via hConOutRW */
772 ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
773 ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
774 str_buf[lstrlenA(test_str2)] = 0;
775 ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
778 /* Now test output codepage handling. Current is 866 as we set earlier. */
779 SetConsoleCursorPosition(hConOutRW, c);
780 ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
781 ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
782 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
783 ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
784 str_wbuf[lstrlenA(test_cp866)] = 0;
785 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
788 * cp866 is OK, let's switch to cp1251.
789 * We expect that this codepage will be used in every SB - active and not.
791 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
792 SetConsoleCursorPosition(hConOutRW, c);
793 ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
794 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
795 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
796 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
797 str_wbuf[lstrlenA(test_cp1251)] = 0;
798 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
800 /* Check what has happened to hConOut. */
801 SetConsoleCursorPosition(hConOut, c);
802 ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
803 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
804 ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
805 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
806 str_wbuf[lstrlenA(test_cp1251)] = 0;
807 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
809 /* Close all handles of current console SB */
810 CloseHandle(hConOutNew);
811 CloseHandle(hConOutRW);
813 /* Now active SB should be hConOut */
814 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
815 NULL, OPEN_EXISTING, 0, 0);
816 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
818 /* Write using hConOutNew... */
819 SetConsoleCursorPosition(hConOutNew, c);
820 ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
821 ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
822 /* ... and read it back via hConOut */
823 ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
824 ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
825 str_buf[lstrlenA(test_str1)] = 0;
826 todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
827 CloseHandle(hConOutNew);
829 /* This is not really needed under Windows */
830 SetConsoleActiveScreenBuffer(hConOut);
832 /* restore codepage */
833 SetConsoleOutputCP(oldcp);
836 static void test_GetSetConsoleInputExeName(void)
838 BOOL ret;
839 DWORD error;
840 char buffer[MAX_PATH], module[MAX_PATH], *p;
841 static char input_exe[MAX_PATH] = "winetest.exe";
843 SetLastError(0xdeadbeef);
844 ret = pGetConsoleInputExeNameA(0, NULL);
845 error = GetLastError();
846 ok(ret, "GetConsoleInputExeNameA failed\n");
847 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
849 SetLastError(0xdeadbeef);
850 ret = pGetConsoleInputExeNameA(0, buffer);
851 error = GetLastError();
852 ok(ret, "GetConsoleInputExeNameA failed\n");
853 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
855 GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
856 p = strrchr(module, '\\') + 1;
858 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
859 ok(ret, "GetConsoleInputExeNameA failed\n");
860 todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
862 SetLastError(0xdeadbeef);
863 ret = pSetConsoleInputExeNameA(NULL);
864 error = GetLastError();
865 ok(!ret, "SetConsoleInputExeNameA failed\n");
866 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
868 SetLastError(0xdeadbeef);
869 ret = pSetConsoleInputExeNameA("");
870 error = GetLastError();
871 ok(!ret, "SetConsoleInputExeNameA failed\n");
872 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
874 ret = pSetConsoleInputExeNameA(input_exe);
875 ok(ret, "SetConsoleInputExeNameA failed\n");
877 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
878 ok(ret, "GetConsoleInputExeNameA failed\n");
879 ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
882 START_TEST(console)
884 HANDLE hConIn, hConOut;
885 BOOL ret;
886 CONSOLE_SCREEN_BUFFER_INFO sbi;
888 init_function_pointers();
890 /* be sure we have a clean console (and that's our own)
891 * FIXME: this will make the test fail (currently) if we don't run
892 * under X11
893 * Another solution would be to rerun the test under wineconsole with
894 * the curses backend
897 /* first, we detach and open a fresh console to play with */
898 FreeConsole();
899 ok(AllocConsole(), "Couldn't alloc console\n");
900 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
901 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
903 /* now verify everything's ok */
904 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
905 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
907 ok(ret = GetConsoleScreenBufferInfo(hConOut, &sbi), "Getting sb info\n");
908 if (!ret) return;
910 /* Non interactive tests */
911 testCursor(hConOut, sbi.dwSize);
912 /* test parameters (FIXME: test functionality) */
913 testCursorInfo(hConOut);
914 /* will test wrapped (on/off) & processed (on/off) strings output */
915 testWrite(hConOut, sbi.dwSize);
916 /* will test line scrolling at the bottom of the screen */
917 /* testBottomScroll(); */
918 /* will test all the scrolling operations */
919 testScroll(hConOut, sbi.dwSize);
920 /* will test sb creation / modification / codepage handling */
921 testScreenBuffer(hConOut);
922 testCtrlHandler();
923 /* still to be done: access rights & access on objects */
925 if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
927 skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
928 return;
930 else
931 test_GetSetConsoleInputExeName();