blob: 1cf1b11a7fa6b8256df31cad6c873e88f4da9be9 [file] [log] [blame]
[email protected]7f4bfe12008-12-12 01:52:251// 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]d07b6b52010-03-23 04:05:015#include "gfx/font.h"
[email protected]7f4bfe12008-12-12 01:52:256
[email protected]5b3a7e92010-10-06 22:56:477#if defined(OS_WIN)
8#include "gfx/platform_font_win.h"
9#endif // defined(OS_WIN)
[email protected]7f4bfe12008-12-12 01:52:2510#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13
[email protected]7322c4402009-05-15 02:16:1014using gfx::Font;
15
[email protected]5e70f142009-09-14 19:26:4916class FontTest : public testing::Test {
17};
[email protected]7f4bfe12008-12-12 01:52:2518
[email protected]5b3a7e92010-10-06 22:56:4719#if defined(OS_WIN)
20class 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
43int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
44#endif // defined(OS_WIN)
45
46
[email protected]7322c4402009-05-15 02:16:1047TEST_F(FontTest, LoadArial) {
[email protected]c6ac8412010-08-13 16:43:0348 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]7f4bfe12008-12-12 01:52:2553}
54
[email protected]7322c4402009-05-15 02:16:1055TEST_F(FontTest, LoadArialBold) {
[email protected]c6ac8412010-08-13 16:43:0356 Font cf(L"Arial", 16);
[email protected]7322c4402009-05-15 02:16:1057 Font bold(cf.DeriveFont(0, Font::BOLD));
[email protected]c6ac8412010-08-13 16:43:0358 ASSERT_TRUE(bold.GetNativeFont());
59 ASSERT_EQ(bold.GetStyle(), Font::BOLD);
[email protected]7f4bfe12008-12-12 01:52:2560}
61
[email protected]7322c4402009-05-15 02:16:1062TEST_F(FontTest, Ascent) {
[email protected]c6ac8412010-08-13 16:43:0363 Font cf(L"Arial", 16);
64 ASSERT_GT(cf.GetBaseline(), 2);
65 ASSERT_LE(cf.GetBaseline(), 22);
[email protected]7f4bfe12008-12-12 01:52:2566}
67
[email protected]7322c4402009-05-15 02:16:1068TEST_F(FontTest, Height) {
[email protected]c6ac8412010-08-13 16:43:0369 Font cf(L"Arial", 16);
70 ASSERT_GE(cf.GetHeight(), 16);
[email protected]4eae2c442009-11-03 23:46:2671 // TODO(akalin): Figure out why height is so large on Linux.
[email protected]c6ac8412010-08-13 16:43:0372 ASSERT_LE(cf.GetHeight(), 26);
[email protected]7f4bfe12008-12-12 01:52:2573}
74
[email protected]7322c4402009-05-15 02:16:1075TEST_F(FontTest, AvgWidths) {
[email protected]c6ac8412010-08-13 16:43:0376 Font cf(L"Arial", 16);
[email protected]7f4bfe12008-12-12 01:52:2577 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]7322c4402009-05-15 02:16:1083TEST_F(FontTest, Widths) {
[email protected]c6ac8412010-08-13 16:43:0384 Font cf(L"Arial", 16);
[email protected]7f4bfe12008-12-12 01:52:2585 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]f80821f2009-06-16 23:10:2991#if defined(OS_WIN)
[email protected]5b3a7e92010-10-06 22:56:4792TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) {
[email protected]f80821f2009-06-16 23:10:2993 // This creates font of height -8.
[email protected]c6ac8412010-08-13 16:43:0394 Font cf(L"Arial", 6);
[email protected]5b3a7e92010-10-06 22:56:4795 // The minimum font size is set to 5 in browser_main.cc.
96 ScopedMinimumFontSizeCallback minimum_size(5);
97
[email protected]f80821f2009-06-16 23:10:2998 Font derived_font = cf.DeriveFont(-4);
99 LOGFONT font_info;
[email protected]c6ac8412010-08-13 16:43:03100 GetObject(derived_font.GetNativeFont(), sizeof(LOGFONT), &font_info);
[email protected]f80821f2009-06-16 23:10:29101 EXPECT_EQ(-5, font_info.lfHeight);
102}
103
[email protected]3bd386d2010-06-16 23:42:58104TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) {
[email protected]f80821f2009-06-16 23:10:29105 // This creates font of height -8.
[email protected]c6ac8412010-08-13 16:43:03106 Font cf(L"Arial", 6);
[email protected]5b3a7e92010-10-06 22:56:47107 // The minimum font size is set to 5 in browser_main.cc.
108 ScopedMinimumFontSizeCallback minimum_size(5);
109
[email protected]f80821f2009-06-16 23:10:29110 Font derived_font = cf.DeriveFont(-2);
111 LOGFONT font_info;
[email protected]c6ac8412010-08-13 16:43:03112 GetObject(derived_font.GetNativeFont(), sizeof(LOGFONT), &font_info);
[email protected]f80821f2009-06-16 23:10:29113 EXPECT_EQ(-6, font_info.lfHeight);
114}
115#endif
[email protected]c6ac8412010-08-13 16:43:03116} // namespace