Merge trunk HEAD (r44067)
[reactos.git] / rostests / winetests / kernel32 / console.c
1 /*
2 * Unit tests for console API
3 *
4 * Copyright (c) 2003,2004 Eric Pouech
5 * Copyright (c) 2007 Kirill K. Smirnov
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "wine/test.h"
23 #include <windows.h>
24 #include <stdio.h>
25
26 static BOOL (WINAPI *pGetConsoleInputExeNameA)(DWORD, LPSTR);
27 static BOOL (WINAPI *pSetConsoleInputExeNameA)(LPCSTR);
28
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
32 */
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'
37 */
38 #define CONTENT(c) ('A' + (((c).Y * 17 + (c).X) % 23))
39
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)
47
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)
55
56 static void init_function_pointers(void)
57 {
58 HMODULE hKernel32;
59
60 #define KERNEL32_GET_PROC(func) \
61 p##func = (void *)GetProcAddress(hKernel32, #func); \
62 if(!p##func) trace("GetProcAddress(hKernel32, '%s') failed\n", #func);
63
64 hKernel32 = GetModuleHandleA("kernel32.dll");
65 KERNEL32_GET_PROC(GetConsoleInputExeNameA);
66 KERNEL32_GET_PROC(SetConsoleInputExeNameA);
67
68 #undef KERNEL32_GET_PROC
69 }
70
71 /* FIXME: this could be optimized on a speed point of view */
72 static void resetContent(HANDLE hCon, COORD sbSize, BOOL content)
73 {
74 COORD c;
75 WORD attr = DEFAULT_ATTRIB;
76 char ch;
77 DWORD len;
78
79 for (c.X = 0; c.X < sbSize.X; c.X++)
80 {
81 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
82 {
83 ch = (content) ? CONTENT(c) : ' ';
84 WriteConsoleOutputAttribute(hCon, &attr, 1, c, &len);
85 WriteConsoleOutputCharacterA(hCon, &ch, 1, c, &len);
86 }
87 }
88 }
89
90 static void testCursor(HANDLE hCon, COORD sbSize)
91 {
92 COORD c;
93
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());
98
99 c.X = c.Y = 0;
100 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
101 okCURSOR(hCon, c);
102
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);
107
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());
113
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());
119
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());
125
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());
131 }
132
133 static void testWriteSimple(HANDLE hCon, COORD sbSize)
134 {
135 COORD c;
136 DWORD len;
137 const char* mytest = "abcdefg";
138 const int mylen = strlen(mytest);
139
140 /* single line write */
141 c.X = c.Y = 0;
142 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left\n");
143
144 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
145 c.Y = 0;
146 for (c.X = 0; c.X < mylen; c.X++)
147 {
148 okCHAR(hCon, c, mytest[c.X], TEST_ATTRIB);
149 }
150
151 okCURSOR(hCon, c);
152 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
153 }
154
155 static void testWriteNotWrappedNotProcessed(HANDLE hCon, COORD sbSize)
156 {
157 COORD c;
158 DWORD len, mode;
159 const char* mytest = "123";
160 const int mylen = strlen(mytest);
161 int ret;
162 int p;
163
164 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode & ~(ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT)),
165 "clearing wrap at EOL & processed output\n");
166
167 /* write line, wrapping disabled, buffer exceeds sb width */
168 c.X = sbSize.X - 3; c.Y = 0;
169 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
170
171 ret = WriteConsole(hCon, mytest, mylen, &len, NULL);
172 ok(ret != 0 && len == mylen, "Couldn't write, ret = %d, len = %d\n", ret, len);
173 c.Y = 0;
174 for (p = mylen - 3; p < mylen; p++)
175 {
176 c.X = sbSize.X - 3 + p % 3;
177 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
178 }
179
180 c.X = 0; c.Y = 1;
181 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
182
183 p = sbSize.X - 3 + mylen % 3;
184 c.X = p; c.Y = 0;
185
186 /* write line, wrapping disabled, strings end on end of line */
187 c.X = sbSize.X - mylen; c.Y = 0;
188 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
189
190 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
191 }
192
193 static void testWriteNotWrappedProcessed(HANDLE hCon, COORD sbSize)
194 {
195 COORD c;
196 DWORD len, mode;
197 const char* mytest = "abcd\nf\tg";
198 const int mylen = strlen(mytest);
199 const int mylen2 = strchr(mytest, '\n') - mytest;
200 int p;
201
202 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, (mode | ENABLE_PROCESSED_OUTPUT) & ~ENABLE_WRAP_AT_EOL_OUTPUT),
203 "clearing wrap at EOL & setting processed output\n");
204
205 /* write line, wrapping disabled, buffer exceeds sb width */
206 c.X = sbSize.X - 5; c.Y = 0;
207 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-5\n");
208
209 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
210 c.Y = 0;
211 for (c.X = sbSize.X - 5; c.X < sbSize.X - 1; c.X++)
212 {
213 okCHAR(hCon, c, mytest[c.X - sbSize.X + 5], TEST_ATTRIB);
214 }
215 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
216
217 c.X = 0; c.Y++;
218 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
219 for (c.X = 1; c.X < 8; c.X++)
220 okCHAR(hCon, c, ' ', TEST_ATTRIB);
221 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
222 c.X++;
223 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
224
225 okCURSOR(hCon, c);
226
227 /* write line, wrapping disabled, strings end on end of line */
228 c.X = sbSize.X - 4; c.Y = 0;
229 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
230
231 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
232 c.Y = 0;
233 for (c.X = sbSize.X - 4; c.X < sbSize.X; c.X++)
234 {
235 okCHAR(hCon, c, mytest[c.X - sbSize.X + 4], TEST_ATTRIB);
236 }
237 c.X = 0; c.Y++;
238 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
239 for (c.X = 1; c.X < 8; c.X++)
240 okCHAR(hCon, c, ' ', TEST_ATTRIB);
241 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
242 c.X++;
243 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
244
245 okCURSOR(hCon, c);
246
247 /* write line, wrapping disabled, strings end after end of line */
248 c.X = sbSize.X - 3; c.Y = 0;
249 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-4\n");
250
251 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
252 c.Y = 0;
253 for (p = mylen2 - 3; p < mylen2; p++)
254 {
255 c.X = sbSize.X - 3 + p % 3;
256 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
257 }
258 c.X = 0; c.Y = 1;
259 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
260 for (c.X = 1; c.X < 8; c.X++)
261 okCHAR(hCon, c, ' ', TEST_ATTRIB);
262 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
263 c.X++;
264 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
265
266 okCURSOR(hCon, c);
267 }
268
269 static void testWriteWrappedNotProcessed(HANDLE hCon, COORD sbSize)
270 {
271 COORD c;
272 DWORD len, mode;
273 const char* mytest = "abcd\nf\tg";
274 const int mylen = strlen(mytest);
275 int p;
276
277 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon,(mode | ENABLE_WRAP_AT_EOL_OUTPUT) & ~(ENABLE_PROCESSED_OUTPUT)),
278 "setting wrap at EOL & clearing processed output\n");
279
280 /* write line, wrapping enabled, buffer doesn't exceed sb width */
281 c.X = sbSize.X - 9; c.Y = 0;
282 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
283
284 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
285 c.Y = 0;
286 for (p = 0; p < mylen; p++)
287 {
288 c.X = sbSize.X - 9 + p;
289 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
290 }
291 c.X = sbSize.X - 9 + mylen;
292 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
293 c.X = 0; c.Y = 1;
294 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
295
296 /* write line, wrapping enabled, buffer does exceed sb width */
297 c.X = sbSize.X - 3; c.Y = 0;
298 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
299
300 c.Y = 1;
301 c.X = mylen - 3;
302 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
303 }
304
305 static void testWriteWrappedProcessed(HANDLE hCon, COORD sbSize)
306 {
307 COORD c;
308 DWORD len, mode;
309 const char* mytest = "abcd\nf\tg";
310 const int mylen = strlen(mytest);
311 int p;
312
313 ok(GetConsoleMode(hCon, &mode) && SetConsoleMode(hCon, mode | (ENABLE_WRAP_AT_EOL_OUTPUT|ENABLE_PROCESSED_OUTPUT)),
314 "setting wrap at EOL & processed output\n");
315
316 /* write line, wrapping enabled, buffer doesn't exceed sb width */
317 c.X = sbSize.X - 9; c.Y = 0;
318 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-9\n");
319
320 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
321 for (p = 0; p < 4; p++)
322 {
323 c.X = sbSize.X - 9 + p;
324 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
325 }
326 c.X = sbSize.X - 9 + p;
327 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
328 c.X = 0; c.Y++;
329 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
330 for (c.X = 1; c.X < 8; c.X++)
331 okCHAR(hCon, c, ' ', TEST_ATTRIB);
332 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
333 c.X++;
334 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
335 okCURSOR(hCon, c);
336
337 /* write line, wrapping enabled, buffer does exceed sb width */
338 c.X = sbSize.X - 3; c.Y = 2;
339 ok(SetConsoleCursorPosition(hCon, c) != 0, "Cursor in upper-left-3\n");
340
341 ok(WriteConsole(hCon, mytest, mylen, &len, NULL) != 0 && len == mylen, "WriteConsole\n");
342 for (p = 0; p < 3; p++)
343 {
344 c.X = sbSize.X - 3 + p;
345 okCHAR(hCon, c, mytest[p], TEST_ATTRIB);
346 }
347 c.X = 0; c.Y++;
348 okCHAR(hCon, c, mytest[3], TEST_ATTRIB);
349 c.X++;
350 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
351
352 c.X = 0; c.Y++;
353 okCHAR(hCon, c, mytest[5], TEST_ATTRIB);
354 for (c.X = 1; c.X < 8; c.X++)
355 okCHAR(hCon, c, ' ', TEST_ATTRIB);
356 okCHAR(hCon, c, mytest[7], TEST_ATTRIB);
357 c.X++;
358 okCHAR(hCon, c, ' ', DEFAULT_ATTRIB);
359 okCURSOR(hCon, c);
360 }
361
362 static void testWrite(HANDLE hCon, COORD sbSize)
363 {
364 /* FIXME: should in fact insure that the sb is at least 10 character wide */
365 ok(SetConsoleTextAttribute(hCon, TEST_ATTRIB), "Setting default text color\n");
366 resetContent(hCon, sbSize, FALSE);
367 testWriteSimple(hCon, sbSize);
368 resetContent(hCon, sbSize, FALSE);
369 testWriteNotWrappedNotProcessed(hCon, sbSize);
370 resetContent(hCon, sbSize, FALSE);
371 testWriteNotWrappedProcessed(hCon, sbSize);
372 resetContent(hCon, sbSize, FALSE);
373 testWriteWrappedNotProcessed(hCon, sbSize);
374 resetContent(hCon, sbSize, FALSE);
375 testWriteWrappedProcessed(hCon, sbSize);
376 }
377
378 static void testScroll(HANDLE hCon, COORD sbSize)
379 {
380 SMALL_RECT scroll, clip;
381 COORD dst, c, tc;
382 CHAR_INFO ci;
383 BOOL ret;
384
385 #define W 11
386 #define H 7
387
388 #define IN_SRECT(r,c) ((r).Left <= (c).X && (c).X <= (r).Right && (r).Top <= (c).Y && (c).Y <= (r).Bottom)
389 #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)
390
391 /* no clipping, src & dst rect don't overlap */
392 resetContent(hCon, sbSize, TRUE);
393
394 scroll.Left = 0;
395 scroll.Right = W - 1;
396 scroll.Top = 0;
397 scroll.Bottom = H - 1;
398 dst.X = W + 3;
399 dst.Y = H + 3;
400 ci.Char.UnicodeChar = '#';
401 ci.Attributes = TEST_ATTRIB;
402
403 clip.Left = 0;
404 clip.Right = sbSize.X - 1;
405 clip.Top = 0;
406 clip.Bottom = sbSize.Y - 1;
407
408 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
409
410 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
411 {
412 for (c.X = 0; c.X < sbSize.X; c.X++)
413 {
414 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
415 {
416 tc.X = c.X - dst.X;
417 tc.Y = c.Y - dst.Y;
418 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
419 }
420 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
421 okCHAR(hCon, c, '#', TEST_ATTRIB);
422 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
423 }
424 }
425
426 /* no clipping, src & dst rect do overlap */
427 resetContent(hCon, sbSize, TRUE);
428
429 scroll.Left = 0;
430 scroll.Right = W - 1;
431 scroll.Top = 0;
432 scroll.Bottom = H - 1;
433 dst.X = W /2;
434 dst.Y = H / 2;
435 ci.Char.UnicodeChar = '#';
436 ci.Attributes = TEST_ATTRIB;
437
438 clip.Left = 0;
439 clip.Right = sbSize.X - 1;
440 clip.Top = 0;
441 clip.Bottom = sbSize.Y - 1;
442
443 ok(ScrollConsoleScreenBuffer(hCon, &scroll, NULL, dst, &ci), "Scrolling SB\n");
444
445 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
446 {
447 for (c.X = 0; c.X < sbSize.X; c.X++)
448 {
449 if (dst.X <= c.X && c.X < dst.X + W && dst.Y <= c.Y && c.Y < dst.Y + H)
450 {
451 tc.X = c.X - dst.X;
452 tc.Y = c.Y - dst.Y;
453 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
454 }
455 else if (c.X < W && c.Y < H) okCHAR(hCon, c, '#', TEST_ATTRIB);
456 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
457 }
458 }
459
460 /* clipping, src & dst rect don't overlap */
461 resetContent(hCon, sbSize, TRUE);
462
463 scroll.Left = 0;
464 scroll.Right = W - 1;
465 scroll.Top = 0;
466 scroll.Bottom = H - 1;
467 dst.X = W + 3;
468 dst.Y = H + 3;
469 ci.Char.UnicodeChar = '#';
470 ci.Attributes = TEST_ATTRIB;
471
472 clip.Left = W / 2;
473 clip.Right = min(W + W / 2, sbSize.X - 1);
474 clip.Top = H / 2;
475 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
476
477 SetLastError(0xdeadbeef);
478 ret = ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci);
479 if (ret)
480 {
481 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
482 {
483 for (c.X = 0; c.X < sbSize.X; c.X++)
484 {
485 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
486 {
487 tc.X = c.X - dst.X;
488 tc.Y = c.Y - dst.Y;
489 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
490 }
491 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
492 okCHAR(hCon, c, '#', TEST_ATTRIB);
493 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
494 }
495 }
496 }
497 else
498 {
499 /* Win9x will fail, Only accept ERROR_NOT_ENOUGH_MEMORY */
500 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY,
501 "Expected ERROR_NOT_ENOUGH_MEMORY, got %u\n", GetLastError());
502 }
503
504 /* clipping, src & dst rect do overlap */
505 resetContent(hCon, sbSize, TRUE);
506
507 scroll.Left = 0;
508 scroll.Right = W - 1;
509 scroll.Top = 0;
510 scroll.Bottom = H - 1;
511 dst.X = W / 2 - 3;
512 dst.Y = H / 2 - 3;
513 ci.Char.UnicodeChar = '#';
514 ci.Attributes = TEST_ATTRIB;
515
516 clip.Left = W / 2;
517 clip.Right = min(W + W / 2, sbSize.X - 1);
518 clip.Top = H / 2;
519 clip.Bottom = min(H + H / 2, sbSize.Y - 1);
520
521 ok(ScrollConsoleScreenBuffer(hCon, &scroll, &clip, dst, &ci), "Scrolling SB\n");
522
523 for (c.Y = 0; c.Y < sbSize.Y; c.Y++)
524 {
525 for (c.X = 0; c.X < sbSize.X; c.X++)
526 {
527 if (IN_SRECT2(scroll, dst, c) && IN_SRECT(clip, c))
528 {
529 tc.X = c.X - dst.X;
530 tc.Y = c.Y - dst.Y;
531 okCHAR(hCon, c, CONTENT(tc), DEFAULT_ATTRIB);
532 }
533 else if (IN_SRECT(scroll, c) && IN_SRECT(clip, c))
534 okCHAR(hCon, c, '#', TEST_ATTRIB);
535 else okCHAR(hCon, c, CONTENT(c), DEFAULT_ATTRIB);
536 }
537 }
538 }
539
540 static int mch_count;
541 /* we need the event as Wine console event generation isn't synchronous
542 * (ie GenerateConsoleCtrlEvent returns before all ctrl-handlers in all
543 * processes have been called).
544 */
545 static HANDLE mch_event;
546 static BOOL WINAPI mch(DWORD event)
547 {
548 mch_count++;
549 SetEvent(mch_event);
550 return TRUE;
551 }
552
553 static void testCtrlHandler(void)
554 {
555 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
556 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
557 ok(SetConsoleCtrlHandler(mch, TRUE), "Couldn't set handler\n");
558 /* wine requires the event for the test, as we cannot insure, so far, that event
559 * are processed synchronously in GenerateConsoleCtrlEvent()
560 */
561 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
562 mch_count = 0;
563 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
564 /* FIXME: it isn't synchronous on wine but it can still happen before we test */
565 if (0) ok(mch_count == 1, "Event isn't synchronous\n");
566 ok(WaitForSingleObject(mch_event, 3000) == WAIT_OBJECT_0, "event sending didn't work\n");
567 CloseHandle(mch_event);
568
569 /* Turning off ctrl-c handling doesn't work on win9x such way ... */
570 ok(SetConsoleCtrlHandler(NULL, TRUE), "Couldn't turn off ctrl-c handling\n");
571 mch_event = CreateEventA(NULL, TRUE, FALSE, NULL);
572 mch_count = 0;
573 if(!(GetVersion() & 0x80000000))
574 /* ... and next line leads to an unhandled exception on 9x. Avoid it on 9x. */
575 ok(GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0), "Couldn't send ctrl-c event\n");
576 ok(WaitForSingleObject(mch_event, 3000) == WAIT_TIMEOUT && mch_count == 0, "Event shouldn't have been sent\n");
577 CloseHandle(mch_event);
578 ok(SetConsoleCtrlHandler(mch, FALSE), "Couldn't remove handler\n");
579 ok(!SetConsoleCtrlHandler(mch, FALSE), "Shouldn't succeed\n");
580 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
581 }
582
583 /*
584 * Test console screen buffer:
585 * 1) Try to set invalid handle.
586 * 2) Try to set non-console handles.
587 * 3) Use CONOUT$ file as active SB.
588 * 4) Test cursor.
589 * 5) Test output codepage to show it is not a property of SB.
590 * 6) Test switching to old SB if we close all handles to current SB - works
591 * in Windows, TODO in wine.
592 *
593 * What is not tested but should be:
594 * 1) ScreenBufferInfo
595 */
596 static void testScreenBuffer(HANDLE hConOut)
597 {
598 HANDLE hConOutRW, hConOutRO, hConOutWT;
599 HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
600 HANDLE hConOutNew;
601 char test_str1[] = "Test for SB1";
602 char test_str2[] = "Test for SB2";
603 char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
604 char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
605 WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
606 WCHAR str_wbuf[20];
607 char str_buf[20];
608 DWORD len;
609 COORD c;
610 BOOL ret;
611 DWORD oldcp;
612
613 if (!IsValidCodePage(866))
614 {
615 skip("Codepage 866 not available\n");
616 return;
617 }
618
619 /* In the beginning set output codepage to 866 */
620 oldcp = GetConsoleOutputCP();
621 SetLastError(0xdeadbeef);
622 ret = SetConsoleOutputCP(866);
623 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
624 {
625 skip("SetConsoleOutputCP is not implemented\n");
626 return;
627 }
628 ok(ret, "Cannot set output codepage to 866\n");
629
630 hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
631 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
632 CONSOLE_TEXTMODE_BUFFER, NULL);
633 ok(hConOutRW != INVALID_HANDLE_VALUE,
634 "Cannot create a new screen buffer for ReadWrite\n");
635 hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
636 FILE_SHARE_READ, NULL,
637 CONSOLE_TEXTMODE_BUFFER, NULL);
638 ok(hConOutRO != INVALID_HANDLE_VALUE,
639 "Cannot create a new screen buffer for ReadOnly\n");
640 hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
641 FILE_SHARE_WRITE, NULL,
642 CONSOLE_TEXTMODE_BUFFER, NULL);
643 ok(hConOutWT != INVALID_HANDLE_VALUE,
644 "Cannot create a new screen buffer for WriteOnly\n");
645
646 hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
647 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
648 OPEN_EXISTING, 0, NULL);
649 ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
650 hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
651 NULL, OPEN_EXISTING, 0, NULL);
652 ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
653 hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
654 NULL, OPEN_EXISTING, 0, NULL);
655 ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
656
657 /* Trying to set invalid handle */
658 SetLastError(0);
659 ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
660 "Shouldn't succeed\n");
661 ok(GetLastError() == ERROR_INVALID_HANDLE,
662 "GetLastError: expecting %u got %u\n",
663 ERROR_INVALID_HANDLE, GetLastError());
664
665 /* Trying to set non-console handles */
666 SetLastError(0);
667 ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
668 ok(GetLastError() == ERROR_INVALID_HANDLE,
669 "GetLastError: expecting %u got %u\n",
670 ERROR_INVALID_HANDLE, GetLastError());
671
672 SetLastError(0);
673 ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
674 ok(GetLastError() == ERROR_INVALID_HANDLE,
675 "GetLastError: expecting %u got %u\n",
676 ERROR_INVALID_HANDLE, GetLastError());
677
678 SetLastError(0);
679 ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
680 ok(GetLastError() == ERROR_INVALID_HANDLE,
681 "GetLastError: expecting %u got %u\n",
682 ERROR_INVALID_HANDLE, GetLastError());
683
684 CloseHandle(hFileOutRW);
685 CloseHandle(hFileOutRO);
686 CloseHandle(hFileOutWT);
687
688 /* Trying to set SB handles with various access modes */
689 SetLastError(0);
690 ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
691 ok(GetLastError() == ERROR_INVALID_HANDLE,
692 "GetLastError: expecting %u got %u\n",
693 ERROR_INVALID_HANDLE, GetLastError());
694
695 ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
696
697 ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
698
699 CloseHandle(hConOutWT);
700 CloseHandle(hConOutRO);
701
702 /* Now we have two ReadWrite SB, active must be hConOutRW */
703 /* Open current SB via CONOUT$ */
704 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
705 NULL, OPEN_EXISTING, 0, 0);
706 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
707
708
709 /* test cursor */
710 c.X = c.Y = 10;
711 SetConsoleCursorPosition(hConOut, c);
712 c.X = c.Y = 5;
713 SetConsoleCursorPosition(hConOutRW, c);
714 okCURSOR(hConOutNew, c);
715 c.X = c.Y = 10;
716 okCURSOR(hConOut, c);
717
718
719 c.X = c.Y = 0;
720
721 /* Write using hConOutNew... */
722 SetConsoleCursorPosition(hConOutNew, c);
723 ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
724 ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
725 /* ... and read it back via hConOutRW */
726 ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
727 ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
728 str_buf[lstrlenA(test_str2)] = 0;
729 ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
730
731
732 /* Now test output codepage handling. Current is 866 as we set earlier. */
733 SetConsoleCursorPosition(hConOutRW, c);
734 ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
735 ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
736 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
737 ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
738 str_wbuf[lstrlenA(test_cp866)] = 0;
739 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
740
741 /*
742 * cp866 is OK, let's switch to cp1251.
743 * We expect that this codepage will be used in every SB - active and not.
744 */
745 ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
746 SetConsoleCursorPosition(hConOutRW, c);
747 ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
748 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
749 ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
750 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
751 str_wbuf[lstrlenA(test_cp1251)] = 0;
752 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
753
754 /* Check what has happened to hConOut. */
755 SetConsoleCursorPosition(hConOut, c);
756 ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
757 ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
758 ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
759 ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
760 str_wbuf[lstrlenA(test_cp1251)] = 0;
761 ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
762
763 /* Close all handles of current console SB */
764 CloseHandle(hConOutNew);
765 CloseHandle(hConOutRW);
766
767 /* Now active SB should be hConOut */
768 hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
769 NULL, OPEN_EXISTING, 0, 0);
770 ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
771
772 /* Write using hConOutNew... */
773 SetConsoleCursorPosition(hConOutNew, c);
774 ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
775 ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
776 /* ... and read it back via hConOut */
777 ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
778 ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
779 str_buf[lstrlenA(test_str1)] = 0;
780 todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
781 CloseHandle(hConOutNew);
782
783 /* This is not really needed under Windows */
784 SetConsoleActiveScreenBuffer(hConOut);
785
786 /* restore codepage */
787 SetConsoleOutputCP(oldcp);
788 }
789
790 static void test_GetSetConsoleInputExeName(void)
791 {
792 BOOL ret;
793 DWORD error;
794 char buffer[MAX_PATH], module[MAX_PATH], *p;
795 static char input_exe[MAX_PATH] = "winetest.exe";
796
797 SetLastError(0xdeadbeef);
798 ret = pGetConsoleInputExeNameA(0, NULL);
799 error = GetLastError();
800 ok(ret, "GetConsoleInputExeNameA failed\n");
801 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
802
803 SetLastError(0xdeadbeef);
804 ret = pGetConsoleInputExeNameA(0, buffer);
805 error = GetLastError();
806 ok(ret, "GetConsoleInputExeNameA failed\n");
807 ok(error == ERROR_BUFFER_OVERFLOW, "got %u expected ERROR_BUFFER_OVERFLOW\n", error);
808
809 GetModuleFileNameA(GetModuleHandle(NULL), module, sizeof(module));
810 p = strrchr(module, '\\') + 1;
811
812 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
813 ok(ret, "GetConsoleInputExeNameA failed\n");
814 todo_wine ok(!lstrcmpA(buffer, p), "got %s expected %s\n", buffer, p);
815
816 SetLastError(0xdeadbeef);
817 ret = pSetConsoleInputExeNameA(NULL);
818 error = GetLastError();
819 ok(!ret, "SetConsoleInputExeNameA failed\n");
820 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
821
822 SetLastError(0xdeadbeef);
823 ret = pSetConsoleInputExeNameA("");
824 error = GetLastError();
825 ok(!ret, "SetConsoleInputExeNameA failed\n");
826 ok(error == ERROR_INVALID_PARAMETER, "got %u expected ERROR_INVALID_PARAMETER\n", error);
827
828 ret = pSetConsoleInputExeNameA(input_exe);
829 ok(ret, "SetConsoleInputExeNameA failed\n");
830
831 ret = pGetConsoleInputExeNameA(sizeof(buffer)/sizeof(buffer[0]), buffer);
832 ok(ret, "GetConsoleInputExeNameA failed\n");
833 ok(!lstrcmpA(buffer, input_exe), "got %s expected %s\n", buffer, input_exe);
834 }
835
836 START_TEST(console)
837 {
838 HANDLE hConIn, hConOut;
839 BOOL ret;
840 CONSOLE_SCREEN_BUFFER_INFO sbi;
841
842 init_function_pointers();
843
844 /* be sure we have a clean console (and that's our own)
845 * FIXME: this will make the test fail (currently) if we don't run
846 * under X11
847 * Another solution would be to rerun the test under wineconsole with
848 * the curses backend
849 */
850
851 /* first, we detach and open a fresh console to play with */
852 FreeConsole();
853 ok(AllocConsole(), "Couldn't alloc console\n");
854 hConIn = CreateFileA("CONIN$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
855 hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
856
857 /* now verify everything's ok */
858 ok(hConIn != INVALID_HANDLE_VALUE, "Opening ConIn\n");
859 ok(hConOut != INVALID_HANDLE_VALUE, "Opening ConOut\n");
860
861 ok(ret = GetConsoleScreenBufferInfo(hConOut, &sbi), "Getting sb info\n");
862 if (!ret) return;
863
864 /* Non interactive tests */
865 testCursor(hConOut, sbi.dwSize);
866 /* will test wrapped (on/off) & processed (on/off) strings output */
867 testWrite(hConOut, sbi.dwSize);
868 /* will test line scrolling at the bottom of the screen */
869 /* testBottomScroll(); */
870 /* will test all the scrolling operations */
871 testScroll(hConOut, sbi.dwSize);
872 /* will test sb creation / modification / codepage handling */
873 testScreenBuffer(hConOut);
874 testCtrlHandler();
875 /* still to be done: access rights & access on objects */
876
877 if (!pGetConsoleInputExeNameA || !pSetConsoleInputExeNameA)
878 {
879 skip("GetConsoleInputExeNameA and/or SetConsoleInputExeNameA is not available\n");
880 return;
881 }
882 else
883 test_GetSetConsoleInputExeName();
884 }