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 // Callback function used by DeriveFontWithHeight_MinSize() below.
73 static int GetMinFontSize() {
77 TEST(PlatformFontWinTest
, DeriveFontWithHeight_MinSize
) {
78 PlatformFontWin::GetMinimumFontSizeCallback old_callback
=
79 PlatformFontWin::get_minimum_font_size_callback
;
80 PlatformFontWin::get_minimum_font_size_callback
= &GetMinFontSize
;
83 const Font
min_font(base_font
.GetFontName(), GetMinFontSize());
84 PlatformFontWin
* platform_font
=
85 static_cast<PlatformFontWin
*>(base_font
.platform_font());
87 const Font derived_font
=
88 platform_font
->DeriveFontWithHeight(min_font
.GetHeight() - 1, 0);
89 EXPECT_EQ(min_font
.GetFontSize(), derived_font
.GetFontSize());
90 EXPECT_EQ(min_font
.GetHeight(), derived_font
.GetHeight());
92 PlatformFontWin::get_minimum_font_size_callback
= old_callback
;
95 TEST(PlatformFontWinTest
, DeriveFontWithHeight_TooSmall
) {
97 PlatformFontWin
* platform_font
=
98 static_cast<PlatformFontWin
*>(base_font
.platform_font());
100 const Font derived_font
= platform_font
->DeriveFontWithHeight(1, 0);
101 EXPECT_GT(derived_font
.GetHeight(), 1);