File: native_window_occlusion_tracker_win_interactive_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (679 lines) | stat: -rw-r--r-- 26,577 bytes parent folder | download | duplicates (7)
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#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/run_loop.h"
#include "base/task/current_thread.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind.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_observer.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/test/ui_controls.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display.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(const MockWindowTreeHostObserver&) = delete;
  MockWindowTreeHostObserver& operator=(const MockWindowTreeHostObserver&) =
      delete;

  ~MockWindowTreeHostObserver() override { EXPECT_FALSE(is_expecting_call()); }

  // WindowTreeHostObserver:
  void OnOcclusionStateChanged(WindowTreeHost* host,
                               Window::OcclusionState new_state,
                               const SkRegion& occluded_region) 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_;
};

class MockWindowObserver : public WindowObserver {
 public:
  explicit MockWindowObserver(Window* window) : window_(window) {
    window_->AddObserver(this);
  }

  ~MockWindowObserver() override {
    if (window_)
      window_->RemoveObserver(this);
  }

  void set_quit_closure(base::OnceClosure quit_closure) {
    quit_closure_ = std::move(quit_closure);
  }

  void set_expectation(Window::OcclusionState expectation) {
    expectation_ = expectation;
  }

  // WindowObserver:
  void OnWindowOcclusionChanged(Window* window) override {
    if (expectation_ == window->GetOcclusionState()) {
      ASSERT_FALSE(quit_closure_.is_null());
      std::move(quit_closure_).Run();
    }
  }

  void OnWindowDestroyed(Window* window) override {
    window_->RemoveObserver(this);
    window_ = nullptr;
  }

 private:
  raw_ptr<Window> window_;
  Window::OcclusionState expectation_ = Window::OcclusionState::UNKNOWN;
  base::OnceClosure quit_closure_;
};

// Test wrapper around native window HWND.
class TestNativeWindow : public gfx::WindowImpl {
 public:
  TestNativeWindow() {}

  TestNativeWindow(const TestNativeWindow&) = delete;
  TestNativeWindow& operator=(const TestNativeWindow&) = delete;

  ~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().
  }
};

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();
  }

  NativeWindowOcclusionTrackerTest(const NativeWindowOcclusionTrackerTest&) =
      delete;
  NativeWindowOcclusionTrackerTest& operator=(
      const NativeWindowOcclusionTrackerTest&) = delete;

  void SetUp() override {
    if (gl::GetGLImplementation() == gl::kGLImplementationNone)
      gl::GLSurfaceTestSupport::InitializeOneOff();

    scoped_feature_list_.InitWithFeatures(
        {features::kCalculateNativeWinOcclusion,
         features::kApplyNativeOccludedRegionToWindowTracker},
        {});

    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) {
    std::unique_ptr<TestNativeWindow> 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);
    ShowWindow(hwnd, SW_SHOWNORMAL);
    EXPECT_TRUE(UpdateWindow(hwnd));
    native_wins_.push_back(std::move(native_win));
    return hwnd;
  }

  Window* CreateTrackedAuraWindowWithBounds(
      MockWindowTreeHostObserver* observer,
      const gfx::Rect& bounds) {
    host()->Show();
    host()->SetBoundsInPixels(bounds);
    if (observer)
      host()->AddObserver(observer);

    Window* window = CreateNormalWindow(1, host()->window(), nullptr);
    window->SetBounds(gfx::Rect(bounds.size()));

    Env::GetInstance()->GetWindowOcclusionTracker()->Track(window);
    return window;
  }

  int GetNumVisibleRootWindows() {
    return NativeWindowOcclusionTrackerWin::GetOrCreateInstance()
        ->num_visible_root_windows_;
  }

  void MakeFullscreen(HWND hwnd) {
    DWORD style = GetWindowLong(hwnd, GWL_STYLE);
    DWORD ex_style = GetWindowLong(hwnd, GWL_STYLE);
    SetWindowLong(hwnd, GWL_STYLE, style & ~(WS_CAPTION | WS_THICKFRAME));
    SetWindowLong(hwnd, GWL_EXSTYLE,
                  ex_style & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE |
                               WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
    MONITORINFO monitor_info;
    monitor_info.cbSize = sizeof(monitor_info);
    GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST),
                   &monitor_info);
    gfx::Rect window_rect(monitor_info.rcMonitor);
    SetWindowPos(hwnd, nullptr, window_rect.x(), window_rect.y(),
                 window_rect.width(), window_rect.height(),
                 SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
  std::vector<std::unique_ptr<TestNativeWindow>> native_wins_;
};

// 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(), &current_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(), &current_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(), &current_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);
}

// Test that display off & on power state notification causes visible windows to
// become occluded, then visible.
TEST_F(NativeWindowOcclusionTrackerTest, DisplayOnOffHandling) {
  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());

  NativeWindowOcclusionTrackerWin* occlusion_tracker =
      NativeWindowOcclusionTrackerWin::GetOrCreateInstance();

  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  base::RunLoop run_loop2;
  observer.set_quit_closure(run_loop2.QuitClosure());

  // Turning display off and on isn't feasible, so send a notification.
  occlusion_tracker->OnDisplayStateChanged(/*display_on=*/false);
  run_loop2.Run();
  EXPECT_FALSE(observer.is_expecting_call());

  observer.set_expectation(Window::OcclusionState::VISIBLE);
  base::RunLoop run_loop3;
  observer.set_quit_closure(run_loop3.QuitClosure());
  occlusion_tracker->OnDisplayStateChanged(/*display_on=*/true);
  run_loop3.Run();
  EXPECT_FALSE(observer.is_expecting_call());
  host()->RemoveObserver(&observer);
}

// Verifies that a window is not occluded if the only window occluding it is
// being moved/dragged.
//
// TODO(crbug.com/40801894): Flaky on Windows.
TEST_F(NativeWindowOcclusionTrackerTest,
       DISABLED_MovingWindowNotConsideredInCalculations) {
  // Needed as this test triggers a native nested message loop.
  base::CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop
      allow_nesting;

  // Create the initial window.
  base::RunLoop run_loop;
  MockWindowTreeHostObserver observer(run_loop.QuitClosure());
  CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(40, 40, 100, 100));
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  run_loop.Run();
  EXPECT_FALSE(observer.is_expecting_call());

  // Creates a new window that obscures the initial window.
  CreateNativeWindowWithBounds(gfx::Rect(0, 0, 200, 200));
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  base::RunLoop run_loop2;
  observer.set_quit_closure(run_loop2.QuitClosure());
  run_loop2.Run();
  EXPECT_FALSE(observer.is_expecting_call());

  // Start a window move loop. As windows being moved/dragged are not considered
  // during occlusion calculation, the initial window should become visible.
  base::RunLoop run_loop3(base::RunLoop::Type::kNestableTasksAllowed);
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  observer.set_quit_closure(base::BindLambdaForTesting([&] {
    // Release the mouse, which should make the initial window occluded.
    observer.set_expectation(Window::OcclusionState::OCCLUDED);
    observer.set_quit_closure(run_loop3.QuitClosure());
    ASSERT_TRUE(
        ui_controls::SendMouseEvents(ui_controls::LEFT, ui_controls::UP));
  }));
  ASSERT_TRUE(ui_controls::SendMouseMove(40, 8));
  ASSERT_TRUE(
      ui_controls::SendMouseEvents(ui_controls::LEFT, ui_controls::DOWN));
  run_loop3.Run();
  EXPECT_FALSE(observer.is_expecting_call());

  host()->RemoveObserver(&observer);
}

// Test that a maximized aura window that is covered by a fullscreen window
// is marked as occluded. TODO(crbug.com/40833493): Fix flakiness.
TEST_F(NativeWindowOcclusionTrackerTest,
       DISABLED_MaximizedOccludedByFullscreenWindow) {
  // Create an aura window that is maximized.
  base::RunLoop run_loop1;
  MockWindowTreeHostObserver observer(run_loop1.QuitClosure());
  HWND hwnd_aura_window_maximized =
      CreateTrackedAuraWindowWithBounds(&observer, gfx::Rect(0, 0, 100, 100))
          ->GetHost()
          ->GetAcceleratedWidget();
  ShowWindow(hwnd_aura_window_maximized, SW_SHOWMAXIMIZED);
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  run_loop1.Run();
  EXPECT_FALSE(observer.is_expecting_call());
  // Create a fullscreen native window that occludes the aura window.
  base::RunLoop run_loop2;
  observer.set_quit_closure(run_loop2.QuitClosure());
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  HWND hwnd_native_window =
      CreateNativeWindowWithBounds(gfx::Rect(0, 0, 100, 100));
  MakeFullscreen(hwnd_native_window);
  run_loop2.Run();
  EXPECT_FALSE(observer.is_expecting_call());
  host()->RemoveObserver(&observer);
}

TEST_F(NativeWindowOcclusionTrackerTest, OccludedRegionSimple) {
  Window* tracked_aura_window =
      CreateTrackedAuraWindowWithBounds(nullptr, gfx::Rect(20, 20, 200, 200));
  tracked_aura_window->SetBounds(gfx::Rect(0, 0, 60, 60));

  MockWindowObserver observer(tracked_aura_window);
  base::RunLoop run_loop;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop.QuitClosure());
  HWND obscuring_hwnd =
      CreateNativeWindowWithBounds(gfx::Rect(20, 20, 110, 110));
  run_loop.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop2;
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  observer.set_quit_closure(run_loop2.QuitClosure());
  tracked_aura_window->SetBounds(gfx::Rect(160, 160, 20, 20));
  run_loop2.Run();
  EXPECT_EQ(Window::OcclusionState::VISIBLE,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop3;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop3.QuitClosure());
  SetNativeWindowBounds(obscuring_hwnd, gfx::Rect(140, 140, 110, 110));
  run_loop3.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());
}

TEST_F(NativeWindowOcclusionTrackerTest, OccludedRegionComplex) {
  Window* tracked_aura_window =
      CreateTrackedAuraWindowWithBounds(nullptr, gfx::Rect(20, 20, 200, 200));
  tracked_aura_window->SetBounds(gfx::Rect(0, 0, 60, 60));

  MockWindowObserver observer(tracked_aura_window);
  base::RunLoop run_loop;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop.QuitClosure());
  CreateNativeWindowWithBounds(gfx::Rect(20, 20, 110, 110));
  run_loop.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop2;
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  observer.set_quit_closure(run_loop2.QuitClosure());
  tracked_aura_window->SetBounds(gfx::Rect(160, 160, 20, 20));
  run_loop2.Run();
  EXPECT_EQ(Window::OcclusionState::VISIBLE,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop3;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop3.QuitClosure());
  CreateNativeWindowWithBounds(gfx::Rect(140, 140, 110, 110));
  run_loop3.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());
}

class NativeWindowOcclusionTrackerTestWithDpi2
    : public NativeWindowOcclusionTrackerTest {
 public:
  // NativeWindowOcclusionTrackerTest:
  void SetUp() override {
    display::Display::SetForceDeviceScaleFactor(2.0);
    NativeWindowOcclusionTrackerTest::SetUp();
  }
};

TEST_F(NativeWindowOcclusionTrackerTestWithDpi2, OccludedRegionSimple) {
  Window* tracked_aura_window =
      CreateTrackedAuraWindowWithBounds(nullptr, gfx::Rect(20, 20, 200, 200));
  tracked_aura_window->SetBounds(gfx::Rect(0, 0, 30, 30));

  MockWindowObserver observer(tracked_aura_window);
  base::RunLoop run_loop;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop.QuitClosure());
  HWND obscuring_hwnd =
      CreateNativeWindowWithBounds(gfx::Rect(20, 20, 110, 110));
  run_loop.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop2;
  observer.set_expectation(Window::OcclusionState::VISIBLE);
  observer.set_quit_closure(run_loop2.QuitClosure());
  tracked_aura_window->SetBounds(gfx::Rect(80, 80, 20, 20));
  run_loop2.Run();
  EXPECT_EQ(Window::OcclusionState::VISIBLE,
            tracked_aura_window->GetOcclusionState());

  base::RunLoop run_loop3;
  observer.set_expectation(Window::OcclusionState::OCCLUDED);
  observer.set_quit_closure(run_loop3.QuitClosure());
  SetNativeWindowBounds(obscuring_hwnd, gfx::Rect(140, 140, 110, 110));
  run_loop3.Run();
  EXPECT_EQ(Window::OcclusionState::OCCLUDED,
            tracked_aura_window->GetOcclusionState());
}

}  // namespace aura