[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / ui / gfx / platform_font_win_unittest.cc
blob257e0684992c6d72bc79be99aabb1be57ceee171
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 "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/win/scoped_hdc.h"
12 #include "base/win/scoped_select_object.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/font.h"
15 #include "ui/gfx/win/direct_write.h"
16 #include "ui/gfx/win/scoped_set_map_mode.h"
18 namespace gfx {
20 TEST(PlatformFontWinTest, DeriveFontWithHeight) {
21 const Font base_font;
22 PlatformFontWin* platform_font =
23 static_cast<PlatformFontWin*>(base_font.platform_font());
25 for (int i = -10; i < 10; i++) {
26 const int target_height = base_font.GetHeight() + i;
28 Font derived_font = platform_font->DeriveFontWithHeight(target_height, 0);
29 EXPECT_LE(derived_font.GetHeight(), target_height);
30 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height);
31 EXPECT_EQ(platform_font->GetActualFontNameForTesting(),
32 derived_font.GetActualFontNameForTesting());
33 EXPECT_EQ(0, derived_font.GetStyle());
35 derived_font = platform_font->DeriveFontWithHeight(target_height,
36 Font::BOLD);
37 EXPECT_LE(derived_font.GetHeight(), target_height);
38 EXPECT_GT(derived_font.Derive(1, 0).GetHeight(), target_height);
39 EXPECT_EQ(platform_font->GetActualFontNameForTesting(),
40 derived_font.GetActualFontNameForTesting());
41 EXPECT_EQ(Font::BOLD, derived_font.GetStyle());
45 TEST(PlatformFontWinTest, DeriveFontWithHeight_Consistency) {
46 gfx::Font arial_12("Arial", 12);
47 ASSERT_GT(16, arial_12.GetHeight());
48 gfx::Font derived_1 = static_cast<PlatformFontWin*>(
49 arial_12.platform_font())->DeriveFontWithHeight(16, 0);
51 gfx::Font arial_15("Arial", 15);
52 ASSERT_LT(16, arial_15.GetHeight());
53 gfx::Font derived_2 = static_cast<PlatformFontWin*>(
54 arial_15.platform_font())->DeriveFontWithHeight(16, 0);
56 EXPECT_EQ(derived_1.GetFontSize(), derived_2.GetFontSize());
57 EXPECT_EQ(16, derived_1.GetHeight());
58 EXPECT_EQ(16, derived_2.GetHeight());
61 // Callback function used by DeriveFontWithHeight_MinSize() below.
62 static int GetMinFontSize() {
63 return 10;
66 TEST(PlatformFontWinTest, DeriveFontWithHeight_MinSize) {
67 PlatformFontWin::GetMinimumFontSizeCallback old_callback =
68 PlatformFontWin::get_minimum_font_size_callback;
69 PlatformFontWin::get_minimum_font_size_callback = &GetMinFontSize;
71 const Font base_font;
72 const Font min_font(base_font.GetFontName(), GetMinFontSize());
73 PlatformFontWin* platform_font =
74 static_cast<PlatformFontWin*>(base_font.platform_font());
76 const Font derived_font =
77 platform_font->DeriveFontWithHeight(min_font.GetHeight() - 1, 0);
78 EXPECT_EQ(min_font.GetFontSize(), derived_font.GetFontSize());
79 EXPECT_EQ(min_font.GetHeight(), derived_font.GetHeight());
81 PlatformFontWin::get_minimum_font_size_callback = old_callback;
84 TEST(PlatformFontWinTest, DeriveFontWithHeight_TooSmall) {
85 const Font base_font;
86 PlatformFontWin* platform_font =
87 static_cast<PlatformFontWin*>(base_font.platform_font());
89 const Font derived_font = platform_font->DeriveFontWithHeight(1, 0);
90 EXPECT_GT(derived_font.GetHeight(), 1);
93 // Test whether font metrics retrieved by DirectWrite (skia) and GDI match as
94 // per assumptions mentioned below:-
95 // 1. Font size is the same
96 // 2. The difference between GDI and DirectWrite for font height, baseline,
97 // and cap height is at most 1. For smaller font sizes under 12, GDI
98 // font heights/baselines/cap height are equal/larger by 1 point. For larger
99 // font sizes DirectWrite font heights/baselines/cap height are equal/larger
100 // by 1 point.
101 TEST(PlatformFontWinTest, Metrics_SkiaVersusGDI) {
102 if (!gfx::win::IsDirectWriteEnabled())
103 return;
105 // Describes the font being tested.
106 struct FontInfo {
107 base::string16 font_name;
108 int font_size;
111 FontInfo fonts[] = {
112 {base::ASCIIToUTF16("Arial"), 6},
113 {base::ASCIIToUTF16("Arial"), 8},
114 {base::ASCIIToUTF16("Arial"), 10},
115 {base::ASCIIToUTF16("Arial"), 12},
116 {base::ASCIIToUTF16("Arial"), 16},
117 {base::ASCIIToUTF16("Symbol"), 6},
118 {base::ASCIIToUTF16("Symbol"), 10},
119 {base::ASCIIToUTF16("Symbol"), 12},
120 {base::ASCIIToUTF16("Tahoma"), 10},
121 {base::ASCIIToUTF16("Tahoma"), 16},
122 {base::ASCIIToUTF16("Segoe UI"), 6},
123 {base::ASCIIToUTF16("Segoe UI"), 8},
124 {base::ASCIIToUTF16("Segoe UI"), 20},
127 base::win::ScopedGetDC screen_dc(NULL);
128 gfx::ScopedSetMapMode mode(screen_dc, MM_TEXT);
130 for (int i = 0; i < arraysize(fonts); ++i) {
131 LOGFONT font_info = {0};
133 font_info.lfHeight = -fonts[i].font_size;
134 font_info.lfWeight = FW_NORMAL;
135 wcscpy_s(font_info.lfFaceName,
136 fonts[i].font_name.length() + 1,
137 fonts[i].font_name.c_str());
139 HFONT font = CreateFontIndirect(&font_info);
141 TEXTMETRIC font_metrics;
142 PlatformFontWin::GetTextMetricsForFont(screen_dc, font, &font_metrics);
144 scoped_refptr<PlatformFontWin::HFontRef> h_font_gdi(
145 PlatformFontWin::CreateHFontRefFromGDI(font, font_metrics));
147 scoped_refptr<PlatformFontWin::HFontRef> h_font_skia(
148 PlatformFontWin::CreateHFontRefFromSkia(font, font_metrics));
150 EXPECT_EQ(h_font_gdi->font_size(), h_font_skia->font_size());
151 EXPECT_EQ(h_font_gdi->style(), h_font_skia->style());
152 EXPECT_EQ(h_font_gdi->font_name(), h_font_skia->font_name());
153 EXPECT_EQ(h_font_gdi->ave_char_width(), h_font_skia->ave_char_width());
155 EXPECT_LE(abs(h_font_gdi->cap_height() - h_font_skia->cap_height()), 1);
156 EXPECT_LE(abs(h_font_gdi->baseline() - h_font_skia->baseline()), 1);
157 EXPECT_LE(abs(h_font_gdi->height() - h_font_skia->height()), 1);
161 } // namespace gfx