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"
13 #include "ui/gfx/platform_font_win.h"
19 using FontTest
= testing::Test
;
22 class ScopedMinimumFontSizeCallback
{
24 explicit ScopedMinimumFontSizeCallback(int minimum_size
) {
25 minimum_size_
= minimum_size
;
26 old_callback_
= PlatformFontWin::get_minimum_font_size_callback
;
27 PlatformFontWin::get_minimum_font_size_callback
= &GetMinimumFontSize
;
30 ~ScopedMinimumFontSizeCallback() {
31 PlatformFontWin::get_minimum_font_size_callback
= old_callback_
;
35 static int GetMinimumFontSize() {
39 PlatformFontWin::GetMinimumFontSizeCallback old_callback_
;
40 static int minimum_size_
;
42 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback
);
45 int ScopedMinimumFontSizeCallback::minimum_size_
= 0;
46 #endif // defined(OS_WIN)
48 #if defined(OS_ANDROID)
49 #define MAYBE_LoadArial DISABLED_LoadArial
51 #define MAYBE_LoadArial LoadArial
53 TEST_F(FontTest
, MAYBE_LoadArial
) {
55 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
56 EXPECT_TRUE(cf
.GetNativeFont());
58 EXPECT_EQ(cf
.GetStyle(), Font::NORMAL
);
59 EXPECT_EQ(cf
.GetFontSize(), 16);
60 EXPECT_EQ(cf
.GetFontName(), "Arial");
62 base::StringToLowerASCII(cf
.GetActualFontNameForTesting()));
65 #if defined(OS_ANDROID)
66 #define MAYBE_LoadArialBold DISABLED_LoadArialBold
68 #define MAYBE_LoadArialBold LoadArialBold
70 TEST_F(FontTest
, MAYBE_LoadArialBold
) {
72 Font
bold(cf
.Derive(0, Font::BOLD
));
73 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
74 EXPECT_TRUE(bold
.GetNativeFont());
76 EXPECT_EQ(bold
.GetStyle(), Font::BOLD
);
78 base::StringToLowerASCII(cf
.GetActualFontNameForTesting()));
81 #if defined(OS_ANDROID)
82 #define MAYBE_Ascent DISABLED_Ascent
84 #define MAYBE_Ascent Ascent
86 TEST_F(FontTest
, MAYBE_Ascent
) {
88 EXPECT_GT(cf
.GetBaseline(), 2);
89 EXPECT_LE(cf
.GetBaseline(), 22);
92 #if defined(OS_ANDROID)
93 #define MAYBE_Height DISABLED_Height
95 #define MAYBE_Height Height
97 TEST_F(FontTest
, MAYBE_Height
) {
99 EXPECT_GE(cf
.GetHeight(), 16);
100 // TODO(akalin): Figure out why height is so large on Linux.
101 EXPECT_LE(cf
.GetHeight(), 26);
104 #if defined(OS_ANDROID)
105 #define MAYBE_CapHeight DISABLED_CapHeight
107 #define MAYBE_CapHeight CapHeight
109 TEST_F(FontTest
, MAYBE_CapHeight
) {
110 Font
cf("Arial", 16);
111 EXPECT_GT(cf
.GetCapHeight(), 0);
112 EXPECT_GT(cf
.GetCapHeight(), cf
.GetHeight() / 2);
113 EXPECT_LT(cf
.GetCapHeight(), cf
.GetBaseline());
116 #if defined(OS_ANDROID)
117 #define MAYBE_AvgWidths DISABLED_AvgWidths
119 #define MAYBE_AvgWidths AvgWidths
121 TEST_F(FontTest
, MAYBE_AvgWidths
) {
122 Font
cf("Arial", 16);
123 EXPECT_EQ(cf
.GetExpectedTextWidth(0), 0);
124 EXPECT_GT(cf
.GetExpectedTextWidth(1), cf
.GetExpectedTextWidth(0));
125 EXPECT_GT(cf
.GetExpectedTextWidth(2), cf
.GetExpectedTextWidth(1));
126 EXPECT_GT(cf
.GetExpectedTextWidth(3), cf
.GetExpectedTextWidth(2));
129 #if defined(OS_WIN) || defined(OS_ANDROID)
130 #define MAYBE_GetActualFontNameForTesting DISABLED_GetActualFontNameForTesting
132 #define MAYBE_GetActualFontNameForTesting GetActualFontNameForTesting
134 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
135 // http://crbug.com/327287
137 // Check that fonts used for testing are installed and enabled. On Mac
138 // fonts may be installed but still need enabling in Font Book.app.
139 // http://crbug.com/347429
140 TEST_F(FontTest
, MAYBE_GetActualFontNameForTesting
) {
141 Font
arial("Arial", 16);
143 base::StringToLowerASCII(arial
.GetActualFontNameForTesting()))
145 << "Your test environment seems to be missing Arial font, which is "
146 << "needed for unittests. Check if Arial font is installed.\n"
148 Font
symbol("Symbol", 16);
150 base::StringToLowerASCII(symbol
.GetActualFontNameForTesting()))
152 << "Your test environment seems to be missing Symbol font, which is "
153 << "needed for unittests. Check if Symbol font is installed.\n"
156 const char* const invalid_font_name
= "no_such_font_name";
157 Font
fallback_font(invalid_font_name
, 16);
158 EXPECT_NE(invalid_font_name
,
159 base::StringToLowerASCII(
160 fallback_font
.GetActualFontNameForTesting()));
164 TEST_F(FontTest
, DeriveResizesIfSizeTooSmall
) {
166 // The minimum font size is set to 5 in browser_main.cc.
167 ScopedMinimumFontSizeCallback
minimum_size(5);
169 Font derived_font
= cf
.Derive(-4, cf
.GetStyle());
170 EXPECT_EQ(5, derived_font
.GetFontSize());
173 TEST_F(FontTest
, DeriveKeepsOriginalSizeIfHeightOk
) {
175 // The minimum font size is set to 5 in browser_main.cc.
176 ScopedMinimumFontSizeCallback
minimum_size(5);
178 Font derived_font
= cf
.Derive(-2, cf
.GetStyle());
179 EXPECT_EQ(6, derived_font
.GetFontSize());
181 #endif // defined(OS_WIN)