| 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/base/test/material_design_controller_test_api.h" | |
| 11 #include "ui/gfx/font_list.h" | |
| 12 #include "ui/views/style/typography.h" | |
| 13 #include "ui/views/style/typography_provider.h" | |
| 14 | |
| 15 #if defined(OS_MACOSX) | |
| 16 #include "base/mac/mac_util.h" | |
| 17 #endif | |
| 18 | |
| 19 // Check legacy font sizes. No new code should be using these constants, but if | |
| 20 // these tests ever fail it probably means something in the old UI will have | |
| 21 // changed by mistake. | |
| 22 // Disabled since this relies on machine configuration. http://crbug.com/701241. | |
| 23 TEST(LayoutDelegateTest, DISABLED_LegacyFontSizeConstants) { | |
| 24 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 25 gfx::FontList label_font = rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); | |
| 26 | |
| 27 EXPECT_EQ(12, label_font.GetFontSize()); | |
| 28 EXPECT_EQ(15, label_font.GetHeight()); | |
| 29 EXPECT_EQ(12, label_font.GetBaseline()); | |
| 30 EXPECT_EQ(9, label_font.GetCapHeight()); | |
| 31 // Note some Windows bots report 11,13,11,9 for the above. | |
| 32 // TODO(tapted): Smoke them out and figure out why. | |
| 33 | |
| 34 #if defined(OS_MACOSX) | |
| 35 if (base::mac::IsOS10_9()) { | |
| 36 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1)); | |
| 37 } else { | |
| 38 EXPECT_EQ(10, label_font.GetExpectedTextWidth(1)); | |
| 39 } | |
| 40 #else | |
| 41 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1)); | |
| 42 // Some Windows bots may say 5. | |
| 43 #endif | |
| 44 | |
| 45 gfx::FontList title_font = rb.GetFontListWithDelta(ui::kTitleFontSizeDelta); | |
| 46 | |
| 47 #if defined(OS_WIN) | |
| 48 EXPECT_EQ(15, title_font.GetFontSize()); | |
| 49 EXPECT_EQ(20, title_font.GetHeight()); | |
| 50 EXPECT_EQ(16, title_font.GetBaseline()); | |
| 51 EXPECT_EQ(11, title_font.GetCapHeight()); | |
| 52 #elif defined(OS_MACOSX) | |
| 53 EXPECT_EQ(14, title_font.GetFontSize()); | |
| 54 EXPECT_EQ(17, title_font.GetHeight()); | |
| 55 EXPECT_EQ(14, title_font.GetBaseline()); | |
| 56 if (base::mac::IsOS10_9()) { | |
| 57 EXPECT_EQ(11, title_font.GetCapHeight()); | |
| 58 } else { | |
| 59 EXPECT_EQ(10, title_font.GetCapHeight()); | |
| 60 } | |
| 61 #else | |
| 62 EXPECT_EQ(15, title_font.GetFontSize()); | |
| 63 EXPECT_EQ(18, title_font.GetHeight()); | |
| 64 EXPECT_EQ(14, title_font.GetBaseline()); | |
| 65 EXPECT_EQ(11, title_font.GetCapHeight()); | |
| 66 #endif | |
| 67 | |
| 68 #if defined(OS_MACOSX) | |
| 69 if (base::mac::IsOS10_9()) { | |
| 70 EXPECT_EQ(7, title_font.GetExpectedTextWidth(1)); | |
| 71 } else { | |
| 72 EXPECT_EQ(12, title_font.GetExpectedTextWidth(1)); | |
| 73 } | |
| 74 #else | |
| 75 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1)); | |
| 76 #endif | |
| 77 | |
| 78 gfx::FontList small_font = rb.GetFontList(ResourceBundle::SmallFont); | |
| 79 gfx::FontList base_font = rb.GetFontList(ResourceBundle::BaseFont); | |
| 80 gfx::FontList bold_font = rb.GetFontList(ResourceBundle::BoldFont); | |
| 81 gfx::FontList medium_font = rb.GetFontList(ResourceBundle::MediumFont); | |
| 82 gfx::FontList medium_bold_font = | |
| 83 rb.GetFontList(ResourceBundle::MediumBoldFont); | |
| 84 gfx::FontList large_font = rb.GetFontList(ResourceBundle::LargeFont); | |
| 85 | |
| 86 #if defined(OS_MACOSX) | |
| 87 EXPECT_EQ(12, small_font.GetFontSize()); | |
| 88 EXPECT_EQ(13, base_font.GetFontSize()); | |
| 89 EXPECT_EQ(13, bold_font.GetFontSize()); | |
| 90 EXPECT_EQ(16, medium_font.GetFontSize()); | |
| 91 EXPECT_EQ(16, medium_bold_font.GetFontSize()); | |
| 92 EXPECT_EQ(21, large_font.GetFontSize()); | |
| 93 #else | |
| 94 EXPECT_EQ(11, small_font.GetFontSize()); | |
| 95 EXPECT_EQ(12, base_font.GetFontSize()); | |
| 96 EXPECT_EQ(12, bold_font.GetFontSize()); | |
| 97 EXPECT_EQ(15, medium_font.GetFontSize()); | |
| 98 EXPECT_EQ(15, medium_bold_font.GetFontSize()); | |
| 99 EXPECT_EQ(20, large_font.GetFontSize()); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 // Check that asking for fonts of a given size match the Harmony spec. If these | |
| 104 // tests fail, the Harmony TypographyProvider needs to be updated to handle the | |
| 105 // new font properties. For example, when title_font.GetHeight() returns 19, the | |
| 106 // Harmony TypographyProvider adds 3 to obtain its target height of 22. If a | |
| 107 // platform starts returning 18 in a standard configuration then the | |
| 108 // TypographyProvider must add 4 instead. We do this so that Chrome adapts | |
| 109 // correctly to _non-standard_ system font configurations on user machines. | |
| 110 // Disabled since this relies on machine configuration. http://crbug.com/701241. | |
| 111 TEST(LayoutDelegateTest, DISABLED_RequestFontBySize) { | |
| 112 #if defined(OS_MACOSX) | |
| 113 constexpr int kBase = 13; | |
| 114 #else | |
| 115 constexpr int kBase = 12; | |
| 116 #endif | |
| 117 // Harmony spec. | |
| 118 constexpr int kHeadline = 20; | |
| 119 constexpr int kTitle = 15; // Leading 22. | |
| 120 constexpr int kBody1 = 13; // Leading 20. | |
| 121 constexpr int kBody2 = 12; // Leading 20. | |
| 122 constexpr int kButton = 12; | |
| 123 | |
| 124 #if defined(OS_WIN) | |
| 125 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::BOLD; | |
| 126 #else | |
| 127 constexpr gfx::Font::Weight kButtonWeight = gfx::Font::Weight::MEDIUM; | |
| 128 #endif | |
| 129 | |
| 130 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 131 | |
| 132 gfx::FontList headline_font = rb.GetFontListWithDelta(kHeadline - kBase); | |
| 133 gfx::FontList title_font = rb.GetFontListWithDelta(kTitle - kBase); | |
| 134 gfx::FontList body1_font = rb.GetFontListWithDelta(kBody1 - kBase); | |
| 135 gfx::FontList body2_font = rb.GetFontListWithDelta(kBody2 - kBase); | |
| 136 gfx::FontList button_font = rb.GetFontListWithDelta( | |
| 137 kButton - kBase, gfx::Font::NORMAL, kButtonWeight); | |
| 138 | |
| 139 // The following checks on leading don't need to match the spec. Instead, it | |
| 140 // means Label::SetLineHeight() needs to be used to increase it. But what we | |
| 141 // are really interested in is the delta between GetFontSize() and GetHeight() | |
| 142 // since that (plus a fixed constant) determines how the leading should change | |
| 143 // when a larger font is configured in the OS. | |
| 144 | |
| 145 EXPECT_EQ(kHeadline, headline_font.GetFontSize()); | |
| 146 | |
| 147 // Headline leading not specified (multiline should be rare). | |
| 148 #if defined(OS_MACOSX) | |
| 149 EXPECT_EQ(25, headline_font.GetHeight()); | |
| 150 #elif defined(OS_WIN) | |
| 151 EXPECT_EQ(28, headline_font.GetHeight()); | |
| 152 #else | |
| 153 EXPECT_EQ(24, headline_font.GetHeight()); | |
| 154 #endif | |
| 155 | |
| 156 EXPECT_EQ(kTitle, title_font.GetFontSize()); | |
| 157 | |
| 158 // Title font leading should be 22. | |
| 159 #if defined(OS_MACOSX) | |
| 160 EXPECT_EQ(19, title_font.GetHeight()); // i.e. Add 3 to obtain line height. | |
| 161 #elif defined(OS_WIN) | |
| 162 EXPECT_EQ(20, title_font.GetHeight()); // Add 2. | |
| 163 #else | |
| 164 EXPECT_EQ(18, title_font.GetHeight()); // Add 4. | |
| 165 #endif | |
| 166 | |
| 167 EXPECT_EQ(kBody1, body1_font.GetFontSize()); | |
| 168 | |
| 169 // Body1 font leading should be 20. | |
| 170 #if defined(OS_MACOSX) | |
| 171 EXPECT_EQ(16, body1_font.GetHeight()); // Add 4. | |
| 172 #else // Win and Linux. | |
| 173 EXPECT_EQ(17, body1_font.GetHeight()); // Add 3. | |
| 174 #endif | |
| 175 | |
| 176 EXPECT_EQ(kBody2, body2_font.GetFontSize()); | |
| 177 | |
| 178 // Body2 font leading should be 20. | |
| 179 EXPECT_EQ(15, body2_font.GetHeight()); // All platforms: Add 5. | |
| 180 | |
| 181 EXPECT_EQ(kButton, button_font.GetFontSize()); | |
| 182 | |
| 183 // Button leading not specified (shouldn't be needed: no multiline buttons). | |
| 184 EXPECT_EQ(15, button_font.GetHeight()); | |
| 185 } | |
| 186 | |
| 187 // Test that the default TypographyProvider correctly maps TextContexts relative | |
| 188 // to the "base" font in the manner that legacy toolkit-views code expects. This | |
| 189 // reads the base font configuration at runtime, and only tests font sizes, so | |
| 190 // should be robust against platform changes. | |
| 191 TEST(LayoutDelegateTest, FontSizeRelativeToBase) { | |
| 192 using views::style::GetFont; | |
| 193 | |
| 194 constexpr int kStyle = views::style::STYLE_PRIMARY; | |
| 195 | |
| 196 // Typography described in chrome_typography.h requires a ChromeViewsDelegate. | |
| 197 ChromeViewsDelegate views_delegate; | |
| 198 | |
| 199 // Legacy code measures everything relative to a default-constructed FontList. | |
| 200 // On Mac, subtract one since that is 13pt instead of 12pt. | |
| 201 #if defined(OS_MACOSX) | |
| 202 const int twelve = gfx::FontList().GetFontSize() - 1; | |
| 203 #else | |
| 204 const int twelve = gfx::FontList().GetFontSize(); | |
| 205 #endif | |
| 206 | |
| 207 EXPECT_EQ(twelve, GetFont(CONTEXT_BODY_TEXT_SMALL, kStyle).GetFontSize()); | |
| 208 EXPECT_EQ(twelve, GetFont(views::style::CONTEXT_LABEL, kStyle).GetFontSize()); | |
| 209 EXPECT_EQ(twelve, | |
| 210 GetFont(views::style::CONTEXT_TEXTFIELD, kStyle).GetFontSize()); | |
| 211 EXPECT_EQ(twelve, | |
| 212 GetFont(views::style::CONTEXT_BUTTON, kStyle).GetFontSize()); | |
| 213 | |
| 214 #if defined(OS_MACOSX) | |
| 215 // We never exposed UI on Mac using these constants so it doesn't matter that | |
| 216 // they are different. They only need to match under Harmony. | |
| 217 EXPECT_EQ(twelve + 9, GetFont(CONTEXT_HEADLINE, kStyle).GetFontSize()); | |
| 218 EXPECT_EQ(twelve + 2, | |
| 219 GetFont(views::style::CONTEXT_DIALOG_TITLE, kStyle).GetFontSize()); | |
| 220 EXPECT_EQ(twelve + 2, GetFont(CONTEXT_BODY_TEXT_LARGE, kStyle).GetFontSize()); | |
| 221 EXPECT_EQ(twelve, GetFont(CONTEXT_DEPRECATED_SMALL, kStyle).GetFontSize()); | |
| 222 #else | |
| 223 // E.g. Headline should give a 20pt font. | |
| 224 EXPECT_EQ(twelve + 8, GetFont(CONTEXT_HEADLINE, kStyle).GetFontSize()); | |
| 225 // Titles should be 15pt. Etc. | |
| 226 EXPECT_EQ(twelve + 3, | |
| 227 GetFont(views::style::CONTEXT_DIALOG_TITLE, kStyle).GetFontSize()); | |
| 228 EXPECT_EQ(twelve + 1, GetFont(CONTEXT_BODY_TEXT_LARGE, kStyle).GetFontSize()); | |
| 229 EXPECT_EQ(twelve - 1, | |
| 230 GetFont(CONTEXT_DEPRECATED_SMALL, kStyle).GetFontSize()); | |
| 231 #endif | |
| 232 } | |
| 233 | |
| 234 // Ensure that line height can be overridden by Chrome's TypographyProvider for | |
| 235 // for the standard set of styles. This varies by platform and test machine | |
| 236 // configuration. Generally, for a particular platform configuration, there | |
| 237 // should be a consistent increase in line height when compared to the height of | |
| 238 // a given font. | |
| 239 TEST(LayoutDelegateTest, TypographyLineHeight) { | |
| 240 constexpr int kStyle = views::style::STYLE_PRIMARY; | |
| 241 | |
| 242 // Only MD overrides the default line spacing. | |
| 243 ui::test::MaterialDesignControllerTestAPI md_test_api( | |
| 244 ui::MaterialDesignController::MATERIAL_NORMAL); | |
| 245 md_test_api.SetSecondaryUiMaterial(true); | |
| 246 | |
| 247 ChromeViewsDelegate views_delegate; | |
| 248 | |
| 249 constexpr struct { | |
| 250 int context; | |
| 251 int min; | |
| 252 int max; | |
| 253 } kExpectedIncreases[] = {{CONTEXT_HEADLINE, 4, 8}, | |
| 254 {views::style::CONTEXT_DIALOG_TITLE, 2, 4}, | |
| 255 {CONTEXT_BODY_TEXT_LARGE, 3, 4}, | |
| 256 {CONTEXT_BODY_TEXT_SMALL, 5, 5}}; | |
| 257 | |
| 258 for (size_t i = 0; i < arraysize(kExpectedIncreases); ++i) { | |
| 259 SCOPED_TRACE(testing::Message() << "Testing index: " << i); | |
| 260 const auto& increase = kExpectedIncreases[i]; | |
| 261 const gfx::FontList& font = views::style::GetFont(increase.context, kStyle); | |
| 262 int line_spacing = views::style::GetLineHeight(increase.context, kStyle); | |
| 263 EXPECT_GE(increase.max, line_spacing - font.GetHeight()); | |
| 264 EXPECT_LE(increase.min, line_spacing - font.GetHeight()); | |
| 265 } | |
| 266 | |
| 267 // Buttons should specify zero line height (i.e. use the font's height) so | |
| 268 // buttons have flexibility to configure their own spacing. | |
| 269 EXPECT_EQ(0, | |
| 270 views::style::GetLineHeight(views::style::CONTEXT_BUTTON, kStyle)); | |
| 271 } | |
| OLD | NEW |