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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
|
// Copyright 2019 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/views/layout/layout_manager_base.h"
#include <algorithm>
#include "base/memory/raw_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
namespace views {
namespace {
constexpr int kChildViewPadding = 5;
constexpr gfx::Size kMinimumSize(40, 50);
constexpr gfx::Size kPreferredSize(100, 90);
constexpr gfx::Size kSquarishSize(10, 11);
constexpr gfx::Size kLongSize(20, 8);
constexpr gfx::Size kTallSize(4, 22);
constexpr gfx::Size kLargeSize(30, 28);
// Dummy class that minimally implements LayoutManagerBase for basic
// functionality testing.
class TestLayoutManagerBase : public LayoutManagerBase {
public:
std::vector<const View*> GetIncludedChildViews() const {
std::vector<const View*> included;
std::ranges::copy_if(host_view()->children(), std::back_inserter(included),
[=, this](const View* child) {
return IsChildIncludedInLayout(child);
});
return included;
}
void OverrideProposedLayout(const ProposedLayout& forced_layout) {
forced_layout_ = forced_layout;
InvalidateHost(true);
}
// LayoutManagerBase:
ProposedLayout CalculateProposedLayout(
const SizeBounds& size_bounds) const override {
if (forced_layout_) {
return *forced_layout_;
}
ProposedLayout layout;
layout.host_size.set_width(std::clamp<SizeBound>(size_bounds.width(),
kMinimumSize.width(),
kPreferredSize.width())
.value());
layout.host_size.set_height(std::clamp<SizeBound>(size_bounds.height(),
kMinimumSize.height(),
kPreferredSize.height())
.value());
return layout;
}
void LayoutImpl() override {
++layout_count_;
LayoutManagerBase::LayoutImpl();
}
size_t layout_count() const { return layout_count_; }
private:
// If specified, will always return this layout.
std::optional<ProposedLayout> forced_layout_;
size_t layout_count_ = 0;
};
// This layout layout lays out included child views in the upper-left of the
// host view with kChildViewPadding around them. Views that will not fit are
// made invisible. Child views are expected to overlap as they all have the
// same top-left corner.
class MockLayoutManagerBase : public LayoutManagerBase {
public:
using LayoutManagerBase::AddOwnedLayout;
using LayoutManagerBase::InvalidateHost;
int num_invalidations() const { return num_invalidations_; }
int num_layouts_generated() const { return num_layouts_generated_; }
// LayoutManagerBase:
ProposedLayout CalculateProposedLayout(
const SizeBounds& size_bounds) const override {
ProposedLayout layout;
layout.host_size = {kChildViewPadding, kChildViewPadding};
for (views::View* it : host_view()->children()) {
if (!IsChildIncludedInLayout(it)) {
continue;
}
const gfx::Size preferred_size = it->GetPreferredSize({});
bool visible = false;
gfx::Rect bounds;
const int required_width = preferred_size.width() + 2 * kChildViewPadding;
const int required_height =
preferred_size.height() + 2 * kChildViewPadding;
if ((required_width <= size_bounds.width()) &&
(required_height <= size_bounds.height())) {
visible = true;
bounds = gfx::Rect(kChildViewPadding, kChildViewPadding,
preferred_size.width(), preferred_size.height());
layout.host_size.set_width(std::max(
layout.host_size.width(), bounds.right() + kChildViewPadding));
layout.host_size.set_height(std::max(
layout.host_size.height(), bounds.bottom() + kChildViewPadding));
}
layout.child_layouts.push_back({it, visible, bounds});
}
++num_layouts_generated_;
return layout;
}
void OnLayoutChanged() override {
LayoutManagerBase::OnLayoutChanged();
++num_invalidations_;
}
using LayoutManagerBase::ApplyLayout;
private:
mutable int num_layouts_generated_ = 0;
mutable int num_invalidations_ = 0;
};
void ExpectSameViews(const std::vector<const View*>& expected,
const std::vector<const View*>& actual) {
EXPECT_EQ(expected.size(), actual.size());
for (size_t i = 0; i < expected.size(); ++i) {
EXPECT_EQ(expected[i], actual[i]);
}
}
} // namespace
TEST(LayoutManagerBaseTest, GetMinimumSize) {
TestLayoutManagerBase layout;
EXPECT_EQ(kMinimumSize, layout.GetMinimumSize(nullptr));
}
TEST(LayoutManagerBaseTest, GetPreferredSize) {
TestLayoutManagerBase layout;
EXPECT_EQ(kPreferredSize, layout.GetPreferredSize(nullptr));
}
TEST(LayoutManagerBaseTest, GetPreferredHeightForWidth) {
constexpr int kWidth = 45;
TestLayoutManagerBase layout;
EXPECT_EQ(kPreferredSize.height(),
layout.GetPreferredHeightForWidth(nullptr, kWidth));
}
TEST(LayoutManagerBaseTest, Installed) {
auto layout_ptr = std::make_unique<TestLayoutManagerBase>();
LayoutManagerBase* layout = layout_ptr.get();
EXPECT_EQ(nullptr, layout->host_view());
View view;
view.SetLayoutManager(std::move(layout_ptr));
EXPECT_EQ(&view, layout->host_view());
}
TEST(LayoutManagerBaseTest, SetChildIncludedInLayout) {
View view;
View* const child1 = view.AddChildView(std::make_unique<View>());
View* const child2 = view.AddChildView(std::make_unique<View>());
View* const child3 = view.AddChildView(std::make_unique<View>());
auto layout_ptr = std::make_unique<TestLayoutManagerBase>();
TestLayoutManagerBase* layout = layout_ptr.get();
view.SetLayoutManager(std::move(layout_ptr));
// All views should be present.
ExpectSameViews({child1, child2, child3}, layout->GetIncludedChildViews());
// Remove one.
child2->SetProperty(kViewIgnoredByLayoutKey, true);
ExpectSameViews({child1, child3}, layout->GetIncludedChildViews());
// Remove another.
child1->SetProperty(kViewIgnoredByLayoutKey, true);
ExpectSameViews({child3}, layout->GetIncludedChildViews());
// Removing it again should have no effect.
child1->SetProperty(kViewIgnoredByLayoutKey, true);
ExpectSameViews({child3}, layout->GetIncludedChildViews());
// Add one back.
child1->SetProperty(kViewIgnoredByLayoutKey, false);
ExpectSameViews({child1, child3}, layout->GetIncludedChildViews());
// Adding it back again should have no effect.
child1->SetProperty(kViewIgnoredByLayoutKey, false);
ExpectSameViews({child1, child3}, layout->GetIncludedChildViews());
// Add the other view back.
child2->SetProperty(kViewIgnoredByLayoutKey, false);
ExpectSameViews({child1, child2, child3}, layout->GetIncludedChildViews());
}
TEST(LayoutManagerBaseTest, InvalidateHost_NotInstalled) {
MockLayoutManagerBase root_layout;
MockLayoutManagerBase* const child1 =
root_layout.AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
MockLayoutManagerBase* const child2 =
root_layout.AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
MockLayoutManagerBase* const grandchild =
child1->AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
root_layout.InvalidateHost(false);
EXPECT_EQ(0, root_layout.num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
child1->InvalidateHost(false);
EXPECT_EQ(0, root_layout.num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
child2->InvalidateHost(false);
EXPECT_EQ(0, root_layout.num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
grandchild->InvalidateHost(false);
EXPECT_EQ(0, root_layout.num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
root_layout.InvalidateHost(true);
EXPECT_EQ(1, root_layout.num_invalidations());
EXPECT_EQ(1, child1->num_invalidations());
EXPECT_EQ(1, child2->num_invalidations());
EXPECT_EQ(1, grandchild->num_invalidations());
child1->InvalidateHost(true);
EXPECT_EQ(2, root_layout.num_invalidations());
EXPECT_EQ(2, child1->num_invalidations());
EXPECT_EQ(2, child2->num_invalidations());
EXPECT_EQ(2, grandchild->num_invalidations());
child2->InvalidateHost(true);
EXPECT_EQ(3, root_layout.num_invalidations());
EXPECT_EQ(3, child1->num_invalidations());
EXPECT_EQ(3, child2->num_invalidations());
EXPECT_EQ(3, grandchild->num_invalidations());
grandchild->InvalidateHost(true);
EXPECT_EQ(4, root_layout.num_invalidations());
EXPECT_EQ(4, child1->num_invalidations());
EXPECT_EQ(4, child2->num_invalidations());
EXPECT_EQ(4, grandchild->num_invalidations());
}
TEST(LayoutManagerBaseTest, InvalidateHost_Installed) {
View view;
MockLayoutManagerBase* const root_layout =
view.SetLayoutManager(std::make_unique<MockLayoutManagerBase>());
MockLayoutManagerBase* const child1 =
root_layout->AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
MockLayoutManagerBase* const child2 =
root_layout->AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
MockLayoutManagerBase* const grandchild =
child1->AddOwnedLayout(std::make_unique<MockLayoutManagerBase>());
root_layout->InvalidateHost(false);
EXPECT_EQ(0, root_layout->num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
child1->InvalidateHost(false);
EXPECT_EQ(0, root_layout->num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
child2->InvalidateHost(false);
EXPECT_EQ(0, root_layout->num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
grandchild->InvalidateHost(false);
EXPECT_EQ(0, root_layout->num_invalidations());
EXPECT_EQ(0, child1->num_invalidations());
EXPECT_EQ(0, child2->num_invalidations());
EXPECT_EQ(0, grandchild->num_invalidations());
root_layout->InvalidateHost(true);
EXPECT_EQ(1, root_layout->num_invalidations());
EXPECT_EQ(1, child1->num_invalidations());
EXPECT_EQ(1, child2->num_invalidations());
EXPECT_EQ(1, grandchild->num_invalidations());
child1->InvalidateHost(true);
EXPECT_EQ(2, root_layout->num_invalidations());
EXPECT_EQ(2, child1->num_invalidations());
EXPECT_EQ(2, child2->num_invalidations());
EXPECT_EQ(2, grandchild->num_invalidations());
child2->InvalidateHost(true);
EXPECT_EQ(3, root_layout->num_invalidations());
EXPECT_EQ(3, child1->num_invalidations());
EXPECT_EQ(3, child2->num_invalidations());
EXPECT_EQ(3, grandchild->num_invalidations());
grandchild->InvalidateHost(true);
EXPECT_EQ(4, root_layout->num_invalidations());
EXPECT_EQ(4, child1->num_invalidations());
EXPECT_EQ(4, child2->num_invalidations());
EXPECT_EQ(4, grandchild->num_invalidations());
}
// Test LayoutManager functionality of LayoutManagerBase:
namespace {
// Base for tests that evaluate the LayoutManager functionality of
// LayoutManagerBase (rather than the LayoutManagerBase-specific behavior).
class LayoutManagerBaseManagerTest : public testing::Test {
public:
void SetUp() override {
host_view_ = std::make_unique<View>();
layout_manager_ =
host_view_->SetLayoutManager(std::make_unique<MockLayoutManagerBase>());
}
View* AddChildView(gfx::Size preferred_size) {
auto child = std::make_unique<StaticSizedView>(preferred_size);
return host_view_->AddChildView(std::move(child));
}
// Directly calls the layout manager's `Layout()` method, as if from within
// `View::Layout()` or an overridden version of the function.
void DoLayoutManagerLayout() { layout_manager_->Layout(host_view_.get()); }
View* host_view() { return host_view_.get(); }
MockLayoutManagerBase* layout_manager() { return layout_manager_; }
View* child(int index) { return host_view_->children().at(index); }
private:
std::unique_ptr<View> host_view_;
raw_ptr<MockLayoutManagerBase> layout_manager_;
};
} // namespace
TEST_F(LayoutManagerBaseManagerTest, ProposedLayout_GetLayoutFor) {
AddChildView(gfx::Size());
AddChildView(gfx::Size());
AddChildView(gfx::Size());
ProposedLayout layout;
constexpr gfx::Rect kChild1Bounds(3, 4, 10, 15);
layout.child_layouts.push_back({child(0), true, kChild1Bounds});
layout.child_layouts.push_back({child(1), false});
const ProposedLayout& const_layout = layout;
EXPECT_EQ(&layout.child_layouts[0], layout.GetLayoutFor(child(0)));
EXPECT_EQ(&const_layout.child_layouts[0],
const_layout.GetLayoutFor(child(0)));
EXPECT_EQ(&layout.child_layouts[1], layout.GetLayoutFor(child(1)));
EXPECT_EQ(&const_layout.child_layouts[1],
const_layout.GetLayoutFor(child(1)));
EXPECT_EQ(nullptr, layout.GetLayoutFor(child(2)));
EXPECT_EQ(nullptr, const_layout.GetLayoutFor(child(2)));
}
TEST_F(LayoutManagerBaseManagerTest, ApplyLayout) {
AddChildView(gfx::Size());
AddChildView(gfx::Size());
AddChildView(gfx::Size());
// We don't want to set the size of the host view because it will trigger a
// superfluous layout, so we'll just keep the old size and make sure it
// doesn't change.
const gfx::Size old_size = host_view()->size();
ProposedLayout layout;
// This should be ignored.
layout.host_size = {123, 456};
// Set the child visibility and bounds.
constexpr gfx::Rect kChild1Bounds(3, 4, 10, 15);
constexpr gfx::Rect kChild3Bounds(20, 21, 12, 14);
layout.child_layouts.push_back({child(0), true, kChild1Bounds});
layout.child_layouts.push_back({child(1), false});
layout.child_layouts.push_back({child(2), true, kChild3Bounds});
layout_manager()->ApplyLayout(layout);
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(kChild1Bounds, child(0)->bounds());
EXPECT_FALSE(child(1)->GetVisible());
EXPECT_TRUE(child(2)->GetVisible());
EXPECT_EQ(kChild3Bounds, child(2)->bounds());
EXPECT_EQ(old_size, host_view()->size());
}
TEST_F(LayoutManagerBaseManagerTest, ApplyLayout_SkipsOmittedViews) {
AddChildView(gfx::Size());
AddChildView(gfx::Size());
AddChildView(gfx::Size());
ProposedLayout layout;
// Set the child visibility and bounds.
constexpr gfx::Rect kChild1Bounds(3, 4, 10, 15);
constexpr gfx::Rect kChild2Bounds(1, 2, 3, 4);
layout.child_layouts.push_back({child(0), true, kChild1Bounds});
layout.child_layouts.push_back({child(2), false});
// We'll set the second child separately.
child(1)->SetVisible(true);
child(1)->SetBoundsRect(kChild2Bounds);
layout_manager()->ApplyLayout(layout);
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(kChild1Bounds, child(0)->bounds());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(kChild2Bounds, child(1)->bounds());
EXPECT_FALSE(child(2)->GetVisible());
}
TEST_F(LayoutManagerBaseManagerTest, Install) {
EXPECT_EQ(host_view(), layout_manager()->host_view());
}
TEST_F(LayoutManagerBaseManagerTest, GetMinimumSize) {
AddChildView(kSquarishSize);
AddChildView(kLongSize);
AddChildView(kTallSize);
EXPECT_EQ(gfx::Size(kChildViewPadding, kChildViewPadding),
host_view()->GetMinimumSize());
}
TEST_F(LayoutManagerBaseManagerTest, GetPreferredSize) {
AddChildView(kSquarishSize);
AddChildView(kLongSize);
AddChildView(kTallSize);
const gfx::Size expected(kLongSize.width() + 2 * kChildViewPadding,
kTallSize.height() + 2 * kChildViewPadding);
EXPECT_EQ(expected, host_view()->GetPreferredSize({}));
}
TEST_F(LayoutManagerBaseManagerTest, GetPreferredHeightForWidth) {
AddChildView(kSquarishSize);
AddChildView(kLargeSize);
const int expected = kSquarishSize.height() + 2 * kChildViewPadding;
EXPECT_EQ(expected,
layout_manager()->GetPreferredHeightForWidth(host_view(), 20));
EXPECT_EQ(1, layout_manager()->num_layouts_generated());
layout_manager()->GetPreferredHeightForWidth(host_view(), 20);
EXPECT_EQ(1, layout_manager()->num_layouts_generated());
layout_manager()->GetPreferredHeightForWidth(host_view(), 25);
EXPECT_EQ(2, layout_manager()->num_layouts_generated());
}
TEST_F(LayoutManagerBaseManagerTest, InvalidateLayout) {
// Some invalidation could have been triggered during setup.
const int old_num_invalidations = layout_manager()->num_invalidations();
host_view()->InvalidateLayout();
EXPECT_EQ(old_num_invalidations + 1, layout_manager()->num_invalidations());
}
TEST_F(LayoutManagerBaseManagerTest, Layout) {
constexpr gfx::Point kUpperLeft(kChildViewPadding, kChildViewPadding);
AddChildView(kSquarishSize);
AddChildView(kLongSize);
AddChildView(kTallSize);
// This should fit all of the child views and trigger layout.
host_view()->SetSize({40, 40});
EXPECT_EQ(1, layout_manager()->num_layouts_generated());
EXPECT_EQ(gfx::Rect(kUpperLeft, child(0)->GetPreferredSize({})),
child(0)->bounds());
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(gfx::Rect(kUpperLeft, child(1)->GetPreferredSize({})),
child(1)->bounds());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(gfx::Rect(kUpperLeft, child(2)->GetPreferredSize({})),
child(2)->bounds());
EXPECT_TRUE(child(2)->GetVisible());
// This should drop out some children.
host_view()->SetSize({25, 25});
EXPECT_EQ(2, layout_manager()->num_layouts_generated());
EXPECT_EQ(gfx::Rect(kUpperLeft, child(0)->GetPreferredSize({})),
child(0)->bounds());
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_FALSE(child(1)->GetVisible());
EXPECT_FALSE(child(2)->GetVisible());
}
TEST_F(LayoutManagerBaseManagerTest, IgnoresChildWithViewIgnoredByLayoutKey) {
AddChildView(kSquarishSize);
AddChildView(kLongSize);
AddChildView(kTallSize);
child(1)->SetProperty(kViewIgnoredByLayoutKey, true);
child(1)->SetSize(kLargeSize);
// Makes enough room for all views, and triggers layout.
host_view()->SetSize({50, 50});
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_EQ(kLargeSize, child(1)->size());
EXPECT_EQ(child(2)->GetPreferredSize({}), child(2)->size());
}
TEST_F(LayoutManagerBaseManagerTest, ViewVisibilitySet) {
AddChildView(kSquarishSize);
AddChildView(kLongSize);
AddChildView(kTallSize);
child(1)->SetVisible(false);
// Makes enough room for all views, and triggers layout.
host_view()->SetSize({50, 50});
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_FALSE(child(1)->GetVisible());
EXPECT_TRUE(child(2)->GetVisible());
EXPECT_EQ(child(2)->GetPreferredSize({}), child(2)->size());
// Turn the second child view back on and verify it's present in the layout
// again.
child(1)->SetVisible(true);
test::RunScheduledLayout(host_view());
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(child(1)->GetPreferredSize({}), child(1)->size());
EXPECT_TRUE(child(2)->GetVisible());
EXPECT_EQ(child(2)->GetPreferredSize({}), child(2)->size());
}
TEST_F(LayoutManagerBaseManagerTest, ViewAdded) {
AddChildView(kLongSize);
AddChildView(kTallSize);
// Makes enough room for all views, and triggers layout.
host_view()->SetSize({50, 50});
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(child(1)->GetPreferredSize({}), child(1)->size());
// Add a new view and verify it is being laid out.
View* new_view = AddChildView(kSquarishSize);
test::RunScheduledLayout(host_view());
EXPECT_TRUE(new_view->GetVisible());
EXPECT_EQ(new_view->GetPreferredSize({}), new_view->size());
}
TEST_F(LayoutManagerBaseManagerTest, ViewAdded_NotVisible) {
AddChildView(kLongSize);
AddChildView(kTallSize);
// Makes enough room for all views, and triggers layout.
host_view()->SetSize({50, 50});
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(child(1)->GetPreferredSize({}), child(1)->size());
// Add a new view that is not visible and ensure that the layout manager
// doesn't touch it during layout.
View* new_view = new StaticSizedView(kSquarishSize);
new_view->SetVisible(false);
host_view()->AddChildViewRaw(new_view);
test::RunScheduledLayout(host_view());
EXPECT_FALSE(new_view->GetVisible());
}
TEST_F(LayoutManagerBaseManagerTest, ViewRemoved) {
AddChildView(kSquarishSize);
View* const child_view = AddChildView(kLongSize);
AddChildView(kTallSize);
// Makes enough room for all views, and triggers layout.
host_view()->SetSize({50, 50});
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(child(1)->GetPreferredSize({}), child(1)->size());
EXPECT_TRUE(child(2)->GetVisible());
EXPECT_EQ(child(2)->GetPreferredSize({}), child(2)->size());
std::unique_ptr<View> owned_child_view =
host_view()->RemoveChildViewT(child_view);
owned_child_view->SetSize(kLargeSize);
test::RunScheduledLayout(host_view());
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(child(0)->GetPreferredSize({}), child(0)->size());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(child(1)->GetPreferredSize({}), child(1)->size());
EXPECT_TRUE(owned_child_view->GetVisible());
EXPECT_EQ(kLargeSize, owned_child_view->size());
}
class TestIgnoredView : public View {
METADATA_HEADER(TestIgnoredView, View)
public:
TestIgnoredView() {
SetProperty(kViewIgnoredByLayoutKey, true);
SetBoundsRect(kExpectedBounds);
}
static constexpr gfx::Rect kExpectedBounds{1, 2, 10, 40};
};
BEGIN_METADATA(TestIgnoredView)
END_METADATA
// An ignored view's visibility and layout are managed outside of the layout
// manager, whether that's in the view itself or in a manual layout method in
// its parent view.
TEST_F(LayoutManagerBaseManagerTest, IgnoredView) {
View* child_view = AddChildView(kSquarishSize);
test::RunScheduledLayout(host_view());
gfx::Rect host_view_bounds = host_view()->bounds();
gfx::Rect child_view_bounds = child_view->bounds();
TestIgnoredView* ignored_view =
host_view()->AddChildView(std::make_unique<TestIgnoredView>());
ignored_view->SetVisible(true);
test::RunScheduledLayout(host_view());
// The ignored view does not affect the layout of other views.
EXPECT_EQ(host_view()->bounds(), host_view_bounds);
EXPECT_EQ(child_view->bounds(), child_view_bounds);
// The ignored view manages its own layout.
EXPECT_EQ(ignored_view->bounds(), ignored_view->kExpectedBounds);
// The ignored view's visibility shouldn't be changed by the layout manager.
ignored_view->SetVisible(false);
test::RunScheduledLayout(host_view());
EXPECT_EQ(ignored_view->GetVisible(), false);
EXPECT_EQ(ignored_view->bounds(), ignored_view->kExpectedBounds);
ignored_view->SetVisible(true);
test::RunScheduledLayout(host_view());
EXPECT_EQ(ignored_view->GetVisible(), true);
EXPECT_EQ(ignored_view->bounds(), ignored_view->kExpectedBounds);
}
TEST(LayoutManagerBase_ProposedLayoutTest, Equality) {
std::array<View, 3> views;
ProposedLayout a;
ProposedLayout b;
EXPECT_TRUE(a == b);
a.host_size = {1, 2};
EXPECT_FALSE(a == b);
b.host_size = {1, 2};
EXPECT_TRUE(a == b);
a.child_layouts.push_back({&views[0], true, {1, 1, 2, 2}});
EXPECT_FALSE(a == b);
b.child_layouts.push_back(a.child_layouts[0]);
EXPECT_TRUE(a == b);
a.child_layouts[0].visible = false;
EXPECT_FALSE(a == b);
b.child_layouts[0].visible = false;
EXPECT_TRUE(a == b);
b.child_layouts[0].bounds = {0, 0, 3, 3};
// Since |visible| == false, changing bounds doesn't change anything.
EXPECT_TRUE(a == b);
a.child_layouts[0].visible = true;
b.child_layouts[0].visible = true;
EXPECT_FALSE(a == b);
a.child_layouts[0].visible = false;
b.child_layouts[0].visible = false;
a.child_layouts.push_back({&views[1], true, {1, 2, 3, 4}});
b.child_layouts.push_back({&views[2], true, {1, 2, 3, 4}});
EXPECT_FALSE(a == b);
b.child_layouts[1].child_view = &views[1];
EXPECT_TRUE(a == b);
}
class LayoutManagerBaseAvailableSizeTest : public testing::Test {
public:
void SetUp() override {
view_ = std::make_unique<View>();
layout_ =
view_->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
}
View* view() { return view_.get(); }
TestLayoutManagerBase* layout() { return layout_; }
private:
std::unique_ptr<View> view_;
raw_ptr<TestLayoutManagerBase> layout_;
};
TEST_F(LayoutManagerBaseAvailableSizeTest, ReturnsCorrectValues) {
const SizeBounds kChild1Bounds(3, 7);
const SizeBounds kChild2Bounds(11, 13);
View* const child1 = view()->AddChildView(std::make_unique<View>());
View* const child2 = view()->AddChildView(std::make_unique<View>());
View not_a_child;
layout()->OverrideProposedLayout(
{{10, 10},
{{child1, true, {1, 1, 1, 1}, kChild1Bounds},
{child2, true, {2, 2, 2, 2}, kChild2Bounds}}});
view()->SizeToPreferredSize();
EXPECT_EQ(kChild1Bounds, view()->GetAvailableSize(child1));
EXPECT_EQ(kChild2Bounds, view()->GetAvailableSize(child2));
EXPECT_EQ(SizeBounds(), view()->GetAvailableSize(¬_a_child));
}
TEST_F(LayoutManagerBaseAvailableSizeTest, AvailableSizesInNestedValuesAdd) {
View* const child = view()->AddChildView(std::make_unique<View>());
View* const grandchild = child->AddChildView(std::make_unique<View>());
auto* const child_layout =
child->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
constexpr gfx::Size kViewSize(18, 17);
constexpr SizeBounds kChildAvailableSize(16, 15);
constexpr gfx::Size kChildSize(13, 12);
constexpr SizeBounds kGrandchildAvailableSize(10, 9);
constexpr gfx::Size kGrandchildSize(3, 2);
layout()->OverrideProposedLayout(
{kViewSize, {{child, true, {{3, 3}, kChildSize}, kChildAvailableSize}}});
child_layout->OverrideProposedLayout({kChildSize,
{{grandchild,
true,
{{2, 2}, kGrandchildSize},
kGrandchildAvailableSize}}});
view()->SizeToPreferredSize();
EXPECT_EQ(kChildAvailableSize, view()->GetAvailableSize(child));
SizeBounds expected;
expected.set_width(kGrandchildAvailableSize.width() +
kChildAvailableSize.width() - kChildSize.width());
expected.set_height(kGrandchildAvailableSize.height() +
kChildAvailableSize.height() - kChildSize.height());
EXPECT_EQ(expected, child->GetAvailableSize(grandchild));
}
TEST_F(LayoutManagerBaseAvailableSizeTest,
PartiallySpecifiedAvailableSizesInNestedLayoutsAddPartially) {
View* const child = view()->AddChildView(std::make_unique<View>());
View* const grandchild = child->AddChildView(std::make_unique<View>());
auto* const child_layout =
child->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
constexpr gfx::Size kViewSize(18, 17);
constexpr SizeBounds kChildAvailableSize(16, SizeBound());
constexpr gfx::Size kChildSize(13, 12);
constexpr SizeBounds kGrandchildAvailableSize(10, 9);
constexpr gfx::Size kGrandchildSize(3, 2);
layout()->OverrideProposedLayout(
{kViewSize, {{child, true, {{3, 3}, kChildSize}, kChildAvailableSize}}});
child_layout->OverrideProposedLayout({kChildSize,
{{grandchild,
true,
{{2, 2}, kGrandchildSize},
kGrandchildAvailableSize}}});
view()->SizeToPreferredSize();
EXPECT_EQ(kChildAvailableSize, view()->GetAvailableSize(child));
SizeBounds expected;
expected.set_width(kGrandchildAvailableSize.width() +
kChildAvailableSize.width() - kChildSize.width());
expected.set_height(kGrandchildAvailableSize.height());
EXPECT_EQ(expected, child->GetAvailableSize(grandchild));
}
TEST_F(LayoutManagerBaseAvailableSizeTest,
MismatchedAvailableSizesInNestedLayoutsDoNotAdd) {
View* const child = view()->AddChildView(std::make_unique<View>());
View* const grandchild = child->AddChildView(std::make_unique<View>());
auto* const child_layout =
child->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
constexpr gfx::Size kViewSize(18, 17);
constexpr SizeBounds kChildAvailableSize(16, SizeBound());
constexpr gfx::Size kChildSize(13, 12);
constexpr SizeBounds kGrandchildAvailableSize(SizeBound(), 9);
constexpr gfx::Size kGrandchildSize(3, 2);
layout()->OverrideProposedLayout(
{kViewSize, {{child, true, {{3, 3}, kChildSize}, kChildAvailableSize}}});
child_layout->OverrideProposedLayout({kChildSize,
{{grandchild,
true,
{{2, 2}, kGrandchildSize},
kGrandchildAvailableSize}}});
view()->SizeToPreferredSize();
EXPECT_EQ(kChildAvailableSize, view()->GetAvailableSize(child));
EXPECT_EQ(kGrandchildAvailableSize, child->GetAvailableSize(grandchild));
}
TEST_F(LayoutManagerBaseAvailableSizeTest,
AvaialbleSizeChangeTriggersDescendantLayout) {
View* const child = view()->AddChildView(std::make_unique<View>());
TestLayoutManagerBase* const child_layout =
child->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
View* const grandchild = child->AddChildView(std::make_unique<View>());
TestLayoutManagerBase* const grandchild_layout =
grandchild->SetLayoutManager(std::make_unique<TestLayoutManagerBase>());
View* const great_grandchild =
grandchild->AddChildView(std::make_unique<View>());
TestLayoutManagerBase* const great_grandchild_layout =
great_grandchild->SetLayoutManager(
std::make_unique<TestLayoutManagerBase>());
// Create a default root layout with non-visible, zero-size child with no
// available size.
ProposedLayout root_layout;
root_layout.child_layouts.emplace_back();
root_layout.child_layouts[0].child_view = child;
root_layout.child_layouts[0].available_size = SizeBounds(0, 0);
// Set some default layouts for the rest of the hierarchy.
layout()->OverrideProposedLayout(root_layout);
child_layout->OverrideProposedLayout({{}, {{grandchild, false, {}, {0, 0}}}});
grandchild_layout->OverrideProposedLayout(
{{}, {{great_grandchild, false, {}, {0, 0}}}});
test::RunScheduledLayout(view());
const size_t num_grandchild_layouts = grandchild_layout->layout_count();
const size_t num_great_grandchild_layouts =
great_grandchild_layout->layout_count();
// Set the same rootlayout again as a control. This should not have an effect
// on the layout of the grand- and great-grandchild views.
layout()->OverrideProposedLayout(root_layout);
test::RunScheduledLayout(view());
EXPECT_EQ(num_grandchild_layouts, grandchild_layout->layout_count());
EXPECT_EQ(num_great_grandchild_layouts,
great_grandchild_layout->layout_count());
// Now set the child view to be visible with nonzero size and even larger
// available size. Applying this layout should change the size available to
// all views down the hierarchy, forcing a re-layout.
root_layout.child_layouts[0].visible = true;
root_layout.child_layouts[0].bounds = gfx::Rect(0, 0, 5, 5);
root_layout.child_layouts[0].available_size = SizeBounds(10, 10);
layout()->OverrideProposedLayout(root_layout);
test::RunScheduledLayout(view());
EXPECT_EQ(num_grandchild_layouts + 1, grandchild_layout->layout_count());
EXPECT_EQ(num_great_grandchild_layouts + 1,
great_grandchild_layout->layout_count());
}
using ManualLayoutUtilTest = LayoutManagerBaseManagerTest;
TEST_F(ManualLayoutUtilTest, SetCanBeVisibleForManualLayout) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
EXPECT_TRUE(child(0)->GetVisible());
const gfx::Rect old_child1_bounds = child(0)->bounds();
const gfx::Rect old_child2_bounds = child(1)->bounds();
// Hide the view and remove from the layout.
manual_layout_util.SetViewHidden(child(0), true);
EXPECT_FALSE(child(0)->GetVisible());
DoLayoutManagerLayout();
EXPECT_FALSE(child(0)->GetVisible());
// The second child should now have moved over into the space of the first.
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
// These lines should have no effect as it is already set to hidden.
manual_layout_util.SetViewHidden(child(0), true);
DoLayoutManagerLayout();
EXPECT_FALSE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
// Enable the view in the layout again. This won't immediately cause the view
// to reappear; it may be made visible during the next layout pass.
manual_layout_util.SetViewHidden(child(0), false);
EXPECT_FALSE(child(0)->GetVisible());
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
// The first child should be in its original bounds.
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
// The second child should move back to its original bounds.
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
// Again, this should have no effect as the view is already enabled.
manual_layout_util.SetViewHidden(child(0), false);
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
EXPECT_EQ(old_layouts + 2, layout_manager()->num_layouts_generated());
EXPECT_EQ(old_invalidations + 2, layout_manager()->num_invalidations());
}
TEST_F(ManualLayoutUtilTest, TemporarilyExcludeOneViewFromLayout) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
EXPECT_TRUE(child(0)->GetVisible());
const gfx::Rect old_child1_bounds = child(0)->bounds();
const gfx::Rect old_child2_bounds = child(1)->bounds();
{
auto exclusion = manual_layout_util.TemporarilyExcludeFromLayout(child(0));
DoLayoutManagerLayout();
// Views should have been slid over in the place of the excluded view.
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
// Excluded view should still be visible and in its previous location.
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_invalidations + 1, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 1, layout_manager()->num_layouts_generated());
}
// Exiting the block should cause the layout to be restored.
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 2, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 2, layout_manager()->num_layouts_generated());
}
TEST_F(ManualLayoutUtilTest, TemporarilyExcludeTwoViewsFromLayout) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
EXPECT_TRUE(child(0)->GetVisible());
const gfx::Rect old_child1_bounds = child(0)->bounds();
const gfx::Rect old_child2_bounds = child(1)->bounds();
const gfx::Rect old_child3_bounds = child(2)->bounds();
{
auto exclusion1 = manual_layout_util.TemporarilyExcludeFromLayout(child(0));
auto exclusion2 = manual_layout_util.TemporarilyExcludeFromLayout(child(1));
DoLayoutManagerLayout();
// Views should have been slid over in the place of the excluded view.
EXPECT_EQ(old_child1_bounds, child(2)->bounds());
// Excluded view should still be visible and in its previous location.
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 2, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 1, layout_manager()->num_layouts_generated());
}
// Exiting the block should cause the layout to be restored.
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_TRUE(child(1)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
EXPECT_EQ(old_child3_bounds, child(2)->bounds());
EXPECT_EQ(old_invalidations + 4, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 2, layout_manager()->num_layouts_generated());
}
TEST_F(ManualLayoutUtilTest, ViewStaysExcludedIfAlreadyExcludedByProperty) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
EXPECT_TRUE(child(0)->GetVisible());
const gfx::Rect old_child1_bounds = child(0)->bounds();
child(0)->SetProperty(kViewIgnoredByLayoutKey, true);
test::RunScheduledLayout(host_view());
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
{
// This will have no effect as the view is already excluded.
auto exclusion = manual_layout_util.TemporarilyExcludeFromLayout(child(0));
DoLayoutManagerLayout();
// Views should have been slid over in the place of the excluded view.
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
// Excluded view should still be visible and in its previous location.
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_invalidations, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts, layout_manager()->num_layouts_generated());
}
// Exiting the block should have no effect.
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts, layout_manager()->num_layouts_generated());
}
TEST_F(ManualLayoutUtilTest, ViewHiddenDuringTemporaryExclusion) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
EXPECT_TRUE(child(0)->GetVisible());
const gfx::Rect old_child1_bounds = child(0)->bounds();
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
{
// This will have no effect as the view is already excluded.
auto exclusion = manual_layout_util.TemporarilyExcludeFromLayout(child(0));
DoLayoutManagerLayout();
// Views should have been slid over in the place of the excluded view.
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
// Excluded view should still be visible and in its previous location.
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(0)->bounds());
EXPECT_EQ(old_invalidations + 1, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 1, layout_manager()->num_layouts_generated());
// This changes the view to hidden, which "stacks" with exclusion. Since it
// is a different property, it will invalidate the layout.
manual_layout_util.SetViewHidden(child(0), true);
DoLayoutManagerLayout();
EXPECT_FALSE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 2, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 2, layout_manager()->num_layouts_generated());
}
// Exiting the block will cause an invalidation, but since visibility
// overrides inclusion, it will not change the layout.
DoLayoutManagerLayout();
EXPECT_FALSE(child(0)->GetVisible());
EXPECT_EQ(old_child1_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 3, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 3, layout_manager()->num_layouts_generated());
}
TEST_F(ManualLayoutUtilTest, ViewUnhiddenDuringTemporaryExclusion) {
ManualLayoutUtil manual_layout_util(layout_manager());
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
AddChildView(kPreferredSize);
child(0)->SetVisible(false);
host_view()->SizeToPreferredSize();
test::RunScheduledLayout(host_view());
EXPECT_FALSE(child(0)->GetVisible());
const gfx::Rect old_child2_bounds = child(1)->bounds();
const gfx::Rect old_child3_bounds = child(2)->bounds();
const int old_invalidations = layout_manager()->num_invalidations();
const int old_layouts = layout_manager()->num_layouts_generated();
{
// This will have no effect as the view is already excluded, but will cause
// another invalidation since exclusion and visibility are different
// properties.
auto exclusion = manual_layout_util.TemporarilyExcludeFromLayout(child(0));
DoLayoutManagerLayout();
// Views should have been slid over in the place of the hidden view.
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
// Excluded and hidden view should not be visible.
EXPECT_FALSE(child(0)->GetVisible());
EXPECT_EQ(old_invalidations + 1, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 1, layout_manager()->num_layouts_generated());
// This changes the view to not hidden. While this would not affect the
// layout due to the exclusion, but since it's a different property, it
// still triggers an invalidation.
manual_layout_util.SetViewHidden(child(0), false);
// Note that since the child view is still excluded from the layout, it
// retains its existing state, including visibility.
DoLayoutManagerLayout();
EXPECT_FALSE(child(0)->GetVisible());
EXPECT_EQ(old_child2_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 2, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 2, layout_manager()->num_layouts_generated());
}
// Exiting the block will re-add the view, which is now made visible by the
// layout.
DoLayoutManagerLayout();
EXPECT_TRUE(child(0)->GetVisible());
EXPECT_EQ(old_child2_bounds, child(0)->bounds());
EXPECT_EQ(old_child3_bounds, child(1)->bounds());
EXPECT_EQ(old_invalidations + 3, layout_manager()->num_invalidations());
EXPECT_EQ(old_layouts + 3, layout_manager()->num_layouts_generated());
}
} // namespace views
|