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/platform_font_win.h"
7 #include "base/logging.h"
8 #include "base/memory/ref_counted.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/font.h"
16 // Returns a font based on |base_font| with height at most |target_height| and
17 // font size maximized. Returns |base_font| if height is already equal.
18 gfx::Font
AdjustFontSizeForHeight(const gfx::Font
& base_font
,
20 Font expected_font
= base_font
;
21 if (base_font
.GetHeight() < target_height
) {
22 // Increase size while height is <= |target_height|.
23 Font larger_font
= base_font
.Derive(1, 0);
24 while (larger_font
.GetHeight() <= target_height
) {
25 expected_font
= larger_font
;
26 larger_font
= larger_font
.Derive(1, 0);
28 } else if (expected_font
.GetHeight() > target_height
) {
29 // Decrease size until height is <= |target_height|.
31 expected_font
= expected_font
.Derive(-1, 0);
32 } while (expected_font
.GetHeight() > target_height
);
39 TEST(PlatformFontWinTest
, DeriveFontWithHeight
) {
41 PlatformFontWin
* platform_font
=
42 static_cast<PlatformFontWin
*>(base_font
.platform_font());
44 for (int i
= -10; i
< 10; i
++) {
45 const int target_height
= base_font
.GetHeight() + i
;
46 Font expected_font
= AdjustFontSizeForHeight(base_font
, target_height
);
47 ASSERT_LE(expected_font
.GetHeight(), target_height
);
49 Font derived_font
= platform_font
->DeriveFontWithHeight(target_height
, 0);
50 EXPECT_EQ(expected_font
.GetFontName(), derived_font
.GetFontName());
51 EXPECT_EQ(expected_font
.GetFontSize(), derived_font
.GetFontSize());
52 EXPECT_LE(expected_font
.GetHeight(), target_height
);
53 EXPECT_EQ(0, derived_font
.GetStyle());
55 derived_font
= platform_font
->DeriveFontWithHeight(target_height
,
57 EXPECT_EQ(expected_font
.GetFontName(), derived_font
.GetFontName());
58 EXPECT_EQ(expected_font
.GetFontSize(), derived_font
.GetFontSize());
59 EXPECT_LE(expected_font
.GetHeight(), target_height
);
60 EXPECT_EQ(Font::BOLD
, derived_font
.GetStyle());
62 // Test that deriving from the new font has the expected result.
63 Font rederived_font
= derived_font
.Derive(1, 0);
64 expected_font
= Font(derived_font
.GetFontName(),
65 derived_font
.GetFontSize() + 1);
66 EXPECT_EQ(expected_font
.GetFontName(), rederived_font
.GetFontName());
67 EXPECT_EQ(expected_font
.GetFontSize(), rederived_font
.GetFontSize());
68 EXPECT_EQ(expected_font
.GetHeight(), rederived_font
.GetHeight());
72 TEST(PlatformFontWinTest
, DeriveFontWithHeight_Consistency
) {
73 gfx::Font
arial_12("Arial", 12);
74 ASSERT_GT(16, arial_12
.GetHeight());
75 gfx::Font derived_1
= static_cast<PlatformFontWin
*>(
76 arial_12
.platform_font())->DeriveFontWithHeight(16, 0);
78 gfx::Font
arial_15("Arial", 15);
79 ASSERT_LT(16, arial_15
.GetHeight());
80 gfx::Font derived_2
= static_cast<PlatformFontWin
*>(
81 arial_15
.platform_font())->DeriveFontWithHeight(16, 0);
83 EXPECT_EQ(derived_1
.GetFontSize(), derived_2
.GetFontSize());
84 EXPECT_EQ(16, derived_1
.GetHeight());
85 EXPECT_EQ(16, derived_2
.GetHeight());
88 // Callback function used by DeriveFontWithHeight_MinSize() below.
89 static int GetMinFontSize() {
93 TEST(PlatformFontWinTest
, DeriveFontWithHeight_MinSize
) {
94 PlatformFontWin::GetMinimumFontSizeCallback old_callback
=
95 PlatformFontWin::get_minimum_font_size_callback
;
96 PlatformFontWin::get_minimum_font_size_callback
= &GetMinFontSize
;
99 const Font
min_font(base_font
.GetFontName(), GetMinFontSize());
100 PlatformFontWin
* platform_font
=
101 static_cast<PlatformFontWin
*>(base_font
.platform_font());
103 const Font derived_font
=
104 platform_font
->DeriveFontWithHeight(min_font
.GetHeight() - 1, 0);
105 EXPECT_EQ(min_font
.GetFontSize(), derived_font
.GetFontSize());
106 EXPECT_EQ(min_font
.GetHeight(), derived_font
.GetHeight());
108 PlatformFontWin::get_minimum_font_size_callback
= old_callback
;
111 TEST(PlatformFontWinTest
, DeriveFontWithHeight_TooSmall
) {
112 const Font base_font
;
113 PlatformFontWin
* platform_font
=
114 static_cast<PlatformFontWin
*>(base_font
.platform_font());
116 const Font derived_font
= platform_font
->DeriveFontWithHeight(1, 0);
117 EXPECT_GT(derived_font
.GetHeight(), 1);