Index: ui/views/corewm/tooltip_aura_unittest.cc |
diff --git a/ui/views/corewm/tooltip_aura_unittest.cc b/ui/views/corewm/tooltip_aura_unittest.cc |
index 1e8981ad79191329c56dccad7d0d6383c9e2b977..df7682a34ec56a8f51ef737b8fa4c0376d2c2438 100644 |
--- a/ui/views/corewm/tooltip_aura_unittest.cc |
+++ b/ui/views/corewm/tooltip_aura_unittest.cc |
@@ -4,128 +4,158 @@ |
#include "ui/views/corewm/tooltip_aura.h" |
+#include "base/strings/string_split.h" |
#include "base/strings/utf_string_conversions.h" |
#include "ui/aura/test/aura_test_base.h" |
-#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/font_list.h" |
#include "ui/gfx/text_elider.h" |
#include "ui/gfx/text_utils.h" |
-using base::ASCIIToUTF16; |
-using base::UTF8ToUTF16; |
- |
namespace views { |
namespace corewm { |
+namespace { |
+ |
+// Generates a string consisting of repeated copies of |word| separated by |
+// |separator|. The string will be long enough to fill |num_lines| lines wrapped |
+// at |max_width| pixels. |
+base::string16 FillLines(const base::string16& word, |
+ const base::string16& separator, |
+ int max_width, |
+ int num_lines, |
+ const gfx::FontList& font_list) { |
+ base::string16 text; |
+ for (int lines = 0; lines < num_lines; ++lines) { |
+ base::string16 line = word; |
+ while (gfx::GetStringWidth(line + separator + word, font_list) <= max_width) |
+ line += separator + word; |
+ |
+ if (lines != 0) |
+ text += separator; |
+ text += line; |
+ } |
+ return text; |
+} |
+ |
+} // namespace |
+ |
typedef aura::test::AuraTestBase TooltipAuraTest; |
-TEST_F(TooltipAuraTest, TrimTooltipToFitTests) { |
- const gfx::FontList font_list; |
- const int max_width = 4000; |
- base::string16 tooltip; |
- int width, line_count, expect_lines; |
- int max_pixel_width = 400; // copied from constants in tooltip_controller.cc |
- int max_lines = 10; // copied from constants in tooltip_controller.cc |
- size_t tooltip_len; |
- |
- // Error in computed size vs. expected size should not be greater than the |
- // size of the longest word. |
- int error_in_pixel_width = gfx::GetStringWidth(ASCIIToUTF16("tooltip"), |
- font_list); |
- |
- // Long tooltips should wrap to next line |
- tooltip.clear(); |
- width = line_count = -1; |
- expect_lines = 3; |
- for (; gfx::GetStringWidth(tooltip, font_list) <= |
- (expect_lines - 1) * max_pixel_width;) |
- tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
- tooltip_len = tooltip.length(); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+TEST_F(TooltipAuraTest, TrimTooltipToFit) { |
+ const gfx::FontList kFontList; |
+ const gfx::Size kMaxSize(2000, 1500); |
+ const base::string16 kWord = base::ASCIIToUTF16("word"); |
+ const base::string16 kSpace = base::ASCIIToUTF16(" "); |
+ const base::string16 kNewline = base::ASCIIToUTF16("\n"); |
+ |
+ // Long tooltips should wrap to the next line. |
+ base::string16 text = FillLines(kWord, kSpace, |
+ TooltipAura::kPreferredWidthPixels, 1, kFontList); |
+ text += kSpace + kWord; |
+ // The last space should be replaced by a newline. |
+ base::string16 expected_text = text; |
+ expected_text[expected_text.find_last_of(L' ')] = L'\n'; |
+ int width = 0, line_count = 0; |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width); |
- EXPECT_EQ(expect_lines, line_count); |
- EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length()); |
- |
- // More than |max_lines| lines should get truncated at 10 lines. |
- tooltip.clear(); |
- width = line_count = -1; |
- expect_lines = 13; |
- for (; gfx::GetStringWidth(tooltip, font_list) <= |
- (expect_lines - 1) * max_pixel_width;) |
- tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
+ EXPECT_EQ(2, line_count); |
+ EXPECT_EQ(expected_text, text); |
+ |
+ // The tooltip should be truncated to the number of lines that fit in the |
+ // passed-in maximum size. |
+ int max_lines = kMaxSize.height() / kFontList.GetHeight(); |
+ text = FillLines( |
+ kWord, kSpace, TooltipAura::kPreferredWidthPixels, max_lines, kFontList); |
+ text += kSpace + kWord; |
+ size_t orig_length = text.size(); |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width); |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
EXPECT_EQ(max_lines, line_count); |
+ EXPECT_LT(text.size(), orig_length); |
+ |
+ // When the original string contains two long lines, each line should be |
+ // wrapped individually. |
+ base::string16 line = FillLines( |
+ kWord, kSpace, TooltipAura::kPreferredWidthPixels, 1, kFontList); |
+ text = line + kSpace + kWord + kNewline + line + kSpace + kWord; |
+ orig_length = text.size(); |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
+ &line_count); |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
+ EXPECT_EQ(4, line_count); |
+ std::vector<base::string16> lines; |
+ base::SplitString(text, L'\n', &lines); |
+ ASSERT_EQ(4U, lines.size()); |
+ EXPECT_EQ(line, lines[0]); |
+ EXPECT_EQ(kWord, lines[1]); |
+ EXPECT_EQ(line, lines[2]); |
+ EXPECT_EQ(kWord, lines[3]); |
- // Long multi line tooltips should wrap individual lines. |
- tooltip.clear(); |
- width = line_count = -1; |
- expect_lines = 4; |
- for (; gfx::GetStringWidth(tooltip, font_list) <= |
- (expect_lines - 2) * max_pixel_width;) |
- tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
- tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n")); |
- tooltip_len = tooltip.length(); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ // The preferred width should be exceeded if there's a long word. |
+ text = FillLines(kWord, base::string16(), |
+ TooltipAura::kPreferredWidthPixels, 1, kFontList); |
+ text += kWord; |
+ expected_text = text; |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_NEAR(max_pixel_width, width, error_in_pixel_width); |
- EXPECT_EQ(expect_lines, line_count); |
- // We may have inserted the line break above near a space which will get |
- // trimmed. Hence we may be off by 1 in the final tooltip length calculation. |
- EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1); |
- |
-#if !defined(OS_WIN) |
- // Tooltip with really long word gets elided. |
- tooltip.clear(); |
- width = line_count = -1; |
- tooltip = UTF8ToUTF16(std::string('a', max_pixel_width)); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ EXPECT_GT(width, TooltipAura::kPreferredWidthPixels); |
+ EXPECT_LT(width, kMaxSize.width()); |
+ EXPECT_EQ(1, line_count); |
+ EXPECT_EQ(expected_text, text); |
+ |
+ // If a word exceeds the maximum width, it should be elided. |
+ const base::string16 kLongWord = |
+ FillLines(kWord, base::string16(), kMaxSize.width(), 1, kFontList) + |
+ kWord; |
+ text = kLongWord; |
+ expected_text = gfx::ElideText( |
+ text, kFontList, kMaxSize.width(), gfx::ELIDE_TAIL); |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_NEAR(max_pixel_width, width, 5); |
+ EXPECT_GT(width, TooltipAura::kPreferredWidthPixels); |
+ EXPECT_LE(width, kMaxSize.width()); |
EXPECT_EQ(1, line_count); |
- EXPECT_EQ(gfx::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), |
- font_list, max_pixel_width, gfx::ELIDE_TAIL), |
- tooltip); |
-#endif |
- |
- // Normal small tooltip should stay as is. |
- tooltip.clear(); |
- width = line_count = -1; |
- tooltip = ASCIIToUTF16("Small Tooltip"); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ EXPECT_EQ(expected_text, text); |
+ |
+ // When a long word makes the tooltip wider than the preferred width, |
+ // other lines should be wrapped at the new, longer size. |
+ text = FillLines(kWord, kSpace, kMaxSize.width(), 1, kFontList); |
+ text += kNewline + kLongWord; |
+ text += kNewline + FillLines(kWord, kSpace, kMaxSize.width(), 1, kFontList); |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
+ &line_count); |
+ EXPECT_GT(width, TooltipAura::kPreferredWidthPixels); |
+ EXPECT_LE(width, kMaxSize.width()); |
+ EXPECT_EQ(3, line_count); |
+ |
+ // A normal small tooltip should stay as is. |
+ text = base::ASCIIToUTF16("Small Tooltip"); |
+ expected_text = text; |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tooltip"), font_list), |
- width); |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
EXPECT_EQ(1, line_count); |
- EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip); |
+ EXPECT_EQ(expected_text, text); |
- // Normal small multi-line tooltip should stay as is. |
- tooltip.clear(); |
- width = line_count = -1; |
- tooltip = ASCIIToUTF16("Multi line\nTooltip"); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ // A normal small multi-line tooltip should stay as is. |
+ text = base::ASCIIToUTF16("Multi line\nTooltip"); |
+ expected_text = text; |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- int expected_width = gfx::GetStringWidth(ASCIIToUTF16("Multi line"), |
- font_list); |
- expected_width = std::max(expected_width, |
- gfx::GetStringWidth(ASCIIToUTF16("Tooltip"), |
- font_list)); |
- EXPECT_EQ(expected_width, width); |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
EXPECT_EQ(2, line_count); |
- EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip); |
+ EXPECT_EQ(expected_text, text); |
- // Whitespaces in tooltips are preserved. |
- tooltip.clear(); |
- width = line_count = -1; |
- tooltip = ASCIIToUTF16("Small Tool t\tip"); |
- TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width, |
+ // Whitespace in the middle of tooltips should be preserved. |
+ text = base::ASCIIToUTF16("Small Tool t\tip"); |
+ expected_text = text; |
+ TooltipAura::TrimTooltipToFit(kFontList, kMaxSize, &text, &width, |
&line_count); |
- EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tool t\tip"), font_list), |
- width); |
+ EXPECT_LE(width, TooltipAura::kPreferredWidthPixels); |
EXPECT_EQ(1, line_count); |
- EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip); |
+ EXPECT_EQ(expected_text, text); |
} |
} // namespace corewm |