blob: 4b81c8129c104448c6d728bcafe00171c85ebb84 [file] [log] [blame]
[email protected]34694762012-01-31 05:15:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7f4bfe12008-12-12 01:52:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]08397d52011-02-05 01:53:385#include "ui/gfx/font.h"
[email protected]7f4bfe12008-12-12 01:52:256
[email protected]f3652ff92013-06-11 13:54:317#include "base/strings/string16.h"
[email protected]2fb26572013-12-11 08:43:068#include "base/strings/string_util.h"
[email protected]c7057fbe2013-06-07 18:54:019#include "base/strings/utf_string_conversions.h"
[email protected]7f4bfe12008-12-12 01:52:2510#include "testing/gtest/include/gtest/gtest.h"
11
Daniel Eratcb2db9b2015-02-11 15:15:0912#if defined(OS_WIN)
[email protected]23fc1152011-02-17 14:55:2613#include "ui/gfx/platform_font_win.h"
14#endif
15
[email protected]6a1cac1c2012-10-29 19:58:0216namespace gfx {
[email protected]7f4bfe12008-12-12 01:52:2517namespace {
18
Daniel Eratcb2db9b2015-02-11 15:15:0919using FontTest = testing::Test;
[email protected]7f4bfe12008-12-12 01:52:2520
[email protected]5b3a7e92010-10-06 22:56:4721#if defined(OS_WIN)
22class ScopedMinimumFontSizeCallback {
23 public:
24 explicit ScopedMinimumFontSizeCallback(int minimum_size) {
25 minimum_size_ = minimum_size;
[email protected]6a1cac1c2012-10-29 19:58:0226 old_callback_ = PlatformFontWin::get_minimum_font_size_callback;
27 PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
[email protected]5b3a7e92010-10-06 22:56:4728 }
29
30 ~ScopedMinimumFontSizeCallback() {
[email protected]6a1cac1c2012-10-29 19:58:0231 PlatformFontWin::get_minimum_font_size_callback = old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4732 }
33
34 private:
35 static int GetMinimumFontSize() {
36 return minimum_size_;
37 }
38
[email protected]6a1cac1c2012-10-29 19:58:0239 PlatformFontWin::GetMinimumFontSizeCallback old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4740 static int minimum_size_;
41
42 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback);
43};
44
45int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
46#endif // defined(OS_WIN)
47
tfarinab7e00dc2015-08-05 22:49:5448#if defined(OS_ANDROID)
49#define MAYBE_LoadArial DISABLED_LoadArial
50#else
51#define MAYBE_LoadArial LoadArial
52#endif
53TEST_F(FontTest, MAYBE_LoadArial) {
[email protected]610ae5f2011-10-27 23:55:3754 Font cf("Arial", 16);
Daniel Eratcb2db9b2015-02-11 15:15:0955#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
56 EXPECT_TRUE(cf.GetNativeFont());
57#endif
[email protected]c831ea9c2013-10-01 10:52:2458 EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
59 EXPECT_EQ(cf.GetFontSize(), 16);
60 EXPECT_EQ(cf.GetFontName(), "Arial");
[email protected]cb1f4ac2014-08-07 16:55:4261 EXPECT_EQ("arial",
62 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
[email protected]7f4bfe12008-12-12 01:52:2563}
64
tfarinab7e00dc2015-08-05 22:49:5465#if defined(OS_ANDROID)
66#define MAYBE_LoadArialBold DISABLED_LoadArialBold
67#else
68#define MAYBE_LoadArialBold LoadArialBold
69#endif
70TEST_F(FontTest, MAYBE_LoadArialBold) {
[email protected]610ae5f2011-10-27 23:55:3771 Font cf("Arial", 16);
[email protected]01430822014-02-13 15:41:4572 Font bold(cf.Derive(0, Font::BOLD));
Daniel Eratcb2db9b2015-02-11 15:15:0973#if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
74 EXPECT_TRUE(bold.GetNativeFont());
75#endif
[email protected]c831ea9c2013-10-01 10:52:2476 EXPECT_EQ(bold.GetStyle(), Font::BOLD);
[email protected]cb1f4ac2014-08-07 16:55:4277 EXPECT_EQ("arial",
78 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
[email protected]7f4bfe12008-12-12 01:52:2579}
80
tfarinab7e00dc2015-08-05 22:49:5481#if defined(OS_ANDROID)
82#define MAYBE_Ascent DISABLED_Ascent
83#else
84#define MAYBE_Ascent Ascent
85#endif
86TEST_F(FontTest, MAYBE_Ascent) {
[email protected]610ae5f2011-10-27 23:55:3787 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:2488 EXPECT_GT(cf.GetBaseline(), 2);
89 EXPECT_LE(cf.GetBaseline(), 22);
[email protected]7f4bfe12008-12-12 01:52:2590}
91
tfarinab7e00dc2015-08-05 22:49:5492#if defined(OS_ANDROID)
93#define MAYBE_Height DISABLED_Height
94#else
95#define MAYBE_Height Height
96#endif
97TEST_F(FontTest, MAYBE_Height) {
[email protected]610ae5f2011-10-27 23:55:3798 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:2499 EXPECT_GE(cf.GetHeight(), 16);
[email protected]4eae2c442009-11-03 23:46:26100 // TODO(akalin): Figure out why height is so large on Linux.
[email protected]c831ea9c2013-10-01 10:52:24101 EXPECT_LE(cf.GetHeight(), 26);
102}
103
tfarinab7e00dc2015-08-05 22:49:54104#if defined(OS_ANDROID)
105#define MAYBE_CapHeight DISABLED_CapHeight
106#else
107#define MAYBE_CapHeight CapHeight
108#endif
109TEST_F(FontTest, MAYBE_CapHeight) {
[email protected]c831ea9c2013-10-01 10:52:24110 Font cf("Arial", 16);
111 EXPECT_GT(cf.GetCapHeight(), 0);
112 EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2);
[email protected]56e50572013-11-01 14:25:31113 EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline());
[email protected]7f4bfe12008-12-12 01:52:25114}
115
tfarinab7e00dc2015-08-05 22:49:54116#if defined(OS_ANDROID)
117#define MAYBE_AvgWidths DISABLED_AvgWidths
118#else
119#define MAYBE_AvgWidths AvgWidths
120#endif
121TEST_F(FontTest, MAYBE_AvgWidths) {
[email protected]610ae5f2011-10-27 23:55:37122 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:24123 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));
[email protected]7f4bfe12008-12-12 01:52:25127}
128
tfarinab7e00dc2015-08-05 22:49:54129#if defined(OS_WIN) || defined(OS_ANDROID)
130#define MAYBE_GetActualFontNameForTesting DISABLED_GetActualFontNameForTesting
131#else
132#define MAYBE_GetActualFontNameForTesting GetActualFontNameForTesting
133#endif
[email protected]2fb26572013-12-11 08:43:06134// On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
135// http://crbug.com/327287
[email protected]85eb3e12014-02-27 20:44:12136//
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
tfarinab7e00dc2015-08-05 22:49:54140TEST_F(FontTest, MAYBE_GetActualFontNameForTesting) {
[email protected]2fb26572013-12-11 08:43:06141 Font arial("Arial", 16);
[email protected]cb1f4ac2014-08-07 16:55:42142 EXPECT_EQ("arial",
143 base::StringToLowerASCII(arial.GetActualFontNameForTesting()))
[email protected]85eb3e12014-02-27 20:44:12144 << "********\n"
145 << "Your test environment seems to be missing Arial font, which is "
146 << "needed for unittests. Check if Arial font is installed.\n"
147 << "********";
[email protected]2fb26572013-12-11 08:43:06148 Font symbol("Symbol", 16);
[email protected]cb1f4ac2014-08-07 16:55:42149 EXPECT_EQ("symbol",
150 base::StringToLowerASCII(symbol.GetActualFontNameForTesting()))
[email protected]85eb3e12014-02-27 20:44:12151 << "********\n"
152 << "Your test environment seems to be missing Symbol font, which is "
153 << "needed for unittests. Check if Symbol font is installed.\n"
154 << "********";
[email protected]2fb26572013-12-11 08:43:06155
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,
[email protected]cb1f4ac2014-08-07 16:55:42159 base::StringToLowerASCII(
160 fallback_font.GetActualFontNameForTesting()));
[email protected]2fb26572013-12-11 08:43:06161}
[email protected]2fb26572013-12-11 08:43:06162
[email protected]f80821f2009-06-16 23:10:29163#if defined(OS_WIN)
[email protected]01430822014-02-13 15:41:45164TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
[email protected]610ae5f2011-10-27 23:55:37165 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47166 // The minimum font size is set to 5 in browser_main.cc.
167 ScopedMinimumFontSizeCallback minimum_size(5);
168
[email protected]01430822014-02-13 15:41:45169 Font derived_font = cf.Derive(-4, cf.GetStyle());
[email protected]c8703bb2011-10-19 14:35:15170 EXPECT_EQ(5, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29171}
172
[email protected]01430822014-02-13 15:41:45173TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) {
[email protected]610ae5f2011-10-27 23:55:37174 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47175 // The minimum font size is set to 5 in browser_main.cc.
176 ScopedMinimumFontSizeCallback minimum_size(5);
177
[email protected]01430822014-02-13 15:41:45178 Font derived_font = cf.Derive(-2, cf.GetStyle());
[email protected]c8703bb2011-10-19 14:35:15179 EXPECT_EQ(6, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29180}
[email protected]23fc1152011-02-17 14:55:26181#endif // defined(OS_WIN)
182
[email protected]c6ac8412010-08-13 16:43:03183} // namespace
[email protected]6a1cac1c2012-10-29 19:58:02184} // namespace gfx