[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 | |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 7 | #include "base/macros.h" |
[email protected] | f3652ff9 | 2013-06-11 13:54:31 | [diff] [blame] | 8 | #include "base/strings/string16.h" |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | c7057fbe | 2013-06-07 18:54:01 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
avi | c89eb8d4 | 2015-12-23 08:08:18 | [diff] [blame] | 11 | #include "build/build_config.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
Daniel Erat | cb2db9b | 2015-02-11 15:15:09 | [diff] [blame] | 14 | #if defined(OS_WIN) |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 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 | |
Daniel Erat | cb2db9b | 2015-02-11 15:15:09 | [diff] [blame] | 21 | using FontTest = testing::Test; |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 22 | |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 23 | #if defined(OS_WIN) |
| 24 | class ScopedMinimumFontSizeCallback { |
| 25 | public: |
| 26 | explicit ScopedMinimumFontSizeCallback(int minimum_size) { |
| 27 | minimum_size_ = minimum_size; |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 28 | old_callback_ = PlatformFontWin::get_minimum_font_size_callback; |
| 29 | PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | ~ScopedMinimumFontSizeCallback() { |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 33 | PlatformFontWin::get_minimum_font_size_callback = old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | private: |
| 37 | static int GetMinimumFontSize() { |
| 38 | return minimum_size_; |
| 39 | } |
| 40 | |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 41 | PlatformFontWin::GetMinimumFontSizeCallback old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 42 | static int minimum_size_; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback); |
| 45 | }; |
| 46 | |
| 47 | int ScopedMinimumFontSizeCallback::minimum_size_ = 0; |
| 48 | #endif // defined(OS_WIN) |
| 49 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 50 | #if defined(OS_ANDROID) |
| 51 | #define MAYBE_LoadArial DISABLED_LoadArial |
| 52 | #else |
| 53 | #define MAYBE_LoadArial LoadArial |
| 54 | #endif |
| 55 | TEST_F(FontTest, MAYBE_LoadArial) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 56 | Font cf("Arial", 16); |
Daniel Erat | cb2db9b | 2015-02-11 15:15:09 | [diff] [blame] | 57 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS) |
| 58 | EXPECT_TRUE(cf.GetNativeFont()); |
| 59 | #endif |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 60 | EXPECT_EQ(cf.GetStyle(), Font::NORMAL); |
| 61 | EXPECT_EQ(cf.GetFontSize(), 16); |
| 62 | EXPECT_EQ(cf.GetFontName(), "Arial"); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 63 | EXPECT_EQ("arial", base::ToLowerASCII(cf.GetActualFontNameForTesting())); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 64 | } |
| 65 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 66 | #if defined(OS_ANDROID) |
| 67 | #define MAYBE_LoadArialBold DISABLED_LoadArialBold |
| 68 | #else |
| 69 | #define MAYBE_LoadArialBold LoadArialBold |
| 70 | #endif |
| 71 | TEST_F(FontTest, MAYBE_LoadArialBold) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 72 | Font cf("Arial", 16); |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 73 | Font bold(cf.Derive(0, Font::NORMAL, Font::Weight::BOLD)); |
Daniel Erat | cb2db9b | 2015-02-11 15:15:09 | [diff] [blame] | 74 | #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS) |
| 75 | EXPECT_TRUE(bold.GetNativeFont()); |
| 76 | #endif |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 77 | EXPECT_EQ(bold.GetStyle(), Font::NORMAL); |
| 78 | EXPECT_EQ(bold.GetWeight(), Font::Weight::BOLD); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 79 | EXPECT_EQ("arial", base::ToLowerASCII(cf.GetActualFontNameForTesting())); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 80 | } |
| 81 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 82 | #if defined(OS_ANDROID) |
| 83 | #define MAYBE_Ascent DISABLED_Ascent |
| 84 | #else |
| 85 | #define MAYBE_Ascent Ascent |
| 86 | #endif |
| 87 | TEST_F(FontTest, MAYBE_Ascent) { |
[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_GT(cf.GetBaseline(), 2); |
| 90 | EXPECT_LE(cf.GetBaseline(), 22); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 91 | } |
| 92 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 93 | #if defined(OS_ANDROID) |
| 94 | #define MAYBE_Height DISABLED_Height |
| 95 | #else |
| 96 | #define MAYBE_Height Height |
| 97 | #endif |
| 98 | TEST_F(FontTest, MAYBE_Height) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 99 | Font cf("Arial", 16); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 100 | EXPECT_GE(cf.GetHeight(), 16); |
[email protected] | 4eae2c44 | 2009-11-03 23:46:26 | [diff] [blame] | 101 | // TODO(akalin): Figure out why height is so large on Linux. |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 102 | EXPECT_LE(cf.GetHeight(), 26); |
| 103 | } |
| 104 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 105 | #if defined(OS_ANDROID) |
| 106 | #define MAYBE_CapHeight DISABLED_CapHeight |
| 107 | #else |
| 108 | #define MAYBE_CapHeight CapHeight |
| 109 | #endif |
| 110 | TEST_F(FontTest, MAYBE_CapHeight) { |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 111 | Font cf("Arial", 16); |
| 112 | EXPECT_GT(cf.GetCapHeight(), 0); |
| 113 | EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2); |
[email protected] | 56e5057 | 2013-11-01 14:25:31 | [diff] [blame] | 114 | EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline()); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 115 | } |
| 116 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 117 | #if defined(OS_ANDROID) |
| 118 | #define MAYBE_AvgWidths DISABLED_AvgWidths |
| 119 | #else |
| 120 | #define MAYBE_AvgWidths AvgWidths |
| 121 | #endif |
| 122 | TEST_F(FontTest, MAYBE_AvgWidths) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 123 | Font cf("Arial", 16); |
[email protected] | c831ea9c | 2013-10-01 10:52:24 | [diff] [blame] | 124 | EXPECT_EQ(cf.GetExpectedTextWidth(0), 0); |
| 125 | EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0)); |
| 126 | EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1)); |
| 127 | EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 128 | } |
| 129 | |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 130 | #if defined(OS_WIN) || defined(OS_ANDROID) |
| 131 | #define MAYBE_GetActualFontNameForTesting DISABLED_GetActualFontNameForTesting |
| 132 | #else |
| 133 | #define MAYBE_GetActualFontNameForTesting GetActualFontNameForTesting |
| 134 | #endif |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 135 | // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now. |
| 136 | // http://crbug.com/327287 |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame] | 137 | // |
| 138 | // Check that fonts used for testing are installed and enabled. On Mac |
| 139 | // fonts may be installed but still need enabling in Font Book.app. |
| 140 | // http://crbug.com/347429 |
tfarina | b7e00dc | 2015-08-05 22:49:54 | [diff] [blame] | 141 | TEST_F(FontTest, MAYBE_GetActualFontNameForTesting) { |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 142 | Font arial("Arial", 16); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 143 | EXPECT_EQ("arial", base::ToLowerASCII(arial.GetActualFontNameForTesting())) |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame] | 144 | << "********\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] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 148 | Font symbol("Symbol", 16); |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 149 | EXPECT_EQ("symbol", base::ToLowerASCII(symbol.GetActualFontNameForTesting())) |
[email protected] | 85eb3e1 | 2014-02-27 20:44:12 | [diff] [blame] | 150 | << "********\n" |
| 151 | << "Your test environment seems to be missing Symbol font, which is " |
| 152 | << "needed for unittests. Check if Symbol font is installed.\n" |
| 153 | << "********"; |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 154 | |
| 155 | const char* const invalid_font_name = "no_such_font_name"; |
| 156 | Font fallback_font(invalid_font_name, 16); |
| 157 | EXPECT_NE(invalid_font_name, |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 158 | base::ToLowerASCII(fallback_font.GetActualFontNameForTesting())); |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 159 | } |
[email protected] | 2fb2657 | 2013-12-11 08:43:06 | [diff] [blame] | 160 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 161 | #if defined(OS_WIN) |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 162 | TEST_F(FontTest, DeriveResizesIfSizeTooSmall) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 163 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 164 | // The minimum font size is set to 5 in browser_main.cc. |
| 165 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 166 | |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 167 | Font derived_font = cf.Derive(-4, cf.GetStyle(), cf.GetWeight()); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 168 | EXPECT_EQ(5, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 0143082 | 2014-02-13 15:41:45 | [diff] [blame] | 171 | TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 172 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 173 | // The minimum font size is set to 5 in browser_main.cc. |
| 174 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 175 | |
mboc | 998e890 | 2016-06-02 11:40:35 | [diff] [blame] | 176 | Font derived_font = cf.Derive(-2, cf.GetStyle(), cf.GetWeight()); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 177 | EXPECT_EQ(6, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 178 | } |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 179 | #endif // defined(OS_WIN) |
| 180 | |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 181 | } // namespace |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame] | 182 | } // namespace gfx |