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"
10 #include "base/strings/string_number_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 // Helper function for comparing fonts for equality.
16 std::string
FontToString(const gfx::Font
& font
) {
17 std::string font_string
= font
.GetFontName();
19 font_string
+= base::IntToString(font
.GetFontSize());
20 int style
= font
.GetStyle();
21 if (style
& gfx::Font::BOLD
)
22 font_string
+= "|bold";
23 if (style
& gfx::Font::ITALIC
)
24 font_string
+= "|italic";
32 TEST(FontListTest
, FontDescString_FromDescString
) {
33 // Test init from font name style size string.
34 FontList font_list
= FontList("Droid Sans serif, Sans serif, 10px");
35 const std::string
& font_str
= font_list
.GetFontDescriptionString();
36 EXPECT_EQ("Droid Sans serif, Sans serif, 10px", font_str
);
39 TEST(FontListTest
, FontDescString_FromFont
) {
40 // Test init from Font.
41 Font
font("Arial", 8);
42 FontList font_list
= FontList(font
);
43 const std::string
& font_str
= font_list
.GetFontDescriptionString();
44 EXPECT_EQ("Arial,8px", font_str
);
47 TEST(FontListTest
, FontDescString_FromFontWithNonNormalStyle
) {
48 // Test init from Font with non-normal style.
49 Font
font("Arial", 8);
50 FontList font_list
= FontList(font
.DeriveFont(2, Font::BOLD
));
51 EXPECT_EQ("Arial,Bold 10px", font_list
.GetFontDescriptionString());
53 font_list
= FontList(font
.DeriveFont(-2, Font::ITALIC
));
54 EXPECT_EQ("Arial,Italic 6px", font_list
.GetFontDescriptionString());
57 TEST(FontListTest
, FontDescString_FromFontVector
) {
58 // Test init from Font vector.
59 Font
font("Arial", 8);
60 Font
font_1("Sans serif", 10);
61 std::vector
<Font
> fonts
;
62 fonts
.push_back(font
.DeriveFont(0, Font::BOLD
));
63 fonts
.push_back(font_1
.DeriveFont(-2, Font::BOLD
));
64 FontList font_list
= FontList(fonts
);
65 const std::string
& font_str
= font_list
.GetFontDescriptionString();
66 EXPECT_EQ("Arial,Sans serif,Bold 8px", font_str
);
69 TEST(FontListTest
, Fonts_FromDescString
) {
70 // Test init from font name size string.
71 FontList font_list
= FontList("serif,Sans serif, 13px");
72 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
73 EXPECT_EQ(2U, fonts
.size());
74 EXPECT_EQ("serif|13", FontToString(fonts
[0]));
75 EXPECT_EQ("Sans serif|13", FontToString(fonts
[1]));
78 TEST(FontListTest
, Fonts_FromDescStringInFlexibleFormat
) {
79 // Test init from font name size string with flexible format.
80 FontList font_list
= FontList(" serif , Sans serif , 13px");
81 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
82 EXPECT_EQ(2U, fonts
.size());
83 EXPECT_EQ("serif|13", FontToString(fonts
[0]));
84 EXPECT_EQ("Sans serif|13", FontToString(fonts
[1]));
87 TEST(FontListTest
, Fonts_FromDescStringWithStyleInFlexibleFormat
) {
88 // Test init from font name style size string with flexible format.
89 FontList font_list
= FontList(" serif , Sans serif , Bold "
91 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
92 EXPECT_EQ(2U, fonts
.size());
93 EXPECT_EQ("serif|13|bold|italic", FontToString(fonts
[0]));
94 EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts
[1]));
97 TEST(FontListTest
, Fonts_FromFont
) {
98 // Test init from Font.
99 Font
font("Arial", 8);
100 FontList font_list
= FontList(font
);
101 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
102 EXPECT_EQ(1U, fonts
.size());
103 EXPECT_EQ("Arial|8", FontToString(fonts
[0]));
106 TEST(FontListTest
, Fonts_FromFontWithNonNormalStyle
) {
107 // Test init from Font with non-normal style.
108 Font
font("Arial", 8);
109 FontList font_list
= FontList(font
.DeriveFont(2, Font::BOLD
));
110 std::vector
<Font
> fonts
= font_list
.GetFonts();
111 EXPECT_EQ(1U, fonts
.size());
112 EXPECT_EQ("Arial|10|bold", FontToString(fonts
[0]));
114 font_list
= FontList(font
.DeriveFont(-2, Font::ITALIC
));
115 fonts
= font_list
.GetFonts();
116 EXPECT_EQ(1U, fonts
.size());
117 EXPECT_EQ("Arial|6|italic", FontToString(fonts
[0]));
120 TEST(FontListTest
, Fonts_FromFontVector
) {
121 // Test init from Font vector.
122 Font
font("Arial", 8);
123 Font
font_1("Sans serif", 10);
124 std::vector
<Font
> input_fonts
;
125 input_fonts
.push_back(font
.DeriveFont(0, Font::BOLD
));
126 input_fonts
.push_back(font_1
.DeriveFont(-2, Font::BOLD
));
127 FontList font_list
= FontList(input_fonts
);
128 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
129 EXPECT_EQ(2U, fonts
.size());
130 EXPECT_EQ("Arial|8|bold", FontToString(fonts
[0]));
131 EXPECT_EQ("Sans serif|8|bold", FontToString(fonts
[1]));
134 TEST(FontListTest
, Fonts_DescStringWithStyleInFlexibleFormat_RoundTrip
) {
135 // Test round trip from font description string to font vector to
136 // font description string.
137 FontList font_list
= FontList(" serif , Sans serif , Bold "
140 const std::vector
<Font
>& fonts
= font_list
.GetFonts();
141 FontList font_list_1
= FontList(fonts
);
142 const std::string
& desc_str
= font_list_1
.GetFontDescriptionString();
144 EXPECT_EQ("serif,Sans serif,Bold Italic 13px", desc_str
);
147 TEST(FontListTest
, Fonts_FontVector_RoundTrip
) {
148 // Test round trip from font vector to font description string to font vector.
149 Font
font("Arial", 8);
150 Font
font_1("Sans serif", 10);
151 std::vector
<Font
> input_fonts
;
152 input_fonts
.push_back(font
.DeriveFont(0, Font::BOLD
));
153 input_fonts
.push_back(font_1
.DeriveFont(-2, Font::BOLD
));
154 FontList font_list
= FontList(input_fonts
);
156 const std::string
& desc_string
= font_list
.GetFontDescriptionString();
157 FontList font_list_1
= FontList(desc_string
);
158 const std::vector
<Font
>& round_trip_fonts
= font_list_1
.GetFonts();
160 EXPECT_EQ(2U, round_trip_fonts
.size());
161 EXPECT_EQ("Arial|8|bold", FontToString(round_trip_fonts
[0]));
162 EXPECT_EQ("Sans serif|8|bold", FontToString(round_trip_fonts
[1]));
165 TEST(FontListTest
, FontDescString_GetStyle
) {
166 FontList font_list
= FontList("Arial,Sans serif, 8px");
167 EXPECT_EQ(Font::NORMAL
, font_list
.GetFontStyle());
169 font_list
= FontList("Arial,Sans serif,Bold 8px");
170 EXPECT_EQ(Font::BOLD
, font_list
.GetFontStyle());
172 font_list
= FontList("Arial,Sans serif,Italic 8px");
173 EXPECT_EQ(Font::ITALIC
, font_list
.GetFontStyle());
175 font_list
= FontList("Arial,Italic Bold 8px");
176 EXPECT_EQ(Font::BOLD
| Font::ITALIC
, font_list
.GetFontStyle());
179 TEST(FontListTest
, Fonts_GetStyle
) {
180 std::vector
<Font
> fonts
;
181 fonts
.push_back(gfx::Font("Arial", 8));
182 fonts
.push_back(gfx::Font("Sans serif", 8));
183 FontList font_list
= FontList(fonts
);
184 EXPECT_EQ(Font::NORMAL
, font_list
.GetFontStyle());
185 fonts
[0] = fonts
[0].DeriveFont(0, Font::ITALIC
| Font::BOLD
);
186 fonts
[1] = fonts
[1].DeriveFont(0, Font::ITALIC
| Font::BOLD
);
187 font_list
= FontList(fonts
);
188 EXPECT_EQ(Font::ITALIC
| Font::BOLD
, font_list
.GetFontStyle());
191 TEST(FontListTest
, FontDescString_DeriveFontList
) {
192 FontList font_list
= FontList("Arial,Sans serif, 8px");
194 FontList derived
= font_list
.DeriveFontList(Font::BOLD
| Font::ITALIC
);
195 EXPECT_EQ("Arial,Sans serif,Bold Italic 8px",
196 derived
.GetFontDescriptionString());
199 TEST(FontListTest
, Fonts_DeriveFontList
) {
200 std::vector
<Font
> fonts
;
201 fonts
.push_back(gfx::Font("Arial", 8));
202 fonts
.push_back(gfx::Font("Sans serif", 8));
203 FontList font_list
= FontList(fonts
);
205 FontList derived
= font_list
.DeriveFontList(Font::BOLD
| Font::ITALIC
);
206 const std::vector
<Font
>& derived_fonts
= derived
.GetFonts();
208 EXPECT_EQ(2U, derived_fonts
.size());
209 EXPECT_EQ("Arial|8|bold|italic", FontToString(derived_fonts
[0]));
210 EXPECT_EQ("Sans serif|8|bold|italic", FontToString(derived_fonts
[1]));
213 TEST(FontListTest
, FontDescString_DeriveFontListWithSize
) {
214 FontList font_list
= FontList("Arial,Sans serif,Bold Italic 8px");
216 FontList derived
= font_list
.DeriveFontListWithSize(10);
217 EXPECT_EQ("Arial,Sans serif,Bold Italic 10px",
218 derived
.GetFontDescriptionString());
221 TEST(FontListTest
, Fonts_DeriveFontListWithSize
) {
222 std::vector
<Font
> fonts
;
223 fonts
.push_back(gfx::Font("Arial", 8));
224 fonts
.push_back(gfx::Font("Sans serif", 8));
225 FontList font_list
= FontList(fonts
);
227 FontList derived
= font_list
.DeriveFontListWithSize(5);
228 const std::vector
<Font
>& derived_fonts
= derived
.GetFonts();
230 EXPECT_EQ(2U, derived_fonts
.size());
231 EXPECT_EQ("Arial|5", FontToString(derived_fonts
[0]));
232 EXPECT_EQ("Sans serif|5", FontToString(derived_fonts
[1]));