blob: 8ca183ede128c275cb3c52464ca6595cd5430873 [file] [log] [blame]
[email protected]34694762012-01-31 05:15:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]7f4bfe12008-12-12 01:52:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]08397d52011-02-05 01:53:385#include "ui/gfx/font.h"
[email protected]7f4bfe12008-12-12 01:52:256
[email protected]7a8edaf2011-11-28 20:58:487#include "base/string16.h"
[email protected]13658c42011-01-04 20:46:148#include "base/utf_string_conversions.h"
[email protected]7f4bfe12008-12-12 01:52:259#include "testing/gtest/include/gtest/gtest.h"
10
[email protected]34694762012-01-31 05:15:1111#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]23fc1152011-02-17 14:55:2612#include <pango/pango.h>
13#elif defined(OS_WIN)
14#include "ui/gfx/platform_font_win.h"
15#endif
16
[email protected]6a1cac1c2012-10-29 19:58:0217namespace gfx {
[email protected]7f4bfe12008-12-12 01:52:2518namespace {
19
[email protected]5e70f142009-09-14 19:26:4920class FontTest : public testing::Test {
[email protected]23fc1152011-02-17 14:55:2621 public:
22 // Fulfills the memory management contract as outlined by the comment at
23 // gfx::Font::GetNativeFont().
[email protected]6a1cac1c2012-10-29 19:58:0224 void FreeIfNecessary(NativeFont font) {
[email protected]34694762012-01-31 05:15:1125#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
[email protected]23fc1152011-02-17 14:55:2626 pango_font_description_free(font);
27#endif
28 }
[email protected]5e70f142009-09-14 19:26:4929};
[email protected]7f4bfe12008-12-12 01:52:2530
[email protected]5b3a7e92010-10-06 22:56:4731#if defined(OS_WIN)
32class ScopedMinimumFontSizeCallback {
33 public:
34 explicit ScopedMinimumFontSizeCallback(int minimum_size) {
35 minimum_size_ = minimum_size;
[email protected]6a1cac1c2012-10-29 19:58:0236 old_callback_ = PlatformFontWin::get_minimum_font_size_callback;
37 PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
[email protected]5b3a7e92010-10-06 22:56:4738 }
39
40 ~ScopedMinimumFontSizeCallback() {
[email protected]6a1cac1c2012-10-29 19:58:0241 PlatformFontWin::get_minimum_font_size_callback = old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4742 }
43
44 private:
45 static int GetMinimumFontSize() {
46 return minimum_size_;
47 }
48
[email protected]6a1cac1c2012-10-29 19:58:0249 PlatformFontWin::GetMinimumFontSizeCallback old_callback_;
[email protected]5b3a7e92010-10-06 22:56:4750 static int minimum_size_;
51
52 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback);
53};
54
55int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
56#endif // defined(OS_WIN)
57
58
[email protected]7322c4402009-05-15 02:16:1059TEST_F(FontTest, LoadArial) {
[email protected]610ae5f2011-10-27 23:55:3760 Font cf("Arial", 16);
[email protected]6a1cac1c2012-10-29 19:58:0261 NativeFont native = cf.GetNativeFont();
[email protected]23fc1152011-02-17 14:55:2662 ASSERT_TRUE(native);
[email protected]c6ac8412010-08-13 16:43:0363 ASSERT_EQ(cf.GetStyle(), Font::NORMAL);
64 ASSERT_EQ(cf.GetFontSize(), 16);
[email protected]610ae5f2011-10-27 23:55:3765 ASSERT_EQ(cf.GetFontName(), "Arial");
[email protected]23fc1152011-02-17 14:55:2666 FreeIfNecessary(native);
[email protected]7f4bfe12008-12-12 01:52:2567}
68
[email protected]7322c4402009-05-15 02:16:1069TEST_F(FontTest, LoadArialBold) {
[email protected]610ae5f2011-10-27 23:55:3770 Font cf("Arial", 16);
[email protected]7322c4402009-05-15 02:16:1071 Font bold(cf.DeriveFont(0, Font::BOLD));
[email protected]6a1cac1c2012-10-29 19:58:0272 NativeFont native = bold.GetNativeFont();
[email protected]23fc1152011-02-17 14:55:2673 ASSERT_TRUE(native);
[email protected]c6ac8412010-08-13 16:43:0374 ASSERT_EQ(bold.GetStyle(), Font::BOLD);
[email protected]23fc1152011-02-17 14:55:2675 FreeIfNecessary(native);
[email protected]7f4bfe12008-12-12 01:52:2576}
77
[email protected]7322c4402009-05-15 02:16:1078TEST_F(FontTest, Ascent) {
[email protected]610ae5f2011-10-27 23:55:3779 Font cf("Arial", 16);
[email protected]c6ac8412010-08-13 16:43:0380 ASSERT_GT(cf.GetBaseline(), 2);
81 ASSERT_LE(cf.GetBaseline(), 22);
[email protected]7f4bfe12008-12-12 01:52:2582}
83
[email protected]7322c4402009-05-15 02:16:1084TEST_F(FontTest, Height) {
[email protected]610ae5f2011-10-27 23:55:3785 Font cf("Arial", 16);
[email protected]c6ac8412010-08-13 16:43:0386 ASSERT_GE(cf.GetHeight(), 16);
[email protected]4eae2c442009-11-03 23:46:2687 // TODO(akalin): Figure out why height is so large on Linux.
[email protected]c6ac8412010-08-13 16:43:0388 ASSERT_LE(cf.GetHeight(), 26);
[email protected]7f4bfe12008-12-12 01:52:2589}
90
[email protected]7322c4402009-05-15 02:16:1091TEST_F(FontTest, AvgWidths) {
[email protected]610ae5f2011-10-27 23:55:3792 Font cf("Arial", 16);
[email protected]7f4bfe12008-12-12 01:52:2593 ASSERT_EQ(cf.GetExpectedTextWidth(0), 0);
94 ASSERT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
95 ASSERT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
96 ASSERT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
97}
98
[email protected]b8fa9078612011-10-17 00:23:3999TEST_F(FontTest, AvgCharWidth) {
[email protected]610ae5f2011-10-27 23:55:37100 Font cf("Arial", 16);
[email protected]b8fa9078612011-10-17 00:23:39101 ASSERT_GT(cf.GetAverageCharacterWidth(), 0);
102}
103
[email protected]7322c4402009-05-15 02:16:10104TEST_F(FontTest, Widths) {
[email protected]610ae5f2011-10-27 23:55:37105 Font cf("Arial", 16);
[email protected]7a8edaf2011-11-28 20:58:48106 ASSERT_EQ(cf.GetStringWidth(string16()), 0);
[email protected]13658c42011-01-04 20:46:14107 ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("a")),
[email protected]7a8edaf2011-11-28 20:58:48108 cf.GetStringWidth(string16()));
[email protected]13658c42011-01-04 20:46:14109 ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("ab")),
110 cf.GetStringWidth(ASCIIToUTF16("a")));
111 ASSERT_GT(cf.GetStringWidth(ASCIIToUTF16("abc")),
112 cf.GetStringWidth(ASCIIToUTF16("ab")));
[email protected]7f4bfe12008-12-12 01:52:25113}
114
[email protected]f80821f2009-06-16 23:10:29115#if defined(OS_WIN)
[email protected]5b3a7e92010-10-06 22:56:47116TEST_F(FontTest, DeriveFontResizesIfSizeTooSmall) {
[email protected]610ae5f2011-10-27 23:55:37117 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47118 // The minimum font size is set to 5 in browser_main.cc.
119 ScopedMinimumFontSizeCallback minimum_size(5);
120
[email protected]f80821f2009-06-16 23:10:29121 Font derived_font = cf.DeriveFont(-4);
[email protected]c8703bb2011-10-19 14:35:15122 EXPECT_EQ(5, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29123}
124
[email protected]3bd386d2010-06-16 23:42:58125TEST_F(FontTest, DeriveFontKeepsOriginalSizeIfHeightOk) {
[email protected]610ae5f2011-10-27 23:55:37126 Font cf("Arial", 8);
[email protected]5b3a7e92010-10-06 22:56:47127 // The minimum font size is set to 5 in browser_main.cc.
128 ScopedMinimumFontSizeCallback minimum_size(5);
129
[email protected]f80821f2009-06-16 23:10:29130 Font derived_font = cf.DeriveFont(-2);
[email protected]c8703bb2011-10-19 14:35:15131 EXPECT_EQ(6, derived_font.GetFontSize());
[email protected]f80821f2009-06-16 23:10:29132}
[email protected]23fc1152011-02-17 14:55:26133#endif // defined(OS_WIN)
134
[email protected]c6ac8412010-08-13 16:43:03135} // namespace
[email protected]6a1cac1c2012-10-29 19:58:02136} // namespace gfx