forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_window_occlusion_tracker_win_interactive_test.cc
403 lines (347 loc) · 15.7 KB
/
native_window_occlusion_tracker_win_interactive_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/aura/native_window_occlusion_tracker_win.h"
#include <winuser.h>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/scoped_feature_list.h"
#include "base/win/scoped_gdi_object.h"
#include "mojo/core/embedder/embedder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/env.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_focus_client.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_window_parenting_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_occlusion_tracker.h"
#include "ui/aura/window_tree_host.h"
#include "ui/aura/window_tree_host_platform.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/win/dpi.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/win/singleton_hwnd.h"
#include "ui/gfx/win/window_impl.h"
#include "ui/gl/test/gl_surface_test_support.h"
namespace aura {
// This class is used to verify expectations about occlusion state changes by
// adding instances of it as an observer of aura:Windows the tests create and
// checking that they get the expected call(s) to OnOcclusionStateChanged.
// The tests verify that the current state, when idle, is the expected state,
// because the state can be VISIBLE before it reaches the expected state.
class MockWindowTreeHostObserver : public WindowTreeHostObserver {
public:
explicit MockWindowTreeHostObserver(base::OnceClosure quit_closure)
: quit_closure_(std::move(quit_closure)) {}
~MockWindowTreeHostObserver() override { EXPECT_FALSE(is_expecting_call()); }
// WindowTreeHostObserver:
void OnOcclusionStateChanged(WindowTreeHost* host,
Window::OcclusionState new_state) override {
// Should only get notified when the occlusion state changes.
EXPECT_NE(new_state, cur_state_);
cur_state_ = new_state;
if (expectation_ != Window::OcclusionState::UNKNOWN &&
cur_state_ == expectation_) {
EXPECT_FALSE(quit_closure_.is_null());
std::move(quit_closure_).Run();
}
}
void set_quit_closure(base::OnceClosure quit_closure) {
quit_closure_ = std::move(quit_closure);
}
void set_expectation(Window::OcclusionState expectation) {
expectation_ = expectation;
}
bool is_expecting_call() const { return expectation_ != cur_state_; }
private:
Window::OcclusionState expectation_ = Window::OcclusionState::UNKNOWN;
Window::OcclusionState cur_state_ = Window::OcclusionState::UNKNOWN;
base::OnceClosure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(MockWindowTreeHostObserver);
};
// Test wrapper around native window HWND.
class TestNativeWindow : public gfx::WindowImpl {
public:
TestNativeWindow() {}
~TestNativeWindow() override;
private:
// Overridden from gfx::WindowImpl:
BOOL ProcessWindowMessage(HWND window,
UINT message,
WPARAM w_param,
LPARAM l_param,
LRESULT& result,
DWORD msg_map_id) override {
return FALSE; // Results in DefWindowProc().
}
DISALLOW_COPY_AND_ASSIGN(TestNativeWindow);
};
TestNativeWindow::~TestNativeWindow() {
if (hwnd())
DestroyWindow(hwnd());
}
class NativeWindowOcclusionTrackerTest : public test::AuraTestBase {
public:
NativeWindowOcclusionTrackerTest() {
// These interactive_ui_tests are not based on browser tests which would
// normally handle initializing mojo. We can safely initialize mojo at the
// start of the test here since a new process is launched for each test.
mojo::core::Init();
}
void SetUp() override {
if (gl::GetGLImplementation() == gl::kGLImplementationNone)
gl::GLSurfaceTestSupport::InitializeOneOff();
scoped_feature_list_.InitAndEnableFeature(
features::kCalculateNativeWinOcclusion);
AuraTestBase::SetUp();
}
void SetNativeWindowBounds(HWND hwnd, const gfx::Rect& bounds) {
RECT wr = bounds.ToRECT();
AdjustWindowRectEx(&wr, GetWindowLong(hwnd, GWL_STYLE), FALSE,
GetWindowLong(hwnd, GWL_EXSTYLE));
// Make sure to keep the window onscreen, as AdjustWindowRectEx() may have
// moved part of it offscreen. But, if the original requested bounds are
// offscreen, don't adjust the position.
gfx::Rect window_bounds(wr);
if (bounds.x() >= 0)
window_bounds.set_x(std::max(0, window_bounds.x()));
if (bounds.y() >= 0)
window_bounds.set_y(std::max(0, window_bounds.y()));
SetWindowPos(hwnd, HWND_TOP, window_bounds.x(), window_bounds.y(),
window_bounds.width(), window_bounds.height(),
SWP_NOREPOSITION);
EXPECT_TRUE(UpdateWindow(hwnd));
}
HWND CreateNativeWindowWithBounds(const gfx::Rect& bounds) {
native_win_ = std::make_unique<TestNativeWindow>();
native_win_->set_window_style(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
native_win_->Init(nullptr, bounds);
HWND hwnd = native_win_->hwnd();
SetNativeWindowBounds(hwnd, bounds);
base::win::ScopedRegion region(CreateRectRgn(0, 0, 0, 0));
if (GetWindowRgn(hwnd, region.get()) == COMPLEXREGION) {
// On Windows 7, the newly created window has a complex region, which
// means it will be ignored during the occlusion calculation. So, force
// it to have a simple region so that we get test coverage on win 7.
RECT bounding_rect;
GetWindowRect(hwnd, &bounding_rect);
base::win::ScopedRegion rectangular_region(
CreateRectRgnIndirect(&bounding_rect));
SetWindowRgn(hwnd, rectangular_region.get(), TRUE);
}
ShowWindow(hwnd, SW_SHOWNORMAL);
EXPECT_TRUE(UpdateWindow(hwnd));
return hwnd;
}
void CreateTrackedAuraWindowWithBounds(MockWindowTreeHostObserver* observer,
gfx::Rect bounds) {
host()->Show();
host()->SetBoundsInPixels(bounds);
host()->AddObserver(observer);
Window* window = CreateNormalWindow(1, host()->window(), nullptr);
window->SetBounds(bounds);
Env::GetInstance()->GetWindowOcclusionTracker()->Track(window);
}
int GetNumVisibleRootWindows() {
return NativeWindowOcclusionTrackerWin::GetOrCreateInstance()
->num_visible_root_windows_;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<TestNativeWindow> native_win_;
DISALLOW_COPY_AND_ASSIGN(NativeWindowOcclusionTrackerTest);
};
// Simple test completely covering an aura window with a native window.
TEST_F(NativeWindowOcclusionTrackerTest, SimpleOcclusion) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::OCCLUDED);
CreateNativeWindowWithBounds(gfx::Rect(0, 0, 100, 100));
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Simple test partially covering an aura window with a native window.
TEST_F(NativeWindowOcclusionTrackerTest, PartialOcclusion) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::VISIBLE);
CreateNativeWindowWithBounds(gfx::Rect(0, 0, 50, 50));
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Simple test that a partly off screen aura window, with the on screen part
// occluded by a native window, is considered occluded.
TEST_F(NativeWindowOcclusionTrackerTest, OffscreenOcclusion) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
// Move the tracked window 50 pixels offscreen to the left.
int screen_left = GetSystemMetrics(SM_XVIRTUALSCREEN);
SetWindowPos(host()->GetAcceleratedWidget(), HWND_TOP, screen_left - 50, 0,
100, 100, SWP_NOZORDER | SWP_NOSIZE);
// Create a native window that covers the onscreen part of the tracked window.
CreateNativeWindowWithBounds(gfx::Rect(screen_left, 0, 50, 100));
observer.set_expectation(Window::OcclusionState::OCCLUDED);
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Simple test with an aura window and native window that do not overlap.
TEST_F(NativeWindowOcclusionTrackerTest, SimpleVisible) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::VISIBLE);
CreateNativeWindowWithBounds(gfx::Rect(200, 0, 100, 100));
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Simple test with a minimized aura window and native window.
TEST_F(NativeWindowOcclusionTrackerTest, SimpleHidden) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
CreateNativeWindowWithBounds(gfx::Rect(200, 0, 100, 100));
// Iconify the tracked aura window and check that its occlusion state
// is HIDDEN.
CloseWindow(host()->GetAcceleratedWidget());
observer.set_expectation(Window::OcclusionState::HIDDEN);
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Test that minimizing and restoring an app window results in the occlusion
// tracker re-registering for win events and detecting that a native window
// occludes the app window.
TEST_F(NativeWindowOcclusionTrackerTest, OcclusionAfterVisibilityToggle) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::VISIBLE);
run_loop.Run();
base::RunLoop run_loop2;
observer.set_expectation(Window::OcclusionState::HIDDEN);
observer.set_quit_closure(run_loop2.QuitClosure());
// host()->window()->Hide() is needed to generate OnWindowVisibilityChanged
// notifications.
host()->window()->Hide();
// This makes the window iconic.
::CloseWindow(host()->GetAcceleratedWidget());
run_loop2.Run();
// HIDDEN state is set synchronously by OnWindowVsiblityChanged notification,
// before occlusion is calculated, so the above expectation will be met w/o an
// occlusion calculation.
// Loop until an occlusion calculation has run with no non-hidden app windows.
do {
// Need to pump events in order for UpdateOcclusionState to get called, and
// update the number of non hidden root windows. When that number is 0,
// occlusion has been calculated with no visible root windows.
base::RunLoop().RunUntilIdle();
} while (GetNumVisibleRootWindows() != 0);
base::RunLoop run_loop3;
observer.set_expectation(Window::OcclusionState::VISIBLE);
observer.set_quit_closure(run_loop3.QuitClosure());
host()->window()->Show();
// This opens the window made iconic above.
OpenIcon(host()->GetAcceleratedWidget());
run_loop3.Run();
// Open a native window that occludes the visible app window.
base::RunLoop run_loop4;
observer.set_expectation(Window::OcclusionState::OCCLUDED);
observer.set_quit_closure(run_loop4.QuitClosure());
CreateNativeWindowWithBounds(gfx::Rect(0, 0, 100, 100));
run_loop4.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Test that locking the screen causes visible windows to become occluded.
TEST_F(NativeWindowOcclusionTrackerTest, LockScreenVisibleOcclusion) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::VISIBLE);
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
observer.set_expectation(Window::OcclusionState::OCCLUDED);
base::RunLoop run_loop2;
observer.set_quit_closure(run_loop2.QuitClosure());
// Unfortunately, this relies on knowing that NativeWindowOcclusionTracker
// uses SessionChangeObserver to listen for WM_WTSSESSION_CHANGE messages, but
// actually locking the screen isn't feasible.
DWORD current_session_id = 0;
ProcessIdToSessionId(::GetCurrentProcessId(), ¤t_session_id);
PostMessage(gfx::SingletonHwnd::GetInstance()->hwnd(), WM_WTSSESSION_CHANGE,
WTS_SESSION_LOCK, current_session_id);
run_loop2.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Test that locking the screen leaves hidden windows as hidden.
TEST_F(NativeWindowOcclusionTrackerTest, LockScreenHiddenOcclusion) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
// Iconify the tracked aura window and check that its occlusion state
// is HIDDEN.
CloseWindow(host()->GetAcceleratedWidget());
observer.set_expectation(Window::OcclusionState::HIDDEN);
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
// Observer only gets notified on occlusion state changes, so force the
// state to VISIBLE so that setting the state to hidden will trigger
// a notification.
host()->SetNativeWindowOcclusionState(Window::OcclusionState::VISIBLE);
observer.set_expectation(Window::OcclusionState::HIDDEN);
base::RunLoop run_loop2;
observer.set_quit_closure(run_loop2.QuitClosure());
// Unfortunately, this relies on knowing that NativeWindowOcclusionTracker
// uses SessionChangeObserver to listen for WM_WTSSESSION_CHANGE messages, but
// actually locking the screen isn't feasible.
DWORD current_session_id = 0;
ProcessIdToSessionId(::GetCurrentProcessId(), ¤t_session_id);
PostMessage(gfx::SingletonHwnd::GetInstance()->hwnd(), WM_WTSSESSION_CHANGE,
WTS_SESSION_LOCK, current_session_id);
run_loop2.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
// Test that locking the screen from a different session doesn't mark window
// as occluded.
TEST_F(NativeWindowOcclusionTrackerTest, LockScreenDifferentSession) {
base::RunLoop run_loop;
MockWindowTreeHostObserver observer(run_loop.QuitClosure());
CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100));
observer.set_expectation(Window::OcclusionState::VISIBLE);
run_loop.Run();
EXPECT_FALSE(observer.is_expecting_call());
// Observer only gets notified on occlusion state changes, so force the
// state to OCCLUDED so that setting the state to VISIBLE will trigger
// a notification.
host()->SetNativeWindowOcclusionState(Window::OcclusionState::OCCLUDED);
// Generate a session change lock screen with a session id that's not
// |current_session_id|.
DWORD current_session_id = 0;
ProcessIdToSessionId(::GetCurrentProcessId(), ¤t_session_id);
PostMessage(gfx::SingletonHwnd::GetInstance()->hwnd(), WM_WTSSESSION_CHANGE,
WTS_SESSION_LOCK, current_session_id + 1);
observer.set_expectation(Window::OcclusionState::VISIBLE);
base::RunLoop run_loop2;
observer.set_quit_closure(run_loop2.QuitClosure());
// Create a native window to trigger occlusion calculation.
CreateNativeWindowWithBounds(gfx::Rect(0, 0, 50, 50));
run_loop2.Run();
EXPECT_FALSE(observer.is_expecting_call());
host()->RemoveObserver(&observer);
}
} // namespace aura