1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/corewm/tooltip_aura.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/aura/test/aura_test_base.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/font_list.h"
11 #include "ui/gfx/text_elider.h"
12 #include "ui/gfx/text_utils.h"
14 using base::ASCIIToUTF16
;
15 using base::UTF8ToUTF16
;
20 typedef aura::test::AuraTestBase TooltipAuraTest
;
22 TEST_F(TooltipAuraTest
, TrimTooltipToFitTests
) {
23 const gfx::FontList font_list
;
24 const int max_width
= 4000;
25 base::string16 tooltip
;
26 int width
, line_count
, expect_lines
;
27 int max_pixel_width
= 400; // copied from constants in tooltip_controller.cc
28 int max_lines
= 10; // copied from constants in tooltip_controller.cc
31 // Error in computed size vs. expected size should not be greater than the
32 // size of the longest word.
33 int error_in_pixel_width
= gfx::GetStringWidth(ASCIIToUTF16("tooltip"),
36 // Long tooltips should wrap to next line
38 width
= line_count
= -1;
40 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
41 (expect_lines
- 1) * max_pixel_width
;)
42 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
43 tooltip_len
= tooltip
.length();
44 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
46 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
47 EXPECT_EQ(expect_lines
, line_count
);
48 EXPECT_EQ(tooltip_len
+ expect_lines
- 1, tooltip
.length());
50 // More than |max_lines| lines should get truncated at 10 lines.
52 width
= line_count
= -1;
54 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
55 (expect_lines
- 1) * max_pixel_width
;)
56 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
57 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
59 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
60 EXPECT_EQ(max_lines
, line_count
);
62 // Long multi line tooltips should wrap individual lines.
64 width
= line_count
= -1;
66 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
67 (expect_lines
- 2) * max_pixel_width
;)
68 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
69 tooltip
.insert(tooltip
.length() / 2, ASCIIToUTF16("\n"));
70 tooltip_len
= tooltip
.length();
71 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
73 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
74 EXPECT_EQ(expect_lines
, line_count
);
75 // We may have inserted the line break above near a space which will get
76 // trimmed. Hence we may be off by 1 in the final tooltip length calculation.
77 EXPECT_NEAR(tooltip_len
+ expect_lines
- 2, tooltip
.length(), 1);
80 // Tooltip with really long word gets elided.
82 width
= line_count
= -1;
83 tooltip
= UTF8ToUTF16(std::string('a', max_pixel_width
));
84 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
86 EXPECT_NEAR(max_pixel_width
, width
, 5);
87 EXPECT_EQ(1, line_count
);
88 EXPECT_EQ(gfx::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width
)),
89 font_list
, max_pixel_width
, gfx::ELIDE_TAIL
),
93 // Normal small tooltip should stay as is.
95 width
= line_count
= -1;
96 tooltip
= ASCIIToUTF16("Small Tooltip");
97 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
99 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tooltip"), font_list
),
101 EXPECT_EQ(1, line_count
);
102 EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip
);
104 // Normal small multi-line tooltip should stay as is.
106 width
= line_count
= -1;
107 tooltip
= ASCIIToUTF16("Multi line\nTooltip");
108 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
110 int expected_width
= gfx::GetStringWidth(ASCIIToUTF16("Multi line"),
112 expected_width
= std::max(expected_width
,
113 gfx::GetStringWidth(ASCIIToUTF16("Tooltip"),
115 EXPECT_EQ(expected_width
, width
);
116 EXPECT_EQ(2, line_count
);
117 EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip
);
119 // Whitespaces in tooltips are preserved.
121 width
= line_count
= -1;
122 tooltip
= ASCIIToUTF16("Small Tool t\tip");
123 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
125 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tool t\tip"), font_list
),
127 EXPECT_EQ(1, line_count
);
128 EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip
);
131 } // namespace corewm