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.h"
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"
12 #if defined(OS_LINUX) && !defined(USE_OZONE)
13 #include <pango/pango.h>
15 #include "ui/gfx/platform_font_win.h"
21 class FontTest
: public testing::Test
{
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
);
33 class ScopedMinimumFontSizeCallback
{
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
;
41 ~ScopedMinimumFontSizeCallback() {
42 PlatformFontWin::get_minimum_font_size_callback
= old_callback_
;
46 static int GetMinimumFontSize() {
50 PlatformFontWin::GetMinimumFontSizeCallback old_callback_
;
51 static int minimum_size_
;
53 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback
);
56 int ScopedMinimumFontSizeCallback::minimum_size_
= 0;
57 #endif // defined(OS_WIN)
60 TEST_F(FontTest
, LoadArial
) {
62 NativeFont native
= cf
.GetNativeFont();
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
);
71 TEST_F(FontTest
, LoadArialBold
) {
73 Font
bold(cf
.Derive(0, Font::BOLD
));
74 NativeFont native
= bold
.GetNativeFont();
76 EXPECT_EQ(bold
.GetStyle(), Font::BOLD
);
77 EXPECT_EQ("arial", StringToLowerASCII(cf
.GetActualFontNameForTesting()));
78 FreeIfNecessary(native
);
81 TEST_F(FontTest
, Ascent
) {
83 EXPECT_GT(cf
.GetBaseline(), 2);
84 EXPECT_LE(cf
.GetBaseline(), 22);
87 TEST_F(FontTest
, Height
) {
89 EXPECT_GE(cf
.GetHeight(), 16);
90 // TODO(akalin): Figure out why height is so large on Linux.
91 EXPECT_LE(cf
.GetHeight(), 26);
94 TEST_F(FontTest
, CapHeight
) {
96 EXPECT_GT(cf
.GetCapHeight(), 0);
97 EXPECT_GT(cf
.GetCapHeight(), cf
.GetHeight() / 2);
98 EXPECT_LT(cf
.GetCapHeight(), cf
.GetBaseline());
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));
110 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
111 // http://crbug.com/327287
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()))
120 << "Your test environment seems to be missing Arial font, which is "
121 << "needed for unittests. Check if Arial font is installed.\n"
123 Font
symbol("Symbol", 16);
124 EXPECT_EQ("symbol", StringToLowerASCII(symbol
.GetActualFontNameForTesting()))
126 << "Your test environment seems to be missing Symbol font, which is "
127 << "needed for unittests. Check if Symbol font is installed.\n"
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()));
138 TEST_F(FontTest
, DeriveResizesIfSizeTooSmall
) {
140 // The minimum font size is set to 5 in browser_main.cc.
141 ScopedMinimumFontSizeCallback
minimum_size(5);
143 Font derived_font
= cf
.Derive(-4, cf
.GetStyle());
144 EXPECT_EQ(5, derived_font
.GetFontSize());
147 TEST_F(FontTest
, DeriveKeepsOriginalSizeIfHeightOk
) {
149 // The minimum font size is set to 5 in browser_main.cc.
150 ScopedMinimumFontSizeCallback
minimum_size(5);
152 Font derived_font
= cf
.Derive(-2, cf
.GetStyle());
153 EXPECT_EQ(6, derived_font
.GetFontSize());
155 #endif // defined(OS_WIN)