Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: chrome/browser/ui/views/harmony/layout_delegate_unittest.cc

Issue 2734113006: "Bootstrap" a toolkit-views Typography spec. (Closed)
Patch Set: Shred Typography class, follow-up on some other things. fix compile. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "chrome/browser/ui/views/harmony/layout_delegate.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/base/default_style.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/views/style/typography_provider.h"
10
11 #if defined(OS_MACOSX)
12 #include "base/mac/mac_util.h"
13 #endif
14
15 using views::TextContext;
16 using views::TextStyle;
17
18 namespace {
19 const gfx::FontList& GetFont(TextContext context, TextStyle style) {
20 return views::TypographyProvider::Get().GetFont(context, style);
21 }
22 } // namespace
23
24 // Check legacy font sizes. No new code should be using these constants, but if
25 // these tests ever fail it probably means something in the old UI will have
26 // changed by mistake.
27 // Disabled since this relies on machine configuration. http://crbug.com/701241.
28 TEST(LayoutDelegateTest, DISABLED_LegacyFontSizeConstants) {
29 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
30 gfx::FontList label_font = rb.GetFontListWithDelta(ui::kLabelFontSizeDelta);
31
32 EXPECT_EQ(12, label_font.GetFontSize());
33 EXPECT_EQ(15, label_font.GetHeight());
34 EXPECT_EQ(12, label_font.GetBaseline());
35 EXPECT_EQ(9, label_font.GetCapHeight());
36 // Note some Windows bots report 11,13,11,9 for the above.
37 // TODO(tapted): Smoke them out and figure out why.
38
39 #if defined(OS_MACOSX)
40 if (base::mac::IsOS10_9()) {
41 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1));
42 } else {
43 EXPECT_EQ(10, label_font.GetExpectedTextWidth(1));
44 }
45 #else
46 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1));
47 // Some Windows bots may say 5.
48 #endif
49
50 gfx::FontList title_font = rb.GetFontListWithDelta(ui::kTitleFontSizeDelta);
51
52 #if defined(OS_WIN)
53 EXPECT_EQ(15, title_font.GetFontSize());
54 EXPECT_EQ(20, title_font.GetHeight());
55 EXPECT_EQ(16, title_font.GetBaseline());
56 EXPECT_EQ(11, title_font.GetCapHeight());
57 #elif defined(OS_MACOSX)
58 EXPECT_EQ(14, title_font.GetFontSize());
59 EXPECT_EQ(17, title_font.GetHeight());
60 EXPECT_EQ(14, title_font.GetBaseline());
61 if (base::mac::IsOS10_9()) {
62 EXPECT_EQ(11, title_font.GetCapHeight());
63 } else {
64 EXPECT_EQ(10, title_font.GetCapHeight());
65 }
66 #else
67 EXPECT_EQ(15, title_font.GetFontSize());
68 EXPECT_EQ(18, title_font.GetHeight());
69 EXPECT_EQ(14, title_font.GetBaseline());
70 EXPECT_EQ(11, title_font.GetCapHeight());
71 #endif
72
73 #if defined(OS_MACOSX)
74 if (base::mac::IsOS10_9()) {
75 EXPECT_EQ(7, title_font.GetExpectedTextWidth(1));
76 } else {
77 EXPECT_EQ(12, title_font.GetExpectedTextWidth(1));
78 }
79 #else
80 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1));
81 #endif
82
83 gfx::FontList small_font = rb.GetFontList(ResourceBundle::SmallFont);
84 gfx::FontList base_font = rb.GetFontList(ResourceBundle::BaseFont);
85 gfx::FontList bold_font = rb.GetFontList(ResourceBundle::BoldFont);
86 gfx::FontList medium_font = rb.GetFontList(ResourceBundle::MediumFont);
87 gfx::FontList medium_bold_font =
88 rb.GetFontList(ResourceBundle::MediumBoldFont);
89 gfx::FontList large_font = rb.GetFontList(ResourceBundle::LargeFont);
90
91 #if defined(OS_MACOSX)
92 EXPECT_EQ(12, small_font.GetFontSize());
93 EXPECT_EQ(13, base_font.GetFontSize());
94 EXPECT_EQ(13, bold_font.GetFontSize());
95 EXPECT_EQ(16, medium_font.GetFontSize());
96 EXPECT_EQ(16, medium_bold_font.GetFontSize());
97 EXPECT_EQ(21, large_font.GetFontSize());
98 #else
99 EXPECT_EQ(11, small_font.GetFontSize());
100 EXPECT_EQ(12, base_font.GetFontSize());
101 EXPECT_EQ(12, bold_font.GetFontSize());
102 EXPECT_EQ(15, medium_font.GetFontSize());
103 EXPECT_EQ(15, medium_bold_font.GetFontSize());
104 EXPECT_EQ(20, large_font.GetFontSize());
105 #endif
106 }
107
108 // Check that asking for fonts of a given size match the Harmony spec.
109 // Disabled since this relies on machine configuration. http://crbug.com/701241.
110 TEST(LayoutDelegateTest, DISABLED_RequestFontBySize) {
111 #if defined(OS_MACOSX)
112 constexpr int kBase = 13;
113 #else
114 constexpr int kBase = 12;
115 #endif
116 // Harmony spec.
117 constexpr int kHeadline = 20;
118 constexpr int kTitle = 15; // Leading 22.
119 constexpr int kBody1 = 13; // Leading 20.
120 constexpr int kBody2 = 12; // Leading 20.
121 constexpr int kButton = 12;
122
123 #if defined(OS_WIN)
124 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::BOLD;
125 #else
126 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::MEDIUM;
127 #endif
128
129 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
130
131 gfx::FontList headline_font = rb.GetFontListWithDelta(kHeadline - kBase);
132 gfx::FontList title_font = rb.GetFontListWithDelta(kTitle - kBase);
133 gfx::FontList body1_font = rb.GetFontListWithDelta(kBody1 - kBase);
134 gfx::FontList body2_font = rb.GetFontListWithDelta(kBody2 - kBase);
135 gfx::FontList button_font = rb.GetFontListWithDelta(
136 kButton - kBase, gfx::Font::NORMAL, kButtonWeight);
137
138 // The following checks on leading don't need to match the spec. Instead, it
139 // means Label::SetLineHeight() needs to be used to increase it. But what we
140 // are really interested in is the delta between GetFontSize() and GetHeight()
141 // since that (plus a fixed constant) determines how the leading should change
142 // when a larger font is configured in the OS.
143
144 EXPECT_EQ(kHeadline, headline_font.GetFontSize());
145
146 // Headline leading not specified (multiline should be rare).
147 #if defined(OS_MACOSX)
148 EXPECT_EQ(25, headline_font.GetHeight());
149 #elif defined(OS_WIN)
150 EXPECT_EQ(28, headline_font.GetHeight());
151 #else
152 EXPECT_EQ(24, headline_font.GetHeight());
153 #endif
154
155 EXPECT_EQ(kTitle, title_font.GetFontSize());
156
157 // Title font leading should be 22.
158 #if defined(OS_MACOSX)
159 EXPECT_EQ(19, title_font.GetHeight()); // i.e. Add 3 to obtain line height.
160 #elif defined(OS_WIN)
161 EXPECT_EQ(20, title_font.GetHeight()); // Add 2.
162 #else
163 EXPECT_EQ(18, title_font.GetHeight()); // Add 4.
164 #endif
165
166 EXPECT_EQ(kBody1, body1_font.GetFontSize());
167
168 // Body1 font leading should be 20.
169 #if defined(OS_MACOSX)
170 EXPECT_EQ(16, body1_font.GetHeight()); // Add 4.
171 #else // Win and Linux.
172 EXPECT_EQ(17, body1_font.GetHeight()); // Add 3.
173 #endif
174
175 EXPECT_EQ(kBody2, body2_font.GetFontSize());
176
177 // Body2 font leading should be 20.
178 EXPECT_EQ(15, body2_font.GetHeight()); // All platforms: Add 5.
179
180 EXPECT_EQ(kButton, button_font.GetFontSize());
181
182 // Button leading not specified (shouldn't be needed: no multiline buttons).
183 EXPECT_EQ(15, button_font.GetHeight());
184 }
185
186 // Test that the default TypographyProivder correctly maps TextContexts relative
187 // to the "base" font in the manner that legacy toolkit-views code expects. This
188 // reads the base font configuration at runtime, and only tests font sizes, so
189 // should be robust against platform changes.
190 TEST(LayoutDelegateTest, FontSizeRelativeToBase) {
191 constexpr TextStyle kStyle = TextStyle::PRIMARY;
192
193 // Ensure the LegacyTypographyProvider is installed.
194 EXPECT_TRUE(LayoutDelegate::Get());
195
196 // Legacy code measures everything relative to a default-constructed FontList.
197 // On Mac, subtract one since that is 13pt instead of 12pt.
198 #if defined(OS_MACOSX)
199 const int twelve = gfx::FontList().GetFontSize() - 1;
200 #else
201 const int twelve = gfx::FontList().GetFontSize();
202 #endif
203
204 EXPECT_EQ(
205 twelve,
206 GetFont(ChromeTextContext::DIALOG_TEXT_SMALL, kStyle).GetFontSize());
207 EXPECT_EQ(twelve, GetFont(TextContext::CONTROL_LABEL, kStyle).GetFontSize());
208 EXPECT_EQ(twelve, GetFont(TextContext::FIELD, kStyle).GetFontSize());
209 EXPECT_EQ(twelve, GetFont(TextContext::BUTTON_TEXT, kStyle).GetFontSize());
210
211 #if defined(OS_MACOSX)
212 // We never exposed UI on Mac using these constants so it doesn't matter that
213 // they are different. They only need to match under Harmony.
214 EXPECT_EQ(twelve + 9,
215 GetFont(ChromeTextContext::HEADLINE, kStyle).GetFontSize());
216 EXPECT_EQ(twelve + 2,
217 GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize());
218 EXPECT_EQ(twelve + 2,
219 GetFont(ChromeTextContext::DIALOG_MESSAGE, kStyle).GetFontSize());
220 EXPECT_EQ(twelve,
221 GetFont(ChromeTextContext::DEPRECATED_SMALL, kStyle).GetFontSize());
222 #else
223 // E.g. Headline should give a 20pt font.
224 EXPECT_EQ(twelve + 8,
225 GetFont(ChromeTextContext::HEADLINE, kStyle).GetFontSize());
226 // Titles should be 15pt. Etc.
227 EXPECT_EQ(twelve + 3,
228 GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize());
229 EXPECT_EQ(twelve + 1,
230 GetFont(ChromeTextContext::DIALOG_MESSAGE, kStyle).GetFontSize());
231 EXPECT_EQ(twelve - 1,
232 GetFont(ChromeTextContext::DEPRECATED_SMALL, kStyle).GetFontSize());
233 #endif
234 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698