[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] | 7a8edaf | 2011-11-28 20:58:48 | [diff] [blame] | 7 | #include "base/string16.h" |
[email protected] | 13658c4 | 2011-01-04 20:46:14 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
[email protected] | 3469476 | 2012-01-31 05:15:11 | [diff] [blame] | 11 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 12 | #include <pango/pango.h> |
| 13 | #elif defined(OS_WIN) |
| 14 | #include "ui/gfx/platform_font_win.h" |
| 15 | #endif |
| 16 | |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 17 | namespace gfx { |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 18 | namespace { |
| 19 | |
[email protected] | 5e70f14 | 2009-09-14 19:26:49 | [diff] [blame] | 20 | class FontTest : public testing::Test { |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 21 | public: |
| 22 | // Fulfills the memory management contract as outlined by the comment at |
| 23 | // gfx::Font::GetNativeFont(). |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 24 | void FreeIfNecessary(NativeFont font) { |
[email protected] | 3469476 | 2012-01-31 05:15:11 | [diff] [blame] | 25 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 26 | pango_font_description_free(font); |
| 27 | #endif |
| 28 | } |
[email protected] | 5e70f14 | 2009-09-14 19:26:49 | [diff] [blame] | 29 | }; |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 30 | |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 31 | #if defined(OS_WIN) |
| 32 | class ScopedMinimumFontSizeCallback { |
| 33 | public: |
| 34 | explicit ScopedMinimumFontSizeCallback(int minimum_size) { |
| 35 | minimum_size_ = minimum_size; |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 36 | old_callback_ = PlatformFontWin::get_minimum_font_size_callback; |
| 37 | PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | ~ScopedMinimumFontSizeCallback() { |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 41 | PlatformFontWin::get_minimum_font_size_callback = old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
| 45 | static int GetMinimumFontSize() { |
| 46 | return minimum_size_; |
| 47 | } |
| 48 | |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 49 | PlatformFontWin::GetMinimumFontSizeCallback old_callback_; |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 50 | static int minimum_size_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback); |
| 53 | }; |
| 54 | |
| 55 | int ScopedMinimumFontSizeCallback::minimum_size_ = 0; |
| 56 | #endif // defined(OS_WIN) |
| 57 | |
| 58 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 59 | TEST_F(FontTest, LoadArial) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 60 | Font cf("Arial", 16); |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 61 | NativeFont native = cf.GetNativeFont(); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 62 | ASSERT_TRUE(native); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 63 | ASSERT_EQ(cf.GetStyle(), Font::NORMAL); |
| 64 | ASSERT_EQ(cf.GetFontSize(), 16); |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 65 | ASSERT_EQ(cf.GetFontName(), "Arial"); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 66 | FreeIfNecessary(native); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 69 | TEST_F(FontTest, LoadArialBold) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 70 | Font cf("Arial", 16); |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 71 | Font bold(cf.DeriveFont(0, Font::BOLD)); |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 72 | NativeFont native = bold.GetNativeFont(); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 73 | ASSERT_TRUE(native); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 74 | ASSERT_EQ(bold.GetStyle(), Font::BOLD); |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 75 | FreeIfNecessary(native); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 78 | TEST_F(FontTest, Ascent) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 79 | Font cf("Arial", 16); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 80 | ASSERT_GT(cf.GetBaseline(), 2); |
| 81 | ASSERT_LE(cf.GetBaseline(), 22); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 84 | TEST_F(FontTest, Height) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 85 | Font cf("Arial", 16); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 86 | ASSERT_GE(cf.GetHeight(), 16); |
[email protected] | 4eae2c44 | 2009-11-03 23:46:26 | [diff] [blame] | 87 | // TODO(akalin): Figure out why height is so large on Linux. |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 88 | ASSERT_LE(cf.GetHeight(), 26); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 91 | TEST_F(FontTest, AvgWidths) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 92 | Font cf("Arial", 16); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 93 | ASSERT_EQ(cf.GetExpectedTextWidth(0), 0); |
| 94 | ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0)); |
| 95 | ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1)); |
| 96 | ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2)); |
| 97 | } |
| 98 | |
[email protected] | b8fa907861 | 2011-10-17 00:23:39 | [diff] [blame] | 99 | TEST_F(FontTest, AvgCharWidth) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 100 | Font cf("Arial", 16); |
[email protected] | b8fa907861 | 2011-10-17 00:23:39 | [diff] [blame] | 101 | ASSERT_GT(cf.GetAverageCharacterWidth(), 0); |
| 102 | } |
| 103 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 104 | TEST_F(FontTest, Widths) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 105 | Font cf("Arial", 16); |
[email protected] | 7a8edaf | 2011-11-28 20:58:48 | [diff] [blame] | 106 | ASSERT_EQ(cf.GetStringWidth(string16()), 0); |
[email protected] | 13658c4 | 2011-01-04 20:46:14 | [diff] [blame] | 107 | ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("a")), |
[email protected] | 7a8edaf | 2011-11-28 20:58:48 | [diff] [blame] | 108 | cf.GetStringWidth(string16())); |
[email protected] | 13658c4 | 2011-01-04 20:46:14 | [diff] [blame] | 109 | ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("ab")), |
| 110 | cf.GetStringWidth(ASCIIToUTF16("a"))); |
| 111 | ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("abc")), |
| 112 | cf.GetStringWidth(ASCIIToUTF16("ab"))); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 115 | #if defined(OS_WIN) |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 116 | TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 117 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 118 | // The minimum font size is set to 5 in browser_main.cc. |
| 119 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 120 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 121 | Font derived_font = cf.DeriveFont(-4); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 122 | EXPECT_EQ(5, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 123 | } |
| 124 | |
[email protected] | 3bd386d | 2010-06-16 23:42:58 | [diff] [blame] | 125 | TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) { |
[email protected] | 610ae5f | 2011-10-27 23:55:37 | [diff] [blame] | 126 | Font cf("Arial", 8); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame] | 127 | // The minimum font size is set to 5 in browser_main.cc. |
| 128 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 129 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 130 | Font derived_font = cf.DeriveFont(-2); |
[email protected] | c8703bb | 2011-10-19 14:35:15 | [diff] [blame] | 131 | EXPECT_EQ(6, derived_font.GetFontSize()); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 132 | } |
[email protected] | 23fc115 | 2011-02-17 14:55:26 | [diff] [blame] | 133 | #endif // defined(OS_WIN) |
| 134 | |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 135 | } // namespace |
[email protected] | 6a1cac1c | 2012-10-29 19:58:02 | [diff] [blame^] | 136 | } // namespace gfx |