OLD | NEW |
---|---|
(Empty) | |
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.DeriveFont(1, 0); | |
24 while (larger_font.GetHeight() <= target_height) { | |
25 expected_font = larger_font; | |
26 larger_font = larger_font.DeriveFont(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.DeriveFont(-1, 0); | |
32 } while (expected_font.GetHeight() > target_height); | |
33 } | |
34 return expected_font; | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 TEST(PlatformFontWinTest, DeriveFontWithHeight_SameHeight) { | |
40 const Font font; | |
41 PlatformFontWin* platform_font = | |
42 static_cast<PlatformFontWin*>(font.platform_font()); | |
43 | |
44 Font derived_font = | |
45 platform_font->DeriveFontWithHeight(font.GetHeight(), font.GetStyle()); | |
46 EXPECT_EQ(font.GetHeight(), derived_font.GetHeight()); | |
47 EXPECT_EQ(font.GetStyle(), derived_font.GetStyle()); | |
48 | |
49 derived_font = platform_font->DeriveFontWithHeight(font.GetHeight(), | |
50 Font::BOLD); | |
51 EXPECT_EQ(font.GetHeight(), derived_font.GetHeight()); | |
52 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | |
53 } | |
54 | |
55 TEST(PlatformFontWinTest, DeriveFontWithHeight) { | |
msw
2012/04/26 22:14:13
Awesome, and now this covers the case of DeriveFon
Alexei Svitkine (slow)
2012/04/26 22:22:29
Good point.
Alexei Svitkine (slow)
2012/04/27 15:15:27
Done.
| |
56 const Font base_font; | |
57 PlatformFontWin* platform_font = | |
58 static_cast<PlatformFontWin*>(base_font.platform_font()); | |
59 | |
60 for (int i = -10; i < 10; i++) { | |
61 const int target_height = base_font.GetHeight() + i; | |
62 Font expected_font = AdjustFontSizeForHeight(base_font, target_height); | |
63 ASSERT_LE(expected_font.GetHeight(), target_height); | |
64 | |
65 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); | |
66 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | |
67 EXPECT_LE(expected_font.GetHeight(), target_height); | |
68 EXPECT_EQ(0, derived_font.GetStyle()); | |
69 | |
70 derived_font = platform_font->DeriveFontWithHeight(target_height, | |
71 Font::BOLD); | |
72 EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); | |
73 EXPECT_LE(expected_font.GetHeight(), target_height); | |
74 EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); | |
75 } | |
76 } | |
77 | |
78 } // namespace gfx | |
OLD | NEW |