Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / font_unittest.cc
blob102c7c8cd7e7d85acd8650d8c978192a9ad14cf0
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_WIN)
13 #include "ui/gfx/platform_font_win.h"
14 #endif
16 namespace gfx {
17 namespace {
19 using FontTest = testing::Test;
21 #if defined(OS_WIN)
22 class ScopedMinimumFontSizeCallback {
23 public:
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_;
34 private:
35 static int GetMinimumFontSize() {
36 return minimum_size_;
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
50 #else
51 #define MAYBE_LoadArial LoadArial
52 #endif
53 TEST_F(FontTest, MAYBE_LoadArial) {
54 Font cf("Arial", 16);
55 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
56 EXPECT_TRUE(cf.GetNativeFont());
57 #endif
58 EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
59 EXPECT_EQ(cf.GetFontSize(), 16);
60 EXPECT_EQ(cf.GetFontName(), "Arial");
61 EXPECT_EQ("arial", base::ToLowerASCII(cf.GetActualFontNameForTesting()));
64 #if defined(OS_ANDROID)
65 #define MAYBE_LoadArialBold DISABLED_LoadArialBold
66 #else
67 #define MAYBE_LoadArialBold LoadArialBold
68 #endif
69 TEST_F(FontTest, MAYBE_LoadArialBold) {
70 Font cf("Arial", 16);
71 Font bold(cf.Derive(0, Font::BOLD));
72 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
73 EXPECT_TRUE(bold.GetNativeFont());
74 #endif
75 EXPECT_EQ(bold.GetStyle(), Font::BOLD);
76 EXPECT_EQ("arial", base::ToLowerASCII(cf.GetActualFontNameForTesting()));
79 #if defined(OS_ANDROID)
80 #define MAYBE_Ascent DISABLED_Ascent
81 #else
82 #define MAYBE_Ascent Ascent
83 #endif
84 TEST_F(FontTest, MAYBE_Ascent) {
85 Font cf("Arial", 16);
86 EXPECT_GT(cf.GetBaseline(), 2);
87 EXPECT_LE(cf.GetBaseline(), 22);
90 #if defined(OS_ANDROID)
91 #define MAYBE_Height DISABLED_Height
92 #else
93 #define MAYBE_Height Height
94 #endif
95 TEST_F(FontTest, MAYBE_Height) {
96 Font cf("Arial", 16);
97 EXPECT_GE(cf.GetHeight(), 16);
98 // TODO(akalin): Figure out why height is so large on Linux.
99 EXPECT_LE(cf.GetHeight(), 26);
102 #if defined(OS_ANDROID)
103 #define MAYBE_CapHeight DISABLED_CapHeight
104 #else
105 #define MAYBE_CapHeight CapHeight
106 #endif
107 TEST_F(FontTest, MAYBE_CapHeight) {
108 Font cf("Arial", 16);
109 EXPECT_GT(cf.GetCapHeight(), 0);
110 EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2);
111 EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline());
114 #if defined(OS_ANDROID)
115 #define MAYBE_AvgWidths DISABLED_AvgWidths
116 #else
117 #define MAYBE_AvgWidths AvgWidths
118 #endif
119 TEST_F(FontTest, MAYBE_AvgWidths) {
120 Font cf("Arial", 16);
121 EXPECT_EQ(cf.GetExpectedTextWidth(0), 0);
122 EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
123 EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
124 EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
127 #if defined(OS_WIN) || defined(OS_ANDROID)
128 #define MAYBE_GetActualFontNameForTesting DISABLED_GetActualFontNameForTesting
129 #else
130 #define MAYBE_GetActualFontNameForTesting GetActualFontNameForTesting
131 #endif
132 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
133 // http://crbug.com/327287
135 // Check that fonts used for testing are installed and enabled. On Mac
136 // fonts may be installed but still need enabling in Font Book.app.
137 // http://crbug.com/347429
138 TEST_F(FontTest, MAYBE_GetActualFontNameForTesting) {
139 Font arial("Arial", 16);
140 EXPECT_EQ("arial", base::ToLowerASCII(arial.GetActualFontNameForTesting()))
141 << "********\n"
142 << "Your test environment seems to be missing Arial font, which is "
143 << "needed for unittests. Check if Arial font is installed.\n"
144 << "********";
145 Font symbol("Symbol", 16);
146 EXPECT_EQ("symbol", base::ToLowerASCII(symbol.GetActualFontNameForTesting()))
147 << "********\n"
148 << "Your test environment seems to be missing Symbol font, which is "
149 << "needed for unittests. Check if Symbol font is installed.\n"
150 << "********";
152 const char* const invalid_font_name = "no_such_font_name";
153 Font fallback_font(invalid_font_name, 16);
154 EXPECT_NE(invalid_font_name,
155 base::ToLowerASCII(fallback_font.GetActualFontNameForTesting()));
158 #if defined(OS_WIN)
159 TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
160 Font cf("Arial", 8);
161 // The minimum font size is set to 5 in browser_main.cc.
162 ScopedMinimumFontSizeCallback minimum_size(5);
164 Font derived_font = cf.Derive(-4, cf.GetStyle());
165 EXPECT_EQ(5, derived_font.GetFontSize());
168 TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) {
169 Font cf("Arial", 8);
170 // The minimum font size is set to 5 in browser_main.cc.
171 ScopedMinimumFontSizeCallback minimum_size(5);
173 Font derived_font = cf.Derive(-2, cf.GetStyle());
174 EXPECT_EQ(6, derived_font.GetFontSize());
176 #endif // defined(OS_WIN)
178 } // namespace
179 } // namespace gfx