diff options
author | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-01 00:20:21 +0000 |
---|---|---|
committer | levin@chromium.org <levin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-01 00:20:21 +0000 |
commit | 40d41b949d3a0b4238f6c01bd165c648375d979e (patch) | |
tree | 96bbe2b0a75296354133696163691feaff102321 /chrome | |
parent | 5affc15f3a8a51f42debbf37806087dfbc84818f (diff) | |
download | chromium_src-40d41b949d3a0b4238f6c01bd165c648375d979e.zip chromium_src-40d41b949d3a0b4238f6c01bd165c648375d979e.tar.gz chromium_src-40d41b949d3a0b4238f6c01bd165c648375d979e.tar.bz2 |
Make multiline labels respect the inset.
Add a unittest for label.cc
Review URL: http://codereview.chromium.org/8902
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4351 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/common/gfx/chrome_canvas.cc | 5 | ||||
-rw-r--r-- | chrome/test/unit/unittests.vcproj | 4 | ||||
-rw-r--r-- | chrome/views/label.cc | 68 | ||||
-rw-r--r-- | chrome/views/label.h | 16 | ||||
-rw-r--r-- | chrome/views/label_unittest.cc | 422 |
5 files changed, 487 insertions, 28 deletions
diff --git a/chrome/common/gfx/chrome_canvas.cc b/chrome/common/gfx/chrome_canvas.cc index 9fc67c8..064a931 100644 --- a/chrome/common/gfx/chrome_canvas.cc +++ b/chrome/common/gfx/chrome_canvas.cc @@ -279,6 +279,10 @@ void ChromeCanvas::SizeStringInt(const std::wstring& text, b.left = 0; b.top = 0; b.right = *width; + if (b.right == 0 && !text.empty()) { + // Width needs to be at least 1 or else DoDrawText will not resize it. + b.right = 1; + } b.bottom = *height; DoDrawText(dc, text, &b, ComputeFormatFlags(flags) | DT_CALCRECT); endPlatformPaint(); @@ -388,4 +392,3 @@ SkBitmap ChromeCanvas::ExtractBitmap() { device_bitmap.copyTo(&result, SkBitmap::kARGB_8888_Config); return result; } - diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index a0c4bc9..9f685e9 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -346,6 +346,10 @@ > </File> <File + RelativePath="..\..\views\label_unittest.cc" + > + </File> + <File RelativePath="..\..\views\view_unittest.cc" > </File> diff --git a/chrome/views/label.cc b/chrome/views/label.cc index ebc04deab..f543a2d 100644 --- a/chrome/views/label.cc +++ b/chrome/views/label.cc @@ -62,8 +62,7 @@ gfx::Size Label::GetPreferredSize() { } gfx::Insets insets = GetInsets(); - prefsize.Enlarge(insets.left() + insets.right(), - insets.top() + insets.bottom()); + prefsize.Enlarge(insets.width(), insets.height()); return prefsize; } @@ -83,14 +82,14 @@ int Label::ComputeMultiLineFlags() { return flags; } -void Label::Paint(ChromeCanvas* canvas) { - PaintBackground(canvas); - std::wstring paint_text; +void Label::CalculateDrawStringParams( + std::wstring* paint_text, gfx::Rect* text_bounds, int* flags) { + DCHECK(paint_text && text_bounds && flags); if (url_set_) { // TODO(jungshik) : Figure out how to get 'intl.accept_languages' // preference and use it when calling ElideUrl. - paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); + *paint_text = gfx::ElideUrl(url_, font_, width(), std::wstring()); // An URLs is always treated as an LTR text and therefore we should // explicitly mark it as such if the locale is RTL so that URLs containing @@ -102,26 +101,43 @@ void Label::Paint(ChromeCanvas* canvas) { // as an LTR string, even if its containing view does not use an RTL UI // layout. if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT) - l10n_util::WrapStringWithLTRFormatting(&paint_text); + l10n_util::WrapStringWithLTRFormatting(paint_text); } else { - paint_text = text_; + *paint_text = text_; } if (is_multi_line_) { - canvas->DrawStringInt(paint_text, font_, color_, 0, 0, width(), - height(), ComputeMultiLineFlags()); + gfx::Insets insets = GetInsets(); + text_bounds->SetRect(insets.left(), + insets.top(), + width() - insets.width(), + height() - insets.height()); + *flags = ComputeMultiLineFlags(); + } else { + *text_bounds = GetTextBounds(); + *flags = 0; + } +} + +void Label::Paint(ChromeCanvas* canvas) { + PaintBackground(canvas); + std::wstring paint_text; + gfx::Rect text_bounds; + int flags = 0; + CalculateDrawStringParams(&paint_text, &text_bounds, &flags); + canvas->DrawStringInt(paint_text, + font_, + color_, + text_bounds.x(), + text_bounds.y(), + text_bounds.width(), + text_bounds.height(), + flags); + + if (is_multi_line_) { PaintFocusBorder(canvas); } else { - gfx::Rect text_bounds = GetTextBounds(); - - canvas->DrawStringInt(paint_text, - font_, - color_, - text_bounds.x(), - text_bounds.y(), - text_bounds.width(), - text_bounds.height()); - // We'll draw the focus border ourselves so it is around the text. + // We'll draw the focus border ourselves, so it is around the text. if (HasFocus()) canvas->DrawFocusRect(text_bounds.x(), text_bounds.y(), @@ -190,10 +206,12 @@ gfx::Size Label::GetTextSize() { int Label::GetHeightForWidth(int w) { if (is_multi_line_) { + gfx::Insets insets = GetInsets(); + w = std::max<int>(0, w - insets.width()); int h = 0; ChromeCanvas cc(0, 0, true); cc.SizeStringInt(text_, font_, &w, &h, ComputeMultiLineFlags()); - return h; + return h + insets.height(); } return View::GetHeightForWidth(w); @@ -311,12 +329,12 @@ void Label::SetContainsMouse(bool contains_mouse) { gfx::Rect Label::GetTextBounds() { gfx::Size text_size = GetTextSize(); gfx::Insets insets = GetInsets(); - int avail_width = width() - insets.left() - insets.right(); + int avail_width = width() - insets.width(); // Respect the size set by the owner view text_size.set_width(std::min(avail_width, text_size.width())); int text_y = insets.top() + - (height() - text_size.height() - insets.top() - insets.bottom()) / 2; + (height() - text_size.height() - insets.height()) / 2; int text_x; switch (horiz_alignment_) { case ALIGN_LEFT: @@ -346,6 +364,9 @@ void Label::SizeToFit(int max_width) { label_width = std::max(label_width, font_.GetStringWidth(*iter)); } + gfx::Insets insets = GetInsets(); + label_width += insets.width(); + if (max_width > 0) label_width = std::min(label_width, max_width); @@ -374,4 +395,3 @@ bool Label::GetAccessibleState(VARIANT* state) { } } // namespace views - diff --git a/chrome/views/label.h b/chrome/views/label.h index c65dea5..3ac82d1 100644 --- a/chrome/views/label.h +++ b/chrome/views/label.h @@ -8,7 +8,8 @@ #include "chrome/common/gfx/chrome_font.h" #include "chrome/views/view.h" #include "googleurl/src/gurl.h" -#include "SkColor.h" +#include "skia/include/SkColor.h" +#include "testing/gtest/include/gtest/gtest_prod.h" namespace views { @@ -139,11 +140,21 @@ class Label : public View { virtual bool GetAccessibleState(VARIANT* state); private: + // These tests call CalculateDrawStringParams in order to verify the + // calculations done for drawing text. + FRIEND_TEST(LabelTest, DrawSingleLineString); + FRIEND_TEST(LabelTest, DrawMultiLineString); + static ChromeFont GetDefaultFont(); + // Returns parameters to be used for the DrawString call. + void CalculateDrawStringParams(std::wstring* paint_text, + gfx::Rect* text_bounds, + int* flags); + // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise // SetContainsMouse(false) is invoked. - void Label::UpdateContainsMouse(const MouseEvent& event); + void UpdateContainsMouse(const MouseEvent& event); // Updates whether the mouse is contained in the Label. If the new value // differs from the current value, and a mouse over background is specified, @@ -174,4 +185,3 @@ class Label : public View { } // namespace views #endif // CHROME_VIEWS_VIEW_H__ - diff --git a/chrome/views/label_unittest.cc b/chrome/views/label_unittest.cc new file mode 100644 index 0000000..b02ee9b --- /dev/null +++ b/chrome/views/label_unittest.cc @@ -0,0 +1,422 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/string_util.h"
+#include "chrome/common/l10n_util.h"
+#include "chrome/views/border.h"
+#include "chrome/views/label.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace views {
+
+// All text sizing measurements (width and height) should be greater than this.
+const int kMinTextDimension = 4;
+
+TEST(LabelTest, FontProperty) {
+ Label label;
+ std::wstring font_name(L"courier");
+ ChromeFont font = ChromeFont::CreateFont(font_name, 30);
+ label.SetFont(font);
+ ChromeFont font_used = label.GetFont();
+ EXPECT_STREQ(font_name.c_str(), font_used.FontName().c_str());
+ EXPECT_EQ(30, font_used.FontSize());
+}
+
+TEST(LabelTest, TextProperty) {
+ Label label;
+ std::wstring test_text(L"A random string.");
+ label.SetText(test_text);
+ EXPECT_STREQ(test_text.c_str(), label.GetText().c_str());
+}
+
+TEST(LabelTest, UrlProperty) {
+ Label label;
+ std::string my_url("http://www.orkut.com/some/Random/path");
+ GURL url(my_url);
+ label.SetURL(url);
+ EXPECT_STREQ(my_url.c_str(), label.GetURL().spec().c_str());
+ EXPECT_STREQ(UTF8ToWide(my_url).c_str(), label.GetText().c_str());
+}
+
+TEST(LabelTest, ColorProperty) {
+ Label label;
+ SkColor color = SkColorSetARGB(20, 40, 10, 5);
+ label.SetColor(color);
+ EXPECT_EQ(color, label.GetColor());
+}
+
+TEST(LabelTest, AlignmentProperty) {
+ Label label;
+ bool reverse_alignment =
+ l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT;
+
+ label.SetHorizontalAlignment(Label::ALIGN_RIGHT);
+ EXPECT_EQ(
+ reverse_alignment ? Label::ALIGN_LEFT : Label::ALIGN_RIGHT,
+ label.GetHorizontalAlignment());
+ label.SetHorizontalAlignment(Label::ALIGN_LEFT);
+ EXPECT_EQ(
+ reverse_alignment ? Label::ALIGN_RIGHT : Label::ALIGN_LEFT,
+ label.GetHorizontalAlignment());
+ label.SetHorizontalAlignment(Label::ALIGN_CENTER);
+ EXPECT_EQ(Label::ALIGN_CENTER, label.GetHorizontalAlignment());
+}
+
+TEST(LabelTest, MultiLineProperty) {
+ Label label;
+ EXPECT_FALSE(label.IsMultiLine());
+ label.SetMultiLine(true);
+ EXPECT_TRUE(label.IsMultiLine());
+ label.SetMultiLine(false);
+ EXPECT_FALSE(label.IsMultiLine());
+}
+
+TEST(LabelTest, TooltipProperty) {
+ Label label;
+ std::wstring test_text(L"My cool string.");
+ label.SetText(test_text);
+
+ std::wstring tooltip;
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+ EXPECT_STREQ(test_text.c_str(), tooltip.c_str());
+
+ std::wstring tooltip_text(L"The tooltip!");
+ label.SetTooltipText(tooltip_text);
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+ EXPECT_STREQ(tooltip_text.c_str(), tooltip.c_str());
+
+ std::wstring empty_text;
+ label.SetTooltipText(empty_text);
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+ EXPECT_STREQ(test_text.c_str(), tooltip.c_str());
+
+ // Make the label big enough to hold the text
+ // and expect there to be no tooltip.
+ label.SetBounds(0, 0, 1000, 40);
+ EXPECT_FALSE(label.GetTooltipText(0, 0, &tooltip));
+
+ // Verify that setting the tooltip still shows it.
+ label.SetTooltipText(tooltip_text);
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+ EXPECT_STREQ(tooltip_text.c_str(), tooltip.c_str());
+ // Clear out the tooltip.
+ label.SetTooltipText(empty_text);
+
+ // Shrink the bounds and the tooltip should come back.
+ label.SetBounds(0, 0, 1, 1);
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+
+ // Make the label multiline and there is no tooltip again.
+ label.SetMultiLine(true);
+ EXPECT_FALSE(label.GetTooltipText(0, 0, &tooltip));
+
+ // Verify that setting the tooltip still shows it.
+ label.SetTooltipText(tooltip_text);
+ EXPECT_TRUE(label.GetTooltipText(0, 0, &tooltip));
+ EXPECT_STREQ(tooltip_text.c_str(), tooltip.c_str());
+ // Clear out the tooltip.
+ label.SetTooltipText(empty_text);
+}
+
+TEST(LabelTest, Accessibility) {
+ Label label;
+ std::wstring test_text(L"My special text.");
+ label.SetText(test_text);
+
+ VARIANT role;
+ ::VariantInit(&role);
+ EXPECT_TRUE(label.GetAccessibleRole(&role));
+ EXPECT_EQ(VT_I4, role.vt);
+ EXPECT_EQ(ROLE_SYSTEM_TEXT, role.lVal);
+
+ std::wstring name;
+ EXPECT_TRUE(label.GetAccessibleName(&name));
+ EXPECT_STREQ(test_text.c_str(), name.c_str());
+
+ VARIANT state;
+ ::VariantInit(&state);
+ state.vt = VT_I4;
+ state.lVal = 0;
+ EXPECT_TRUE(label.GetAccessibleState(&state));
+ EXPECT_EQ(VT_I4, state.vt);
+ EXPECT_EQ(STATE_SYSTEM_READONLY, state.lVal);
+}
+
+TEST(LabelTest, SingleLineSizing) {
+ Label label;
+ std::wstring test_text(L"A not so random string in one line.");
+ label.SetText(test_text);
+
+ // GetPreferredSize
+ gfx::Size required_size = label.GetPreferredSize();
+ EXPECT_GT(required_size.height(), kMinTextDimension);
+ EXPECT_GT(required_size.width(), kMinTextDimension);
+
+ // Test everything with borders.
+ gfx::Insets border(10, 20, 30, 40);
+ label.SetBorder(Border::CreateEmptyBorder(border.top(),
+ border.left(),
+ border.bottom(),
+ border.right()));
+
+ // GetPreferredSize and borders.
+ label.SetBounds(0, 0, 0, 0);
+ gfx::Size required_size_with_border = label.GetPreferredSize();
+ EXPECT_EQ(required_size_with_border.height(),
+ required_size.height() + border.height());
+ EXPECT_EQ(required_size_with_border.width(),
+ required_size.width() + border.width());
+}
+
+TEST(LabelTest, MultiLineSizing) {
+ Label label;
+ std::wstring test_text(L"A random string\nwith multiple lines\nand returns!");
+ label.SetText(test_text);
+ label.SetMultiLine(true);
+
+ // GetPreferredSize
+ gfx::Size required_size = label.GetPreferredSize();
+ EXPECT_GT(required_size.height(), kMinTextDimension);
+ EXPECT_GT(required_size.width(), kMinTextDimension);
+
+ // SizeToFit with unlimited width.
+ label.SizeToFit(0);
+ int required_width = label.GetLocalBounds(true).width();
+ EXPECT_GT(required_width, kMinTextDimension);
+
+ // SizeToFit with limited width.
+ label.SizeToFit(required_width - 1);
+ int constrained_width = label.GetLocalBounds(true).width();
+ EXPECT_LT(constrained_width, required_width);
+ EXPECT_GT(constrained_width, kMinTextDimension);
+
+ // Change the width back to the desire width.
+ label.SizeToFit(required_width);
+ EXPECT_EQ(required_width, label.GetLocalBounds(true).width());
+
+ // General tests for GetHeightForWidth.
+ int required_height = label.GetHeightForWidth(required_width);
+ EXPECT_GT(required_height, kMinTextDimension);
+ int height_for_constrained_width = label.GetHeightForWidth(constrained_width);
+ EXPECT_GT(height_for_constrained_width, required_height);
+ // Using the constrained width or the required_width - 1 should give the
+ // same result for the height because the constrainted width is the tight
+ // width when given "required_width - 1" as the max width.
+ EXPECT_EQ(height_for_constrained_width,
+ label.GetHeightForWidth(required_width - 1));
+
+ // Test everything with borders.
+ gfx::Insets border(10, 20, 30, 40);
+ label.SetBorder(Border::CreateEmptyBorder(border.top(),
+ border.left(),
+ border.bottom(),
+ border.right()));
+
+ // SizeToFit and borders.
+ label.SizeToFit(0);
+ int required_width_with_border = label.GetLocalBounds(true).width();
+ EXPECT_EQ(required_width_with_border, required_width + border.width());
+
+ // GetHeightForWidth and borders.
+ int required_height_with_border =
+ label.GetHeightForWidth(required_width_with_border);
+ EXPECT_EQ(required_height_with_border, required_height + border.height());
+
+ // Test that the border width is subtracted before doing the height
+ // calculation. If it is, then the height will grow when width
+ // is shrunk.
+ int height1 = label.GetHeightForWidth(required_width_with_border - 1);
+ EXPECT_GT(height1, required_height_with_border);
+ EXPECT_EQ(height1, height_for_constrained_width + border.height());
+
+ // GetPreferredSize and borders.
+ label.SetBounds(0, 0, 0, 0);
+ gfx::Size required_size_with_border = label.GetPreferredSize();
+ EXPECT_EQ(required_size_with_border.height(),
+ required_size.height() + border.height());
+ EXPECT_EQ(required_size_with_border.width(),
+ required_size.width() + border.width());
+}
+
+TEST(LabelTest, DrawSingleLineString) {
+ Label label;
+
+ // Turn off mirroring so that we don't need to figure out if
+ // align right really means align left.
+ label.EnableUIMirroringForRTLLanguages(false);
+
+ std::wstring test_text(L"Here's a string with no returns.");
+ label.SetText(test_text);
+ gfx::Size required_size(label.GetPreferredSize());
+ gfx::Size extra(22, 8);
+ label.SetBounds(0,
+ 0,
+ required_size.width() + extra.width(),
+ required_size.height() + extra.height());
+
+ // Do some basic verifications for all three alignments.
+ std::wstring paint_text;
+ gfx::Rect text_bounds;
+ int flags;
+
+ // Centered text.
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be centered horizontally and vertically.
+ EXPECT_EQ(extra.width() / 2, text_bounds.x());
+ EXPECT_EQ(extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+
+ // Left aligned text.
+ label.SetHorizontalAlignment(Label::ALIGN_LEFT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be left aligned horizontally and centered vertically.
+ EXPECT_EQ(0, text_bounds.x());
+ EXPECT_EQ(extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+
+ // Right aligned text.
+ label.SetHorizontalAlignment(Label::ALIGN_RIGHT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be right aligned horizontally and centered vertically.
+ EXPECT_EQ(extra.width(), text_bounds.x());
+ EXPECT_EQ(extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+
+ // Test single line drawing with a border.
+ gfx::Insets border(39, 34, 8, 96);
+ label.SetBorder(Border::CreateEmptyBorder(border.top(),
+ border.left(),
+ border.bottom(),
+ border.right()));
+
+ gfx::Size required_size_with_border(label.GetPreferredSize());
+ EXPECT_EQ(required_size.width() + border.width(),
+ required_size_with_border.width());
+ EXPECT_EQ(required_size.height() + border.height(),
+ required_size_with_border.height());
+ label.SetBounds(0,
+ 0,
+ required_size_with_border.width() + extra.width(),
+ required_size_with_border.height() + extra.height());
+
+ // Centered text with border.
+ label.SetHorizontalAlignment(Label::ALIGN_CENTER);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be centered horizontally and vertically within the border.
+ EXPECT_EQ(border.left() + extra.width() / 2, text_bounds.x());
+ EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+
+ // Left aligned text with border.
+ label.SetHorizontalAlignment(Label::ALIGN_LEFT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be left aligned horizontally and centered vertically.
+ EXPECT_EQ(border.left(), text_bounds.x());
+ EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+
+ // Right aligned text.
+ label.SetHorizontalAlignment(Label::ALIGN_RIGHT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ // The text should be right aligned horizontally and centered vertically.
+ EXPECT_EQ(border.left() + extra.width(), text_bounds.x());
+ EXPECT_EQ(border.top() + extra.height() / 2 , text_bounds.y());
+ EXPECT_EQ(required_size.width(), text_bounds.width());
+ EXPECT_EQ(required_size.height(), text_bounds.height());
+ EXPECT_EQ(0, flags);
+}
+
+TEST(LabelTest, DrawMultiLineString) {
+ Label label;
+
+ // Turn off mirroring so that we don't need to figure out if
+ // align right really means align left.
+ label.EnableUIMirroringForRTLLanguages(false);
+
+ std::wstring test_text(L"Another string\nwith returns\n\n!");
+ label.SetText(test_text);
+ label.SetMultiLine(true);
+ label.SizeToFit(0);
+
+ // Do some basic verifications for all three alignments.
+ std::wstring paint_text;
+ gfx::Rect text_bounds;
+ int flags;
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ EXPECT_EQ(0, text_bounds.x());
+ EXPECT_EQ(0, text_bounds.y());
+ EXPECT_GT(text_bounds.width(), kMinTextDimension);
+ EXPECT_GT(text_bounds.height(), kMinTextDimension);
+ EXPECT_EQ(ChromeCanvas::MULTI_LINE | ChromeCanvas::TEXT_ALIGN_CENTER, flags);
+ gfx::Rect center_bounds(text_bounds);
+
+ label.SetHorizontalAlignment(Label::ALIGN_LEFT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ EXPECT_EQ(0, text_bounds.x());
+ EXPECT_EQ(0, text_bounds.y());
+ EXPECT_GT(text_bounds.width(), kMinTextDimension);
+ EXPECT_GT(text_bounds.height(), kMinTextDimension);
+ EXPECT_EQ(ChromeCanvas::MULTI_LINE | ChromeCanvas::TEXT_ALIGN_LEFT, flags);
+
+ label.SetHorizontalAlignment(Label::ALIGN_RIGHT);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ EXPECT_EQ(0, text_bounds.x());
+ EXPECT_EQ(0, text_bounds.y());
+ EXPECT_GT(text_bounds.width(), kMinTextDimension);
+ EXPECT_GT(text_bounds.height(), kMinTextDimension);
+ EXPECT_EQ(ChromeCanvas::MULTI_LINE | ChromeCanvas::TEXT_ALIGN_RIGHT, flags);
+
+ // Test multiline drawing with a border.
+ gfx::Insets border(19, 92, 23, 2);
+ label.SetBorder(Border::CreateEmptyBorder(border.top(),
+ border.left(),
+ border.bottom(),
+ border.right()));
+ label.SizeToFit(0);
+ label.SetHorizontalAlignment(Label::ALIGN_CENTER);
+ paint_text.clear();
+ text_bounds.SetRect(0, 0, 0, 0);
+ label.CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
+ EXPECT_STREQ(test_text.c_str(), paint_text.c_str());
+ EXPECT_EQ(center_bounds.x() + border.left(), text_bounds.x());
+ EXPECT_EQ(center_bounds.y() + border.top(), text_bounds.y());
+ EXPECT_EQ(center_bounds.width(), text_bounds.width());
+ EXPECT_EQ(center_bounds.height(), text_bounds.height());
+ EXPECT_EQ(ChromeCanvas::MULTI_LINE | ChromeCanvas::TEXT_ALIGN_CENTER, flags);
+}
+
+} // namespace views
|