Remove WebKitTestRunner::setClientWindowRect.
[chromium-blink-merge.git] / ui / gfx / font_list_unittest.cc
bloba62a13db469e312edc6b85c301412109a5f396e4
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"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/strings/string_number_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
16 // Helper function for comparing fonts for equality.
17 std::string FontToString(const gfx::Font& font) {
18 std::string font_string = font.GetFontName();
19 font_string += "|";
20 font_string += base::IntToString(font.GetFontSize());
21 int style = font.GetStyle();
22 if (style & gfx::Font::BOLD)
23 font_string += "|bold";
24 if (style & gfx::Font::ITALIC)
25 font_string += "|italic";
26 return font_string;
29 } // namespace
31 namespace gfx {
33 TEST(FontListTest, FontDescString_FromDescString) {
34 // Test init from font name style size string.
35 FontList font_list = FontList("Droid Sans serif, Sans serif, 10px");
36 EXPECT_EQ("Droid Sans serif, Sans serif, 10px",
37 font_list.GetFontDescriptionString());
40 TEST(FontListTest, FontDescString_FromFontNamesStyleAndSize) {
41 // Test init from font names, style and size.
42 std::vector<std::string> font_names;
43 font_names.push_back("Arial");
44 font_names.push_back("Droid Sans serif");
45 int font_style = Font::BOLD | Font::ITALIC;
46 int font_size = 11;
47 FontList font_list = FontList(font_names, font_style, font_size);
48 EXPECT_EQ("Arial,Droid Sans serif,Bold Italic 11px",
49 font_list.GetFontDescriptionString());
52 TEST(FontListTest, FontDescString_FromFont) {
53 // Test init from Font.
54 Font font("Arial", 8);
55 FontList font_list = FontList(font);
56 EXPECT_EQ("Arial,8px", font_list.GetFontDescriptionString());
59 TEST(FontListTest, FontDescString_FromFontWithNonNormalStyle) {
60 // Test init from Font with non-normal style.
61 Font font("Arial", 8);
62 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD));
63 EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString());
65 font_list = FontList(font.DeriveFont(-2, Font::ITALIC));
66 EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString());
69 TEST(FontListTest, FontDescString_FromFontVector) {
70 // Test init from Font vector.
71 Font font("Arial", 8);
72 Font font_1("Sans serif", 10);
73 std::vector<Font> fonts;
74 fonts.push_back(font.DeriveFont(0, Font::BOLD));
75 fonts.push_back(font_1.DeriveFont(-2, Font::BOLD));
76 FontList font_list = FontList(fonts);
77 EXPECT_EQ("Arial,Sans serif,Bold 8px", font_list.GetFontDescriptionString());
80 TEST(FontListTest, Fonts_FromDescString) {
81 // Test init from font name size string.
82 FontList font_list = FontList("serif,Sans serif, 13px");
83 const std::vector<Font>& fonts = font_list.GetFonts();
84 EXPECT_EQ(2U, fonts.size());
85 EXPECT_EQ("serif|13", FontToString(fonts[0]));
86 EXPECT_EQ("Sans serif|13", FontToString(fonts[1]));
89 TEST(FontListTest, Fonts_FromDescStringInFlexibleFormat) {
90 // Test init from font name size string with flexible format.
91 FontList font_list = FontList(" serif , Sans serif , 13px");
92 const std::vector<Font>& fonts = font_list.GetFonts();
93 EXPECT_EQ(2U, fonts.size());
94 EXPECT_EQ("serif|13", FontToString(fonts[0]));
95 EXPECT_EQ("Sans serif|13", FontToString(fonts[1]));
98 TEST(FontListTest, Fonts_FromDescStringWithStyleInFlexibleFormat) {
99 // Test init from font name style size string with flexible format.
100 FontList font_list = FontList(" serif , Sans serif , Bold "
101 " Italic 13px");
102 const std::vector<Font>& fonts = font_list.GetFonts();
103 EXPECT_EQ(2U, fonts.size());
104 EXPECT_EQ("serif|13|bold|italic", FontToString(fonts[0]));
105 EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts[1]));
108 TEST(FontListTest, Fonts_FromFont) {
109 // Test init from Font.
110 Font font("Arial", 8);
111 FontList font_list = FontList(font);
112 const std::vector<Font>& fonts = font_list.GetFonts();
113 EXPECT_EQ(1U, fonts.size());
114 EXPECT_EQ("Arial|8", FontToString(fonts[0]));
117 TEST(FontListTest, Fonts_FromFontWithNonNormalStyle) {
118 // Test init from Font with non-normal style.
119 Font font("Arial", 8);
120 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD));
121 std::vector<Font> fonts = font_list.GetFonts();
122 EXPECT_EQ(1U, fonts.size());
123 EXPECT_EQ("Arial|10|bold", FontToString(fonts[0]));
125 font_list = FontList(font.DeriveFont(-2, Font::ITALIC));
126 fonts = font_list.GetFonts();
127 EXPECT_EQ(1U, fonts.size());
128 EXPECT_EQ("Arial|6|italic", FontToString(fonts[0]));
131 TEST(FontListTest, Fonts_FromFontVector) {
132 // Test init from Font vector.
133 Font font("Arial", 8);
134 Font font_1("Sans serif", 10);
135 std::vector<Font> input_fonts;
136 input_fonts.push_back(font.DeriveFont(0, Font::BOLD));
137 input_fonts.push_back(font_1.DeriveFont(-2, Font::BOLD));
138 FontList font_list = FontList(input_fonts);
139 const std::vector<Font>& fonts = font_list.GetFonts();
140 EXPECT_EQ(2U, fonts.size());
141 EXPECT_EQ("Arial|8|bold", FontToString(fonts[0]));
142 EXPECT_EQ("Sans serif|8|bold", FontToString(fonts[1]));
145 TEST(FontListTest, Fonts_DescStringWithStyleInFlexibleFormat_RoundTrip) {
146 // Test round trip from font description string to font vector to
147 // font description string.
148 FontList font_list = FontList(" serif , Sans serif , Bold "
149 " Italic 13px");
151 const std::vector<Font>& fonts = font_list.GetFonts();
152 FontList font_list_1 = FontList(fonts);
153 const std::string& desc_str = font_list_1.GetFontDescriptionString();
155 EXPECT_EQ("serif,Sans serif,Bold Italic 13px", desc_str);
158 TEST(FontListTest, Fonts_FontVector_RoundTrip) {
159 // Test round trip from font vector to font description string to font vector.
160 Font font("Arial", 8);
161 Font font_1("Sans serif", 10);
162 std::vector<Font> input_fonts;
163 input_fonts.push_back(font.DeriveFont(0, Font::BOLD));
164 input_fonts.push_back(font_1.DeriveFont(-2, Font::BOLD));
165 FontList font_list = FontList(input_fonts);
167 const std::string& desc_string = font_list.GetFontDescriptionString();
168 FontList font_list_1 = FontList(desc_string);
169 const std::vector<Font>& round_trip_fonts = font_list_1.GetFonts();
171 EXPECT_EQ(2U, round_trip_fonts.size());
172 EXPECT_EQ("Arial|8|bold", FontToString(round_trip_fonts[0]));
173 EXPECT_EQ("Sans serif|8|bold", FontToString(round_trip_fonts[1]));
176 TEST(FontListTest, FontDescString_GetStyle) {
177 FontList font_list = FontList("Arial,Sans serif, 8px");
178 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle());
180 font_list = FontList("Arial,Sans serif,Bold 8px");
181 EXPECT_EQ(Font::BOLD, font_list.GetFontStyle());
183 font_list = FontList("Arial,Sans serif,Italic 8px");
184 EXPECT_EQ(Font::ITALIC, font_list.GetFontStyle());
186 font_list = FontList("Arial,Italic Bold 8px");
187 EXPECT_EQ(Font::BOLD | Font::ITALIC, font_list.GetFontStyle());
190 TEST(FontListTest, Fonts_GetStyle) {
191 std::vector<Font> fonts;
192 fonts.push_back(gfx::Font("Arial", 8));
193 fonts.push_back(gfx::Font("Sans serif", 8));
194 FontList font_list = FontList(fonts);
195 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle());
196 fonts[0] = fonts[0].DeriveFont(0, Font::ITALIC | Font::BOLD);
197 fonts[1] = fonts[1].DeriveFont(0, Font::ITALIC | Font::BOLD);
198 font_list = FontList(fonts);
199 EXPECT_EQ(Font::ITALIC | Font::BOLD, font_list.GetFontStyle());
202 TEST(FontListTest, FontDescString_DeriveFontList) {
203 FontList font_list = FontList("Arial,Sans serif, 8px");
205 FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC);
206 EXPECT_EQ("Arial,Sans serif,Bold Italic 8px",
207 derived.GetFontDescriptionString());
210 TEST(FontListTest, Fonts_DeriveFontList) {
211 std::vector<Font> fonts;
212 fonts.push_back(gfx::Font("Arial", 8));
213 fonts.push_back(gfx::Font("Sans serif", 8));
214 FontList font_list = FontList(fonts);
216 FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC);
217 const std::vector<Font>& derived_fonts = derived.GetFonts();
219 EXPECT_EQ(2U, derived_fonts.size());
220 EXPECT_EQ("Arial|8|bold|italic", FontToString(derived_fonts[0]));
221 EXPECT_EQ("Sans serif|8|bold|italic", FontToString(derived_fonts[1]));
224 TEST(FontListTest, FontDescString_DeriveFontListWithSize) {
225 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px");
227 FontList derived = font_list.DeriveFontListWithSize(10);
228 EXPECT_EQ("Arial,Sans serif,Bold Italic 10px",
229 derived.GetFontDescriptionString());
232 TEST(FontListTest, Fonts_DeriveFontListWithSize) {
233 std::vector<Font> fonts;
234 fonts.push_back(gfx::Font("Arial", 8));
235 fonts.push_back(gfx::Font("Sans serif", 8));
236 FontList font_list = FontList(fonts);
238 FontList derived = font_list.DeriveFontListWithSize(5);
239 const std::vector<Font>& derived_fonts = derived.GetFonts();
241 EXPECT_EQ(2U, derived_fonts.size());
242 EXPECT_EQ("Arial|5", FontToString(derived_fonts[0]));
243 EXPECT_EQ("Sans serif|5", FontToString(derived_fonts[1]));
246 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDelta) {
247 FontList font_list = FontList("Arial,Sans serif,Bold 18px");
249 FontList derived = font_list.DeriveFontListWithSizeDelta(-8);
250 EXPECT_EQ("Arial,Sans serif,Bold 10px",
251 derived.GetFontDescriptionString());
254 TEST(FontListTest, Fonts_DeriveFontListWithSizeDelta) {
255 std::vector<Font> fonts;
256 fonts.push_back(gfx::Font("Arial", 18).DeriveFont(0, Font::ITALIC));
257 fonts.push_back(gfx::Font("Sans serif", 18).DeriveFont(0, Font::ITALIC));
258 FontList font_list = FontList(fonts);
260 FontList derived = font_list.DeriveFontListWithSizeDelta(-5);
261 const std::vector<Font>& derived_fonts = derived.GetFonts();
263 EXPECT_EQ(2U, derived_fonts.size());
264 EXPECT_EQ("Arial|13|italic", FontToString(derived_fonts[0]));
265 EXPECT_EQ("Sans serif|13|italic", FontToString(derived_fonts[1]));
268 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDeltaAndStyle) {
269 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px");
271 FontList derived =
272 font_list.DeriveFontListWithSizeDeltaAndStyle(10, Font::ITALIC);
273 EXPECT_EQ("Arial,Sans serif,Italic 18px",
274 derived.GetFontDescriptionString());
277 TEST(FontListTest, Fonts_DeriveFontListWithSizeDeltaAndStyle) {
278 std::vector<Font> fonts;
279 fonts.push_back(gfx::Font("Arial", 8));
280 fonts.push_back(gfx::Font("Sans serif", 8));
281 FontList font_list = FontList(fonts);
283 FontList derived =
284 font_list.DeriveFontListWithSizeDeltaAndStyle(5, Font::BOLD);
285 const std::vector<Font>& derived_fonts = derived.GetFonts();
287 EXPECT_EQ(2U, derived_fonts.size());
288 EXPECT_EQ("Arial|13|bold", FontToString(derived_fonts[0]));
289 EXPECT_EQ("Sans serif|13|bold", FontToString(derived_fonts[1]));
292 TEST(FontListTest, Fonts_GetHeight_GetBaseline) {
293 // If a font list has only one font, the height and baseline must be the same.
294 Font font1("Arial", 16);
295 FontList font_list1("Arial, 16px");
296 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight());
297 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline());
299 // If there are two different fonts, the font list returns the max value
300 // for ascent and descent.
301 Font font2("Symbol", 16);
302 EXPECT_NE(font1.GetBaseline(), font2.GetBaseline());
303 EXPECT_NE(font1.GetHeight() - font1.GetBaseline(),
304 font2.GetHeight() - font2.GetBaseline());
305 std::vector<Font> fonts;
306 fonts.push_back(font1);
307 fonts.push_back(font2);
308 FontList font_list_mix(fonts);
309 // ascent of FontList == max(ascent of Fonts)
310 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(),
311 font2.GetHeight() - font2.GetBaseline()),
312 font_list_mix.GetHeight() - font_list_mix.GetBaseline());
313 // descent of FontList == max(descent of Fonts)
314 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()),
315 font_list_mix.GetBaseline());
318 } // namespace gfx