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

Side by Side Diff: ui/views/style/typography_unittest.cc

Issue 2734113006: "Bootstrap" a toolkit-views Typography spec. (Closed)
Patch Set: fix windows 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
« ui/views/style/typography.cc ('K') | « ui/views/style/typography.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "ui/views/style/typography.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
10 #if defined(OS_MACOSX)
11 #include "base/mac/mac_util.h"
12 #endif
13
14 namespace views {
15
16 // Check legacy font sizes. No new code should be using these constants, but if
17 // these tests ever fail it probably means something in the old UI will have
18 // changed by mistake.
19 // Disabled since this relies on machine configuration. http://crbug.com/701241.
20 TEST(TypographyTest, DISABLED_LegacyFontSizeConstants) {
21 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
22 gfx::FontList label_font = rb.GetFontListWithDelta(ui::kLabelFontSizeDelta);
23
24 #if defined(OS_WIN)
25 // On Windows, some bots say 11 and some bots say 12. This is a bad thing.
Peter Kasting 2017/03/14 23:53:34 OS versions differ? Might check OSInfo::version()
tapted 2017/03/15 08:26:25 I'll need to smoke out the bot - might have been a
26 if (label_font.GetFontSize() == 11) {
27 EXPECT_EQ(11, label_font.GetFontSize());
28 EXPECT_EQ(13, label_font.GetHeight());
29 EXPECT_EQ(11, label_font.GetBaseline());
30 } else if (label_font.GetFontSize() == 12) {
31 EXPECT_EQ(12, label_font.GetFontSize());
32 EXPECT_EQ(15, label_font.GetHeight());
33 EXPECT_EQ(12, label_font.GetBaseline());
34 } else {
35 ADD_FAILURE() << "Label font size is not 11 or 12 on Windows.";
36 }
37 EXPECT_EQ(9, label_font.GetCapHeight());
38 #else
39 EXPECT_EQ(12, label_font.GetFontSize());
40 EXPECT_EQ(15, label_font.GetHeight());
41 EXPECT_EQ(12, label_font.GetBaseline());
42 EXPECT_EQ(9, label_font.GetCapHeight());
43 #endif
44
45 #if defined(OS_MACOSX)
46 if (base::mac::IsOS10_9()) {
47 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1));
48 } else {
49 EXPECT_EQ(10, label_font.GetExpectedTextWidth(1));
50 }
51 #elif defined(OS_WIN)
52 int label_font_width = label_font.GetExpectedTextWidth(1);
53 EXPECT_TRUE(label_font_width == 5 || label_font_width == 6);
54 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1));
55 #else
56 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1));
57 #endif
58
59 int windows_fudge_delta = 0;
60 #if defined(OS_WIN)
61 windows_fudge_delta = label_font.GetFontSize() == 11 ? 1 : 0;
62 #endif
63
64 gfx::FontList title_font =
65 rb.GetFontListWithDelta(ui::kTitleFontSizeDelta + windows_fudge_delta);
66
67 #if defined(OS_WIN)
68 EXPECT_EQ(15, title_font.GetFontSize());
69 EXPECT_EQ(20, title_font.GetHeight());
70 EXPECT_EQ(16, title_font.GetBaseline());
71 EXPECT_EQ(11, title_font.GetCapHeight());
72 #elif defined(OS_MACOSX)
73 EXPECT_EQ(14, title_font.GetFontSize());
74 EXPECT_EQ(17, title_font.GetHeight());
75 EXPECT_EQ(14, title_font.GetBaseline());
76 if (base::mac::IsOS10_9()) {
77 EXPECT_EQ(11, title_font.GetCapHeight());
78 } else {
79 EXPECT_EQ(10, title_font.GetCapHeight());
80 }
81 #else
82 EXPECT_EQ(15, title_font.GetFontSize());
83 EXPECT_EQ(18, title_font.GetHeight());
84 EXPECT_EQ(14, title_font.GetBaseline());
85 EXPECT_EQ(11, title_font.GetCapHeight());
86 #endif
87
88 #if defined(OS_MACOSX)
89 if (base::mac::IsOS10_9()) {
90 EXPECT_EQ(7, title_font.GetExpectedTextWidth(1));
91 } else {
92 EXPECT_EQ(12, title_font.GetExpectedTextWidth(1));
93 }
94 #elif defined(OS_WIN)
95 int title_font_width = title_font.GetExpectedTextWidth(1);
96 EXPECT_TRUE(title_font_width == 6 || title_font_width == 8); // Weird.
Peter Kasting 2017/03/14 23:53:34 What.
tapted 2017/03/15 08:26:25 Yeah - there was one bot run that was different to
97 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1));
Peter Kasting 2017/03/14 23:53:34 This line kinda contradicts the one just above
tapted 2017/03/15 08:26:25 Acknowledged.
98 #else
99 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1));
100 #endif
101
102 gfx::FontList small_font = rb.GetFontList(ResourceBundle::SmallFont);
103 gfx::FontList base_font = rb.GetFontList(ResourceBundle::BaseFont);
104 gfx::FontList bold_font = rb.GetFontList(ResourceBundle::BoldFont);
105 gfx::FontList medium_font = rb.GetFontList(ResourceBundle::MediumFont);
106 gfx::FontList medium_bold_font =
107 rb.GetFontList(ResourceBundle::MediumBoldFont);
108 gfx::FontList large_font = rb.GetFontList(ResourceBundle::LargeFont);
109
110 #if defined(OS_MACOSX)
111 EXPECT_EQ(12, small_font.GetFontSize());
112 EXPECT_EQ(13, base_font.GetFontSize());
113 EXPECT_EQ(13, bold_font.GetFontSize());
114 EXPECT_EQ(16, medium_font.GetFontSize());
115 EXPECT_EQ(16, medium_bold_font.GetFontSize());
116 EXPECT_EQ(21, large_font.GetFontSize());
117 #else
118 EXPECT_EQ(11 - windows_fudge_delta, small_font.GetFontSize());
119 EXPECT_EQ(12 - windows_fudge_delta, base_font.GetFontSize());
120 EXPECT_EQ(12 - windows_fudge_delta, bold_font.GetFontSize());
121 EXPECT_EQ(15 - windows_fudge_delta, medium_font.GetFontSize());
122 EXPECT_EQ(15 - windows_fudge_delta, medium_bold_font.GetFontSize());
123 EXPECT_EQ(20 - windows_fudge_delta, large_font.GetFontSize());
124 #endif
125 }
126
127 // Check that asking for fonts of a given size match the Harmony spec.
128 // Disabled since this relies on machine configuration. http://crbug.com/701241.
129 TEST(TypographyTest, DISABLED_RequestFontBySize) {
130 #if defined(OS_MACOSX)
131 constexpr int kBase = 13;
132 #else
133 constexpr int kBase = 12;
134 #endif
135 // Harmony spec.
136 constexpr int kHeadline = 20;
137 constexpr int kTitle = 15; // Leading 22.
138 constexpr int kBody1 = 13; // Leading 20.
139 constexpr int kBody2 = 12; // Leading 20.
140 constexpr int kButton = 12;
141
142 // Cater for bots that report 11 as the base font size rather than 12.
143 int windows_fudge_delta = 0;
144
145 #if defined(OS_WIN)
146 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::BOLD;
147 windows_fudge_delta = gfx::FontList().GetFontSize() == 11 ? 1 : 0;
148 #else
149 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::MEDIUM;
150 #endif
151
152 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
153
154 gfx::FontList headline_font =
155 rb.GetFontListWithDelta(kHeadline - kBase + windows_fudge_delta);
156 gfx::FontList title_font =
157 rb.GetFontListWithDelta(kTitle - kBase + windows_fudge_delta);
158 gfx::FontList body1_font =
159 rb.GetFontListWithDelta(kBody1 - kBase + windows_fudge_delta);
160 gfx::FontList body2_font =
161 rb.GetFontListWithDelta(kBody2 - kBase + windows_fudge_delta);
162 gfx::FontList button_font = rb.GetFontListWithDelta(
163 kButton - kBase + windows_fudge_delta, gfx::Font::NORMAL, kButtonWeight);
164
165 // The following checks on leading don't need to match the spec. Instead, it
166 // means Label::SetLineHeight() needs to be used to increase it. But what we
167 // are really interested in is the delta between GetFontSize() and GetHeight()
168 // since that (plus a fixed constant) determines how the leading should change
169 // when a larger font is configured in the OS.
170
171 EXPECT_EQ(kHeadline, headline_font.GetFontSize());
172
173 // Headline leading not specified (multiline should be rare).
174 #if defined(OS_MACOSX)
175 EXPECT_EQ(25, headline_font.GetHeight());
176 #elif defined(OS_WIN)
177 EXPECT_EQ(28, headline_font.GetHeight());
178 #else
179 EXPECT_EQ(24, headline_font.GetHeight());
180 #endif
181
182 EXPECT_EQ(kTitle, title_font.GetFontSize());
183
184 // Title font leading should be 22.
185 #if defined(OS_MACOSX)
186 EXPECT_EQ(19, title_font.GetHeight()); // i.e. Add 3 to obtain line height.
187 #elif defined(OS_WIN)
188 EXPECT_EQ(20, title_font.GetHeight()); // Add 2.
189 #else
190 EXPECT_EQ(18, title_font.GetHeight()); // Add 4.
191 #endif
192
193 EXPECT_EQ(kBody1, body1_font.GetFontSize());
194
195 // Body1 font leading should be 20.
196 #if defined(OS_MACOSX)
197 EXPECT_EQ(16, body1_font.GetHeight()); // Add 4.
198 #else // Win and Linux.
199 EXPECT_EQ(17, body1_font.GetHeight()); // Add 3.
200 #endif
201
202 EXPECT_EQ(kBody2, body2_font.GetFontSize());
203
204 // Body2 font leading should be 20.
205 EXPECT_EQ(15, body2_font.GetHeight()); // All platforms: Add 5.
206
207 EXPECT_EQ(kButton, button_font.GetFontSize());
208
209 // Button leading not specified (shouldn't be needed: no multiline buttons).
210 EXPECT_EQ(15, button_font.GetHeight());
211 }
tapted 2017/03/14 09:56:18 The tests above were a means to explore how "leadi
212
213 // Test that the default TypographyProivder correctly maps TextContexts relative
214 // to the "base" font in the manner that legacy toolkit-views code expects. This
215 // reads the base font configuration at runtime, and only tests font sizes, so
216 // should be robust against platform changes.
217 TEST(TypographyTest, FontSizeRelativeToBase) {
218 constexpr TextStyle kStyle = TextStyle::PRIMARY;
219
220 // Legacy code measures everything relative to a default-constructed FontList.
221 // On Mac, subtract one since that is 13pt instead of 12pt.
222 #if defined(OS_MACOSX)
223 const int kTwelve = gfx::FontList().GetFontSize() - 1;
224 #else
225 const int kTwelve = gfx::FontList().GetFontSize();
226 #endif
227
228 EXPECT_EQ(kTwelve, Typography::GetFont(TextContext::DIALOG_TEXT_SMALL, kStyle)
229 .GetFontSize());
230 EXPECT_EQ(
231 kTwelve,
232 Typography::GetFont(TextContext::CONTROL_LABEL, kStyle).GetFontSize());
233 EXPECT_EQ(kTwelve,
234 Typography::GetFont(TextContext::FIELD, kStyle).GetFontSize());
235 EXPECT_EQ(
236 kTwelve,
237 Typography::GetFont(TextContext::BUTTON_TEXT, kStyle).GetFontSize());
238
239 #if defined(OS_MACOSX)
240 // We never exposed UI on Mac using these constants so it doesn't matter that
241 // they are different. They only need to match under Harmony.
242 EXPECT_EQ(kTwelve + 9,
243 Typography::GetFont(TextContext::HEADLINE, kStyle).GetFontSize());
244 EXPECT_EQ(
245 kTwelve + 2,
246 Typography::GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize());
247 EXPECT_EQ(
248 kTwelve + 2,
249 Typography::GetFont(TextContext::DIALOG_MESSAGE, kStyle).GetFontSize());
250 EXPECT_EQ(
251 kTwelve,
252 Typography::GetFont(TextContext::DEPRECATED_SMALL, kStyle).GetFontSize());
253 #else
254 // E.g. Headline should give a 20pt font.
255 EXPECT_EQ(kTwelve + 8,
256 Typography::GetFont(TextContext::HEADLINE, kStyle).GetFontSize());
257 // Titles should be 15pt. Etc.
258 EXPECT_EQ(
259 kTwelve + 3,
260 Typography::GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize());
261 EXPECT_EQ(
262 kTwelve + 1,
263 Typography::GetFont(TextContext::DIALOG_MESSAGE, kStyle).GetFontSize());
264 EXPECT_EQ(
265 kTwelve - 1,
266 Typography::GetFont(TextContext::DEPRECATED_SMALL, kStyle).GetFontSize());
267 #endif
268 }
269
270 } // namespace views
OLDNEW
« ui/views/style/typography.cc ('K') | « ui/views/style/typography.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698