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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/compose/compose_dialog_view.h"
#include "components/compose/core/browser/config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
std::string ParamToTestSuffix(
::testing::TestParamInfo<compose::DialogFallbackPositioningStrategy>
paramInfo) {
switch (paramInfo.param) {
case compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen:
return "kShiftUpUntilMaxSizeIsOnscreen";
case compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect:
return "kCenterOnAnchorRect";
case compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen:
return "kShiftUpUntilOnscreen";
default:
return "InvalidStrategy";
}
}
gfx::Size DefaultWidgetSize() {
return gfx::Size(448, 220);
}
std::optional<gfx::Rect> DefaultBrowserWindow() {
return std::make_optional<gfx::Rect>(100, 100, 600, 500);
}
std::optional<gfx::Rect> SmallBrowserWindow() {
return std::make_optional<gfx::Rect>(100, 100, 60, 50);
}
gfx::Rect DefaultScreenWorkArea() {
return gfx::Rect(0, 0, 1000, 1000);
}
gfx::Size DefaultAnchorSize() {
return gfx::Size(400, 400);
}
gfx::Size SmallAnchorSize() {
return gfx::Size(200, 50);
}
} // namespace
class ComposeDialogViewTest : public testing::TestWithParam<
compose::DialogFallbackPositioningStrategy> {
protected:
void SetUp() override {
auto& config = compose::GetMutableConfigForTesting();
config.stay_in_window_bounds = false;
config.positioning_strategy = GetParam();
}
compose::DialogFallbackPositioningStrategy FallbackPositioningStrategy() {
return GetParam();
}
void TearDown() override { compose::ResetConfigForTesting(); }
};
TEST_P(ComposeDialogViewTest, TestLayoutBelow) {
// Set up params such that the compose dialog will fit in the optimal position
// directly below and left aligned with the anchor.
gfx::Rect anchor_bounds{{100, 100}, DefaultAnchorSize()};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be onscreen.
EXPECT_TRUE(DefaultScreenWorkArea().Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewTest, TestLayoutAbove) {
// Set up params such that the compose dialog will fit in the optimal position
// directly below and left aligned with the anchor.
gfx::Rect anchor_bounds{{100, 500}, DefaultAnchorSize()};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be onscreen.
EXPECT_TRUE(DefaultScreenWorkArea().Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged above the anchor,
EXPECT_EQ(anchor_bounds.y(),
bounds.bottom() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewTest, TestAnchorOnRight) {
gfx::Rect anchor_bounds({800, 100}, DefaultAnchorSize());
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be onscreen. In this case, that means that the bounds rect will be
// shifted to the left to remain onscreen..
EXPECT_TRUE(DefaultScreenWorkArea().Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewTest, TestAnchorOnLeft) {
gfx::Rect anchor_bounds({-100, 100}, DefaultAnchorSize());
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be onscreen. In this case, that means that the bounds rect will be
// shifted to the left to remain onscreen..
EXPECT_TRUE(DefaultScreenWorkArea().Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewTest, TestFallbackVertical) {
// Too big to fit the dialog entirely on any side.
gfx::Rect anchor_bounds{{100, 100}, gfx::Size(800, 800)};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
switch (FallbackPositioningStrategy()) {
case compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect:
EXPECT_EQ(bounds.CenterPoint(), anchor_bounds.CenterPoint());
break;
case compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen:
// Must be |padding| away from the bottom of the screen.
EXPECT_EQ(bounds.bottom(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding);
break;
case compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen:
// fallthrough - invalid should be the same as this.
default:
// Must be at least |padding| away from the bottom of the screen.
EXPECT_LT(bounds.bottom(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding);
// Should always be rendered a fixed position from the work area bottom
// (since max height is fixed).
EXPECT_EQ(bounds.y(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding -
ComposeDialogView::kComposeMaxDialogHeightPx);
}
}
INSTANTIATE_TEST_SUITE_P(
FallbackPositioningStrategy,
ComposeDialogViewTest,
testing::ValuesIn(
{compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect,
compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen,
compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen,
// 999 should behave like the default
static_cast<compose::DialogFallbackPositioningStrategy>(999)}),
&ParamToTestSuffix);
class ComposeDialogViewInWindowBoundsTest : public ComposeDialogViewTest {
void SetUp() override {
ComposeDialogViewTest::SetUp();
auto& config = compose::GetMutableConfigForTesting();
config.stay_in_window_bounds = true;
}
};
TEST_P(ComposeDialogViewInWindowBoundsTest, TestLayoutBelow) {
// Set up params such that the compose dialog will fit in the optimal position
// directly below and left aligned with the anchor.
gfx::Rect anchor_bounds{{100, 100}, SmallAnchorSize()};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be within parent window.
EXPECT_TRUE(DefaultBrowserWindow()->Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewInWindowBoundsTest, TestLayoutAbove) {
// Set up params such that the compose dialog will fit in the optimal position
// directly below and left aligned with the anchor.
gfx::Rect anchor_bounds{{100, 500}, DefaultAnchorSize()};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be within parent window.
EXPECT_TRUE(DefaultBrowserWindow()->Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged above the anchor,
EXPECT_EQ(anchor_bounds.y(),
bounds.bottom() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewInWindowBoundsTest, TestAnchorOnRight) {
gfx::Rect anchor_bounds({800, 100}, SmallAnchorSize());
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be within parent window. In this case, that means that the bounds rect
// will be shifted to the left to remain so.
EXPECT_TRUE(DefaultBrowserWindow()->Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewInWindowBoundsTest, TestAnchorOnLeft) {
gfx::Rect anchor_bounds({-100, 100}, SmallAnchorSize());
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
// Must be within parent window. In this case, that means that the bounds rect
// will be shifted to remain so.
EXPECT_TRUE(DefaultBrowserWindow()->Contains(bounds));
// Must not change the size
EXPECT_EQ(bounds.size(), DefaultWidgetSize());
// Doesn't matter which param in this case, since this is not a fallback.
// Assert that we are arranged below the anchor,
EXPECT_EQ(anchor_bounds.bottom(),
bounds.y() + ComposeDialogView::kComposeDialogAnchorPadding);
}
TEST_P(ComposeDialogViewInWindowBoundsTest, TestFallbackVertical) {
// Too big to fit the dialog entirely on any side.
gfx::Rect anchor_bounds{{100, 100}, gfx::Size(500, 400)};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
DefaultBrowserWindow());
switch (FallbackPositioningStrategy()) {
case compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect:
EXPECT_EQ(bounds.CenterPoint(), anchor_bounds.CenterPoint());
break;
case compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen:
// No padding is applied to the browser window bounds.
EXPECT_EQ(bounds.bottom(), DefaultBrowserWindow()->bottom());
break;
case compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen:
// fallthrough - invalid should be the same as this.
default:
// No padding is applied to the browser window bounds.
EXPECT_LT(bounds.bottom(), DefaultBrowserWindow()->bottom());
// Should always be rendered a fixed position from the work area bottom
// (since max height is fixed).
EXPECT_EQ(bounds.y(), DefaultBrowserWindow()->bottom() -
ComposeDialogView::kComposeMaxDialogHeightPx);
}
}
// This should behave exactly as if the browser window were configured to be
// ignored, as it is too small.
TEST_P(ComposeDialogViewInWindowBoundsTest,
TestFallbackVerticalWhenWindowIsTooSmall) {
// Too big to fit the dialog entirely on any side.
gfx::Rect anchor_bounds{{100, 100}, gfx::Size(800, 800)};
gfx::Rect bounds = ComposeDialogView::CalculateBubbleBounds(
DefaultScreenWorkArea(), DefaultWidgetSize(), anchor_bounds,
SmallBrowserWindow());
switch (FallbackPositioningStrategy()) {
case compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect:
EXPECT_EQ(bounds.CenterPoint(), anchor_bounds.CenterPoint());
break;
case compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen:
// Must be |padding| away from the bottom of the screen.
EXPECT_EQ(bounds.bottom(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding);
break;
case compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen:
// fallthrough - invalid should be the same as this.
default:
// Must be at least |padding| away from the bottom of the screen.
EXPECT_LT(bounds.bottom(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding);
// Should always be rendered a fixed position from the work area bottom
// (since max height is fixed).
EXPECT_EQ(bounds.y(),
DefaultScreenWorkArea().bottom() -
ComposeDialogView::kComposeDialogWorkAreaPadding -
ComposeDialogView::kComposeMaxDialogHeightPx);
}
}
INSTANTIATE_TEST_SUITE_P(
FallbackPositioningStrategy,
ComposeDialogViewInWindowBoundsTest,
testing::ValuesIn(
{compose::DialogFallbackPositioningStrategy::kCenterOnAnchorRect,
compose::DialogFallbackPositioningStrategy::kShiftUpUntilOnscreen,
compose::DialogFallbackPositioningStrategy::
kShiftUpUntilMaxSizeIsOnscreen,
// 999 should behave like the default
static_cast<compose::DialogFallbackPositioningStrategy>(999)}),
&ParamToTestSuffix);
|