Index: ui/gfx/platform_font_win_unittest.cc |
=================================================================== |
--- ui/gfx/platform_font_win_unittest.cc (revision 0) |
+++ ui/gfx/platform_font_win_unittest.cc (revision 0) |
@@ -0,0 +1,78 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/platform_font_win.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/ref_counted.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/font.h" |
+ |
+namespace gfx { |
+ |
+namespace { |
+ |
+// Returns a font based on |base_font| with height at most |target_height| and |
+// font size maximized. Returns |base_font| if height is already equal. |
+gfx::Font AdjustFontSizeForHeight(const gfx::Font& base_font, |
+ int target_height) { |
+ Font expected_font = base_font; |
+ if (base_font.GetHeight() < target_height) { |
+ // Increase size while height is <= |target_height|. |
+ Font larger_font = base_font.DeriveFont(1, 0); |
+ while (larger_font.GetHeight() <= target_height) { |
+ expected_font = larger_font; |
+ larger_font = larger_font.DeriveFont(1, 0); |
+ } |
+ } else if (expected_font.GetHeight() > target_height) { |
+ // Decrease size until height is <= |target_height|. |
+ do { |
+ expected_font = expected_font.DeriveFont(-1, 0); |
+ } while (expected_font.GetHeight() > target_height); |
+ } |
+ return expected_font; |
+} |
+ |
+} // namespace |
+ |
+TEST(PlatformFontWinTest, DeriveFontWithHeight_SameHeight) { |
+ const Font font; |
+ PlatformFontWin* platform_font = |
+ static_cast<PlatformFontWin*>(font.platform_font()); |
+ |
+ Font derived_font = |
+ platform_font->DeriveFontWithHeight(font.GetHeight(), font.GetStyle()); |
+ EXPECT_EQ(font.GetHeight(), derived_font.GetHeight()); |
+ EXPECT_EQ(font.GetStyle(), derived_font.GetStyle()); |
+ |
+ derived_font = platform_font->DeriveFontWithHeight(font.GetHeight(), |
+ Font::BOLD); |
+ EXPECT_EQ(font.GetHeight(), derived_font.GetHeight()); |
+ EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); |
+} |
+ |
+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.
|
+ const Font base_font; |
+ PlatformFontWin* platform_font = |
+ static_cast<PlatformFontWin*>(base_font.platform_font()); |
+ |
+ for (int i = -10; i < 10; i++) { |
+ const int target_height = base_font.GetHeight() + i; |
+ Font expected_font = AdjustFontSizeForHeight(base_font, target_height); |
+ ASSERT_LE(expected_font.GetHeight(), target_height); |
+ |
+ Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0); |
+ EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); |
+ EXPECT_LE(expected_font.GetHeight(), target_height); |
+ EXPECT_EQ(0, derived_font.GetStyle()); |
+ |
+ derived_font = platform_font->DeriveFontWithHeight(target_height, |
+ Font::BOLD); |
+ EXPECT_EQ(expected_font.GetFontSize(), derived_font.GetFontSize()); |
+ EXPECT_LE(expected_font.GetHeight(), target_height); |
+ EXPECT_EQ(Font::BOLD, derived_font.GetStyle()); |
+ } |
+} |
+ |
+} // namespace gfx |