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. 4 5 #include "ui/gfx/font_list.h" 6 7 #include <algorithm> 8 #include <string> 9 #include <vector> 10 11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_util.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace { 16 17 // Helper function for comparing fonts for equality. 18 std::string FontToString(const gfx::Font& font) { 19 std::string font_string = font.GetFontName(); 20 font_string += "|"; 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 return font_string; 28 } 29 30 } // namespace 31 32 namespace gfx { 33 34 TEST(FontListTest, FontDescString_FromDescString) { 35 // Test init from font name style size string. 36 FontList font_list = FontList("Droid Sans serif, Sans serif, 10px"); 37 EXPECT_EQ("Droid Sans serif, Sans serif, 10px", 38 font_list.GetFontDescriptionString()); 39 } 40 41 TEST(FontListTest, FontDescString_FromFontNamesStyleAndSize) { 42 // Test init from font names, style and size. 43 std::vector<std::string> font_names; 44 font_names.push_back("Arial"); 45 font_names.push_back("Droid Sans serif"); 46 int font_style = Font::BOLD | Font::ITALIC; 47 int font_size = 11; 48 FontList font_list = FontList(font_names, font_style, font_size); 49 EXPECT_EQ("Arial,Droid Sans serif,Bold Italic 11px", 50 font_list.GetFontDescriptionString()); 51 } 52 53 TEST(FontListTest, FontDescString_FromFont) { 54 // Test init from Font. 55 Font font("Arial", 8); 56 FontList font_list = FontList(font); 57 EXPECT_EQ("Arial,8px", font_list.GetFontDescriptionString()); 58 } 59 60 TEST(FontListTest, FontDescString_FromFontWithNonNormalStyle) { 61 // Test init from Font with non-normal style. 62 Font font("Arial", 8); 63 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD)); 64 EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString()); 65 66 font_list = FontList(font.DeriveFont(-2, Font::ITALIC)); 67 EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString()); 68 } 69 70 TEST(FontListTest, FontDescString_FromFontVector) { 71 // Test init from Font vector. 72 Font font("Arial", 8); 73 Font font_1("Sans serif", 10); 74 std::vector<Font> fonts; 75 fonts.push_back(font.DeriveFont(0, Font::BOLD)); 76 fonts.push_back(font_1.DeriveFont(-2, Font::BOLD)); 77 FontList font_list = FontList(fonts); 78 EXPECT_EQ("Arial,Sans serif,Bold 8px", font_list.GetFontDescriptionString()); 79 } 80 81 TEST(FontListTest, Fonts_FromDescString) { 82 // Test init from font name size string. 83 FontList font_list = FontList("serif,Sans serif, 13px"); 84 const std::vector<Font>& fonts = font_list.GetFonts(); 85 EXPECT_EQ(2U, fonts.size()); 86 EXPECT_EQ("serif|13", FontToString(fonts[0])); 87 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); 88 } 89 90 TEST(FontListTest, Fonts_FromDescStringInFlexibleFormat) { 91 // Test init from font name size string with flexible format. 92 FontList font_list = FontList(" serif , Sans serif , 13px"); 93 const std::vector<Font>& fonts = font_list.GetFonts(); 94 EXPECT_EQ(2U, fonts.size()); 95 EXPECT_EQ("serif|13", FontToString(fonts[0])); 96 EXPECT_EQ("Sans serif|13", FontToString(fonts[1])); 97 } 98 99 TEST(FontListTest, Fonts_FromDescStringWithStyleInFlexibleFormat) { 100 // Test init from font name style size string with flexible format. 101 FontList font_list = FontList(" serif , Sans serif , Bold " 102 " Italic 13px"); 103 const std::vector<Font>& fonts = font_list.GetFonts(); 104 EXPECT_EQ(2U, fonts.size()); 105 EXPECT_EQ("serif|13|bold|italic", FontToString(fonts[0])); 106 EXPECT_EQ("Sans serif|13|bold|italic", FontToString(fonts[1])); 107 } 108 109 TEST(FontListTest, Fonts_FromFont) { 110 // Test init from Font. 111 Font font("Arial", 8); 112 FontList font_list = FontList(font); 113 const std::vector<Font>& fonts = font_list.GetFonts(); 114 EXPECT_EQ(1U, fonts.size()); 115 EXPECT_EQ("Arial|8", FontToString(fonts[0])); 116 } 117 118 TEST(FontListTest, Fonts_FromFontWithNonNormalStyle) { 119 // Test init from Font with non-normal style. 120 Font font("Arial", 8); 121 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD)); 122 std::vector<Font> fonts = font_list.GetFonts(); 123 EXPECT_EQ(1U, fonts.size()); 124 EXPECT_EQ("Arial|10|bold", FontToString(fonts[0])); 125 126 font_list = FontList(font.DeriveFont(-2, Font::ITALIC)); 127 fonts = font_list.GetFonts(); 128 EXPECT_EQ(1U, fonts.size()); 129 EXPECT_EQ("Arial|6|italic", FontToString(fonts[0])); 130 } 131 132 TEST(FontListTest, Fonts_FromFontVector) { 133 // Test init from Font vector. 134 Font font("Arial", 8); 135 Font font_1("Sans serif", 10); 136 std::vector<Font> input_fonts; 137 input_fonts.push_back(font.DeriveFont(0, Font::BOLD)); 138 input_fonts.push_back(font_1.DeriveFont(-2, Font::BOLD)); 139 FontList font_list = FontList(input_fonts); 140 const std::vector<Font>& fonts = font_list.GetFonts(); 141 EXPECT_EQ(2U, fonts.size()); 142 EXPECT_EQ("Arial|8|bold", FontToString(fonts[0])); 143 EXPECT_EQ("Sans serif|8|bold", FontToString(fonts[1])); 144 } 145 146 TEST(FontListTest, Fonts_DescStringWithStyleInFlexibleFormat_RoundTrip) { 147 // Test round trip from font description string to font vector to 148 // font description string. 149 FontList font_list = FontList(" serif , Sans serif , Bold " 150 " Italic 13px"); 151 152 const std::vector<Font>& fonts = font_list.GetFonts(); 153 FontList font_list_1 = FontList(fonts); 154 const std::string& desc_str = font_list_1.GetFontDescriptionString(); 155 156 EXPECT_EQ("serif,Sans serif,Bold Italic 13px", desc_str); 157 } 158 159 TEST(FontListTest, Fonts_FontVector_RoundTrip) { 160 // Test round trip from font vector to font description string to font vector. 161 Font font("Arial", 8); 162 Font font_1("Sans serif", 10); 163 std::vector<Font> input_fonts; 164 input_fonts.push_back(font.DeriveFont(0, Font::BOLD)); 165 input_fonts.push_back(font_1.DeriveFont(-2, Font::BOLD)); 166 FontList font_list = FontList(input_fonts); 167 168 const std::string& desc_string = font_list.GetFontDescriptionString(); 169 FontList font_list_1 = FontList(desc_string); 170 const std::vector<Font>& round_trip_fonts = font_list_1.GetFonts(); 171 172 EXPECT_EQ(2U, round_trip_fonts.size()); 173 EXPECT_EQ("Arial|8|bold", FontToString(round_trip_fonts[0])); 174 EXPECT_EQ("Sans serif|8|bold", FontToString(round_trip_fonts[1])); 175 } 176 177 TEST(FontListTest, FontDescString_GetStyle) { 178 FontList font_list = FontList("Arial,Sans serif, 8px"); 179 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); 180 181 font_list = FontList("Arial,Sans serif,Bold 8px"); 182 EXPECT_EQ(Font::BOLD, font_list.GetFontStyle()); 183 184 font_list = FontList("Arial,Sans serif,Italic 8px"); 185 EXPECT_EQ(Font::ITALIC, font_list.GetFontStyle()); 186 187 font_list = FontList("Arial,Italic Bold 8px"); 188 EXPECT_EQ(Font::BOLD | Font::ITALIC, font_list.GetFontStyle()); 189 } 190 191 TEST(FontListTest, Fonts_GetStyle) { 192 std::vector<Font> fonts; 193 fonts.push_back(gfx::Font("Arial", 8)); 194 fonts.push_back(gfx::Font("Sans serif", 8)); 195 FontList font_list = FontList(fonts); 196 EXPECT_EQ(Font::NORMAL, font_list.GetFontStyle()); 197 fonts[0] = fonts[0].DeriveFont(0, Font::ITALIC | Font::BOLD); 198 fonts[1] = fonts[1].DeriveFont(0, Font::ITALIC | Font::BOLD); 199 font_list = FontList(fonts); 200 EXPECT_EQ(Font::ITALIC | Font::BOLD, font_list.GetFontStyle()); 201 } 202 203 TEST(FontListTest, FontDescString_DeriveFontList) { 204 FontList font_list = FontList("Arial,Sans serif, 8px"); 205 206 FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC); 207 EXPECT_EQ("Arial,Sans serif,Bold Italic 8px", 208 derived.GetFontDescriptionString()); 209 } 210 211 TEST(FontListTest, Fonts_DeriveFontList) { 212 std::vector<Font> fonts; 213 fonts.push_back(gfx::Font("Arial", 8)); 214 fonts.push_back(gfx::Font("Sans serif", 8)); 215 FontList font_list = FontList(fonts); 216 217 FontList derived = font_list.DeriveFontList(Font::BOLD | Font::ITALIC); 218 const std::vector<Font>& derived_fonts = derived.GetFonts(); 219 220 EXPECT_EQ(2U, derived_fonts.size()); 221 EXPECT_EQ("Arial|8|bold|italic", FontToString(derived_fonts[0])); 222 EXPECT_EQ("Sans serif|8|bold|italic", FontToString(derived_fonts[1])); 223 } 224 225 TEST(FontListTest, FontDescString_DeriveFontListWithSize) { 226 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px"); 227 228 FontList derived = font_list.DeriveFontListWithSize(10); 229 EXPECT_EQ("Arial,Sans serif,Bold Italic 10px", 230 derived.GetFontDescriptionString()); 231 } 232 233 TEST(FontListTest, Fonts_DeriveFontListWithSize) { 234 std::vector<Font> fonts; 235 fonts.push_back(gfx::Font("Arial", 8)); 236 fonts.push_back(gfx::Font("Sans serif", 8)); 237 FontList font_list = FontList(fonts); 238 239 FontList derived = font_list.DeriveFontListWithSize(5); 240 const std::vector<Font>& derived_fonts = derived.GetFonts(); 241 242 EXPECT_EQ(2U, derived_fonts.size()); 243 EXPECT_EQ("Arial|5", FontToString(derived_fonts[0])); 244 EXPECT_EQ("Sans serif|5", FontToString(derived_fonts[1])); 245 } 246 247 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDelta) { 248 FontList font_list = FontList("Arial,Sans serif,Bold 18px"); 249 250 FontList derived = font_list.DeriveFontListWithSizeDelta(-8); 251 EXPECT_EQ("Arial,Sans serif,Bold 10px", 252 derived.GetFontDescriptionString()); 253 } 254 255 TEST(FontListTest, Fonts_DeriveFontListWithSizeDelta) { 256 std::vector<Font> fonts; 257 fonts.push_back(gfx::Font("Arial", 18).DeriveFont(0, Font::ITALIC)); 258 fonts.push_back(gfx::Font("Sans serif", 18).DeriveFont(0, Font::ITALIC)); 259 FontList font_list = FontList(fonts); 260 261 FontList derived = font_list.DeriveFontListWithSizeDelta(-5); 262 const std::vector<Font>& derived_fonts = derived.GetFonts(); 263 264 EXPECT_EQ(2U, derived_fonts.size()); 265 EXPECT_EQ("Arial|13|italic", FontToString(derived_fonts[0])); 266 EXPECT_EQ("Sans serif|13|italic", FontToString(derived_fonts[1])); 267 } 268 269 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDeltaAndStyle) { 270 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px"); 271 272 FontList derived = 273 font_list.DeriveFontListWithSizeDeltaAndStyle(10, Font::ITALIC); 274 EXPECT_EQ("Arial,Sans serif,Italic 18px", 275 derived.GetFontDescriptionString()); 276 } 277 278 TEST(FontListTest, Fonts_DeriveFontListWithSizeDeltaAndStyle) { 279 std::vector<Font> fonts; 280 fonts.push_back(gfx::Font("Arial", 8)); 281 fonts.push_back(gfx::Font("Sans serif", 8)); 282 FontList font_list = FontList(fonts); 283 284 FontList derived = 285 font_list.DeriveFontListWithSizeDeltaAndStyle(5, Font::BOLD); 286 const std::vector<Font>& derived_fonts = derived.GetFonts(); 287 288 EXPECT_EQ(2U, derived_fonts.size()); 289 EXPECT_EQ("Arial|13|bold", FontToString(derived_fonts[0])); 290 EXPECT_EQ("Sans serif|13|bold", FontToString(derived_fonts[1])); 291 } 292 293 TEST(FontListTest, Fonts_GetHeight_GetBaseline) { 294 // If a font list has only one font, the height and baseline must be the same. 295 Font font1("Arial", 16); 296 ASSERT_EQ("arial", StringToLowerASCII(font1.GetActualFontNameForTesting())); 297 FontList font_list1("Arial, 16px"); 298 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight()); 299 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline()); 300 301 // If there are two different fonts, the font list returns the max value 302 // for ascent and descent. 303 Font font2("Symbol", 16); 304 ASSERT_EQ("symbol", StringToLowerASCII(font2.GetActualFontNameForTesting())); 305 EXPECT_NE(font1.GetBaseline(), font2.GetBaseline()); 306 EXPECT_NE(font1.GetHeight() - font1.GetBaseline(), 307 font2.GetHeight() - font2.GetBaseline()); 308 std::vector<Font> fonts; 309 fonts.push_back(font1); 310 fonts.push_back(font2); 311 FontList font_list_mix(fonts); 312 // ascent of FontList == max(ascent of Fonts) 313 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(), 314 font2.GetHeight() - font2.GetBaseline()), 315 font_list_mix.GetHeight() - font_list_mix.GetBaseline()); 316 // descent of FontList == max(descent of Fonts) 317 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()), 318 font_list_mix.GetBaseline()); 319 } 320 321 } // namespace gfx 322