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.h" 6 7 #include "base/strings/string16.h" 8 #include "base/strings/string_util.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 #if defined(OS_LINUX) && !defined(USE_OZONE) 13 #include <pango/pango.h> 14 #elif defined(OS_WIN) 15 #include "ui/gfx/platform_font_win.h" 16 #endif 17 18 namespace gfx { 19 namespace { 20 21 class FontTest : public testing::Test { 22 public: 23 // Fulfills the memory management contract as outlined by the comment at 24 // gfx::Font::GetNativeFont(). 25 void FreeIfNecessary(NativeFont font) { 26 #if defined(OS_LINUX) && !defined(USE_OZONE) 27 pango_font_description_free(font); 28 #endif 29 } 30 }; 31 32 #if defined(OS_WIN) 33 class ScopedMinimumFontSizeCallback { 34 public: 35 explicit ScopedMinimumFontSizeCallback(int minimum_size) { 36 minimum_size_ = minimum_size; 37 old_callback_ = PlatformFontWin::get_minimum_font_size_callback; 38 PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize; 39 } 40 41 ~ScopedMinimumFontSizeCallback() { 42 PlatformFontWin::get_minimum_font_size_callback = old_callback_; 43 } 44 45 private: 46 static int GetMinimumFontSize() { 47 return minimum_size_; 48 } 49 50 PlatformFontWin::GetMinimumFontSizeCallback old_callback_; 51 static int minimum_size_; 52 53 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback); 54 }; 55 56 int ScopedMinimumFontSizeCallback::minimum_size_ = 0; 57 #endif // defined(OS_WIN) 58 59 60 TEST_F(FontTest, LoadArial) { 61 Font cf("Arial", 16); 62 NativeFont native = cf.GetNativeFont(); 63 EXPECT_TRUE(native); 64 EXPECT_EQ(cf.GetStyle(), Font::NORMAL); 65 EXPECT_EQ(cf.GetFontSize(), 16); 66 EXPECT_EQ(cf.GetFontName(), "Arial"); 67 EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting())); 68 FreeIfNecessary(native); 69 } 70 71 TEST_F(FontTest, LoadArialBold) { 72 Font cf("Arial", 16); 73 Font bold(cf.Derive(0, Font::BOLD)); 74 NativeFont native = bold.GetNativeFont(); 75 EXPECT_TRUE(native); 76 EXPECT_EQ(bold.GetStyle(), Font::BOLD); 77 EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting())); 78 FreeIfNecessary(native); 79 } 80 81 TEST_F(FontTest, Ascent) { 82 Font cf("Arial", 16); 83 EXPECT_GT(cf.GetBaseline(), 2); 84 EXPECT_LE(cf.GetBaseline(), 22); 85 } 86 87 TEST_F(FontTest, Height) { 88 Font cf("Arial", 16); 89 EXPECT_GE(cf.GetHeight(), 16); 90 // TODO(akalin): Figure out why height is so large on Linux. 91 EXPECT_LE(cf.GetHeight(), 26); 92 } 93 94 TEST_F(FontTest, CapHeight) { 95 Font cf("Arial", 16); 96 EXPECT_GT(cf.GetCapHeight(), 0); 97 EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2); 98 EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline()); 99 } 100 101 TEST_F(FontTest, AvgWidths) { 102 Font cf("Arial", 16); 103 EXPECT_EQ(cf.GetExpectedTextWidth(0), 0); 104 EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0)); 105 EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1)); 106 EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2)); 107 } 108 109 #if !defined(OS_WIN) 110 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now. 111 // http://crbug.com/327287 112 // 113 // Check that fonts used for testing are installed and enabled. On Mac 114 // fonts may be installed but still need enabling in Font Book.app. 115 // http://crbug.com/347429 116 TEST_F(FontTest, GetActualFontNameForTesting) { 117 Font arial("Arial", 16); 118 EXPECT_EQ("arial", StringToLowerASCII(arial.GetActualFontNameForTesting())) 119 << "********\n" 120 << "Your test environment seems to be missing Arial font, which is " 121 << "needed for unittests. Check if Arial font is installed.\n" 122 << "********"; 123 Font symbol("Symbol", 16); 124 EXPECT_EQ("symbol", StringToLowerASCII(symbol.GetActualFontNameForTesting())) 125 << "********\n" 126 << "Your test environment seems to be missing Symbol font, which is " 127 << "needed for unittests. Check if Symbol font is installed.\n" 128 << "********"; 129 130 const char* const invalid_font_name = "no_such_font_name"; 131 Font fallback_font(invalid_font_name, 16); 132 EXPECT_NE(invalid_font_name, 133 StringToLowerASCII(fallback_font.GetActualFontNameForTesting())); 134 } 135 #endif 136 137 #if defined(OS_WIN) 138 TEST_F(FontTest, DeriveResizesIfSizeTooSmall) { 139 Font cf("Arial", 8); 140 // The minimum font size is set to 5 in browser_main.cc. 141 ScopedMinimumFontSizeCallback minimum_size(5); 142 143 Font derived_font = cf.Derive(-4, cf.GetStyle()); 144 EXPECT_EQ(5, derived_font.GetFontSize()); 145 } 146 147 TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) { 148 Font cf("Arial", 8); 149 // The minimum font size is set to 5 in browser_main.cc. 150 ScopedMinimumFontSizeCallback minimum_size(5); 151 152 Font derived_font = cf.Derive(-2, cf.GetStyle()); 153 EXPECT_EQ(6, derived_font.GetFontSize()); 154 } 155 #endif // defined(OS_WIN) 156 157 } // namespace 158 } // namespace gfx 159