1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/cefclient/browser/window_test_runner_win.h"
6
7 #include "tests/shared/browser/main_message_loop.h"
8
9 namespace client {
10 namespace window_test {
11
12 namespace {
13
GetRootHwnd(CefRefPtr<CefBrowser> browser)14 HWND GetRootHwnd(CefRefPtr<CefBrowser> browser) {
15 return ::GetAncestor(browser->GetHost()->GetWindowHandle(), GA_ROOT);
16 }
17
18 // Toggles the current display state.
Toggle(HWND root_hwnd,UINT nCmdShow)19 void Toggle(HWND root_hwnd, UINT nCmdShow) {
20 // Retrieve current window placement information.
21 WINDOWPLACEMENT placement;
22 ::GetWindowPlacement(root_hwnd, &placement);
23
24 if (placement.showCmd == nCmdShow)
25 ::ShowWindow(root_hwnd, SW_RESTORE);
26 else
27 ::ShowWindow(root_hwnd, nCmdShow);
28 }
29
SetPosImpl(CefRefPtr<CefBrowser> browser,int x,int y,int width,int height)30 void SetPosImpl(CefRefPtr<CefBrowser> browser,
31 int x,
32 int y,
33 int width,
34 int height) {
35 HWND root_hwnd = GetRootHwnd(browser);
36 if (!root_hwnd)
37 return;
38
39 // Retrieve current window placement information.
40 WINDOWPLACEMENT placement;
41 ::GetWindowPlacement(root_hwnd, &placement);
42
43 // Retrieve information about the display that contains the window.
44 HMONITOR monitor =
45 MonitorFromRect(&placement.rcNormalPosition, MONITOR_DEFAULTTONEAREST);
46 MONITORINFO info;
47 info.cbSize = sizeof(info);
48 GetMonitorInfo(monitor, &info);
49
50 // Make sure the window is inside the display.
51 CefRect display_rect(info.rcWork.left, info.rcWork.top,
52 info.rcWork.right - info.rcWork.left,
53 info.rcWork.bottom - info.rcWork.top);
54 CefRect window_rect(x, y, width, height);
55 WindowTestRunner::ModifyBounds(display_rect, window_rect);
56
57 if (placement.showCmd == SW_MINIMIZE || placement.showCmd == SW_MAXIMIZE) {
58 // The window is currently minimized or maximized. Restore it to the desired
59 // position.
60 placement.rcNormalPosition.left = window_rect.x;
61 placement.rcNormalPosition.right = window_rect.x + window_rect.width;
62 placement.rcNormalPosition.top = window_rect.y;
63 placement.rcNormalPosition.bottom = window_rect.y + window_rect.height;
64 ::SetWindowPlacement(root_hwnd, &placement);
65 ::ShowWindow(root_hwnd, SW_RESTORE);
66 } else {
67 // Set the window position.
68 ::SetWindowPos(root_hwnd, nullptr, window_rect.x, window_rect.y,
69 window_rect.width, window_rect.height, SWP_NOZORDER);
70 }
71 }
72
MinimizeImpl(CefRefPtr<CefBrowser> browser)73 void MinimizeImpl(CefRefPtr<CefBrowser> browser) {
74 HWND root_hwnd = GetRootHwnd(browser);
75 if (!root_hwnd)
76 return;
77 Toggle(root_hwnd, SW_MINIMIZE);
78 }
79
MaximizeImpl(CefRefPtr<CefBrowser> browser)80 void MaximizeImpl(CefRefPtr<CefBrowser> browser) {
81 HWND root_hwnd = GetRootHwnd(browser);
82 if (!root_hwnd)
83 return;
84 Toggle(root_hwnd, SW_MAXIMIZE);
85 }
86
RestoreImpl(CefRefPtr<CefBrowser> browser)87 void RestoreImpl(CefRefPtr<CefBrowser> browser) {
88 HWND root_hwnd = GetRootHwnd(browser);
89 if (!root_hwnd)
90 return;
91 ::ShowWindow(root_hwnd, SW_RESTORE);
92 }
93
94 } // namespace
95
WindowTestRunnerWin()96 WindowTestRunnerWin::WindowTestRunnerWin() {}
97
SetPos(CefRefPtr<CefBrowser> browser,int x,int y,int width,int height)98 void WindowTestRunnerWin::SetPos(CefRefPtr<CefBrowser> browser,
99 int x,
100 int y,
101 int width,
102 int height) {
103 if (CURRENTLY_ON_MAIN_THREAD()) {
104 SetPosImpl(browser, x, y, width, height);
105 } else {
106 // Execute on the main application thread.
107 MAIN_POST_CLOSURE(base::BindOnce(SetPosImpl, browser, x, y, width, height));
108 }
109 }
110
Minimize(CefRefPtr<CefBrowser> browser)111 void WindowTestRunnerWin::Minimize(CefRefPtr<CefBrowser> browser) {
112 if (CURRENTLY_ON_MAIN_THREAD()) {
113 MinimizeImpl(browser);
114 } else {
115 // Execute on the main application thread.
116 MAIN_POST_CLOSURE(base::BindOnce(MinimizeImpl, browser));
117 }
118 }
119
Maximize(CefRefPtr<CefBrowser> browser)120 void WindowTestRunnerWin::Maximize(CefRefPtr<CefBrowser> browser) {
121 if (CURRENTLY_ON_MAIN_THREAD()) {
122 MaximizeImpl(browser);
123 } else {
124 // Execute on the main application thread.
125 MAIN_POST_CLOSURE(base::BindOnce(MaximizeImpl, browser));
126 }
127 }
128
Restore(CefRefPtr<CefBrowser> browser)129 void WindowTestRunnerWin::Restore(CefRefPtr<CefBrowser> browser) {
130 if (CURRENTLY_ON_MAIN_THREAD()) {
131 RestoreImpl(browser);
132 } else {
133 // Execute on the main application thread.
134 MAIN_POST_CLOSURE(base::BindOnce(RestoreImpl, browser));
135 }
136 }
137
138 } // namespace window_test
139 } // namespace client
140