font_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include "ui/gfx/font.h"
  5. #include <string>
  6. #include "base/strings/string_util.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "build/build_config.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/gfx/font_names_testing.h"
  11. #if BUILDFLAG(IS_WIN)
  12. #include "ui/gfx/system_fonts_win.h"
  13. #endif
  14. namespace gfx {
  15. namespace {
  16. class FontTest : public testing::Test {
  17. public:
  18. FontTest() = default;
  19. FontTest(const FontTest&) = delete;
  20. FontTest& operator=(const FontTest&) = delete;
  21. protected:
  22. void SetUp() override {
  23. #if BUILDFLAG(IS_WIN)
  24. // System fonts is keeping a cache of loaded system fonts. These fonts are
  25. // scaled based on global callbacks configured on startup. The tests in this
  26. // file are testing these callbacks and need to be sure we cleared the
  27. // global state to avoid flaky tests.
  28. win::ResetSystemFontsForTesting();
  29. #endif
  30. }
  31. };
  32. TEST_F(FontTest, DefaultFont) {
  33. Font cf;
  34. EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
  35. EXPECT_EQ(cf.GetWeight(), Font::Weight::NORMAL);
  36. // Ensures that font metrics are generated. Some fonts backends do not provide
  37. // some metrics (e.g. DWrite do not produce average character width).
  38. EXPECT_GT(cf.GetFontSize(), 0);
  39. EXPECT_GT(cf.GetHeight(), 0);
  40. EXPECT_GT(cf.GetBaseline(), 0);
  41. EXPECT_GT(cf.GetCapHeight(), 0);
  42. EXPECT_GT(cf.GetExpectedTextWidth(1), 0);
  43. }
  44. TEST_F(FontTest, LoadArial) {
  45. Font cf(kTestFontName, 16);
  46. #if BUILDFLAG(IS_APPLE)
  47. EXPECT_TRUE(cf.GetNativeFont());
  48. #endif
  49. EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
  50. EXPECT_EQ(cf.GetFontSize(), 16);
  51. EXPECT_EQ(cf.GetFontName(), kTestFontName);
  52. EXPECT_EQ(base::ToLowerASCII(kTestFontName),
  53. base::ToLowerASCII(cf.GetActualFontName()));
  54. }
  55. TEST_F(FontTest, LoadArialBold) {
  56. Font cf(kTestFontName, 16);
  57. Font bold(cf.Derive(0, Font::NORMAL, Font::Weight::BOLD));
  58. #if BUILDFLAG(IS_APPLE)
  59. EXPECT_TRUE(bold.GetNativeFont());
  60. #endif
  61. EXPECT_EQ(bold.GetStyle(), Font::NORMAL);
  62. EXPECT_EQ(bold.GetWeight(), Font::Weight::BOLD);
  63. EXPECT_EQ(base::ToLowerASCII(kTestFontName),
  64. base::ToLowerASCII(cf.GetActualFontName()));
  65. }
  66. TEST_F(FontTest, Ascent) {
  67. Font cf(kTestFontName, 16);
  68. EXPECT_GT(cf.GetBaseline(), 2);
  69. EXPECT_LE(cf.GetBaseline(), 22);
  70. }
  71. TEST_F(FontTest, Height) {
  72. Font cf(kTestFontName, 16);
  73. EXPECT_GE(cf.GetHeight(), 16);
  74. // TODO(akalin): Figure out why height is so large on Linux.
  75. EXPECT_LE(cf.GetHeight(), 26);
  76. }
  77. TEST_F(FontTest, CapHeight) {
  78. Font cf(kTestFontName, 16);
  79. EXPECT_GT(cf.GetCapHeight(), 0);
  80. EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2);
  81. EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline());
  82. }
  83. TEST_F(FontTest, AvgWidths) {
  84. Font cf(kTestFontName, 16);
  85. EXPECT_EQ(cf.GetExpectedTextWidth(0), 0);
  86. EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
  87. EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
  88. EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
  89. }
  90. // Check that fonts used for testing are installed and enabled. On Mac
  91. // fonts may be installed but still need enabling in Font Book.app.
  92. // http://crbug.com/347429
  93. TEST_F(FontTest, GetActualFontName) {
  94. Font arial(kTestFontName, 16);
  95. EXPECT_EQ(base::ToLowerASCII(kTestFontName),
  96. base::ToLowerASCII(arial.GetActualFontName()))
  97. << "********\n"
  98. << "Your test environment seems to be missing Arial font, which is "
  99. << "needed for unittests. Check if Arial font is installed.\n"
  100. << "********";
  101. Font symbol(kSymbolFontName, 16);
  102. EXPECT_EQ(base::ToLowerASCII(kSymbolFontName),
  103. base::ToLowerASCII(symbol.GetActualFontName()))
  104. << "********\n"
  105. << "Your test environment seems to be missing the " << kSymbolFontName
  106. << " font, which is "
  107. << "needed for unittests. Check if " << kSymbolFontName
  108. << " font is installed.\n"
  109. << "********";
  110. const char* const invalid_font_name = "no_such_font_name";
  111. Font fallback_font(invalid_font_name, 16);
  112. EXPECT_NE(invalid_font_name,
  113. base::ToLowerASCII(fallback_font.GetActualFontName()));
  114. }
  115. TEST_F(FontTest, DeriveFont) {
  116. Font cf(kTestFontName, 8);
  117. const int kSizeDelta = 2;
  118. Font cf_underlined =
  119. cf.Derive(0, cf.GetStyle() | gfx::Font::UNDERLINE, cf.GetWeight());
  120. Font cf_underlined_resized = cf_underlined.Derive(
  121. kSizeDelta, cf_underlined.GetStyle(), cf_underlined.GetWeight());
  122. EXPECT_EQ(cf.GetStyle() | gfx::Font::UNDERLINE,
  123. cf_underlined_resized.GetStyle());
  124. EXPECT_EQ(cf.GetFontSize() + kSizeDelta, cf_underlined_resized.GetFontSize());
  125. EXPECT_EQ(cf.GetWeight(), cf_underlined_resized.GetWeight());
  126. }
  127. #if BUILDFLAG(IS_WIN)
  128. TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
  129. Font cf(kTestFontName, 8);
  130. gfx::win::SetGetMinimumFontSizeCallback([] { return 5; });
  131. Font derived_font = cf.Derive(-4, cf.GetStyle(), cf.GetWeight());
  132. EXPECT_EQ(5, derived_font.GetFontSize());
  133. }
  134. TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) {
  135. Font cf(kTestFontName, 8);
  136. gfx::win::SetGetMinimumFontSizeCallback([] { return 5; });
  137. Font derived_font = cf.Derive(-2, cf.GetStyle(), cf.GetWeight());
  138. EXPECT_EQ(6, derived_font.GetFontSize());
  139. }
  140. #endif // BUILDFLAG(IS_WIN)
  141. TEST_F(FontTest, WeightConversion) {
  142. struct WeightMatchExpectation {
  143. int weight;
  144. Font::Weight enum_value;
  145. } expectations[] = {
  146. {-10, Font::Weight::INVALID}, {-1, Font::Weight::INVALID},
  147. {0, Font::Weight::THIN}, {1, Font::Weight::THIN},
  148. {100, Font::Weight::THIN}, {350, Font::Weight::NORMAL},
  149. {400, Font::Weight::NORMAL}, {899, Font::Weight::BLACK},
  150. {900, Font::Weight::BLACK}, {901, Font::Weight::INVALID}};
  151. for (const auto& expectation : expectations) {
  152. EXPECT_EQ(FontWeightFromInt(expectation.weight), expectation.enum_value);
  153. }
  154. }
  155. } // namespace
  156. } // namespace gfx