1 // Copyright (c) 2012 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/gfx/font_list.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 // Helper function for comparing fonts for equality.
18 std::string
FontToString(const gfx::Font
& font
) {
19 std::string font_string
= font
.GetFontName();
21 font_string
+= base::IntToString(font
.GetFontSize());
22 int style
= font
.GetStyle();
23 if (style
& gfx::Font::BOLD
)
24 font_string
+= "|bold";
25 if (style
& gfx::Font::ITALIC
)
26 font_string
+= "|italic";
27 if (style
& gfx::Font::UNDERLINE
)
28 font_string
+= "|underline";
36 TEST(FontListTest
, ParseDescription
) {
37 std::vector
<std::string
> families
;
38 int style
= gfx::Font::NORMAL
;
41 // Parse a well-formed description containing styles and a size.
42 EXPECT_TRUE(FontList::ParseDescription("Arial,Helvetica,Bold Italic 12px",
43 &families
, &style
, &size_pixels
));
44 ASSERT_EQ(2U, families
.size());
45 EXPECT_EQ("Arial", families
[0]);
46 EXPECT_EQ("Helvetica", families
[1]);
47 EXPECT_EQ(gfx::Font::BOLD
| gfx::Font::ITALIC
, style
);
48 EXPECT_EQ(12, size_pixels
);
50 // Whitespace should be removed.
51 EXPECT_TRUE(FontList::ParseDescription(" Verdana , Italic Bold 10px ",
52 &families
, &style
, &size_pixels
));
53 ASSERT_EQ(1U, families
.size());
54 EXPECT_EQ("Verdana", families
[0]);
55 EXPECT_EQ(gfx::Font::BOLD
| gfx::Font::ITALIC
, style
);
56 EXPECT_EQ(10, size_pixels
);
58 // Invalid descriptions should be rejected.
59 EXPECT_FALSE(FontList::ParseDescription("", &families
, &style
, &size_pixels
));
60 EXPECT_FALSE(FontList::ParseDescription("Arial", &families
, &style
,
62 EXPECT_FALSE(FontList::ParseDescription("Arial,12", &families
, &style
,
64 EXPECT_FALSE(FontList::ParseDescription("Arial 12px", &families
, &style
,
66 EXPECT_FALSE(FontList::ParseDescription("Arial,12px,", &families
, &style
,
68 EXPECT_FALSE(FontList::ParseDescription("Arial,0px", &families
, &style
,
70 EXPECT_FALSE(FontList::ParseDescription("Arial,-1px", &families
, &style
,
72 EXPECT_FALSE(FontList::ParseDescription("Arial,foo 12px", &families
, &style
,
76 // TODO(489354): Enable this on android.
77 #if defined(OS_ANDROID)
78 #define MAYBE_Fonts_FromDescString DISABLED_Fonts_FromDescString
80 #define MAYBE_Fonts_FromDescString Fonts_FromDescString
82 TEST(FontListTest
, MAYBE_Fonts_FromDescString
) {
83 // Test init from font name size string.
84 FontList font_list
= FontList("arial, Courier New, 13px");
85 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
86 ASSERT_EQ(2U, fonts
.size());
87 EXPECT_EQ("arial|13", FontToString(fonts
[0]));
88 EXPECT_EQ("Courier New|13", FontToString(fonts
[1]));
91 // TODO(489354): Enable this on android.
92 #if defined(OS_ANDROID)
93 #define MAYBE_Fonts_FromDescStringInFlexibleFormat \
94 DISABLED_Fonts_FromDescStringInFlexibleFormat
96 #define MAYBE_Fonts_FromDescStringInFlexibleFormat \
97 Fonts_FromDescStringInFlexibleFormat
99 TEST(FontListTest
, MAYBE_Fonts_FromDescStringInFlexibleFormat
) {
100 // Test init from font name size string with flexible format.
101 FontList font_list
= FontList(" arial , Courier New , 13px");
102 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
103 ASSERT_EQ(2U, fonts
.size());
104 EXPECT_EQ("arial|13", FontToString(fonts
[0]));
105 EXPECT_EQ("Courier New|13", FontToString(fonts
[1]));
108 // TODO(489354): Enable this on android.
109 #if defined(OS_ANDROID)
110 #define MAYBE_Fonts_FromDescStringWithStyleInFlexibleFormat \
111 DISABLED_Fonts_FromDescStringWithStyleInFlexibleFormat
113 #define MAYBE_Fonts_FromDescStringWithStyleInFlexibleFormat \
114 Fonts_FromDescStringWithStyleInFlexibleFormat
116 TEST(FontListTest
, MAYBE_Fonts_FromDescStringWithStyleInFlexibleFormat
) {
117 // Test init from font name style size string with flexible format.
118 FontList font_list
= FontList(" arial , Courier New , Bold "
120 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
121 ASSERT_EQ(2U, fonts
.size());
122 EXPECT_EQ("arial|13|bold|italic", FontToString(fonts
[0]));
123 EXPECT_EQ("Courier New|13|bold|italic", FontToString(fonts
[1]));
126 // TODO(489354): Enable this on android.
127 #if defined(OS_ANDROID)
128 #define MAYBE_Fonts_FromFont DISABLED_Fonts_FromFont
130 #define MAYBE_Fonts_FromFont Fonts_FromFont
132 TEST(FontListTest
, MAYBE_Fonts_FromFont
) {
133 // Test init from Font.
134 Font
font("Arial", 8);
135 FontList font_list
= FontList(font
);
136 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
137 ASSERT_EQ(1U, fonts
.size());
138 EXPECT_EQ("Arial|8", FontToString(fonts
[0]));
141 // TODO(489354): Enable this on android.
142 #if defined(OS_ANDROID)
143 #define MAYBE_Fonts_FromFontWithNonNormalStyle \
144 DISABLED_Fonts_FromFontWithNonNormalStyle
146 #define MAYBE_Fonts_FromFontWithNonNormalStyle Fonts_FromFontWithNonNormalStyle
148 TEST(FontListTest
, MAYBE_Fonts_FromFontWithNonNormalStyle
) {
149 // Test init from Font with non-normal style.
150 Font
font("Arial", 8);
151 FontList font_list
= FontList(font
.Derive(2, Font::BOLD
));
152 std::vector
<Font
> fonts
= font_list
.GetFonts();
153 ASSERT_EQ(1U, fonts
.size());
154 EXPECT_EQ("Arial|10|bold", FontToString(fonts
[0]));
156 font_list
= FontList(font
.Derive(-2, Font::ITALIC
));
157 fonts
= font_list
.GetFonts();
158 ASSERT_EQ(1U, fonts
.size());
159 EXPECT_EQ("Arial|6|italic", FontToString(fonts
[0]));
162 // TODO(489354): Enable this on android.
163 #if defined(OS_ANDROID)
164 #define MAYBE_Fonts_FromFontVector DISABLED_Fonts_FromFontVector
166 #define MAYBE_Fonts_FromFontVector Fonts_FromFontVector
168 TEST(FontListTest
, MAYBE_Fonts_FromFontVector
) {
169 // Test init from Font vector.
170 Font
font("Arial", 8);
171 Font
font_1("Courier New", 10);
172 std::vector
<Font
> input_fonts
;
173 input_fonts
.push_back(font
.Derive(0, Font::BOLD
));
174 input_fonts
.push_back(font_1
.Derive(-2, Font::BOLD
));
175 FontList font_list
= FontList(input_fonts
);
176 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
177 ASSERT_EQ(2U, fonts
.size());
178 EXPECT_EQ("Arial|8|bold", FontToString(fonts
[0]));
179 EXPECT_EQ("Courier New|8|bold", FontToString(fonts
[1]));
182 TEST(FontListTest
, FontDescString_GetStyle
) {
183 FontList font_list
= FontList("Arial,Sans serif, 8px");
184 EXPECT_EQ(Font::NORMAL
, font_list
.GetFontStyle());
186 font_list
= FontList("Arial,Sans serif,Bold 8px");
187 EXPECT_EQ(Font::BOLD
, font_list
.GetFontStyle());
189 font_list
= FontList("Arial,Sans serif,Italic 8px");
190 EXPECT_EQ(Font::ITALIC
, font_list
.GetFontStyle());
192 font_list
= FontList("Arial,Italic Bold 8px");
193 EXPECT_EQ(Font::BOLD
| Font::ITALIC
, font_list
.GetFontStyle());
196 // TODO(489354): Enable this on android.
197 #if defined(OS_ANDROID)
198 #define MAYBE_Fonts_GetStyle DISABLED_Fonts_GetStyle
200 #define MAYBE_Fonts_GetStyle Fonts_GetStyle
202 TEST(FontListTest
, MAYBE_Fonts_GetStyle
) {
203 std::vector
<Font
> fonts
;
204 fonts
.push_back(gfx::Font("Arial", 8));
205 fonts
.push_back(gfx::Font("Sans serif", 8));
206 FontList font_list
= FontList(fonts
);
207 EXPECT_EQ(Font::NORMAL
, font_list
.GetFontStyle());
208 fonts
[0] = fonts
[0].Derive(0, Font::ITALIC
| Font::BOLD
);
209 fonts
[1] = fonts
[1].Derive(0, Font::ITALIC
| Font::BOLD
);
210 font_list
= FontList(fonts
);
211 EXPECT_EQ(Font::ITALIC
| Font::BOLD
, font_list
.GetFontStyle());
214 // TODO(489354): Enable this on android.
215 #if defined(OS_ANDROID)
216 #define MAYBE_Fonts_Derive DISABLED_Fonts_Derive
218 #define MAYBE_Fonts_Derive Fonts_Derive
220 TEST(FontListTest
, MAYBE_Fonts_Derive
) {
221 std::vector
<Font
> fonts
;
222 fonts
.push_back(gfx::Font("Arial", 8));
223 fonts
.push_back(gfx::Font("Courier New", 8));
224 FontList font_list
= FontList(fonts
);
226 FontList derived
= font_list
.Derive(5, Font::BOLD
| Font::UNDERLINE
);
227 const std::vector
<Font
>& derived_fonts
= derived
.GetFonts();
229 EXPECT_EQ(2U, derived_fonts
.size());
230 EXPECT_EQ("Arial|13|bold|underline", FontToString(derived_fonts
[0]));
231 EXPECT_EQ("Courier New|13|bold|underline", FontToString(derived_fonts
[1]));
234 // TODO(489354): Enable this on android.
235 #if defined(OS_ANDROID)
236 #define MAYBE_Fonts_DeriveWithSizeDelta DISABLED_Fonts_DeriveWithSizeDelta
238 #define MAYBE_Fonts_DeriveWithSizeDelta Fonts_DeriveWithSizeDelta
240 TEST(FontListTest
, MAYBE_Fonts_DeriveWithSizeDelta
) {
241 std::vector
<Font
> fonts
;
242 fonts
.push_back(gfx::Font("Arial", 18).Derive(0, Font::ITALIC
));
243 fonts
.push_back(gfx::Font("Courier New", 18).Derive(0, Font::ITALIC
));
244 FontList font_list
= FontList(fonts
);
246 FontList derived
= font_list
.DeriveWithSizeDelta(-5);
247 const std::vector
<Font
>& derived_fonts
= derived
.GetFonts();
249 EXPECT_EQ(2U, derived_fonts
.size());
250 EXPECT_EQ("Arial|13|italic", FontToString(derived_fonts
[0]));
251 EXPECT_EQ("Courier New|13|italic", FontToString(derived_fonts
[1]));
254 // TODO(489354): Enable this on android.
255 #if defined(OS_ANDROID)
256 #define MAYBE_Fonts_GetHeight_GetBaseline DISABLED_Fonts_GetHeight_GetBaseline
258 #define MAYBE_Fonts_GetHeight_GetBaseline Fonts_GetHeight_GetBaseline
260 TEST(FontListTest
, MAYBE_Fonts_GetHeight_GetBaseline
) {
261 // If a font list has only one font, the height and baseline must be the same.
262 Font
font1("Arial", 16);
264 base::StringToLowerASCII(font1
.GetActualFontNameForTesting()));
265 FontList
font_list1("Arial, 16px");
266 EXPECT_EQ(font1
.GetHeight(), font_list1
.GetHeight());
267 EXPECT_EQ(font1
.GetBaseline(), font_list1
.GetBaseline());
269 // If there are two different fonts, the font list returns the max value
270 // for the baseline (ascent) and height.
271 Font
font2("Symbol", 16);
273 base::StringToLowerASCII(font2
.GetActualFontNameForTesting()));
274 EXPECT_NE(font1
.GetBaseline(), font2
.GetBaseline());
275 // TODO(ananta): Find a size and font pair with reliably distinct descents.
276 EXPECT_NE(font1
.GetHeight(), font2
.GetHeight());
277 std::vector
<Font
> fonts
;
278 fonts
.push_back(font1
);
279 fonts
.push_back(font2
);
280 FontList
font_list_mix(fonts
);
281 // ascent of FontList == max(ascent of Fonts)
282 EXPECT_EQ(std::max(font1
.GetBaseline(), font2
.GetBaseline()),
283 font_list_mix
.GetBaseline());
284 // descent of FontList == max(descent of Fonts)
285 EXPECT_EQ(std::max(font1
.GetHeight() - font1
.GetBaseline(),
286 font2
.GetHeight() - font2
.GetBaseline()),
287 font_list_mix
.GetHeight() - font_list_mix
.GetBaseline());
290 // TODO(489354): Enable this on android.
291 #if defined(OS_ANDROID)
292 #define MAYBE_Fonts_DeriveWithHeightUpperBound \
293 DISABLED_Fonts_DeriveWithHeightUpperBound
295 #define MAYBE_Fonts_DeriveWithHeightUpperBound Fonts_DeriveWithHeightUpperBound
297 TEST(FontListTest
, MAYBE_Fonts_DeriveWithHeightUpperBound
) {
298 std::vector
<Font
> fonts
;
300 fonts
.push_back(gfx::Font("Arial", 18));
301 fonts
.push_back(gfx::Font("Sans serif", 18));
302 fonts
.push_back(gfx::Font("Symbol", 18));
303 FontList font_list
= FontList(fonts
);
305 // A smaller upper bound should derive a font list with a smaller height.
306 const int height_1
= font_list
.GetHeight() - 5;
307 FontList derived_1
= font_list
.DeriveWithHeightUpperBound(height_1
);
308 EXPECT_LE(derived_1
.GetHeight(), height_1
);
309 EXPECT_LT(derived_1
.GetHeight(), font_list
.GetHeight());
310 EXPECT_LT(derived_1
.GetFontSize(), font_list
.GetFontSize());
312 // A larger upper bound should not change the height of the font list.
313 const int height_2
= font_list
.GetHeight() + 5;
314 FontList derived_2
= font_list
.DeriveWithHeightUpperBound(height_2
);
315 EXPECT_LE(derived_2
.GetHeight(), height_2
);
316 EXPECT_EQ(font_list
.GetHeight(), derived_2
.GetHeight());
317 EXPECT_EQ(font_list
.GetFontSize(), derived_2
.GetFontSize());