Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / font_unittest.cc
blob84908071d895b8c206c588f8c3a6fa9e8c60e0ff
1 // Copyright (c) 2012 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 "ui/gfx/font.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_WIN)
13 #include "ui/gfx/platform_font_win.h"
14 #endif
16 namespace gfx {
17 namespace {
19 using FontTest = testing::Test;
21 #if defined(OS_WIN)
22 class ScopedMinimumFontSizeCallback {
23 public:
24 explicit ScopedMinimumFontSizeCallback(int minimum_size) {
25 minimum_size_ = minimum_size;
26 old_callback_ = PlatformFontWin::get_minimum_font_size_callback;
27 PlatformFontWin::get_minimum_font_size_callback = &GetMinimumFontSize;
30 ~ScopedMinimumFontSizeCallback() {
31 PlatformFontWin::get_minimum_font_size_callback = old_callback_;
34 private:
35 static int GetMinimumFontSize() {
36 return minimum_size_;
39 PlatformFontWin::GetMinimumFontSizeCallback old_callback_;
40 static int minimum_size_;
42 DISALLOW_COPY_AND_ASSIGN(ScopedMinimumFontSizeCallback);
45 int ScopedMinimumFontSizeCallback::minimum_size_ = 0;
46 #endif // defined(OS_WIN)
49 TEST_F(FontTest, LoadArial) {
50 Font cf("Arial", 16);
51 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
52 EXPECT_TRUE(cf.GetNativeFont());
53 #endif
54 EXPECT_EQ(cf.GetStyle(), Font::NORMAL);
55 EXPECT_EQ(cf.GetFontSize(), 16);
56 EXPECT_EQ(cf.GetFontName(), "Arial");
57 EXPECT_EQ("arial",
58 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
61 TEST_F(FontTest, LoadArialBold) {
62 Font cf("Arial", 16);
63 Font bold(cf.Derive(0, Font::BOLD));
64 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_IOS)
65 EXPECT_TRUE(bold.GetNativeFont());
66 #endif
67 EXPECT_EQ(bold.GetStyle(), Font::BOLD);
68 EXPECT_EQ("arial",
69 base::StringToLowerASCII(cf.GetActualFontNameForTesting()));
72 TEST_F(FontTest, Ascent) {
73 Font cf("Arial", 16);
74 EXPECT_GT(cf.GetBaseline(), 2);
75 EXPECT_LE(cf.GetBaseline(), 22);
78 TEST_F(FontTest, Height) {
79 Font cf("Arial", 16);
80 EXPECT_GE(cf.GetHeight(), 16);
81 // TODO(akalin): Figure out why height is so large on Linux.
82 EXPECT_LE(cf.GetHeight(), 26);
85 TEST_F(FontTest, CapHeight) {
86 Font cf("Arial", 16);
87 EXPECT_GT(cf.GetCapHeight(), 0);
88 EXPECT_GT(cf.GetCapHeight(), cf.GetHeight() / 2);
89 EXPECT_LT(cf.GetCapHeight(), cf.GetBaseline());
92 TEST_F(FontTest, AvgWidths) {
93 Font cf("Arial", 16);
94 EXPECT_EQ(cf.GetExpectedTextWidth(0), 0);
95 EXPECT_GT(cf.GetExpectedTextWidth(1), cf.GetExpectedTextWidth(0));
96 EXPECT_GT(cf.GetExpectedTextWidth(2), cf.GetExpectedTextWidth(1));
97 EXPECT_GT(cf.GetExpectedTextWidth(3), cf.GetExpectedTextWidth(2));
100 #if !defined(OS_WIN)
101 // On Windows, Font::GetActualFontNameForTesting() doesn't work well for now.
102 // http://crbug.com/327287
104 // Check that fonts used for testing are installed and enabled. On Mac
105 // fonts may be installed but still need enabling in Font Book.app.
106 // http://crbug.com/347429
107 TEST_F(FontTest, GetActualFontNameForTesting) {
108 Font arial("Arial", 16);
109 EXPECT_EQ("arial",
110 base::StringToLowerASCII(arial.GetActualFontNameForTesting()))
111 << "********\n"
112 << "Your test environment seems to be missing Arial font, which is "
113 << "needed for unittests. Check if Arial font is installed.\n"
114 << "********";
115 Font symbol("Symbol", 16);
116 EXPECT_EQ("symbol",
117 base::StringToLowerASCII(symbol.GetActualFontNameForTesting()))
118 << "********\n"
119 << "Your test environment seems to be missing Symbol font, which is "
120 << "needed for unittests. Check if Symbol font is installed.\n"
121 << "********";
123 const char* const invalid_font_name = "no_such_font_name";
124 Font fallback_font(invalid_font_name, 16);
125 EXPECT_NE(invalid_font_name,
126 base::StringToLowerASCII(
127 fallback_font.GetActualFontNameForTesting()));
129 #endif
131 #if defined(OS_WIN)
132 TEST_F(FontTest, DeriveResizesIfSizeTooSmall) {
133 Font cf("Arial", 8);
134 // The minimum font size is set to 5 in browser_main.cc.
135 ScopedMinimumFontSizeCallback minimum_size(5);
137 Font derived_font = cf.Derive(-4, cf.GetStyle());
138 EXPECT_EQ(5, derived_font.GetFontSize());
141 TEST_F(FontTest, DeriveKeepsOriginalSizeIfHeightOk) {
142 Font cf("Arial", 8);
143 // The minimum font size is set to 5 in browser_main.cc.
144 ScopedMinimumFontSizeCallback minimum_size(5);
146 Font derived_font = cf.Derive(-2, cf.GetStyle());
147 EXPECT_EQ(6, derived_font.GetFontSize());
149 #endif // defined(OS_WIN)
151 } // namespace
152 } // namespace gfx