blob: da4bdfcc4bd05a82d85d8d2581263d63408f247c [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
[email protected]34ed4672013-11-06 06:02:4512#if defined(OS_LINUX) && !defined(USE_OZONE)
[email protected]23fc1152011-02-17 14:55:2613#include <pango/pango.h>
14#elif defined(OS_WIN)
15#include "ui/gfx/platform_font_win.h"
16#endif
17
[email protected]6a1cac1c2012-10-29 19:58:0218namespace gfx {
[email protected]7f4bfe12008-12-12 01:52:2519namespace {
20
[email protected]5e70f142009-09-14 19:26:4921class FontTest : public testing::Test {
[email protected]23fc1152011-02-17 14:55:2622 public:
23 // Fulfills the memory management contract as outlined by the comment at
24 // gfx::Font::GetNativeFont().
[email protected]6a1cac1c2012-10-29 19:58:0225 void FreeIfNecessary(NativeFont font) {
[email protected]34ed4672013-11-06 06:02:4526#if defined(OS_LINUX) && !defined(USE_OZONE)
[email protected]23fc1152011-02-17 14:55:2627 pango_font_description_free(font);
28#endif
29 }
[email protected]5e70f142009-09-14 19:26:4930};
[email protected]7f4bfe12008-12-12 01:52:2531
[email protected]5b3a7e92010-10-06 22:56:4732#if defined(OS_WIN)
33class ScopedMinimumFontSizeCallback {
34 public:
35 explicit ScopedMinimumFontSizeCallback(int minimum_size) {
36 minimum_size_ = minimum_size;
[email protected]6a1cac1c2012-10-29 19:58:0237 old_callback_ = PlatformFontWin::get_minimum_font_size_callback;
38 PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
[email protected]5b3a7e92010-10-06 22:56:4739 }
40
41 ~ScopedMinimumFontSizeCallback() {
[email protected]6a1cac1c2012-10-29 19:58:0242 PlatformFontWin::get_minimum_font_size_callback = old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4743 }
44
45 private:
46 static int GetMinimumFontSize() {
47 return minimum_size_;
48 }
49
[email protected]6a1cac1c2012-10-29 19:58:0250 PlatformFontWin::GetMinimumFontSizeCallback old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4751 static int minimum_size_;
52
53 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback);
54};
55
56int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
57#endif // defined(OS_WIN)
58
59
[email protected]7322c4402009-05-15 02:16:1060TEST_F(FontTest, LoadArial) {
[email protected]610ae5f2011-10-27 23:55:3761 Font cf("Arial", 16);
[email protected]6a1cac1c2012-10-29 19:58:0262 NativeFont native = cf.GetNativeFont();
[email protected]c831ea9c2013-10-01 10:52:2463 EXPECT_TRUE(native);
64 EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
65 EXPECT_EQ(cf.GetFontSize(), 16);
66 EXPECT_EQ(cf.GetFontName(), "Arial");
[email protected]2fb26572013-12-11 08:43:0667 EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting()));
[email protected]23fc1152011-02-17 14:55:2668 FreeIfNecessary(native);
[email protected]7f4bfe12008-12-12 01:52:2569}
70
[email protected]7322c4402009-05-15 02:16:1071TEST_F(FontTest, LoadArialBold) {
[email protected]610ae5f2011-10-27 23:55:3772 Font cf("Arial", 16);
[email protected]01430822014-02-13 15:41:4573 Font bold(cf.Derive(0, Font::BOLD));
[email protected]6a1cac1c2012-10-29 19:58:0274 NativeFont native = bold.GetNativeFont();
[email protected]c831ea9c2013-10-01 10:52:2475 EXPECT_TRUE(native);
76 EXPECT_EQ(bold.GetStyle(), Font::BOLD);
[email protected]2fb26572013-12-11 08:43:0677 EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting()));
[email protected]23fc1152011-02-17 14:55:2678 FreeIfNecessary(native);
[email protected]7f4bfe12008-12-12 01:52:2579}
80
[email protected]7322c4402009-05-15 02:16:1081TEST_F(FontTest, Ascent) {
[email protected]610ae5f2011-10-27 23:55:3782 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:2483 EXPECT_GT(cf.GetBaseline(), 2);
84 EXPECT_LE(cf.GetBaseline(), 22);
[email protected]7f4bfe12008-12-12 01:52:2585}
86
[email protected]7322c4402009-05-15 02:16:1087TEST_F(FontTest, Height) {
[email protected]610ae5f2011-10-27 23:55:3788 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:2489 EXPECT_GE(cf.GetHeight(), 16);
[email protected]4eae2c442009-11-03 23:46:2690 // TODO(akalin): Figure out why height is so large on Linux.
[email protected]c831ea9c2013-10-01 10:52:2491 EXPECT_LE(cf.GetHeight(), 26);
92}
93
94TEST_F(FontTest, CapHeight) {
95 Font cf("Arial", 16);
96 EXPECT_GT(cf.GetCapHeight(), 0);
97 EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2);
[email protected]56e50572013-11-01 14:25:3198 EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline());
[email protected]7f4bfe12008-12-12 01:52:2599}
100
[email protected]7322c4402009-05-15 02:16:10101TEST_F(FontTest, AvgWidths) {
[email protected]610ae5f2011-10-27 23:55:37102 Font cf("Arial", 16);
[email protected]c831ea9c2013-10-01 10:52:24103 EXPECT_EQ(cf.GetExpectedTextWidth(0), 0);
104 EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
105 EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
106 EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
[email protected]7f4bfe12008-12-12 01:52:25107}
108
[email protected]2fb26572013-12-11 08:43:06109#if !defined(OS_WIN)
110// On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
111// http://crbug.com/327287
[email protected]85eb3e12014-02-27 20:44:12112//
113// Check that fonts used for testing are installed and enabled. On Mac
114// fonts may be installed but still need enabling in Font Book.app.
115// http://crbug.com/347429
[email protected]2fb26572013-12-11 08:43:06116TEST_F(FontTest, GetActualFontNameForTesting) {
117 Font arial("Arial", 16);
[email protected]85eb3e12014-02-27 20:44:12118 EXPECT_EQ("arial", StringToLowerASCII(arial.GetActualFontNameForTesting()))
119 << "********\n"
120 << "Your test environment seems to be missing Arial font, which is "
121 << "needed for unittests. Check if Arial font is installed.\n"
122 << "********";
[email protected]2fb26572013-12-11 08:43:06123 Font symbol("Symbol", 16);
[email protected]85eb3e12014-02-27 20:44:12124 EXPECT_EQ("symbol", StringToLowerASCII(symbol.GetActualFontNameForTesting()))
125 << "********\n"
126 << "Your test environment seems to be missing Symbol font, which is "
127 << "needed for unittests. Check if Symbol font is installed.\n"
128 << "********";
[email protected]2fb26572013-12-11 08:43:06129
130 const char* const invalid_font_name = "no_such_font_name";
131 Font fallback_font(invalid_font_name, 16);
132 EXPECT_NE(invalid_font_name,
133 StringToLowerASCII(fallback_font.GetActualFontNameForTesting()));
134}
135#endif
136
[email protected]f80821f2009-06-16 23:10:29137#if defined(OS_WIN)
[email protected]01430822014-02-13 15:41:45138TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
[email protected]610ae5f2011-10-27 23:55:37139 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47140 // The minimum font size is set to 5 in browser_main.cc.
141 ScopedMinimumFontSizeCallback minimum_size(5);
142
[email protected]01430822014-02-13 15:41:45143 Font derived_font = cf.Derive(-4, cf.GetStyle());
[email protected]c8703bb2011-10-19 14:35:15144 EXPECT_EQ(5, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29145}
146
[email protected]01430822014-02-13 15:41:45147TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) {
[email protected]610ae5f2011-10-27 23:55:37148 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47149 // The minimum font size is set to 5 in browser_main.cc.
150 ScopedMinimumFontSizeCallback minimum_size(5);
151
[email protected]01430822014-02-13 15:41:45152 Font derived_font = cf.Derive(-2, cf.GetStyle());
[email protected]c8703bb2011-10-19 14:35:15153 EXPECT_EQ(6, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29154}
[email protected]23fc1152011-02-17 14:55:26155#endif // defined(OS_WIN)
156
[email protected]c6ac8412010-08-13 16:43:03157} // namespace
[email protected]6a1cac1c2012-10-29 19:58:02158} // namespace gfx