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/gfx/font_list.h"
10 #include "ui/gfx/text_elider.h"
11 #include "ui/gfx/text_utils.h"
13 using base::ASCIIToUTF16
;
14 using base::UTF8ToUTF16
;
19 typedef aura::test::AuraTestBase TooltipAuraTest
;
21 TEST_F(TooltipAuraTest
, TrimTooltipToFitTests
) {
22 const gfx::FontList font_list
;
23 const int max_width
= 4000;
24 base::string16 tooltip
;
25 int width
, line_count
, expect_lines
;
26 int max_pixel_width
= 400; // copied from constants in tooltip_controller.cc
27 int max_lines
= 10; // copied from constants in tooltip_controller.cc
30 // Error in computed size vs. expected size should not be greater than the
31 // size of the longest word.
32 int error_in_pixel_width
= gfx::GetStringWidth(ASCIIToUTF16("tooltip"),
35 // Long tooltips should wrap to next line
37 width
= line_count
= -1;
39 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
40 (expect_lines
- 1) * max_pixel_width
;)
41 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
42 tooltip_len
= tooltip
.length();
43 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
45 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
46 EXPECT_EQ(expect_lines
, line_count
);
47 EXPECT_EQ(tooltip_len
+ expect_lines
- 1, tooltip
.length());
49 // More than |max_lines| lines should get truncated at 10 lines.
51 width
= line_count
= -1;
53 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
54 (expect_lines
- 1) * max_pixel_width
;)
55 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
56 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
58 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
59 EXPECT_EQ(max_lines
, line_count
);
61 // Long multi line tooltips should wrap individual lines.
63 width
= line_count
= -1;
65 for (; gfx::GetStringWidth(tooltip
, font_list
) <=
66 (expect_lines
- 2) * max_pixel_width
;)
67 tooltip
.append(ASCIIToUTF16("This is part of the tooltip"));
68 tooltip
.insert(tooltip
.length() / 2, ASCIIToUTF16("\n"));
69 tooltip_len
= tooltip
.length();
70 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
72 EXPECT_NEAR(max_pixel_width
, width
, error_in_pixel_width
);
73 EXPECT_EQ(expect_lines
, line_count
);
74 // We may have inserted the line break above near a space which will get
75 // trimmed. Hence we may be off by 1 in the final tooltip length calculation.
76 EXPECT_NEAR(tooltip_len
+ expect_lines
- 2, tooltip
.length(), 1);
79 // Tooltip with really long word gets elided.
81 width
= line_count
= -1;
82 tooltip
= UTF8ToUTF16(std::string('a', max_pixel_width
));
83 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
85 EXPECT_NEAR(max_pixel_width
, width
, 5);
86 EXPECT_EQ(1, line_count
);
87 EXPECT_EQ(gfx::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width
)),
88 font_list
, max_pixel_width
, gfx::ELIDE_TAIL
),
92 // Normal small tooltip should stay as is.
94 width
= line_count
= -1;
95 tooltip
= ASCIIToUTF16("Small Tooltip");
96 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
98 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tooltip"), font_list
),
100 EXPECT_EQ(1, line_count
);
101 EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip
);
103 // Normal small multi-line tooltip should stay as is.
105 width
= line_count
= -1;
106 tooltip
= ASCIIToUTF16("Multi line\nTooltip");
107 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
109 int expected_width
= gfx::GetStringWidth(ASCIIToUTF16("Multi line"),
111 expected_width
= std::max(expected_width
,
112 gfx::GetStringWidth(ASCIIToUTF16("Tooltip"),
114 EXPECT_EQ(expected_width
, width
);
115 EXPECT_EQ(2, line_count
);
116 EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip
);
118 // Whitespaces in tooltips are preserved.
120 width
= line_count
= -1;
121 tooltip
= ASCIIToUTF16("Small Tool t\tip");
122 TooltipAura::TrimTooltipToFit(font_list
, max_width
, &tooltip
, &width
,
124 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tool t\tip"), font_list
),
126 EXPECT_EQ(1, line_count
);
127 EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip
);
130 } // namespace corewm