diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn index 3773fac4b6..70344e5ba8 100644 --- a/modules/desktop_capture/BUILD.gn +++ b/modules/desktop_capture/BUILD.gn @@ -115,6 +115,8 @@ if (rtc_include_tests) { "win/cursor_unittest_resources.rc", "win/screen_capture_utils_unittest.cc", "win/screen_capturer_win_directx_unittest.cc", + "win/test_support/test_window.cc", + "win/test_support/test_window.h", "win/window_capture_utils_unittest.cc", ] } diff --git a/modules/desktop_capture/win/test_support/test_window.cc b/modules/desktop_capture/win/test_support/test_window.cc new file mode 100644 index 0000000000..dc94ee0d6e --- /dev/null +++ b/modules/desktop_capture/win/test_support/test_window.cc @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "modules/desktop_capture/win/test_support/test_window.h" + +namespace webrtc { +namespace { + +const WCHAR kWindowClass[] = L"DesktopCaptureTestWindowClass"; +const int kWindowHeight = 200; +const int kWindowWidth = 300; + +} // namespace + +WindowInfo CreateTestWindow(const WCHAR* window_title, + const int height, + const int width) { + WindowInfo info; + ::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(&::DefWindowProc), + &info.window_instance); + + WNDCLASSEXW wcex; + memset(&wcex, 0, sizeof(wcex)); + wcex.cbSize = sizeof(wcex); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.hInstance = info.window_instance; + wcex.lpfnWndProc = &::DefWindowProc; + wcex.lpszClassName = kWindowClass; + info.window_class = ::RegisterClassExW(&wcex); + + // Use the default height and width if the caller did not supply the optional + // height and width parameters, or if they supplied invalid values. + int window_height = height <= 0 ? kWindowHeight : height; + int window_width = width <= 0 ? kWindowWidth : width; + info.hwnd = ::CreateWindowW(kWindowClass, window_title, WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, window_width, + window_height, /*parent_window=*/nullptr, + /*menu_bar=*/nullptr, info.window_instance, + /*additional_params=*/nullptr); + + ::ShowWindow(info.hwnd, SW_SHOWNORMAL); + ::UpdateWindow(info.hwnd); + return info; +} + +void ResizeTestWindow(const HWND hwnd, const int width, const int height) { + ::SetWindowPos(hwnd, HWND_TOP, /*x-coord=*/0, /*y-coord=*/0, width, height, + SWP_SHOWWINDOW); + ::UpdateWindow(hwnd); +} + +void MinimizeTestWindow(const HWND hwnd) { + ::ShowWindow(hwnd, SW_MINIMIZE); +} + +void UnminimizeTestWindow(const HWND hwnd) { + ::OpenIcon(hwnd); +} + +void DestroyTestWindow(WindowInfo info) { + ::DestroyWindow(info.hwnd); + ::UnregisterClass(MAKEINTATOM(info.window_class), info.window_instance); +} + +} // namespace webrtc diff --git a/modules/desktop_capture/win/test_support/test_window.h b/modules/desktop_capture/win/test_support/test_window.h new file mode 100644 index 0000000000..a5962b5819 --- /dev/null +++ b/modules/desktop_capture/win/test_support/test_window.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef MODULES_DESKTOP_CAPTURE_WIN_TEST_SUPPORT_TEST_WINDOW_H_ +#define MODULES_DESKTOP_CAPTURE_WIN_TEST_SUPPORT_TEST_WINDOW_H_ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include + +namespace webrtc { + +struct WindowInfo { + HWND hwnd; + HINSTANCE window_instance; + ATOM window_class; +}; + +WindowInfo CreateTestWindow(const WCHAR* window_title, + const int height = 0, + const int width = 0); + +void ResizeTestWindow(const HWND hwnd, const int width, const int height); + +void MinimizeTestWindow(const HWND hwnd); + +void UnminimizeTestWindow(const HWND hwnd); + +void DestroyTestWindow(WindowInfo info); + +} // namespace webrtc + +#endif // MODULES_DESKTOP_CAPTURE_WIN_TEST_SUPPORT_TEST_WINDOW_H_ diff --git a/modules/desktop_capture/win/window_capture_utils_unittest.cc b/modules/desktop_capture/win/window_capture_utils_unittest.cc index 804cee9656..52f6714383 100644 --- a/modules/desktop_capture/win/window_capture_utils_unittest.cc +++ b/modules/desktop_capture/win/window_capture_utils_unittest.cc @@ -16,6 +16,7 @@ #include #include "modules/desktop_capture/desktop_capturer.h" +#include "modules/desktop_capture/win/test_support/test_window.h" #include "rtc_base/thread.h" #include "test/gtest.h" @@ -23,48 +24,7 @@ namespace webrtc { namespace { const char kWindowThreadName[] = "window_capture_utils_test_thread"; -const WCHAR kWindowClass[] = L"WindowCaptureUtilsTestClass"; const WCHAR kWindowTitle[] = L"Window Capture Utils Test"; -const int kWindowWidth = 300; -const int kWindowHeight = 200; - -struct WindowInfo { - HWND hwnd; - HINSTANCE window_instance; - ATOM window_class; -}; - -WindowInfo CreateTestWindow(const WCHAR* window_title) { - WindowInfo info; - ::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - reinterpret_cast(&::DefWindowProc), - &info.window_instance); - - WNDCLASSEXW wcex; - memset(&wcex, 0, sizeof(wcex)); - wcex.cbSize = sizeof(wcex); - wcex.style = CS_HREDRAW | CS_VREDRAW; - wcex.hInstance = info.window_instance; - wcex.lpfnWndProc = &::DefWindowProc; - wcex.lpszClassName = kWindowClass; - info.window_class = ::RegisterClassExW(&wcex); - - info.hwnd = ::CreateWindowW(kWindowClass, window_title, WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, CW_USEDEFAULT, kWindowWidth, - kWindowHeight, /*parent_window=*/nullptr, - /*menu_bar=*/nullptr, info.window_instance, - /*additional_params=*/nullptr); - - ::ShowWindow(info.hwnd, SW_SHOWNORMAL); - ::UpdateWindow(info.hwnd); - return info; -} - -void DestroyTestWindow(WindowInfo info) { - ::DestroyWindow(info.hwnd); - ::UnregisterClass(MAKEINTATOM(info.window_class), info.window_instance); -} std::unique_ptr SetUpUnresponsiveWindow(std::mutex& mtx, WindowInfo& info) {