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