[email protected] | 3469476 | 2012-01-31 05:15:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 5 | #include "ui/gfx/font.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 6 | |
[email protected] | f3652ff9 | 2013-06-11 13:54:31 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | c7057fbe | 2013-06-07 18:54:01 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
[email protected] | 34ed467 | 2013-11-06 06:02:45 | [diff] [blame] | 12 | #if defined(OS_LINUX) && !defined(USE_OZONE) |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 13 | #include <pango/pango.h> |
| 14 | #elif defined(OS_WIN) |
| 15 | #include "ui/gfx/platform_font_win.h" |
| 16 | #endif |
| 17 | |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 18 | namespace gfx { |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 5e70f14 | 2009-09-14 19:26:49 | [diff] [blame] | 21 | class FontTest : public testing::Test { |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 22 | public: |
| 23 | // Fulfills the memory management contract as outlined by the comment at |
| 24 | // gfx::Font::GetNativeFont(). |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 25 | void FreeIfNecessary(NativeFont font) { |
[email protected] | 34ed467 | 2013-11-06 06:02:45 | [diff] [blame] | 26 | #if defined(OS_LINUX) && !defined(USE_OZONE) |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 27 | pango_font_description_free(font); |
| 28 | #endif |
| 29 | } |
[email protected] | 5e70f14 | 2009-09-14 19:26:49 | [diff] [blame] | 30 | }; |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 31 | |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 32 | #if defined(OS_WIN) |
| 33 | class ScopedMinimumFontSizeCallback { |
| 34 | public: |
| 35 | explicit ScopedMinimumFontSizeCallback(int minimum_size) { |
| 36 | minimum_size_ = minimum_size; |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 37 | old_callback_ = PlatformFontWin::get_minimum_font_size_callback; |
| 38 | PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | ~ScopedMinimumFontSizeCallback() { |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 42 | PlatformFontWin::get_minimum_font_size_callback = old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private: |
| 46 | static int GetMinimumFontSize() { |
| 47 | return minimum_size_; |
| 48 | } |
| 49 | |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 50 | PlatformFontWin::GetMinimumFontSizeCallback old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 51 | static int minimum_size_; |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback); |
| 54 | }; |
| 55 | |
| 56 | int ScopedMinimumFontSizeCallback::minimum_size_ = 0; |
| 57 | #endif // defined(OS_WIN) |
| 58 | |
| 59 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 60 | TEST_F(FontTest, LoadArial) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 61 | Font cf("Arial", 16); |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 62 | NativeFont native = cf.GetNativeFont(); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 63 | EXPECT_TRUE(native); |
| 64 | EXPECT_EQ(cf.GetStyle(), Font::NORMAL); |
| 65 | EXPECT_EQ(cf.GetFontSize(), 16); |
| 66 | EXPECT_EQ(cf.GetFontName(), "Arial"); |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 67 | EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting())); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 68 | FreeIfNecessary(native); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 71 | TEST_F(FontTest, LoadArialBold) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 72 | Font cf("Arial", 16); |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 73 | Font bold(cf.Derive(0, Font::BOLD)); |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 74 | NativeFont native = bold.GetNativeFont(); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 75 | EXPECT_TRUE(native); |
| 76 | EXPECT_EQ(bold.GetStyle(), Font::BOLD); |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 77 | EXPECT_EQ("arial", StringToLowerASCII(cf.GetActualFontNameForTesting())); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 78 | FreeIfNecessary(native); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 81 | TEST_F(FontTest, Ascent) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 82 | Font cf("Arial", 16); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 83 | EXPECT_GT(cf.GetBaseline(), 2); |
| 84 | EXPECT_LE(cf.GetBaseline(), 22); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 87 | TEST_F(FontTest, Height) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 88 | Font cf("Arial", 16); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 89 | EXPECT_GE(cf.GetHeight(), 16); |
[email protected] | 4eae2c44 | 2009-11-03 23:46:26 | [diff] [blame] | 90 | // TODO(akalin): Figure out why height is so large on Linux. |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 91 | EXPECT_LE(cf.GetHeight(), 26); |
| 92 | } |
| 93 | |
| 94 | TEST_F(FontTest, CapHeight) { |
| 95 | Font cf("Arial", 16); |
| 96 | EXPECT_GT(cf.GetCapHeight(), 0); |
| 97 | EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2); |
[email protected] | 56e5057 | 2013-11-01 14:25:31 | [diff] [blame] | 98 | EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline()); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 101 | TEST_F(FontTest, AvgWidths) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 102 | Font cf("Arial", 16); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 103 | 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] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 109 | #if !defined(OS_WIN) |
| 110 | // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now. |
| 111 | // http://crbug.com/327287 |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame^] | 112 | // |
| 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] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 116 | TEST_F(FontTest, GetActualFontNameForTesting) { |
| 117 | Font arial("Arial", 16); |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame^] | 118 | 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] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 123 | Font symbol("Symbol", 16); |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame^] | 124 | 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] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 129 | |
| 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] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 137 | #if defined(OS_WIN) |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 138 | TEST_F(FontTest, DeriveResizesIfSizeTooSmall) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 139 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 140 | // The minimum font size is set to 5 in browser_main.cc. |
| 141 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 142 | |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 143 | Font derived_font = cf.Derive(-4, cf.GetStyle()); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 144 | EXPECT_EQ(5, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 147 | TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 148 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 149 | // The minimum font size is set to 5 in browser_main.cc. |
| 150 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 151 | |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 152 | Font derived_font = cf.Derive(-2, cf.GetStyle()); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 153 | EXPECT_EQ(6, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 154 | } |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 155 | #endif // defined(OS_WIN) |
| 156 | |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 157 | } // namespace |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 158 | } // namespace gfx |