[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] | 8252251 | 2009-05-15 07:37:29 | [diff] [blame] | 5 | #include "app/gfx/font.h" |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 6 | |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | |
| 9 | namespace { |
| 10 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 11 | using gfx::Font; |
| 12 | |
| 13 | class FontTest : public testing::Test { |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 14 | }; |
| 15 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 16 | TEST_F(FontTest, LoadArial) { |
| 17 | Font cf(Font::CreateFont(L"Arial", 16)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 18 | ASSERT_TRUE(cf.nativeFont()); |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 19 | ASSERT_EQ(cf.style(), Font::NORMAL); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 20 | ASSERT_EQ(cf.FontSize(), 16); |
| 21 | ASSERT_EQ(cf.FontName(), L"Arial"); |
| 22 | } |
| 23 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 24 | TEST_F(FontTest, LoadArialBold) { |
| 25 | Font cf(Font::CreateFont(L"Arial", 16)); |
| 26 | Font bold(cf.DeriveFont(0, Font::BOLD)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 27 | ASSERT_TRUE(bold.nativeFont()); |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 28 | ASSERT_EQ(bold.style(), Font::BOLD); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 31 | TEST_F(FontTest, Ascent) { |
| 32 | Font cf(Font::CreateFont(L"Arial", 16)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 33 | ASSERT_GT(cf.baseline(), 2); |
| 34 | ASSERT_LT(cf.baseline(), 20); |
| 35 | } |
| 36 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 37 | TEST_F(FontTest, Height) { |
| 38 | Font cf(Font::CreateFont(L"Arial", 16)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 39 | ASSERT_GT(cf.baseline(), 2); |
| 40 | ASSERT_LT(cf.baseline(), 20); |
| 41 | } |
| 42 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 43 | TEST_F(FontTest, AvgWidths) { |
| 44 | Font cf(Font::CreateFont(L"Arial", 16)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 45 | ASSERT_EQ(cf.GetExpectedTextWidth(0), 0); |
| 46 | ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0)); |
| 47 | ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1)); |
| 48 | ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2)); |
| 49 | } |
| 50 | |
[email protected] | 7322c440 | 2009-05-15 02:16:10 | [diff] [blame] | 51 | TEST_F(FontTest, Widths) { |
| 52 | Font cf(Font::CreateFont(L"Arial", 16)); |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 53 | ASSERT_EQ(cf.GetStringWidth(L""), 0); |
| 54 | ASSERT_GT(cf.GetStringWidth(L"a"), cf.GetStringWidth(L"")); |
| 55 | ASSERT_GT(cf.GetStringWidth(L"ab"), cf.GetStringWidth(L"a")); |
| 56 | ASSERT_GT(cf.GetStringWidth(L"abc"), cf.GetStringWidth(L"ab")); |
| 57 | } |
| 58 | |
[email protected] | f80821f | 2009-06-16 23:10:29 | [diff] [blame^] | 59 | #if defined(OS_WIN) |
| 60 | TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) { |
| 61 | // This creates font of height -8. |
| 62 | Font cf(Font::CreateFont(L"Arial", 6)); |
| 63 | Font derived_font = cf.DeriveFont(-4); |
| 64 | LOGFONT font_info; |
| 65 | GetObject(derived_font.hfont(), sizeof(LOGFONT), &font_info); |
| 66 | EXPECT_EQ(-5, font_info.lfHeight); |
| 67 | } |
| 68 | |
| 69 | TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) { |
| 70 | // This creates font of height -8. |
| 71 | Font cf(Font::CreateFont(L"Arial", 6)); |
| 72 | Font derived_font = cf.DeriveFont(-2); |
| 73 | LOGFONT font_info; |
| 74 | GetObject(derived_font.hfont(), sizeof(LOGFONT), &font_info); |
| 75 | EXPECT_EQ(-6, font_info.lfHeight); |
| 76 | } |
| 77 | #endif |
[email protected] | 7f4bfe1 | 2008-12-12 01:52:25 | [diff] [blame] | 78 | } // anonymous namespace |