xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/test_support/test_window.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/desktop_capture/win/test_support/test_window.h"
12 
13 namespace webrtc {
14 namespace {
15 
16 const WCHAR kWindowClass[] = L"DesktopCaptureTestWindowClass";
17 const int kWindowHeight = 200;
18 const int kWindowWidth = 300;
19 
WindowProc(HWND hwnd,UINT msg,WPARAM w_param,LPARAM l_param)20 LRESULT CALLBACK WindowProc(HWND hwnd,
21                             UINT msg,
22                             WPARAM w_param,
23                             LPARAM l_param) {
24   switch (msg) {
25     case WM_PAINT:
26       PAINTSTRUCT paint_struct;
27       HDC hdc = BeginPaint(hwnd, &paint_struct);
28 
29       // Paint the window so the color is consistent and we can inspect the
30       // pixels in tests and know what to expect.
31       FillRect(hdc, &paint_struct.rcPaint,
32                CreateSolidBrush(RGB(kTestWindowRValue, kTestWindowGValue,
33                                     kTestWindowBValue)));
34 
35       EndPaint(hwnd, &paint_struct);
36   }
37   return DefWindowProc(hwnd, msg, w_param, l_param);
38 }
39 
40 }  // namespace
41 
CreateTestWindow(const WCHAR * window_title,const int height,const int width,const LONG extended_styles)42 WindowInfo CreateTestWindow(const WCHAR* window_title,
43                             const int height,
44                             const int width,
45                             const LONG extended_styles) {
46   WindowInfo info;
47   ::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
48                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
49                        reinterpret_cast<LPCWSTR>(&WindowProc),
50                        &info.window_instance);
51 
52   WNDCLASSEXW wcex;
53   memset(&wcex, 0, sizeof(wcex));
54   wcex.cbSize = sizeof(wcex);
55   wcex.style = CS_HREDRAW | CS_VREDRAW;
56   wcex.hInstance = info.window_instance;
57   wcex.lpfnWndProc = &WindowProc;
58   wcex.lpszClassName = kWindowClass;
59   info.window_class = ::RegisterClassExW(&wcex);
60 
61   // Use the default height and width if the caller did not supply the optional
62   // height and width parameters, or if they supplied invalid values.
63   int window_height = height <= 0 ? kWindowHeight : height;
64   int window_width = width <= 0 ? kWindowWidth : width;
65   info.hwnd =
66       ::CreateWindowExW(extended_styles, kWindowClass, window_title,
67                         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
68                         window_width, window_height, /*parent_window=*/nullptr,
69                         /*menu_bar=*/nullptr, info.window_instance,
70                         /*additional_params=*/nullptr);
71 
72   ::ShowWindow(info.hwnd, SW_SHOWNORMAL);
73   ::UpdateWindow(info.hwnd);
74   return info;
75 }
76 
ResizeTestWindow(const HWND hwnd,const int width,const int height)77 void ResizeTestWindow(const HWND hwnd, const int width, const int height) {
78   // SWP_NOMOVE results in the x and y params being ignored.
79   ::SetWindowPos(hwnd, HWND_TOP, /*x-coord=*/0, /*y-coord=*/0, width, height,
80                  SWP_SHOWWINDOW | SWP_NOMOVE);
81   ::UpdateWindow(hwnd);
82 }
83 
MoveTestWindow(const HWND hwnd,const int x,const int y)84 void MoveTestWindow(const HWND hwnd, const int x, const int y) {
85   // SWP_NOSIZE results in the width and height params being ignored.
86   ::SetWindowPos(hwnd, HWND_TOP, x, y, /*width=*/0, /*height=*/0,
87                  SWP_SHOWWINDOW | SWP_NOSIZE);
88   ::UpdateWindow(hwnd);
89 }
90 
MinimizeTestWindow(const HWND hwnd)91 void MinimizeTestWindow(const HWND hwnd) {
92   ::ShowWindow(hwnd, SW_MINIMIZE);
93 }
94 
UnminimizeTestWindow(const HWND hwnd)95 void UnminimizeTestWindow(const HWND hwnd) {
96   ::OpenIcon(hwnd);
97 }
98 
DestroyTestWindow(WindowInfo info)99 void DestroyTestWindow(WindowInfo info) {
100   ::DestroyWindow(info.hwnd);
101   ::UnregisterClass(MAKEINTATOM(info.window_class), info.window_instance);
102 }
103 
104 }  // namespace webrtc
105