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 "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. |
| 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. |
| 97 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1)); |
| 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 } |
| 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 #if 0 |
| 229 EXPECT_EQ(kTwelve, Typography::GetFont(TextContext::DIALOG_TEXT_SMALL, kStyle) |
| 230 .GetFontSize()); |
| 231 #endif |
| 232 EXPECT_EQ( |
| 233 kTwelve, |
| 234 Typography::GetFont(TextContext::CONTROL_LABEL, kStyle).GetFontSize()); |
| 235 EXPECT_EQ(kTwelve, |
| 236 Typography::GetFont(TextContext::FIELD, kStyle).GetFontSize()); |
| 237 EXPECT_EQ( |
| 238 kTwelve, |
| 239 Typography::GetFont(TextContext::BUTTON_TEXT, kStyle).GetFontSize()); |
| 240 |
| 241 #if defined(OS_MACOSX) |
| 242 // We never exposed UI on Mac using these constants so it doesn't matter that |
| 243 // they are different. They only need to match under Harmony. |
| 244 EXPECT_EQ(kTwelve + 9, |
| 245 Typography::GetFont(TextContext::HEADLINE, kStyle).GetFontSize()); |
| 246 EXPECT_EQ( |
| 247 kTwelve + 2, |
| 248 Typography::GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize()); |
| 249 EXPECT_EQ( |
| 250 kTwelve + 2, |
| 251 Typography::GetFont(TextContext::DIALOG_MESSAGE, kStyle).GetFontSize()); |
| 252 #if 0 |
| 253 EXPECT_EQ( |
| 254 kTwelve, |
| 255 Typography::GetFont(TextContext::DEPRECATED_SMALL, kStyle).GetFontSize()); |
| 256 #endif |
| 257 #else |
| 258 // E.g. Headline should give a 20pt font. |
| 259 EXPECT_EQ(kTwelve + 8, |
| 260 Typography::GetFont(TextContext::HEADLINE, kStyle).GetFontSize()); |
| 261 // Titles should be 15pt. Etc. |
| 262 EXPECT_EQ( |
| 263 kTwelve + 3, |
| 264 Typography::GetFont(TextContext::DIALOG_TITLE, kStyle).GetFontSize()); |
| 265 EXPECT_EQ( |
| 266 kTwelve + 1, |
| 267 Typography::GetFont(TextContext::DIALOG_MESSAGE, kStyle).GetFontSize()); |
| 268 #if 0 |
| 269 EXPECT_EQ( |
| 270 kTwelve - 1, |
| 271 Typography::GetFont(TextContext::DEPRECATED_SMALL, kStyle).GetFontSize()); |
| 272 #endif |
| 273 #endif |
| 274 } |
| 275 |
| 276 } // namespace views |
OLD | NEW |