Commit bf6ff2e8 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Fix problems with StyledLabel::CalculatePreferredSize().

Calling GetPreferredSize() on a label which hadn't otherwise computed a layout
would return an empty size, which seems wrong.

Calling GetHeightForWidth() (a const method) could change the results of
GetPreferredSize(), a violation of logical constness.

Bug: none
Change-Id: I2729b9d83ffa94e368fb6553e9ead4b2646e01c2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1884360
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710622}
parent d2849642
......@@ -213,8 +213,8 @@ void StyledLabel::GetAccessibleNodeData(ui::AXNodeData* node_data) {
}
gfx::Size StyledLabel::CalculatePreferredSize() const {
// TODO(pkasting): This seems suspicious; shouldn't there be a call to
// CalculateLayout(std::numeric_limits<int>::max()) here?
// Respect any existing size. If there is none, default to a single line.
CalculateLayout((width() == 0) ? std::numeric_limits<int>::max() : width());
return layout_size_info_.total_size;
}
......
......@@ -776,4 +776,34 @@ TEST_F(StyledLabelTest, SizeToFit) {
EXPECT_EQ(1000, styled()->children().front()->bounds().right());
}
// Verifies that a non-empty label has a preferred size by default.
TEST_F(StyledLabelTest, PreferredSizeNonEmpty) {
const std::string text("text");
InitStyledLabel(text);
EXPECT_FALSE(styled()->GetPreferredSize().IsEmpty());
}
// Verifies that GetPreferredSize() respects the existing wrapping.
TEST_F(StyledLabelTest, PreferredSizeRespectsWrapping) {
const std::string text("Long text that can be split across lines");
InitStyledLabel(text);
gfx::Size size = styled()->GetPreferredSize();
size.set_width(size.width() / 2);
size.set_height(styled()->GetHeightForWidth(size.width()));
styled()->SetSize(size);
styled()->Layout();
const gfx::Size new_size = styled()->GetPreferredSize();
EXPECT_LE(new_size.width(), size.width());
EXPECT_EQ(new_size.height(), size.height());
}
// Verifies that calling a const method does not change the preferred size.
TEST_F(StyledLabelTest, PreferredSizeAcrossConstCall) {
const std::string text("Long text that can be split across lines");
InitStyledLabel(text);
const gfx::Size size = styled()->GetPreferredSize();
styled()->GetHeightForWidth(size.width() / 2);
EXPECT_EQ(size, styled()->GetPreferredSize());
}
} // namespace views
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment