cc: Add more eviction categories to picture layer impl.
[chromium-blink-merge.git] / ui / gfx / font_unittest.cc
blob3c043c0622e351adeb500314f472b60b196bd5f6
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>
14 #elif defined(OS_WIN)
15 #include "ui/gfx/platform_font_win.h"
16 #endif
18 namespace gfx {
19 namespace {
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
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;
41 ~ScopedMinimumFontSizeCallback() {
42 PlatformFontWin::get_minimum_font_size_callback = old_callback_;
45 private:
46 static int GetMinimumFontSize() {
47 return minimum_size_;
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) {
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",
68 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
69 FreeIfNecessary(native);
72 TEST_F(FontTest, LoadArialBold) {
73 Font cf("Arial", 16);
74 Font bold(cf.Derive(0, Font::BOLD));
75 NativeFont native = bold.GetNativeFont();
76 EXPECT_TRUE(native);
77 EXPECT_EQ(bold.GetStyle(), Font::BOLD);
78 EXPECT_EQ("arial",
79 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
80 FreeIfNecessary(native);
83 TEST_F(FontTest, Ascent) {
84 Font cf("Arial", 16);
85 EXPECT_GT(cf.GetBaseline(), 2);
86 EXPECT_LE(cf.GetBaseline(), 22);
89 TEST_F(FontTest, Height) {
90 Font cf("Arial", 16);
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) {
97 Font cf("Arial", 16);
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));
111 #if !defined(OS_WIN)
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);
120 EXPECT_EQ("arial",
121 base::StringToLowerASCII(arial.GetActualFontNameForTesting()))
122 << "********\n"
123 << "Your test environment seems to be missing Arial font, which is "
124 << "needed for unittests. Check if Arial font is installed.\n"
125 << "********";
126 Font symbol("Symbol", 16);
127 EXPECT_EQ("symbol",
128 base::StringToLowerASCII(symbol.GetActualFontNameForTesting()))
129 << "********\n"
130 << "Your test environment seems to be missing Symbol font, which is "
131 << "needed for unittests. Check if Symbol font is installed.\n"
132 << "********";
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()));
140 #endif
142 #if defined(OS_WIN)
143 TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
144 Font cf("Arial", 8);
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) {
153 Font cf("Arial", 8);
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)
162 } // namespace
163 } // namespace gfx