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/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
12 #include <pango/pango.h>
14 #include "ui/gfx/platform_font_win.h"
20 class FontTest
: public testing::Test
{
22 // Fulfills the memory management contract as outlined by the comment at
23 // gfx::Font::GetNativeFont().
24 void FreeIfNecessary(NativeFont font
) {
25 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
26 pango_font_description_free(font
);
32 class ScopedMinimumFontSizeCallback
{
34 explicit ScopedMinimumFontSizeCallback(int minimum_size
) {
35 minimum_size_
= minimum_size
;
36 old_callback_
= PlatformFontWin::get_minimum_font_size_callback
;
37 PlatformFontWin::get_minimum_font_size_callback
= &GetMinimumFontSize
;
40 ~ScopedMinimumFontSizeCallback() {
41 PlatformFontWin::get_minimum_font_size_callback
= old_callback_
;
45 static int GetMinimumFontSize() {
49 PlatformFontWin::GetMinimumFontSizeCallback old_callback_
;
50 static int minimum_size_
;
52 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback
);
55 int ScopedMinimumFontSizeCallback::minimum_size_
= 0;
56 #endif // defined(OS_WIN)
59 TEST_F(FontTest
, LoadArial
) {
61 NativeFont native
= cf
.GetNativeFont();
63 ASSERT_EQ(cf
.GetStyle(), Font::NORMAL
);
64 ASSERT_EQ(cf
.GetFontSize(), 16);
65 ASSERT_EQ(cf
.GetFontName(), "Arial");
66 FreeIfNecessary(native
);
69 TEST_F(FontTest
, LoadArialBold
) {
71 Font
bold(cf
.DeriveFont(0, Font::BOLD
));
72 NativeFont native
= bold
.GetNativeFont();
74 ASSERT_EQ(bold
.GetStyle(), Font::BOLD
);
75 FreeIfNecessary(native
);
78 TEST_F(FontTest
, Ascent
) {
80 ASSERT_GT(cf
.GetBaseline(), 2);
81 ASSERT_LE(cf
.GetBaseline(), 22);
84 TEST_F(FontTest
, Height
) {
86 ASSERT_GE(cf
.GetHeight(), 16);
87 // TODO(akalin): Figure out why height is so large on Linux.
88 ASSERT_LE(cf
.GetHeight(), 26);
91 TEST_F(FontTest
, AvgWidths
) {
93 ASSERT_EQ(cf
.GetExpectedTextWidth(0), 0);
94 ASSERT_GT(cf
.GetExpectedTextWidth(1), cf
.GetExpectedTextWidth(0));
95 ASSERT_GT(cf
.GetExpectedTextWidth(2), cf
.GetExpectedTextWidth(1));
96 ASSERT_GT(cf
.GetExpectedTextWidth(3), cf
.GetExpectedTextWidth(2));
99 TEST_F(FontTest
, AvgCharWidth
) {
100 Font
cf("Arial", 16);
101 ASSERT_GT(cf
.GetAverageCharacterWidth(), 0);
104 TEST_F(FontTest
, Widths
) {
105 Font
cf("Arial", 16);
106 ASSERT_EQ(cf
.GetStringWidth(base::string16()), 0);
107 ASSERT_GT(cf
.GetStringWidth(ASCIIToUTF16("a")),
108 cf
.GetStringWidth(base::string16()));
109 ASSERT_GT(cf
.GetStringWidth(ASCIIToUTF16("ab")),
110 cf
.GetStringWidth(ASCIIToUTF16("a")));
111 ASSERT_GT(cf
.GetStringWidth(ASCIIToUTF16("abc")),
112 cf
.GetStringWidth(ASCIIToUTF16("ab")));
116 TEST_F(FontTest
, DeriveFontResizesIfSizeTooSmall
) {
118 // The minimum font size is set to 5 in browser_main.cc.
119 ScopedMinimumFontSizeCallback
minimum_size(5);
121 Font derived_font
= cf
.DeriveFont(-4);
122 EXPECT_EQ(5, derived_font
.GetFontSize());
125 TEST_F(FontTest
, DeriveFontKeepsOriginalSizeIfHeightOk
) {
127 // The minimum font size is set to 5 in browser_main.cc.
128 ScopedMinimumFontSizeCallback
minimum_size(5);
130 Font derived_font
= cf
.DeriveFont(-2);
131 EXPECT_EQ(6, derived_font
.GetFontSize());
133 #endif // defined(OS_WIN)