Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / platform_font_win_unittest.cc
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.
4
5 #include "ui/gfx/platform_font_win.h"
6
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"
11
12 namespace gfx {
13
14 namespace {
15
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,
19                                   int target_height) {
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);
27     }
28   } else if (expected_font.GetHeight() > target_height) {
29     // Decrease size until height is <= |target_height|.
30     do {
31       expected_font = expected_font.Derive(-1, 0);
32     } while (expected_font.GetHeight() > target_height);
33   }
34   return expected_font;
35 }
36
37 }  // namespace
38
39 TEST(PlatformFontWinTest, DeriveFontWithHeight) {
40   const Font base_font;
41   PlatformFontWin* platform_font =
42       static_cast<PlatformFontWin*>(base_font.platform_font());
43
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);
48
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());
54
55     derived_font = platform_font->DeriveFontWithHeight(target_height,
56                                                        Font::BOLD);
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());
61
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());
69   }
70 }
71
72 // Callback function used by DeriveFontWithHeight_MinSize() below.
73 static int GetMinFontSize() {
74   return 10;
75 }
76
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;
81
82   const Font base_font;
83   const Font min_font(base_font.GetFontName(), GetMinFontSize());
84   PlatformFontWin* platform_font =
85       static_cast<PlatformFontWin*>(base_font.platform_font());
86
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());
91
92   PlatformFontWin::get_minimum_font_size_callback = old_callback;
93 }
94
95 TEST(PlatformFontWinTest, DeriveFontWithHeight_TooSmall) {
96   const Font base_font;
97   PlatformFontWin* platform_font =
98       static_cast<PlatformFontWin*>(base_font.platform_font());
99
100   const Font derived_font = platform_font->DeriveFontWithHeight(1, 0);
101   EXPECT_GT(derived_font.GetHeight(), 1);
102 }
103
104 }  // namespace gfx