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