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
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback_forward.h"
#include "base/test/test_future.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/scoped_display_for_new_windows.h"
namespace ash {
using ScreenAshTest = AshTestBase;
// Tests that ScreenAsh::GetWindowAtScreenPoint() returns the correct window on
// the correct display.
TEST_F(ScreenAshTest, TestGetWindowAtScreenPoint) {
UpdateDisplay("300x200,500x400");
aura::test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> win1(CreateTestWindowInShellWithDelegate(
&delegate, 0, gfx::Rect(0, 0, 200, 200)));
std::unique_ptr<aura::Window> win2(CreateTestWindowInShellWithDelegate(
&delegate, 1, gfx::Rect(300, 200, 100, 100)));
ASSERT_NE(win1->GetRootWindow(), win2->GetRootWindow());
EXPECT_EQ(win1.get(), display::Screen::GetScreen()->GetWindowAtScreenPoint(
gfx::Point(50, 60)));
EXPECT_EQ(win2.get(), display::Screen::GetScreen()->GetWindowAtScreenPoint(
gfx::Point(350, 260)));
}
TEST_F(ScreenAshTest, GetDisplayForNewWindows) {
UpdateDisplay("300x200,500x400");
display::Screen* screen = display::Screen::GetScreen();
const std::vector<display::Display> displays = screen->GetAllDisplays();
ASSERT_EQ(2u, displays.size());
// The display for new windows defaults to primary display.
EXPECT_EQ(displays[0].id(), screen->GetDisplayForNewWindows().id());
// The display for new windows is updated when the root window for new windows
// changes.
display::ScopedDisplayForNewWindows scoped_display(
Shell::GetAllRootWindows()[1]);
EXPECT_EQ(displays[1].id(), screen->GetDisplayForNewWindows().id());
}
namespace {
// Simulates an observer that tries to get the primary display when notified of
// displays addition or removal when switching to or from the Unified Desktop
// mode.
class TestDisplayRemoveObserver : public display::DisplayObserver {
public:
TestDisplayRemoveObserver() = default;
TestDisplayRemoveObserver(const TestDisplayRemoveObserver&) = delete;
TestDisplayRemoveObserver& operator=(const TestDisplayRemoveObserver&) =
delete;
~TestDisplayRemoveObserver() override = default;
int added_displays() const { return added_displays_; }
int removed_displays() const { return removed_displays_; }
// display::DisplayObserver:
void OnDisplayAdded(const display::Display& new_display) override {
TestPrimaryDisplay();
++added_displays_;
}
void OnDisplaysRemoved(const display::Displays& removed_displays) override {
TestPrimaryDisplay();
removed_displays_ += removed_displays.size();
}
private:
void TestPrimaryDisplay() const {
auto display = display::Screen::GetScreen()->GetPrimaryDisplay();
DCHECK_NE(display.id(), display::kInvalidDisplayId);
}
int added_displays_ = 0;
int removed_displays_ = 0;
};
// Invokes the given callback when the code is inside the destructor of the
// root window.
class RootWindowDestructorObserver : aura::WindowObserver {
public:
RootWindowDestructorObserver(aura::Window* child_window,
base::OnceClosure callback)
: callback_(std::move(callback)),
root_window_(child_window->GetRootWindow()) {
root_window_->AddObserver(this);
}
~RootWindowDestructorObserver() override {
if (root_window_) {
root_window_->RemoveObserver(this);
}
}
private:
void OnWindowDestroying(aura::Window* window) override {
CHECK_EQ(window, root_window_);
std::move(callback_).Run();
root_window_ = nullptr;
}
base::OnceClosure callback_;
raw_ptr<aura::Window> root_window_;
};
} // namespace
// Switching to Unified Desktop removes all current displays (including primary
// display) and replaces them with the unified display. The display manager
// notifies observers of display removals before display additions. At this
// point if an observer tries to get the primary display, it could lead to a
// crash because all displays have been removed. This test makes sure doesn't
// happen anymore. https://crbug.com/866714.
TEST_F(ScreenAshTest, TestNoCrashesOnGettingPrimaryDisplayOnDisplayRemoved) {
UpdateDisplay("400x500,300x200");
TestDisplayRemoveObserver observer;
display_manager()->AddDisplayObserver(&observer);
// Enter Unified Mode.
display_manager()->SetUnifiedDesktopEnabled(true);
EXPECT_TRUE(display_manager()->IsInUnifiedMode());
EXPECT_EQ(observer.added_displays(), 1);
EXPECT_EQ(observer.removed_displays(), 2);
// Exit Unified Mode, there shouldn't be any crashes either.
display_manager()->SetUnifiedDesktopEnabled(false);
EXPECT_FALSE(display_manager()->IsInUnifiedMode());
EXPECT_EQ(observer.added_displays(), 3);
EXPECT_EQ(observer.removed_displays(), 3);
display_manager()->RemoveDisplayObserver(&observer);
}
TEST_F(ScreenAshTest,
GetDisplayNearestWindowShouldNotCrashWhenWindowIsBeingDestroyed) {
UpdateDisplay("400x500,300x200");
std::unique_ptr<aura::Window> window_on_second_display(
CreateTestWindow(gfx::Rect(400, 0, 100, 100)));
base::test::TestFuture<void> root_window_destroyed_waiter;
RootWindowDestructorObserver observer(
window_on_second_display.get(),
base::BindOnce(
[](aura::Window* window) {
// This callback is invoked from inside the destructor of the root
// window. Calling `GetDisplayNearestWindow` from here used to
// crash (https://crbug.com/376575664).
// This tests it doesn't.
display::Screen::GetScreen()->GetDisplayNearestWindow(window);
},
window_on_second_display.get())
.Then(root_window_destroyed_waiter.GetCallback()));
// Destroy the second display
UpdateDisplay("400x500");
EXPECT_TRUE(root_window_destroyed_waiter.Wait());
}
} // namespace ash
|