#include "tests/cefclient/browser/window_test_runner_win.h"
#include "tests/shared/browser/main_message_loop.h"
namespace client {
namespace window_test {
namespace {
HWND GetRootHwnd(CefRefPtr<CefBrowser> browser) {
return ::GetAncestor(browser->GetHost()->GetWindowHandle(), GA_ROOT);
}
void Toggle(HWND root_hwnd, UINT nCmdShow) {
WINDOWPLACEMENT placement;
::GetWindowPlacement(root_hwnd, &placement);
if (placement.showCmd == nCmdShow)
::ShowWindow(root_hwnd, SW_RESTORE);
else
::ShowWindow(root_hwnd, nCmdShow);
}
void SetPosImpl(CefRefPtr<CefBrowser> browser,
int x,
int y,
int width,
int height) {
HWND root_hwnd = GetRootHwnd(browser);
if (!root_hwnd)
return;
WINDOWPLACEMENT placement;
::GetWindowPlacement(root_hwnd, &placement);
HMONITOR monitor =
MonitorFromRect(&placement.rcNormalPosition, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(info);
GetMonitorInfo(monitor, &info);
CefRect display_rect(info.rcWork.left, info.rcWork.top,
info.rcWork.right - info.rcWork.left,
info.rcWork.bottom - info.rcWork.top);
CefRect window_rect(x, y, width, height);
WindowTestRunner::ModifyBounds(display_rect, window_rect);
if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_MAXIMIZE) {
placement.rcNormalPosition.left = window_rect.x;
placement.rcNormalPosition.right = window_rect.x + window_rect.width;
placement.rcNormalPosition.top = window_rect.y;
placement.rcNormalPosition.bottom = window_rect.y + window_rect.height;
::SetWindowPlacement(root_hwnd, &placement);
::ShowWindow(root_hwnd, SW_RESTORE);
} else {
::SetWindowPos(root_hwnd, nullptr, window_rect.x, window_rect.y,
window_rect.width, window_rect.height, SWP_NOZORDER);
}
}
void MinimizeImpl(CefRefPtr<CefBrowser> browser) {
HWND root_hwnd = GetRootHwnd(browser);
if (!root_hwnd)
return;
Toggle(root_hwnd, SW_MINIMIZE);
}
void MaximizeImpl(CefRefPtr<CefBrowser> browser) {
HWND root_hwnd = GetRootHwnd(browser);
if (!root_hwnd)
return;
Toggle(root_hwnd, SW_MAXIMIZE);
}
void RestoreImpl(CefRefPtr<CefBrowser> browser) {
HWND root_hwnd = GetRootHwnd(browser);
if (!root_hwnd)
return;
::ShowWindow(root_hwnd, SW_RESTORE);
}
}
WindowTestRunnerWin::WindowTestRunnerWin() {}
void WindowTestRunnerWin::SetPos(CefRefPtr<CefBrowser> browser,
int x,
int y,
int width,
int height) {
if (CURRENTLY_ON_MAIN_THREAD()) {
SetPosImpl(browser, x, y, width, height);
} else {
MAIN_POST_CLOSURE(base::BindOnce(SetPosImpl, browser, x, y, width, height));
}
}
void WindowTestRunnerWin::Minimize(CefRefPtr<CefBrowser> browser) {
if (CURRENTLY_ON_MAIN_THREAD()) {
MinimizeImpl(browser);
} else {
MAIN_POST_CLOSURE(base::BindOnce(MinimizeImpl, browser));
}
}
void WindowTestRunnerWin::Maximize(CefRefPtr<CefBrowser> browser) {
if (CURRENTLY_ON_MAIN_THREAD()) {
MaximizeImpl(browser);
} else {
MAIN_POST_CLOSURE(base::BindOnce(MaximizeImpl, browser));
}
}
void WindowTestRunnerWin::Restore(CefRefPtr<CefBrowser> browser) {
if (CURRENTLY_ON_MAIN_THREAD()) {
RestoreImpl(browser);
} else {
MAIN_POST_CLOSURE(base::BindOnce(RestoreImpl, browser));
}
}
}
}