Refactor test window creation functions to be reusable.

This change moves several functions used to create test windows from
WindowCaptureUtilsTest into a reusable file test_window.cc. This is so
we can reuse this code in the window capturer tests in this CL:
196624: Finish implementing WGC Window Capturer and add unit tests. |
https://webrtc-review.googlesource.com/c/src/+/196624

Bug: webrtc:9273
Change-Id: I62dc53e6c3475e49ca4ae4cf6d1ef02fc8781b89
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/196623
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Commit-Queue: Jamie Walch <jamiewalch@chromium.org>
Commit-Queue: Austin Orion <auorion@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#32838}
diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn
index 3773fac..70344e5 100644
--- a/modules/desktop_capture/BUILD.gn
+++ b/modules/desktop_capture/BUILD.gn
@@ -115,6 +115,8 @@
         "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 0000000..dc94ee0
--- /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<LPCWSTR>(&::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 0000000..a5962b5
--- /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 <windows.h>
+
+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 804cee9..52f6714 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 <mutex>

 

 #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 {

 

 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<LPCWSTR>(&::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<rtc::Thread> SetUpUnresponsiveWindow(std::mutex& mtx,

                                                      WindowInfo& info) {