WebKit Roll 56306:56312.
[chromium-blink-merge.git] / app / gfx / font_unittest.cc
blob4e866e7a0e003d3f689032469eda46d6c60527f6
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.
5 #include "app/gfx/font.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace {
11 using gfx::Font;
13 class FontTest : public testing::Test {
16 TEST_F(FontTest, LoadArial) {
17 Font cf(Font::CreateFont(L"Arial", 16));
18 ASSERT_TRUE(cf.nativeFont());
19 ASSERT_EQ(cf.style(), Font::NORMAL);
20 ASSERT_EQ(cf.FontSize(), 16);
21 ASSERT_EQ(cf.FontName(), L"Arial");
24 TEST_F(FontTest, LoadArialBold) {
25 Font cf(Font::CreateFont(L"Arial", 16));
26 Font bold(cf.DeriveFont(0, Font::BOLD));
27 ASSERT_TRUE(bold.nativeFont());
28 ASSERT_EQ(bold.style(), Font::BOLD);
31 TEST_F(FontTest, Ascent) {
32 Font cf(Font::CreateFont(L"Arial", 16));
33 ASSERT_GT(cf.baseline(), 2);
34 ASSERT_LE(cf.baseline(), 22);
37 TEST_F(FontTest, Height) {
38 Font cf(Font::CreateFont(L"Arial", 16));
39 ASSERT_GE(cf.height(), 16);
40 // TODO(akalin): Figure out why height is so large on Linux.
41 ASSERT_LE(cf.height(), 26);
44 TEST_F(FontTest, AvgWidths) {
45 Font cf(Font::CreateFont(L"Arial", 16));
46 ASSERT_EQ(cf.GetExpectedTextWidth(0), 0);
47 ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
48 ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
49 ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
52 TEST_F(FontTest, Widths) {
53 Font cf(Font::CreateFont(L"Arial", 16));
54 ASSERT_EQ(cf.GetStringWidth(L""), 0);
55 ASSERT_GT(cf.GetStringWidth(L"a"), cf.GetStringWidth(L""));
56 ASSERT_GT(cf.GetStringWidth(L"ab"), cf.GetStringWidth(L"a"));
57 ASSERT_GT(cf.GetStringWidth(L"abc"), cf.GetStringWidth(L"ab"));
60 #if defined(OS_WIN)
61 TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) {
62 // This creates font of height -8.
63 Font cf(Font::CreateFont(L"Arial", 6));
64 Font derived_font = cf.DeriveFont(-4);
65 LOGFONT font_info;
66 GetObject(derived_font.hfont(), sizeof(LOGFONT), &font_info);
67 EXPECT_EQ(-5, font_info.lfHeight);
70 TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) {
71 // This creates font of height -8.
72 Font cf(Font::CreateFont(L"Arial", 6));
73 Font derived_font = cf.DeriveFont(-2);
74 LOGFONT font_info;
75 GetObject(derived_font.hfont(), sizeof(LOGFONT), &font_info);
76 EXPECT_EQ(-6, font_info.lfHeight);
78 #endif
79 } // anonymous namespace