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