Signed-off-by: Micah N Gorrell mgorrell@codeweavers.com --- dlls/user32/tests/Makefile.in | 1 + dlls/user32/tests/misc.c | 163 ++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 dlls/user32/tests/misc.c
diff --git a/dlls/user32/tests/Makefile.in b/dlls/user32/tests/Makefile.in index 7149dc824e..da55fa58e5 100644 --- a/dlls/user32/tests/Makefile.in +++ b/dlls/user32/tests/Makefile.in @@ -15,6 +15,7 @@ C_SRCS = \ input.c \ listbox.c \ menu.c \ + misc.c \ monitor.c \ msg.c \ resource.c \ diff --git a/dlls/user32/tests/misc.c b/dlls/user32/tests/misc.c new file mode 100644 index 0000000000..16fed49f8d --- /dev/null +++ b/dlls/user32/tests/misc.c @@ -0,0 +1,163 @@ +/* + * Unit tests for misc functions + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include <assert.h> +#include <stdlib.h> +#include <stdarg.h> +#include <stdio.h> + +#include "wine/test.h" +#include "windef.h" +#include "winbase.h" +#include "winuser.h" +#include "dbt.h" +#include "initguid.h" +#include "ddk/hidclass.h" + +static const WCHAR mainwindowclassW[] = {'M','a','i','n','W','i','n','d','o','w','C','l','a','s','s',0}; + +static HWND create_message_window(void) +{ + static const WCHAR message_windowW[] = {'m','e','s','s','a','g','e',' ','w','i','n','d','o','w',0}; + HWND hwnd; + + hwnd = CreateWindowExW(0, mainwindowclassW, message_windowW, 0, + 0, 0, 0, 0, HWND_MESSAGE, 0, 0, NULL); + ok( hwnd != 0, "CreateWindowExW with parent HWND_MESSAGE failed\n" ); + return hwnd; +} + +static void prepare_dbh(DEV_BROADCAST_DEVICEINTERFACE_W *dbh) +{ + if (!dbh) return; + + memset(dbh, 0, sizeof(*dbh)); + + dbh->dbcc_size = sizeof(*dbh); + dbh->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; + dbh->dbcc_classguid = GUID_DEVINTERFACE_HID; +} + +static void test_register_device_notification(void) +{ + HDEVNOTIFY hnotify1, hnotify2; + DEV_BROADCAST_DEVICEINTERFACE_W dbh; + HWND hwnd = create_message_window(); + + prepare_dbh(&dbh); + + /* Test RegisterDeviceNotification behavior */ + + /* Prior to Windows 8 a NULL recipient handle caused a failure, but more + * recent versions of windows allow it. + */ + hnotify1 = RegisterDeviceNotificationW( NULL, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ); + /* ok( hnotify1 != 0, "RegisterDeviceNotificationW failed when called with a NULL recipient window handle\n" ); */ + if ( hnotify1 != 0 ) + ok( UnregisterDeviceNotification( hnotify1 ), "UnregisterDeviceNotification failed with a valid handle\n" ); + + hnotify1 = RegisterDeviceNotificationW( hwnd, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ); + ok( hnotify1 != 0, "RegisterDeviceNotificationW failed when called with a message only window as recipient\n" ); + + hnotify2 = RegisterDeviceNotificationW( hwnd, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ); + ok( hnotify2 != 0, "RegisterDeviceNotificationW failed when called with a window that has already been registered as a recipient\n" ); + + ok( UnregisterDeviceNotification( hnotify1 ), "UnregisterDeviceNotification failed with a valid handle\n" ); + ok( UnregisterDeviceNotification( hnotify2 ), "UnregisterDeviceNotification failed with a valid handle\n" ); + + hnotify1 = RegisterDeviceNotificationW( hwnd, &dbh, 0xffff ); + ok( hnotify1 == 0, "RegisterDeviceNotificationW accepted invalid flags\n" ); + + DestroyWindow(hwnd); +} + +static void test_unregister_device_notification(void) +{ + HDEVNOTIFY hnotify; + DEV_BROADCAST_DEVICEINTERFACE_W dbh; + HWND hwnd = create_message_window(); + + prepare_dbh(&dbh); + + hnotify = RegisterDeviceNotificationW( hwnd, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ); + ok( hnotify != 0, "RegisterDeviceNotificationW failed when called with a message only window as recipient\n" ); + + /* Destroy the window before calling UnregisterDeviceNotification */ + DestroyWindow(hwnd); + + ok( UnregisterDeviceNotification( hnotify ), "UnregisterDeviceNotification failed with a valid handle\n" ); + ok( !UnregisterDeviceNotification( hnotify ), "UnregisterDeviceNotification succeeded with an already released handle\n" ); + ok( !UnregisterDeviceNotification( NULL ), "UnregisterDeviceNotification succeeded with NULL handle\n" ); +} + +static void test_device_notification_handles( char *argv0 ) +{ + HDEVNOTIFY hnotify; + DEV_BROADCAST_DEVICEINTERFACE_W dbh; + HWND hwnd = create_message_window(); + PROCESS_INFORMATION info; + STARTUPINFOA startup; + char cmd[MAX_PATH]; + + prepare_dbh(&dbh); + + hnotify = RegisterDeviceNotificationW( hwnd, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ); + ok( hnotify != 0, "RegisterDeviceNotificationW failed when called with a message only window as recipient\n" ); + + /* Ensure that another process can't mess with our handle */ + sprintf(cmd, "%s misc unregister_device_notification %p", argv0, hnotify); + memset(&startup, 0, sizeof(startup)); + startup.cb = sizeof(startup); + ok(CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, + &startup, &info), "CreateProcess failed.\n"); + winetest_wait_child_process(info.hProcess); + CloseHandle(info.hProcess); + CloseHandle(info.hThread); + + ok( UnregisterDeviceNotification( hnotify ), "UnregisterDeviceNotification failed with a valid handle\n" ); +} + +START_TEST(misc) +{ + char **argv; + int argc = winetest_get_mainargs( &argv ); + WNDCLASSEXW cls; + + if (argc==4 && !strcmp(argv[2], "unregister_device_notification")) + { + HDEVNOTIFY hnotify; + + sscanf(argv[3], "%p", &hnotify); + ok( !UnregisterDeviceNotification( hnotify ), "UnregisterDeviceNotification succeeded on a handle owned by another process\n" ); + return; + } + + memset(&cls, 0, sizeof(cls)); + cls.cbSize = sizeof(cls); + cls.hInstance = 0; + cls.lpszClassName = mainwindowclassW; + cls.lpfnWndProc = DefWindowProcW; + + if (!RegisterClassExW(&cls)) assert(0); + + test_register_device_notification(); + test_unregister_device_notification(); + test_device_notification_handles(argv[0]); + + /* FIXME: Find a way to trigger a device notification for testing */ +}