| 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 |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Helper function for comparing fonts for equality. | 17 // Helper function for comparing fonts for equality. |
| 18 std::string FontToString(const gfx::Font& font) { | 18 std::string FontToString(const gfx::Font& font) { |
| 19 std::string font_string = font.GetFontName(); | 19 std::string font_string = font.GetFontName(); |
| 20 font_string += "|"; | 20 font_string += "|"; |
| 21 font_string += base::IntToString(font.GetFontSize()); | 21 font_string += base::IntToString(font.GetFontSize()); |
| 22 int style = font.GetStyle(); | 22 int style = font.GetStyle(); |
| 23 if (style & gfx::Font::BOLD) | 23 if (style & gfx::Font::BOLD) |
| 24 font_string += "|bold"; | 24 font_string += "|bold"; |
| 25 if (style & gfx::Font::ITALIC) | 25 if (style & gfx::Font::ITALIC) |
| 26 font_string += "|italic"; | 26 font_string += "|italic"; |
| 27 if (style & gfx::Font::UNDERLINE) |
| 28 font_string += "|underline"; |
| 27 return font_string; | 29 return font_string; |
| 28 } | 30 } |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 namespace gfx { | 34 namespace gfx { |
| 33 | 35 |
| 34 TEST(FontListTest, FontDescString_FromDescString) { | 36 TEST(FontListTest, FontDescString_FromDescString) { |
| 35 // Test init from font name style size string. | 37 // Test init from font name style size string. |
| 36 FontList font_list = FontList("Droid Sans serif, Sans serif, 10px"); | 38 FontList font_list = FontList("Droid Sans serif, Sans serif, 10px"); |
| 37 EXPECT_EQ("Droid Sans serif, Sans serif, 10px", | 39 EXPECT_EQ("Droid Sans serif, Sans serif, 10px", |
| 38 font_list.GetFontDescriptionString()); | 40 font_list.GetFontDescriptionString()); |
| 39 } | 41 } |
| 40 | 42 |
| 41 TEST(FontListTest, FontDescString_FromFontNamesStyleAndSize) { | 43 TEST(FontListTest, FontDescString_FromFontNamesStyleAndSize) { |
| 42 // Test init from font names, style and size. | 44 // Test init from font names, style and size. |
| 43 std::vector<std::string> font_names; | 45 std::vector<std::string> font_names; |
| 44 font_names.push_back("Arial"); | 46 font_names.push_back("Arial"); |
| 45 font_names.push_back("Droid Sans serif"); | 47 font_names.push_back("Droid Sans serif"); |
| 46 int font_style = Font::BOLD | Font::ITALIC; | 48 int font_style = Font::BOLD | Font::ITALIC | Font::UNDERLINE; |
| 47 int font_size = 11; | 49 int font_size = 11; |
| 48 FontList font_list = FontList(font_names, font_style, font_size); | 50 FontList font_list = FontList(font_names, font_style, font_size); |
| 51 // "Underline" doesn't appear in the font description string. |
| 49 EXPECT_EQ("Arial,Droid Sans serif,Bold Italic 11px", | 52 EXPECT_EQ("Arial,Droid Sans serif,Bold Italic 11px", |
| 50 font_list.GetFontDescriptionString()); | 53 font_list.GetFontDescriptionString()); |
| 51 } | 54 } |
| 52 | 55 |
| 53 TEST(FontListTest, FontDescString_FromFont) { | 56 TEST(FontListTest, FontDescString_FromFont) { |
| 54 // Test init from Font. | 57 // Test init from Font. |
| 55 Font font("Arial", 8); | 58 Font font("Arial", 8); |
| 56 FontList font_list = FontList(font); | 59 FontList font_list = FontList(font); |
| 57 EXPECT_EQ("Arial,8px", font_list.GetFontDescriptionString()); | 60 EXPECT_EQ("Arial,8px", font_list.GetFontDescriptionString()); |
| 58 } | 61 } |
| 59 | 62 |
| 60 TEST(FontListTest, FontDescString_FromFontWithNonNormalStyle) { | 63 TEST(FontListTest, FontDescString_FromFontWithNonNormalStyle) { |
| 61 // Test init from Font with non-normal style. | 64 // Test init from Font with non-normal style. |
| 62 Font font("Arial", 8); | 65 Font font("Arial", 8); |
| 63 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD)); | 66 FontList font_list = FontList(font.DeriveFont(2, Font::BOLD)); |
| 64 EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString()); | 67 EXPECT_EQ("Arial,Bold 10px", font_list.GetFontDescriptionString()); |
| 65 | 68 |
| 66 font_list = FontList(font.DeriveFont(-2, Font::ITALIC)); | 69 font_list = FontList(font.DeriveFont(-2, Font::ITALIC)); |
| 67 EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString()); | 70 EXPECT_EQ("Arial,Italic 6px", font_list.GetFontDescriptionString()); |
| 71 |
| 72 // "Underline" doesn't appear in the font description string. |
| 73 font_list = FontList(font.DeriveFont(-4, Font::UNDERLINE)); |
| 74 EXPECT_EQ("Arial,4px", font_list.GetFontDescriptionString()); |
| 68 } | 75 } |
| 69 | 76 |
| 70 TEST(FontListTest, FontDescString_FromFontVector) { | 77 TEST(FontListTest, FontDescString_FromFontVector) { |
| 71 // Test init from Font vector. | 78 // Test init from Font vector. |
| 72 Font font("Arial", 8); | 79 Font font("Arial", 8); |
| 73 Font font_1("Sans serif", 10); | 80 Font font_1("Sans serif", 10); |
| 74 std::vector<Font> fonts; | 81 std::vector<Font> fonts; |
| 75 fonts.push_back(font.DeriveFont(0, Font::BOLD)); | 82 fonts.push_back(font.DeriveFont(0, Font::BOLD)); |
| 76 fonts.push_back(font_1.DeriveFont(-2, Font::BOLD)); | 83 fonts.push_back(font_1.DeriveFont(-2, Font::BOLD)); |
| 77 FontList font_list = FontList(fonts); | 84 FontList font_list = FontList(fonts); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 const std::vector<Font>& derived_fonts = derived.GetFonts(); | 269 const std::vector<Font>& derived_fonts = derived.GetFonts(); |
| 263 | 270 |
| 264 EXPECT_EQ(2U, derived_fonts.size()); | 271 EXPECT_EQ(2U, derived_fonts.size()); |
| 265 EXPECT_EQ("Arial|13|italic", FontToString(derived_fonts[0])); | 272 EXPECT_EQ("Arial|13|italic", FontToString(derived_fonts[0])); |
| 266 EXPECT_EQ("Sans serif|13|italic", FontToString(derived_fonts[1])); | 273 EXPECT_EQ("Sans serif|13|italic", FontToString(derived_fonts[1])); |
| 267 } | 274 } |
| 268 | 275 |
| 269 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDeltaAndStyle) { | 276 TEST(FontListTest, FontDescString_DeriveFontListWithSizeDeltaAndStyle) { |
| 270 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px"); | 277 FontList font_list = FontList("Arial,Sans serif,Bold Italic 8px"); |
| 271 | 278 |
| 272 FontList derived = | 279 FontList derived = font_list.DeriveFontListWithSizeDeltaAndStyle( |
| 273 font_list.DeriveFontListWithSizeDeltaAndStyle(10, Font::ITALIC); | 280 10, Font::ITALIC | Font::UNDERLINE); |
| 274 EXPECT_EQ("Arial,Sans serif,Italic 18px", | 281 EXPECT_EQ("Arial,Sans serif,Italic 18px", |
| 275 derived.GetFontDescriptionString()); | 282 derived.GetFontDescriptionString()); |
| 283 EXPECT_EQ(Font::ITALIC | Font::UNDERLINE, |
| 284 derived.GetFontStyle()); |
| 285 |
| 286 // FontList has a special care for Font::UNDERLINE. See if the handling of |
| 287 // Font::UNDERLINE in GetFonts() is okay or not. |
| 288 derived.GetFonts(); |
| 289 EXPECT_EQ(Font::ITALIC | Font::UNDERLINE, |
| 290 derived.GetFontStyle()); |
| 276 } | 291 } |
| 277 | 292 |
| 278 TEST(FontListTest, Fonts_DeriveFontListWithSizeDeltaAndStyle) { | 293 TEST(FontListTest, Fonts_DeriveFontListWithSizeDeltaAndStyle) { |
| 279 std::vector<Font> fonts; | 294 std::vector<Font> fonts; |
| 280 fonts.push_back(gfx::Font("Arial", 8)); | 295 fonts.push_back(gfx::Font("Arial", 8)); |
| 281 fonts.push_back(gfx::Font("Sans serif", 8)); | 296 fonts.push_back(gfx::Font("Sans serif", 8)); |
| 282 FontList font_list = FontList(fonts); | 297 FontList font_list = FontList(fonts); |
| 283 | 298 |
| 284 FontList derived = | 299 FontList derived = font_list.DeriveFontListWithSizeDeltaAndStyle( |
| 285 font_list.DeriveFontListWithSizeDeltaAndStyle(5, Font::BOLD); | 300 5, Font::BOLD | Font::UNDERLINE); |
| 286 const std::vector<Font>& derived_fonts = derived.GetFonts(); | 301 const std::vector<Font>& derived_fonts = derived.GetFonts(); |
| 287 | 302 |
| 288 EXPECT_EQ(2U, derived_fonts.size()); | 303 EXPECT_EQ(2U, derived_fonts.size()); |
| 289 EXPECT_EQ("Arial|13|bold", FontToString(derived_fonts[0])); | 304 EXPECT_EQ("Arial|13|bold|underline", FontToString(derived_fonts[0])); |
| 290 EXPECT_EQ("Sans serif|13|bold", FontToString(derived_fonts[1])); | 305 EXPECT_EQ("Sans serif|13|bold|underline", FontToString(derived_fonts[1])); |
| 291 } | 306 } |
| 292 | 307 |
| 293 TEST(FontListTest, Fonts_GetHeight_GetBaseline) { | 308 TEST(FontListTest, Fonts_GetHeight_GetBaseline) { |
| 294 // If a font list has only one font, the height and baseline must be the same. | 309 // If a font list has only one font, the height and baseline must be the same. |
| 295 Font font1("Arial", 16); | 310 Font font1("Arial", 16); |
| 296 ASSERT_EQ("arial", StringToLowerASCII(font1.GetActualFontNameForTesting())); | 311 ASSERT_EQ("arial", StringToLowerASCII(font1.GetActualFontNameForTesting())); |
| 297 FontList font_list1("Arial, 16px"); | 312 FontList font_list1("Arial, 16px"); |
| 298 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight()); | 313 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight()); |
| 299 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline()); | 314 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline()); |
| 300 | 315 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 312 // ascent of FontList == max(ascent of Fonts) | 327 // ascent of FontList == max(ascent of Fonts) |
| 313 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(), | 328 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(), |
| 314 font2.GetHeight() - font2.GetBaseline()), | 329 font2.GetHeight() - font2.GetBaseline()), |
| 315 font_list_mix.GetHeight() - font_list_mix.GetBaseline()); | 330 font_list_mix.GetHeight() - font_list_mix.GetBaseline()); |
| 316 // descent of FontList == max(descent of Fonts) | 331 // descent of FontList == max(descent of Fonts) |
| 317 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()), | 332 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()), |
| 318 font_list_mix.GetBaseline()); | 333 font_list_mix.GetBaseline()); |
| 319 } | 334 } |
| 320 | 335 |
| 321 } // namespace gfx | 336 } // namespace gfx |
| OLD | NEW |