[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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] | d07b6b5 | 2010-03-23 04:05:01 | [diff] [blame] | 5 | #include "gfx/font.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 6 | |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame^] | 7 | #if defined(OS_WIN) |
| 8 | #include "gfx/platform_font_win.h" |
| 9 | #endif // defined(OS_WIN) |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 14 | using gfx::Font; |
| 15 | |
[email protected] | 5e70f14 | 2009-09-14 19:26:49 | [diff] [blame] | 16 | class FontTest : public testing::Test { |
| 17 | }; |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 18 | |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame^] | 19 | #if defined(OS_WIN) |
| 20 | class ScopedMinimumFontSizeCallback { |
| 21 | public: |
| 22 | explicit ScopedMinimumFontSizeCallback(int minimum_size) { |
| 23 | minimum_size_ = minimum_size; |
| 24 | old_callback_ = gfx::PlatformFontWin::get_minimum_font_size_callback; |
| 25 | gfx::PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize; |
| 26 | } |
| 27 | |
| 28 | ~ScopedMinimumFontSizeCallback() { |
| 29 | gfx::PlatformFontWin::get_minimum_font_size_callback = old_callback_; |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | static int GetMinimumFontSize() { |
| 34 | return minimum_size_; |
| 35 | } |
| 36 | |
| 37 | gfx::PlatformFontWin::GetMinimumFontSizeCallback old_callback_; |
| 38 | static int minimum_size_; |
| 39 | |
| 40 | DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback); |
| 41 | }; |
| 42 | |
| 43 | int ScopedMinimumFontSizeCallback::minimum_size_ = 0; |
| 44 | #endif // defined(OS_WIN) |
| 45 | |
| 46 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 47 | TEST_F(FontTest, LoadArial) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 48 | Font cf(L"Arial", 16); |
| 49 | ASSERT_TRUE(cf.GetNativeFont()); |
| 50 | ASSERT_EQ(cf.GetStyle(), Font::NORMAL); |
| 51 | ASSERT_EQ(cf.GetFontSize(), 16); |
| 52 | ASSERT_EQ(cf.GetFontName(), L"Arial"); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 55 | TEST_F(FontTest, LoadArialBold) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 56 | Font cf(L"Arial", 16); |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 57 | Font bold(cf.DeriveFont(0, Font::BOLD)); |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 58 | ASSERT_TRUE(bold.GetNativeFont()); |
| 59 | ASSERT_EQ(bold.GetStyle(), Font::BOLD); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 62 | TEST_F(FontTest, Ascent) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 63 | Font cf(L"Arial", 16); |
| 64 | ASSERT_GT(cf.GetBaseline(), 2); |
| 65 | ASSERT_LE(cf.GetBaseline(), 22); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 68 | TEST_F(FontTest, Height) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 69 | Font cf(L"Arial", 16); |
| 70 | ASSERT_GE(cf.GetHeight(), 16); |
[email protected] | 4eae2c44 | 2009-11-03 23:46:26 | [diff] [blame] | 71 | // TODO(akalin): Figure out why height is so large on Linux. |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 72 | ASSERT_LE(cf.GetHeight(), 26); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 75 | TEST_F(FontTest, AvgWidths) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 76 | Font cf(L"Arial", 16); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 77 | ASSERT_EQ(cf.GetExpectedTextWidth(0), 0); |
| 78 | ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0)); |
| 79 | ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1)); |
| 80 | ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2)); |
| 81 | } |
| 82 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 83 | TEST_F(FontTest, Widths) { |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 84 | Font cf(L"Arial", 16); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 85 | ASSERT_EQ(cf.GetStringWidth(L""), 0); |
| 86 | ASSERT_GT(cf.GetStringWidth(L"a"), cf.GetStringWidth(L"")); |
| 87 | ASSERT_GT(cf.GetStringWidth(L"ab"), cf.GetStringWidth(L"a")); |
| 88 | ASSERT_GT(cf.GetStringWidth(L"abc"), cf.GetStringWidth(L"ab")); |
| 89 | } |
| 90 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 91 | #if defined(OS_WIN) |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame^] | 92 | TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) { |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 93 | // This creates font of height -8. |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 94 | Font cf(L"Arial", 6); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame^] | 95 | // The minimum font size is set to 5 in browser_main.cc. |
| 96 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 97 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 98 | Font derived_font = cf.DeriveFont(-4); |
| 99 | LOGFONT font_info; |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 100 | GetObject(derived_font.GetNativeFont(), sizeof(LOGFONT), &font_info); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 101 | EXPECT_EQ(-5, font_info.lfHeight); |
| 102 | } |
| 103 | |
[email protected] | 3bd386d | 2010-06-16 23:42:58 | [diff] [blame] | 104 | TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) { |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 105 | // This creates font of height -8. |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 106 | Font cf(L"Arial", 6); |
[email protected] | 5b3a7e9 | 2010-10-06 22:56:47 | [diff] [blame^] | 107 | // The minimum font size is set to 5 in browser_main.cc. |
| 108 | ScopedMinimumFontSizeCallback minimum_size(5); |
| 109 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 110 | Font derived_font = cf.DeriveFont(-2); |
| 111 | LOGFONT font_info; |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 112 | GetObject(derived_font.GetNativeFont(), sizeof(LOGFONT), &font_info); |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame] | 113 | EXPECT_EQ(-6, font_info.lfHeight); |
| 114 | } |
| 115 | #endif |
[email protected] | c6ac841 | 2010-08-13 16:43:03 | [diff] [blame] | 116 | } // namespace |