Index: ash/tooltips/tooltip_controller_unittest.cc |
diff --git a/ash/tooltips/tooltip_controller_unittest.cc b/ash/tooltips/tooltip_controller_unittest.cc |
index f07dfd559b7926c9bf87cf4490835681d5603901..044432613e9a9e9bf30ccd4fbfad8ca1878be852 100644 |
--- a/ash/tooltips/tooltip_controller_unittest.cc |
+++ b/ash/tooltips/tooltip_controller_unittest.cc |
@@ -10,6 +10,9 @@ |
#include "ui/aura/root_window.h" |
#include "ui/aura/test/event_generator.h" |
#include "ui/aura/window.h" |
+#include "ui/base/resource/resource_bundle.h" |
+#include "ui/base/text/text_elider.h" |
+#include "ui/gfx/font.h" |
#include "ui/gfx/point.h" |
#include "ui/views/view.h" |
#include "ui/views/widget/widget.h" |
@@ -71,6 +74,11 @@ ash::internal::TooltipController* GetController() { |
aura::client::GetTooltipClient(Shell::GetRootWindow())); |
} |
+gfx::Font GetDefaultFont() { |
+ return ui::ResourceBundle::GetSharedInstance().GetFont( |
+ ui::ResourceBundle::BaseFont); |
+} |
+ |
} // namespace |
class TooltipControllerTest : public AshTestBase { |
@@ -94,6 +102,15 @@ class TooltipControllerTest : public AshTestBase { |
return GetController()->IsTooltipVisible(); |
} |
+ void TrimTooltipToFit(string16* text, |
+ int* max_width, |
+ int* line_count, |
+ int x, |
+ int y) { |
+ ash::internal::TooltipController::TrimTooltipToFit(text, max_width, |
+ line_count, x, y); |
+ } |
+ |
private: |
DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest); |
}; |
@@ -208,5 +225,95 @@ TEST_F(TooltipControllerTest, EnableOrDisableTooltips) { |
EXPECT_TRUE(IsTooltipVisible()); |
} |
+TEST_F(TooltipControllerTest, TrimTooltipToFitTests) { |
+ string16 tooltip; |
+ int max_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 |
+ gfx::Font font = GetDefaultFont(); |
+ 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 = font.GetStringWidth(ASCIIToUTF16("tooltip")); |
+ |
+ // Long tooltips should wrap to next line |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ expect_lines = 3; |
+ for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
+ tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
+ tooltip_len = tooltip.length(); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_NEAR(max_pixel_width, max_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(); |
+ max_width = line_count = -1; |
+ expect_lines = 13; |
+ for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
+ tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
+ EXPECT_EQ(max_lines, line_count); |
+ |
+ // Long multi line tooltips should wrap individual lines. |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ expect_lines = 4; |
+ for (; font.GetStringWidth(tooltip) <= (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(); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_NEAR(max_pixel_width, max_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); |
+ |
+ // Tooltip with really long word gets elided. |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ tooltip = UTF8ToUTF16(std::string('a', max_pixel_width)); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_NEAR(max_pixel_width, max_width, 5); |
+ EXPECT_EQ(1, line_count); |
+ EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font, |
+ max_pixel_width, ui::ELIDE_AT_END), tooltip); |
+ |
+ // Normal small tooltip should stay as is. |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ tooltip = ASCIIToUTF16("Small Tooltip"); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tooltip")), max_width); |
+ EXPECT_EQ(1, line_count); |
+ EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip); |
+ |
+ // Normal small multi-line tooltip should stay as is. |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ tooltip = ASCIIToUTF16("Multi line\nTooltip"); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ int expected_width = font.GetStringWidth(ASCIIToUTF16("Multi line")); |
+ expected_width = std::max(expected_width, |
+ font.GetStringWidth(ASCIIToUTF16("Tooltip"))); |
+ EXPECT_EQ(expected_width, max_width); |
+ EXPECT_EQ(2, line_count); |
+ EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip); |
+ |
+ // Whitespaces in tooltips are preserved. |
+ tooltip.clear(); |
+ max_width = line_count = -1; |
+ tooltip = ASCIIToUTF16("Small Tool t\tip"); |
+ TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
+ EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tool t\tip")), max_width); |
+ EXPECT_EQ(1, line_count); |
+ EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip); |
+} |
+ |
} // namespace test |
} // namespace ash |