Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / views / corewm / tooltip_aura_unittest.cc
bloba77db2f0b108a4c2eb1891ec3d4d02e2caf8e066
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;
16 namespace views {
17 namespace corewm {
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
28 size_t tooltip_len;
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"),
33 font_list);
35 // Long tooltips should wrap to next line
36 tooltip.clear();
37 width = line_count = -1;
38 expect_lines = 3;
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,
44 &line_count);
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.
50 tooltip.clear();
51 width = line_count = -1;
52 expect_lines = 13;
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,
57 &line_count);
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.
62 tooltip.clear();
63 width = line_count = -1;
64 expect_lines = 4;
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,
71 &line_count);
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);
78 #if !defined(OS_WIN)
79 // Tooltip with really long word gets elided.
80 tooltip.clear();
81 width = line_count = -1;
82 tooltip = UTF8ToUTF16(std::string('a', max_pixel_width));
83 TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
84 &line_count);
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),
89 tooltip);
90 #endif
92 // Normal small tooltip should stay as is.
93 tooltip.clear();
94 width = line_count = -1;
95 tooltip = ASCIIToUTF16("Small Tooltip");
96 TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
97 &line_count);
98 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tooltip"), font_list),
99 width);
100 EXPECT_EQ(1, line_count);
101 EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip);
103 // Normal small multi-line tooltip should stay as is.
104 tooltip.clear();
105 width = line_count = -1;
106 tooltip = ASCIIToUTF16("Multi line\nTooltip");
107 TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
108 &line_count);
109 int expected_width = gfx::GetStringWidth(ASCIIToUTF16("Multi line"),
110 font_list);
111 expected_width = std::max(expected_width,
112 gfx::GetStringWidth(ASCIIToUTF16("Tooltip"),
113 font_list));
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.
119 tooltip.clear();
120 width = line_count = -1;
121 tooltip = ASCIIToUTF16("Small Tool t\tip");
122 TooltipAura::TrimTooltipToFit(font_list, max_width, &tooltip, &width,
123 &line_count);
124 EXPECT_EQ(gfx::GetStringWidth(ASCIIToUTF16("Small Tool t\tip"), font_list),
125 width);
126 EXPECT_EQ(1, line_count);
127 EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip);
130 } // namespace corewm
131 } // namespace views