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");
68 base::StringToLowerASCII(cf
.GetActualFontNameForTesting()));
69 FreeIfNecessary(native
);
72 TEST_F(FontTest
, LoadArialBold
) {
74 Font
bold(cf
.Derive(0, Font::BOLD
));
75 NativeFont native
= bold
.GetNativeFont();
77 EXPECT_EQ(bold
.GetStyle(), Font::BOLD
);
79 base::StringToLowerASCII(cf
.GetActualFontNameForTesting()));
80 FreeIfNecessary(native
);
83 TEST_F(FontTest
, Ascent
) {
85 EXPECT_GT(cf
.GetBaseline(), 2);
86 EXPECT_LE(cf
.GetBaseline(), 22);
89 TEST_F(FontTest
, Height
) {
91 EXPECT_GE(cf
.GetHeight(), 16);
92 // TODO(akalin): Figure out why height is so large on Linux.
93 EXPECT_LE(cf
.GetHeight(), 26);
96 TEST_F(FontTest
, CapHeight
) {
98 EXPECT_GT(cf
.GetCapHeight(), 0);
99 EXPECT_GT(cf
.GetCapHeight(), cf
.GetHeight() / 2);
100 EXPECT_LT(cf
.GetCapHeight(), cf
.GetBaseline());
103 TEST_F(FontTest
, AvgWidths
) {
104 Font
cf("Arial", 16);
105 EXPECT_EQ(cf
.GetExpectedTextWidth(0), 0);
106 EXPECT_GT(cf
.GetExpectedTextWidth(1), cf
.GetExpectedTextWidth(0));
107 EXPECT_GT(cf
.GetExpectedTextWidth(2), cf
.GetExpectedTextWidth(1));
108 EXPECT_GT(cf
.GetExpectedTextWidth(3), cf
.GetExpectedTextWidth(2));
112 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
113 // http://crbug.com/327287
115 // Check that fonts used for testing are installed and enabled. On Mac
116 // fonts may be installed but still need enabling in Font Book.app.
117 // http://crbug.com/347429
118 TEST_F(FontTest
, GetActualFontNameForTesting
) {
119 Font
arial("Arial", 16);
121 base::StringToLowerASCII(arial
.GetActualFontNameForTesting()))
123 << "Your test environment seems to be missing Arial font, which is "
124 << "needed for unittests. Check if Arial font is installed.\n"
126 Font
symbol("Symbol", 16);
128 base::StringToLowerASCII(symbol
.GetActualFontNameForTesting()))
130 << "Your test environment seems to be missing Symbol font, which is "
131 << "needed for unittests. Check if Symbol font is installed.\n"
134 const char* const invalid_font_name
= "no_such_font_name";
135 Font
fallback_font(invalid_font_name
, 16);
136 EXPECT_NE(invalid_font_name
,
137 base::StringToLowerASCII(
138 fallback_font
.GetActualFontNameForTesting()));
143 TEST_F(FontTest
, DeriveResizesIfSizeTooSmall
) {
145 // The minimum font size is set to 5 in browser_main.cc.
146 ScopedMinimumFontSizeCallback
minimum_size(5);
148 Font derived_font
= cf
.Derive(-4, cf
.GetStyle());
149 EXPECT_EQ(5, derived_font
.GetFontSize());
152 TEST_F(FontTest
, DeriveKeepsOriginalSizeIfHeightOk
) {
154 // The minimum font size is set to 5 in browser_main.cc.
155 ScopedMinimumFontSizeCallback
minimum_size(5);
157 Font derived_font
= cf
.Derive(-2, cf
.GetStyle());
158 EXPECT_EQ(6, derived_font
.GetFontSize());
160 #endif // defined(OS_WIN)