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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/x/geometry_cache.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/xproto.h"
namespace x11 {
namespace {
class ScopedWindow {
public:
ScopedWindow(Connection* connection, Window parent, const gfx::Rect& bounds)
: connection_(connection) {
id_ = connection_->GenerateId<Window>();
connection_->CreateWindow({
.wid = id_,
.parent = parent,
.x = static_cast<int16_t>(bounds.x()),
.y = static_cast<int16_t>(bounds.y()),
.width = static_cast<uint16_t>(bounds.width()),
.height = static_cast<uint16_t>(bounds.height()),
.c_class = WindowClass::InputOnly,
.override_redirect = Bool32(true),
});
}
ScopedWindow(ScopedWindow&&) = delete;
ScopedWindow& operator=(ScopedWindow&&) = delete;
~ScopedWindow() { connection_->DestroyWindow(id_); }
Window id() const { return id_; }
private:
const raw_ptr<Connection> connection_;
Window id_;
};
} // namespace
TEST(GeometryCacheTest, ConstructAndDestruct) {
Connection* connection = Connection::Get();
ScopedWindow window(connection, connection->default_root(),
gfx::Rect(12, 34, 56, 78));
GeometryCache geometry_cache(
connection, window.id(),
base::BindRepeating([](const std::optional<gfx::Rect>& old_bounds,
const gfx::Rect& new_bounds) {}));
}
TEST(GeometryCacheTest, GetBounds) {
Connection* connection = Connection::Get();
gfx::Rect bounds(12, 34, 56, 78);
ScopedWindow window(connection, connection->default_root(), bounds);
GeometryCache geometry_cache(
connection, window.id(),
base::BindRepeating([](const std::optional<gfx::Rect>& old_bounds,
const gfx::Rect& new_bounds) {}));
// Calling GetBoundsPx() should return the initial bounds of the window.
EXPECT_EQ(geometry_cache.GetBoundsPx(), bounds);
// Simulate setting window geometry.
gfx::Rect new_geometry(10, 10, 100, 100);
connection->ConfigureWindow({
.window = window.id(),
.x = new_geometry.x(),
.y = new_geometry.y(),
.width = new_geometry.width(),
.height = new_geometry.height(),
});
connection->Sync();
connection->DispatchAll();
// The geometry cache should now reflect the new geometry.
EXPECT_EQ(geometry_cache.GetBoundsPx(), new_geometry);
}
TEST(GeometryCacheTest, BoundsChangedCallback) {
Connection* connection = Connection::Get();
gfx::Rect bounds(12, 34, 56, 78);
ScopedWindow window(connection, connection->default_root(), bounds);
std::optional<gfx::Rect> last_bounds;
auto bounds_changed_callback = [](std::optional<gfx::Rect>* last_bounds,
const std::optional<gfx::Rect>& old_bounds,
const gfx::Rect& new_bounds) {
*last_bounds = new_bounds;
};
GeometryCache geometry_cache(
connection, window.id(),
base::BindRepeating(bounds_changed_callback, &last_bounds));
// Initially, no bounds change should have been recorded.
EXPECT_FALSE(last_bounds.has_value());
EXPECT_EQ(geometry_cache.GetBoundsPx(), bounds);
// `last_bounds` should have gotten set after the call to GetBoundsPx().
// Reset it for the next part of the test.
EXPECT_TRUE(last_bounds.has_value());
last_bounds = std::nullopt;
// Simulate setting window geometry.
gfx::Rect new_geometry(10, 10, 100, 100);
connection->ConfigureWindow({
.window = window.id(),
.x = new_geometry.x(),
.y = new_geometry.y(),
.width = new_geometry.width(),
.height = new_geometry.height(),
});
connection->Sync();
connection->DispatchAll();
// The callback should have been invoked with the new geometry.
ASSERT_TRUE(last_bounds.has_value());
EXPECT_EQ(last_bounds.value(), new_geometry);
}
TEST(GeometryCacheTest, NestedWindows) {
Connection* connection = Connection::Get();
gfx::Rect parent_bounds(12, 34, 56, 78);
ScopedWindow parent_window(connection, connection->default_root(),
parent_bounds);
gfx::Rect child_bounds(5, 5, 30, 30);
ScopedWindow child_window(connection, parent_window.id(), child_bounds);
// Set up the GeometryCache for the child window.
GeometryCache child_geometry_cache(
connection, child_window.id(),
base::BindRepeating([](const std::optional<gfx::Rect>& old_bounds,
const gfx::Rect& new_bounds) {}));
// Initial bounds should be the sum of child window position and parent window
// position.
EXPECT_EQ(child_geometry_cache.GetBoundsPx(),
child_bounds + gfx::Vector2d(parent_bounds.x(), parent_bounds.y()));
// Simulate moving the child window within the parent window.
gfx::Rect new_child_bounds(10, 10, 40, 40);
connection->ConfigureWindow({
.window = child_window.id(),
.x = new_child_bounds.x(),
.y = new_child_bounds.y(),
.width = new_child_bounds.width(),
.height = new_child_bounds.height(),
});
connection->Sync();
connection->DispatchAll();
// Verify the child window's new bounds are updated correctly.
EXPECT_EQ(
child_geometry_cache.GetBoundsPx(),
new_child_bounds + gfx::Vector2d(parent_bounds.x(), parent_bounds.y()));
}
} // namespace x11
|