OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/font_list.h" | 5 #include "ui/gfx/font_list.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 font_string += "|italic"; | 26 font_string += "|italic"; |
27 if (style & gfx::Font::UNDERLINE) | 27 if (style & gfx::Font::UNDERLINE) |
28 font_string += "|underline"; | 28 font_string += "|underline"; |
29 return font_string; | 29 return font_string; |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 namespace gfx { | 34 namespace gfx { |
35 | 35 |
36 TEST(FontListTest, FontDescString_FromDescString) { | 36 TEST(FontListTest, ParseDescription) { |
37 // Test init from font name style size string. | 37 std::vector<std::string> families; |
38 FontList font_list = FontList("Droid Sans serif, Sans serif, 10px"); | 38 int style = gfx::Font::NORMAL; |
39 EXPECT_EQ("Droid Sans serif, Sans serif, 10px", | 39 int size_pixels = 0; |
40 font_list.GetFontDescriptionString()); | |
41 } | |
42 | 40 |
43 TEST(FontListTest, FontDescString_FromFontNamesStyleAndSize) { | 41 // Parse a well-formed description containing styles and a size. |
44 // Test init from font names, style and size. | 42 EXPECT_TRUE(FontList::ParseDescription("Arial,Helvetica,Bold Italic 12px", |
45 std::vector<std::string> font_names; | 43 &families, &style, &size_pixels)); |
46 font_names.push_back("Arial"); | 44 ASSERT_EQ(2U, families.size()); |
47 font_names.push_back("Droid Sans serif"); | 45 EXPECT_EQ("Arial", families[0]); |
48 int font_style = Font::BOLD | Font::ITALIC | Font::UNDERLINE; | 46 EXPECT_EQ("Helvetica", families[1]); |
49 int font_size = 11; | 47 EXPECT_EQ(gfx::Font::BOLD | gfx::Font::ITALIC, style); |
50 FontList font_list = FontList(font_names, font_style, font_size); | 48 EXPECT_EQ(12, size_pixels); |
51 // "Underline" doesn't appear in the font description string. | |
52 EXPECT_EQ("Arial,Droid Sans serif,Bold Italic 11px", | |
53 font_list.GetFontDescriptionString()); | |
54 } | |
55 | 49 |
56 TEST(FontListTest, FontDescString_FromFont) { | 50 // Whitespace should be removed. |
57 // Test init from Font. | 51 EXPECT_TRUE(FontList::ParseDescription(" Verdana , Italic Bold 10px ", |
58 Font font("Arial", 8); | 52 &families, &style, &size_pixels)); |
59 FontList font_list = FontList(font); | 53 ASSERT_EQ(1U, families.size()); |
60 EXPECT_EQ("Arial,8px", font_list.GetFontDescriptionString()); | 54 EXPECT_EQ("Verdana", families[0]); |
61 } | 55 EXPECT_EQ(gfx::Font::BOLD | gfx::Font::ITALIC, style); |
| 56 EXPECT_EQ(10, size_pixels); |
62 | 57 |
63 TEST(FontListTest, FontDescString_FromFontWithNonNormalStyle) { | 58 // Invalid descriptions should be rejected. |
64 // Test init from Font with non-normal style. | 59 EXPECT_FALSE(FontList::ParseDescription("", &families, &style, &size_pixels)); |
65 Font font("Arial", 8); | 60 EXPECT_FALSE(FontList::ParseDescription("Arial", &families, &style, |
66 FontList font_list = FontList(font.Derive(2, Font::BOLD)); | 61 &size_pixels)); |
67 EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString()); | 62 EXPECT_FALSE(FontList::ParseDescription("Arial,12", &families, &style, |
68 | 63 &size_pixels)); |
69 font_list = FontList(font.Derive(-2, Font::ITALIC)); | 64 EXPECT_FALSE(FontList::ParseDescription("Arial 12px", &families, &style, |
70 EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString()); | 65 &size_pixels)); |
71 | 66 EXPECT_FALSE(FontList::ParseDescription("Arial,12px,", &families, &style, |
72 // "Underline" doesn't appear in the font description string. | 67 &size_pixels)); |
73 font_list = FontList(font.Derive(-4, Font::UNDERLINE)); | 68 EXPECT_FALSE(FontList::ParseDescription("Arial,0px", &families, &style, |
74 EXPECT_EQ("Arial,4px", font_list.GetFontDescriptionString()); | 69 &size_pixels)); |
75 } | 70 EXPECT_FALSE(FontList::ParseDescription("Arial,-1px", &families, &style, |
76 | 71 &size_pixels)); |
77 TEST(FontListTest, FontDescString_FromFontVector) { | 72 EXPECT_FALSE(FontList::ParseDescription("Arial,foo 12px", &families, &style, |
78 // Test init from Font vector. | 73 &size_pixels)); |
79 Font font("Arial", 8); | |
80 Font font_1("Sans serif", 10); | |
81 std::vector<Font> fonts; | |
82 fonts.push_back(font.Derive(0, Font::BOLD)); | |
83 fonts.push_back(font_1.Derive(-2, Font::BOLD)); | |
84 FontList font_list = FontList(fonts); | |
85 EXPECT_EQ("Arial,Sans serif,Bold 8px", font_list.GetFontDescriptionString()); | |
86 } | 74 } |
87 | 75 |
88 TEST(FontListTest, Fonts_FromDescString) { | 76 TEST(FontListTest, Fonts_FromDescString) { |
89 // Test init from font name size string. | 77 // Test init from font name size string. |
90 FontList font_list = FontList("serif,Sans serif, 13px"); | 78 FontList font_list = FontList("serif,Sans serif, 13px"); |
91 const std::vector<Font>& fonts = font_list.GetFonts(); | 79 const std::vector<Font>& fonts = font_list.GetFonts(); |
92 EXPECT_EQ(2U, fonts.size()); | 80 ASSERT_EQ(2U, fonts.size()); |
93 EXPECT_EQ("serif|13", FontToString(fonts[0])); | 81 EXPECT_EQ("serif|13", FontToString(fonts[0])); |
94 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); | 82 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); |
95 } | 83 } |
96 | 84 |
97 TEST(FontListTest, Fonts_FromDescStringInFlexibleFormat) { | 85 TEST(FontListTest, Fonts_FromDescStringInFlexibleFormat) { |
98 // Test init from font name size string with flexible format. | 86 // Test init from font name size string with flexible format. |
99 FontList font_list = FontList(" serif , Sans serif , 13px"); | 87 FontList font_list = FontList(" serif , Sans serif , 13px"); |
100 const std::vector<Font>& fonts = font_list.GetFonts(); | 88 const std::vector<Font>& fonts = font_list.GetFonts(); |
101 EXPECT_EQ(2U, fonts.size()); | 89 ASSERT_EQ(2U, fonts.size()); |
102 EXPECT_EQ("serif|13", FontToString(fonts[0])); | 90 EXPECT_EQ("serif|13", FontToString(fonts[0])); |
103 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); | 91 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); |
104 } | 92 } |
105 | 93 |
106 TEST(FontListTest, Fonts_FromDescStringWithStyleInFlexibleFormat) { | 94 TEST(FontListTest, Fonts_FromDescStringWithStyleInFlexibleFormat) { |
107 // Test init from font name style size string with flexible format. | 95 // Test init from font name style size string with flexible format. |
108 FontList font_list = FontList(" serif , Sans serif , Bold " | 96 FontList font_list = FontList(" serif , Sans serif , Bold " |
109 " Italic 13px"); | 97 " Italic 13px"); |
110 const std::vector<Font>& fonts = font_list.GetFonts(); | 98 const std::vector<Font>& fonts = font_list.GetFonts(); |
111 EXPECT_EQ(2U, fonts.size()); | 99 ASSERT_EQ(2U, fonts.size()); |
112 EXPECT_EQ("serif|13|bold|italic", FontToString(fonts[0])); | 100 EXPECT_EQ("serif|13|bold|italic", FontToString(fonts[0])); |
113 EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts[1])); | 101 EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts[1])); |
114 } | 102 } |
115 | 103 |
116 TEST(FontListTest, Fonts_FromFont) { | 104 TEST(FontListTest, Fonts_FromFont) { |
117 // Test init from Font. | 105 // Test init from Font. |
118 Font font("Arial", 8); | 106 Font font("Arial", 8); |
119 FontList font_list = FontList(font); | 107 FontList font_list = FontList(font); |
120 const std::vector<Font>& fonts = font_list.GetFonts(); | 108 const std::vector<Font>& fonts = font_list.GetFonts(); |
121 EXPECT_EQ(1U, fonts.size()); | 109 ASSERT_EQ(1U, fonts.size()); |
122 EXPECT_EQ("Arial|8", FontToString(fonts[0])); | 110 EXPECT_EQ("Arial|8", FontToString(fonts[0])); |
123 } | 111 } |
124 | 112 |
125 TEST(FontListTest, Fonts_FromFontWithNonNormalStyle) { | 113 TEST(FontListTest, Fonts_FromFontWithNonNormalStyle) { |
126 // Test init from Font with non-normal style. | 114 // Test init from Font with non-normal style. |
127 Font font("Arial", 8); | 115 Font font("Arial", 8); |
128 FontList font_list = FontList(font.Derive(2, Font::BOLD)); | 116 FontList font_list = FontList(font.Derive(2, Font::BOLD)); |
129 std::vector<Font> fonts = font_list.GetFonts(); | 117 std::vector<Font> fonts = font_list.GetFonts(); |
130 EXPECT_EQ(1U, fonts.size()); | 118 ASSERT_EQ(1U, fonts.size()); |
131 EXPECT_EQ("Arial|10|bold", FontToString(fonts[0])); | 119 EXPECT_EQ("Arial|10|bold", FontToString(fonts[0])); |
132 | 120 |
133 font_list = FontList(font.Derive(-2, Font::ITALIC)); | 121 font_list = FontList(font.Derive(-2, Font::ITALIC)); |
134 fonts = font_list.GetFonts(); | 122 fonts = font_list.GetFonts(); |
135 EXPECT_EQ(1U, fonts.size()); | 123 ASSERT_EQ(1U, fonts.size()); |
136 EXPECT_EQ("Arial|6|italic", FontToString(fonts[0])); | 124 EXPECT_EQ("Arial|6|italic", FontToString(fonts[0])); |
137 } | 125 } |
138 | 126 |
139 TEST(FontListTest, Fonts_FromFontVector) { | 127 TEST(FontListTest, Fonts_FromFontVector) { |
140 // Test init from Font vector. | 128 // Test init from Font vector. |
141 Font font("Arial", 8); | 129 Font font("Arial", 8); |
142 Font font_1("Sans serif", 10); | 130 Font font_1("Sans serif", 10); |
143 std::vector<Font> input_fonts; | 131 std::vector<Font> input_fonts; |
144 input_fonts.push_back(font.Derive(0, Font::BOLD)); | 132 input_fonts.push_back(font.Derive(0, Font::BOLD)); |
145 input_fonts.push_back(font_1.Derive(-2, Font::BOLD)); | 133 input_fonts.push_back(font_1.Derive(-2, Font::BOLD)); |
146 FontList font_list = FontList(input_fonts); | 134 FontList font_list = FontList(input_fonts); |
147 const std::vector<Font>& fonts = font_list.GetFonts(); | 135 const std::vector<Font>& fonts = font_list.GetFonts(); |
148 EXPECT_EQ(2U, fonts.size()); | 136 ASSERT_EQ(2U, fonts.size()); |
149 EXPECT_EQ("Arial|8|bold", FontToString(fonts[0])); | 137 EXPECT_EQ("Arial|8|bold", FontToString(fonts[0])); |
150 EXPECT_EQ("Sans serif|8|bold", FontToString(fonts[1])); | 138 EXPECT_EQ("Sans serif|8|bold", FontToString(fonts[1])); |
151 } | 139 } |
152 | 140 |
153 TEST(FontListTest, Fonts_DescStringWithStyleInFlexibleFormat_RoundTrip) { | |
154 // Test round trip from font description string to font vector to | |
155 // font description string. | |
156 FontList font_list = FontList(" serif , Sans serif , Bold " | |
157 " Italic 13px"); | |
158 | |
159 const std::vector<Font>& fonts = font_list.GetFonts(); | |
160 FontList font_list_1 = FontList(fonts); | |
161 const std::string& desc_str = font_list_1.GetFontDescriptionString(); | |
162 | |
163 EXPECT_EQ("serif,Sans serif,Bold Italic 13px", desc_str); | |
164 } | |
165 | |
166 TEST(FontListTest, Fonts_FontVector_RoundTrip) { | |
167 // Test round trip from font vector to font description string to font vector. | |
168 Font font("Arial", 8); | |
169 Font font_1("Sans serif", 10); | |
170 std::vector<Font> input_fonts; | |
171 input_fonts.push_back(font.Derive(0, Font::BOLD)); | |
172 input_fonts.push_back(font_1.Derive(-2, Font::BOLD)); | |
173 FontList font_list = FontList(input_fonts); | |
174 | |
175 const std::string& desc_string = font_list.GetFontDescriptionString(); | |
176 FontList font_list_1 = FontList(desc_string); | |
177 const std::vector<Font>& round_trip_fonts = font_list_1.GetFonts(); | |
178 | |
179 EXPECT_EQ(2U, round_trip_fonts.size()); | |
180 EXPECT_EQ("Arial|8|bold", FontToString(round_trip_fonts[0])); | |
181 EXPECT_EQ("Sans serif|8|bold", FontToString(round_trip_fonts[1])); | |
182 } | |
183 | |
184 TEST(FontListTest, FontDescString_GetStyle) { | 141 TEST(FontListTest, FontDescString_GetStyle) { |
185 FontList font_list = FontList("Arial,Sans serif, 8px"); | 142 FontList font_list = FontList("Arial,Sans serif, 8px"); |
186 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); | 143 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); |
187 | 144 |
188 font_list = FontList("Arial,Sans serif,Bold 8px"); | 145 font_list = FontList("Arial,Sans serif,Bold 8px"); |
189 EXPECT_EQ(Font::BOLD, font_list.GetFontStyle()); | 146 EXPECT_EQ(Font::BOLD, font_list.GetFontStyle()); |
190 | 147 |
191 font_list = FontList("Arial,Sans serif,Italic 8px"); | 148 font_list = FontList("Arial,Sans serif,Italic 8px"); |
192 EXPECT_EQ(Font::ITALIC, font_list.GetFontStyle()); | 149 EXPECT_EQ(Font::ITALIC, font_list.GetFontStyle()); |
193 | 150 |
194 font_list = FontList("Arial,Italic Bold 8px"); | 151 font_list = FontList("Arial,Italic Bold 8px"); |
195 EXPECT_EQ(Font::BOLD | Font::ITALIC, font_list.GetFontStyle()); | 152 EXPECT_EQ(Font::BOLD | Font::ITALIC, font_list.GetFontStyle()); |
196 } | 153 } |
197 | 154 |
198 TEST(FontListTest, Fonts_GetStyle) { | 155 TEST(FontListTest, Fonts_GetStyle) { |
199 std::vector<Font> fonts; | 156 std::vector<Font> fonts; |
200 fonts.push_back(gfx::Font("Arial", 8)); | 157 fonts.push_back(gfx::Font("Arial", 8)); |
201 fonts.push_back(gfx::Font("Sans serif", 8)); | 158 fonts.push_back(gfx::Font("Sans serif", 8)); |
202 FontList font_list = FontList(fonts); | 159 FontList font_list = FontList(fonts); |
203 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); | 160 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); |
204 fonts[0] = fonts[0].Derive(0, Font::ITALIC | Font::BOLD); | 161 fonts[0] = fonts[0].Derive(0, Font::ITALIC | Font::BOLD); |
205 fonts[1] = fonts[1].Derive(0, Font::ITALIC | Font::BOLD); | 162 fonts[1] = fonts[1].Derive(0, Font::ITALIC | Font::BOLD); |
206 font_list = FontList(fonts); | 163 font_list = FontList(fonts); |
207 EXPECT_EQ(Font::ITALIC | Font::BOLD, font_list.GetFontStyle()); | 164 EXPECT_EQ(Font::ITALIC | Font::BOLD, font_list.GetFontStyle()); |
208 } | 165 } |
209 | 166 |
210 TEST(FontListTest, FontDescString_Derive) { | |
211 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px"); | |
212 | |
213 FontList derived = font_list.Derive(10, Font::ITALIC | Font::UNDERLINE); | |
214 EXPECT_EQ("Arial,Sans serif,Italic 18px", derived.GetFontDescriptionString()); | |
215 EXPECT_EQ(Font::ITALIC | Font::UNDERLINE, derived.GetFontStyle()); | |
216 | |
217 // FontList has a special case for Font::UNDERLINE. See if the handling of | |
218 // Font::UNDERLINE in GetFonts() is okay or not. | |
219 derived.GetFonts(); | |
220 EXPECT_EQ(Font::ITALIC | Font::UNDERLINE, derived.GetFontStyle()); | |
221 } | |
222 | |
223 TEST(FontListTest, Fonts_Derive) { | 167 TEST(FontListTest, Fonts_Derive) { |
224 std::vector<Font> fonts; | 168 std::vector<Font> fonts; |
225 fonts.push_back(gfx::Font("Arial", 8)); | 169 fonts.push_back(gfx::Font("Arial", 8)); |
226 fonts.push_back(gfx::Font("Sans serif", 8)); | 170 fonts.push_back(gfx::Font("Sans serif", 8)); |
227 FontList font_list = FontList(fonts); | 171 FontList font_list = FontList(fonts); |
228 | 172 |
229 FontList derived = font_list.Derive(5, Font::BOLD | Font::UNDERLINE); | 173 FontList derived = font_list.Derive(5, Font::BOLD | Font::UNDERLINE); |
230 const std::vector<Font>& derived_fonts = derived.GetFonts(); | 174 const std::vector<Font>& derived_fonts = derived.GetFonts(); |
231 | 175 |
232 EXPECT_EQ(2U, derived_fonts.size()); | 176 EXPECT_EQ(2U, derived_fonts.size()); |
233 EXPECT_EQ("Arial|13|bold|underline", FontToString(derived_fonts[0])); | 177 EXPECT_EQ("Arial|13|bold|underline", FontToString(derived_fonts[0])); |
234 EXPECT_EQ("Sans serif|13|bold|underline", FontToString(derived_fonts[1])); | 178 EXPECT_EQ("Sans serif|13|bold|underline", FontToString(derived_fonts[1])); |
235 } | 179 } |
236 | 180 |
237 TEST(FontListTest, FontDescString_DeriveWithSizeDelta) { | |
238 FontList font_list = FontList("Arial,Sans serif,Bold 18px"); | |
239 | |
240 FontList derived = font_list.DeriveWithSizeDelta(-8); | |
241 EXPECT_EQ("Arial,Sans serif,Bold 10px", | |
242 derived.GetFontDescriptionString()); | |
243 } | |
244 | |
245 TEST(FontListTest, Fonts_DeriveWithSizeDelta) { | 181 TEST(FontListTest, Fonts_DeriveWithSizeDelta) { |
246 std::vector<Font> fonts; | 182 std::vector<Font> fonts; |
247 fonts.push_back(gfx::Font("Arial", 18).Derive(0, Font::ITALIC)); | 183 fonts.push_back(gfx::Font("Arial", 18).Derive(0, Font::ITALIC)); |
248 fonts.push_back(gfx::Font("Sans serif", 18).Derive(0, Font::ITALIC)); | 184 fonts.push_back(gfx::Font("Sans serif", 18).Derive(0, Font::ITALIC)); |
249 FontList font_list = FontList(fonts); | 185 FontList font_list = FontList(fonts); |
250 | 186 |
251 FontList derived = font_list.DeriveWithSizeDelta(-5); | 187 FontList derived = font_list.DeriveWithSizeDelta(-5); |
252 const std::vector<Font>& derived_fonts = derived.GetFonts(); | 188 const std::vector<Font>& derived_fonts = derived.GetFonts(); |
253 | 189 |
254 EXPECT_EQ(2U, derived_fonts.size()); | 190 EXPECT_EQ(2U, derived_fonts.size()); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 | 239 |
304 // A larger upper bound should not change the height of the font list. | 240 // A larger upper bound should not change the height of the font list. |
305 const int height_2 = font_list.GetHeight() + 5; | 241 const int height_2 = font_list.GetHeight() + 5; |
306 FontList derived_2 = font_list.DeriveWithHeightUpperBound(height_2); | 242 FontList derived_2 = font_list.DeriveWithHeightUpperBound(height_2); |
307 EXPECT_LE(derived_2.GetHeight(), height_2); | 243 EXPECT_LE(derived_2.GetHeight(), height_2); |
308 EXPECT_EQ(font_list.GetHeight(), derived_2.GetHeight()); | 244 EXPECT_EQ(font_list.GetHeight(), derived_2.GetHeight()); |
309 EXPECT_EQ(font_list.GetFontSize(), derived_2.GetFontSize()); | 245 EXPECT_EQ(font_list.GetFontSize(), derived_2.GetFontSize()); |
310 } | 246 } |
311 | 247 |
312 } // namespace gfx | 248 } // namespace gfx |
OLD | NEW |