[1/4] kernel32/tests: Add tests for console screen buffer [resend]

Kirill K. Smirnov lich at math.spbu.ru
Wed May 9 11:55:08 CDT 2007


-------------- next part --------------
diff --git a/dlls/kernel32/tests/console.c b/dlls/kernel32/tests/console.c
index abd42bd..c398145 100644
--- a/dlls/kernel32/tests/console.c
+++ b/dlls/kernel32/tests/console.c
@@ -2,6 +2,7 @@
  * Unit tests for console API
  *
  * Copyright (c) 2003,2004 Eric Pouech
+ * Copyright (c) 2007 Kirill K. Smirnov
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -551,6 +552,200 @@ static void testCtrlHandler(void)
     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Bad error %u\n", GetLastError());
 }
 
+/*
+ * Test console screen buffer:
+ * 1) Try to set invalid handle.
+ * 2) Try to set non-console handles.
+ * 3) Use CONOUT$ file as active SB.
+ * 4) Test cursor.
+ * 5) Test output codepage to show it is not a property of SB.
+ * 6) Test switching to old SB if we close all handles to current SB - works
+ * in Windows, TODO in wine.
+ *
+ * What is not tested but should be:
+ * 1) ScreenBufferInfo
+ */
+void testScreenBuffer(HANDLE hConOut)
+{
+    HANDLE hConOutRW, hConOutRO, hConOutWT;
+    HANDLE hFileOutRW, hFileOutRO, hFileOutWT;
+    HANDLE hConOutNew;
+    char test_str1[] = "Test for SB1";
+    char test_str2[] = "Test for SB2";
+    char test_cp866[] = {0xe2, 0xa5, 0xe1, 0xe2, 0};
+    char test_cp1251[] = {0xf2, 0xe5, 0xf1, 0xf2, 0};
+    WCHAR test_unicode[] = {0x0442, 0x0435, 0x0441, 0x0442, 0};
+    WCHAR str_wbuf[20];
+    char str_buf[20];
+    DWORD len;
+    COORD c;
+    BOOL ret;
+    DWORD oldcp;
+
+    /* In the beginning set output codepage to 866 */
+    oldcp = GetConsoleOutputCP();
+    ok(SetConsoleOutputCP(866), "Cannot set output codepage to 866\n");
+
+    hConOutRW = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
+                         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+                         CONSOLE_TEXTMODE_BUFFER, NULL);
+    ok(hConOutRW != INVALID_HANDLE_VALUE,
+       "Cannot create a new screen buffer for ReadWrite\n");
+    hConOutRO = CreateConsoleScreenBuffer(GENERIC_READ,
+                         FILE_SHARE_READ, NULL,
+                         CONSOLE_TEXTMODE_BUFFER, NULL);
+    ok(hConOutRO != INVALID_HANDLE_VALUE,
+       "Cannot create a new screen buffer for ReadOnly\n");
+    hConOutWT = CreateConsoleScreenBuffer(GENERIC_WRITE,
+                         FILE_SHARE_WRITE, NULL,
+                         CONSOLE_TEXTMODE_BUFFER, NULL);
+    ok(hConOutWT != INVALID_HANDLE_VALUE,
+       "Cannot create a new screen buffer for WriteOnly\n");
+
+    hFileOutRW = CreateFileA("NUL", GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+                             OPEN_EXISTING, 0, NULL);
+    ok(hFileOutRW != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadWrite\n");
+    hFileOutRO = CreateFileA("NUL", GENERIC_READ, FILE_SHARE_READ,
+                             NULL, OPEN_EXISTING, 0, NULL);
+    ok(hFileOutRO != INVALID_HANDLE_VALUE, "Cannot open NUL for ReadOnly\n");
+    hFileOutWT = CreateFileA("NUL", GENERIC_WRITE, FILE_SHARE_WRITE,
+                             NULL, OPEN_EXISTING, 0, NULL);
+    ok(hFileOutWT != INVALID_HANDLE_VALUE, "Cannot open NUL for WriteOnly\n");
+
+    /* Trying to set invalid handle */
+    SetLastError(0);
+    ok(!SetConsoleActiveScreenBuffer(INVALID_HANDLE_VALUE),
+       "Shouldn't succeed\n");
+    ok(GetLastError() == ERROR_INVALID_HANDLE,
+       "GetLastError: expecting %u got %u\n",
+       ERROR_INVALID_HANDLE, GetLastError());
+
+    /* Trying to set non-console handles */
+    SetLastError(0);
+    ok(!SetConsoleActiveScreenBuffer(hFileOutRW), "Shouldn't succeed\n");
+    ok(GetLastError() == ERROR_INVALID_HANDLE,
+       "GetLastError: expecting %u got %u\n",
+       ERROR_INVALID_HANDLE, GetLastError());
+
+    SetLastError(0);
+    ok(!SetConsoleActiveScreenBuffer(hFileOutRO), "Shouldn't succeed\n");
+    ok(GetLastError() == ERROR_INVALID_HANDLE,
+       "GetLastError: expecting %u got %u\n",
+       ERROR_INVALID_HANDLE, GetLastError());
+
+    SetLastError(0);
+    ok(!SetConsoleActiveScreenBuffer(hFileOutWT), "Shouldn't succeed\n");
+    ok(GetLastError() == ERROR_INVALID_HANDLE,
+       "GetLastError: expecting %u got %u\n",
+       ERROR_INVALID_HANDLE, GetLastError());
+
+    CloseHandle(hFileOutRW);
+    CloseHandle(hFileOutRO);
+    CloseHandle(hFileOutWT);
+
+    /* Trying to set SB handles with various access modes */
+    SetLastError(0);
+    ok(!SetConsoleActiveScreenBuffer(hConOutRO), "Shouldn't succeed\n");
+    ok(GetLastError() == ERROR_INVALID_HANDLE,
+       "GetLastError: expecting %u got %u\n",
+       ERROR_INVALID_HANDLE, GetLastError());
+
+    ok(SetConsoleActiveScreenBuffer(hConOutWT), "Couldn't set new WriteOnly SB\n");
+
+    ok(SetConsoleActiveScreenBuffer(hConOutRW), "Couldn't set new ReadWrite SB\n");
+
+    CloseHandle(hConOutWT);
+    CloseHandle(hConOutRO);
+
+    /* Now we have two ReadWrite SB, active must be hConOutRW */
+    /* Open current SB via CONOUT$ */
+    hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
+                             NULL, OPEN_EXISTING, 0, 0);
+    ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
+
+
+    /* test cursor */
+    c.X = c.Y = 10;
+    SetConsoleCursorPosition(hConOut, c);
+    c.X = c.Y = 5;
+    SetConsoleCursorPosition(hConOutRW, c);
+    okCURSOR(hConOutNew, c);
+    c.X = c.Y = 10;
+    okCURSOR(hConOut, c);
+
+
+    c.X = c.Y = 0;
+
+    /* Write using hConOutNew... */
+    SetConsoleCursorPosition(hConOutNew, c);
+    ret = WriteConsoleA(hConOutNew, test_str2, lstrlenA(test_str2), &len, NULL);
+    ok (ret && len == lstrlenA(test_str2), "WriteConsoleA failed\n");
+    /* ... and read it back via hConOutRW */
+    ret = ReadConsoleOutputCharacterA(hConOutRW, str_buf, lstrlenA(test_str2), c, &len);
+    ok(ret && len == lstrlenA(test_str2), "ReadConsoleOutputCharacterA failed\n");
+    str_buf[lstrlenA(test_str2)] = 0;
+    ok(!lstrcmpA(str_buf, test_str2), "got '%s' expected '%s'\n", str_buf, test_str2);
+
+
+    /* Now test output codepage handling. Current is 866 as we set earlier. */
+    SetConsoleCursorPosition(hConOutRW, c);
+    ret = WriteConsoleA(hConOutRW, test_cp866, lstrlenA(test_cp866), &len, NULL);
+    ok(ret && len == lstrlenA(test_cp866), "WriteConsoleA failed\n");
+    ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp866), c, &len);
+    ok(ret && len == lstrlenA(test_cp866), "ReadConsoleOutputCharacterW failed\n");
+    str_wbuf[lstrlenA(test_cp866)] = 0;
+    ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
+
+    /*
+     * cp866 is OK, let's switch to cp1251.
+     * We expect that this codepage will be used in every SB - active and not.
+     */
+    ok(SetConsoleOutputCP(1251), "Cannot set output cp to 1251\n");
+    SetConsoleCursorPosition(hConOutRW, c);
+    ret = WriteConsoleA(hConOutRW, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
+    ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
+    ret = ReadConsoleOutputCharacterW(hConOutRW, str_wbuf, lstrlenA(test_cp1251), c, &len);
+    ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
+    str_wbuf[lstrlenA(test_cp1251)] = 0;
+    ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
+
+    /* Check what has happened to hConOut. */
+    SetConsoleCursorPosition(hConOut, c);
+    ret = WriteConsoleA(hConOut, test_cp1251, lstrlenA(test_cp1251), &len, NULL);
+    ok(ret && len == lstrlenA(test_cp1251), "WriteConsoleA failed\n");
+    ret = ReadConsoleOutputCharacterW(hConOut, str_wbuf, lstrlenA(test_cp1251), c, &len);
+    ok(ret && len == lstrlenA(test_cp1251), "ReadConsoleOutputCharacterW failed\n");
+    str_wbuf[lstrlenA(test_cp1251)] = 0;
+    ok(!lstrcmpW(str_wbuf, test_unicode), "string does not match the pattern\n");
+
+    /* Close all handles of current console SB */
+    CloseHandle(hConOutNew);
+    CloseHandle(hConOutRW);
+
+    /* Now active SB should be hConOut */
+    hConOutNew = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0,
+                             NULL, OPEN_EXISTING, 0, 0);
+    ok(hConOutNew != INVALID_HANDLE_VALUE, "CONOUT$ is not opened\n");
+
+    /* Write using hConOutNew... */
+    SetConsoleCursorPosition(hConOutNew, c);
+    ret = WriteConsoleA(hConOutNew, test_str1, lstrlenA(test_str1), &len, NULL);
+    ok (ret && len == lstrlenA(test_str1), "WriteConsoleA failed\n");
+    /* ... and read it back via hConOut */
+    ret = ReadConsoleOutputCharacterA(hConOut, str_buf, lstrlenA(test_str1), c, &len);
+    ok(ret && len == lstrlenA(test_str1), "ReadConsoleOutputCharacterA failed\n");
+    str_buf[lstrlenA(test_str1)] = 0;
+    todo_wine ok(!lstrcmpA(str_buf, test_str1), "got '%s' expected '%s'\n", str_buf, test_str1);
+    CloseHandle(hConOutNew);
+
+    /* This is not really needed under Windows */
+    SetConsoleActiveScreenBuffer(hConOut);
+
+    /* restore codepage */
+    SetConsoleOutputCP(oldcp);
+}
+
 START_TEST(console)
 {
     HANDLE hConIn, hConOut;
@@ -585,8 +780,8 @@ START_TEST(console)
     /* testBottomScroll(); */
     /* will test all the scrolling operations */
     testScroll(hConOut, sbi.dwSize);
-    /* will test sb creation / modification... */
-    /* testScreenBuffer() */
+    /* will test sb creation / modification / codepage handling */
+    testScreenBuffer(hConOut);
     testCtrlHandler();
     /* still to be done: access rights & access on objects */
 }


More information about the wine-patches mailing list