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 // Check legacy font sizes. No new code should be using these constants, but if |
| 19 // these tests ever fail it probably means something in the old UI will have |
| 20 // changed by mistake. |
| 21 // Disabled since this relies on machine configuration. http://crbug.com/701241. |
| 22 TEST(LayoutDelegateTest, DISABLED_LegacyFontSizeConstants) { |
| 23 ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 24 gfx::FontList label_font = rb.GetFontListWithDelta(ui::kLabelFontSizeDelta); |
| 25 |
| 26 EXPECT_EQ(12, label_font.GetFontSize()); |
| 27 EXPECT_EQ(15, label_font.GetHeight()); |
| 28 EXPECT_EQ(12, label_font.GetBaseline()); |
| 29 EXPECT_EQ(9, label_font.GetCapHeight()); |
| 30 // Note some Windows bots report 11,13,11,9 for the above. |
| 31 // TODO(tapted): Smoke them out and figure out why. |
| 32 |
| 33 #if defined(OS_MACOSX) |
| 34 if (base::mac::IsOS10_9()) { |
| 35 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1)); |
| 36 } else { |
| 37 EXPECT_EQ(10, label_font.GetExpectedTextWidth(1)); |
| 38 } |
| 39 #else |
| 40 EXPECT_EQ(6, label_font.GetExpectedTextWidth(1)); |
| 41 // Some Windows bots may say 5. |
| 42 #endif |
| 43 |
| 44 gfx::FontList title_font = rb.GetFontListWithDelta(ui::kTitleFontSizeDelta); |
| 45 |
| 46 #if defined(OS_WIN) |
| 47 EXPECT_EQ(15, title_font.GetFontSize()); |
| 48 EXPECT_EQ(20, title_font.GetHeight()); |
| 49 EXPECT_EQ(16, title_font.GetBaseline()); |
| 50 EXPECT_EQ(11, title_font.GetCapHeight()); |
| 51 #elif defined(OS_MACOSX) |
| 52 EXPECT_EQ(14, title_font.GetFontSize()); |
| 53 EXPECT_EQ(17, title_font.GetHeight()); |
| 54 EXPECT_EQ(14, title_font.GetBaseline()); |
| 55 if (base::mac::IsOS10_9()) { |
| 56 EXPECT_EQ(11, title_font.GetCapHeight()); |
| 57 } else { |
| 58 EXPECT_EQ(10, title_font.GetCapHeight()); |
| 59 } |
| 60 #else |
| 61 EXPECT_EQ(15, title_font.GetFontSize()); |
| 62 EXPECT_EQ(18, title_font.GetHeight()); |
| 63 EXPECT_EQ(14, title_font.GetBaseline()); |
| 64 EXPECT_EQ(11, title_font.GetCapHeight()); |
| 65 #endif |
| 66 |
| 67 #if defined(OS_MACOSX) |
| 68 if (base::mac::IsOS10_9()) { |
| 69 EXPECT_EQ(7, title_font.GetExpectedTextWidth(1)); |
| 70 } else { |
| 71 EXPECT_EQ(12, title_font.GetExpectedTextWidth(1)); |
| 72 } |
| 73 #else |
| 74 EXPECT_EQ(8, title_font.GetExpectedTextWidth(1)); |
| 75 #endif |
| 76 |
| 77 gfx::FontList small_font = rb.GetFontList(ResourceBundle::SmallFont); |
| 78 gfx::FontList base_font = rb.GetFontList(ResourceBundle::BaseFont); |
| 79 gfx::FontList bold_font = rb.GetFontList(ResourceBundle::BoldFont); |
| 80 gfx::FontList medium_font = rb.GetFontList(ResourceBundle::MediumFont); |
| 81 gfx::FontList medium_bold_font = |
| 82 rb.GetFontList(ResourceBundle::MediumBoldFont); |
| 83 gfx::FontList large_font = rb.GetFontList(ResourceBundle::LargeFont); |
| 84 |
| 85 #if defined(OS_MACOSX) |
| 86 EXPECT_EQ(12, small_font.GetFontSize()); |
| 87 EXPECT_EQ(13, base_font.GetFontSize()); |
| 88 EXPECT_EQ(13, bold_font.GetFontSize()); |
| 89 EXPECT_EQ(16, medium_font.GetFontSize()); |
| 90 EXPECT_EQ(16, medium_bold_font.GetFontSize()); |
| 91 EXPECT_EQ(21, large_font.GetFontSize()); |
| 92 #else |
| 93 EXPECT_EQ(11, small_font.GetFontSize()); |
| 94 EXPECT_EQ(12, base_font.GetFontSize()); |
| 95 EXPECT_EQ(12, bold_font.GetFontSize()); |
| 96 EXPECT_EQ(15, medium_font.GetFontSize()); |
| 97 EXPECT_EQ(15, medium_bold_font.GetFontSize()); |
| 98 EXPECT_EQ(20, large_font.GetFontSize()); |
| 99 #endif |
| 100 } |
| 101 |
| 102 // Check that asking for fonts of a given size match the Harmony spec. If these |
| 103 // tests fail, the Harmony TypographyProvider needs to be updated to handle the |
| 104 // new font properties. For example, when title_font.GetHeight() returns 19, the |
| 105 // Harmony TypographyProvider adds 3 to obtain its target height of 22. If a |
| 106 // platform starts returning 18 in a standard configuration then the |
| 107 // TypographyProvider must add 4 instead. We do this so that Chrome adapts |
| 108 // correctly to _non-standard_ system font configurations on user machines. |
| 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 TypographyProvider 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 using views::style::GetFont; |
| 192 |
| 193 constexpr int kStyle = views::style::STYLE_PRIMARY; |
| 194 |
| 195 // Typography described in chrome_typography.h requires a ChromeViewsDelegate. |
| 196 ChromeViewsDelegate views_delegate; |
| 197 |
| 198 // Legacy code measures everything relative to a default-constructed FontList. |
| 199 // On Mac, subtract one since that is 13pt instead of 12pt. |
| 200 #if defined(OS_MACOSX) |
| 201 const int twelve = gfx::FontList().GetFontSize() - 1; |
| 202 #else |
| 203 const int twelve = gfx::FontList().GetFontSize(); |
| 204 #endif |
| 205 |
| 206 EXPECT_EQ(twelve, GetFont(CONTEXT_BODY_TEXT_SMALL, kStyle).GetFontSize()); |
| 207 EXPECT_EQ(twelve, GetFont(views::style::CONTEXT_LABEL, kStyle).GetFontSize()); |
| 208 EXPECT_EQ(twelve, |
| 209 GetFont(views::style::CONTEXT_TEXTFIELD, kStyle).GetFontSize()); |
| 210 EXPECT_EQ(twelve, |
| 211 GetFont(views::style::CONTEXT_BUTTON, kStyle).GetFontSize()); |
| 212 |
| 213 #if defined(OS_MACOSX) |
| 214 // We never exposed UI on Mac using these constants so it doesn't matter that |
| 215 // they are different. They only need to match under Harmony. |
| 216 EXPECT_EQ(twelve + 9, GetFont(CONTEXT_HEADLINE, kStyle).GetFontSize()); |
| 217 EXPECT_EQ(twelve + 2, |
| 218 GetFont(views::style::CONTEXT_DIALOG_TITLE, kStyle).GetFontSize()); |
| 219 EXPECT_EQ(twelve + 2, GetFont(CONTEXT_BODY_TEXT_LARGE, kStyle).GetFontSize()); |
| 220 EXPECT_EQ(twelve, GetFont(CONTEXT_DEPRECATED_SMALL, kStyle).GetFontSize()); |
| 221 #else |
| 222 // E.g. Headline should give a 20pt font. |
| 223 EXPECT_EQ(twelve + 8, GetFont(CONTEXT_HEADLINE, kStyle).GetFontSize()); |
| 224 // Titles should be 15pt. Etc. |
| 225 EXPECT_EQ(twelve + 3, |
| 226 GetFont(views::style::CONTEXT_DIALOG_TITLE, kStyle).GetFontSize()); |
| 227 EXPECT_EQ(twelve + 1, GetFont(CONTEXT_BODY_TEXT_LARGE, kStyle).GetFontSize()); |
| 228 EXPECT_EQ(twelve - 1, |
| 229 GetFont(CONTEXT_DEPRECATED_SMALL, kStyle).GetFontSize()); |
| 230 #endif |
| 231 } |
OLD | NEW |