| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/font_list.h" | 5 #include "ui/gfx/font_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "ui/gfx/font_names_testing.h" |
| 15 | 16 |
| 16 namespace gfx { | 17 namespace gfx { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Helper function for comparing fonts for equality. | 21 // Helper function for comparing fonts for equality. |
| 21 std::string FontToString(const Font& font) { | 22 std::string FontToString(const Font& font) { |
| 22 std::string font_string = font.GetFontName(); | 23 std::string font_string = font.GetFontName(); |
| 23 font_string += "|"; | 24 font_string += "|"; |
| 24 font_string += base::IntToString(font.GetFontSize()); | 25 font_string += base::IntToString(font.GetFontSize()); |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 TEST(FontListTest, MAYBE_Fonts_GetHeight_GetBaseline) { | 300 TEST(FontListTest, MAYBE_Fonts_GetHeight_GetBaseline) { |
| 300 // If a font list has only one font, the height and baseline must be the same. | 301 // If a font list has only one font, the height and baseline must be the same. |
| 301 Font font1("Arial", 16); | 302 Font font1("Arial", 16); |
| 302 ASSERT_EQ("arial", base::ToLowerASCII(font1.GetActualFontNameForTesting())); | 303 ASSERT_EQ("arial", base::ToLowerASCII(font1.GetActualFontNameForTesting())); |
| 303 FontList font_list1("Arial, 16px"); | 304 FontList font_list1("Arial, 16px"); |
| 304 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight()); | 305 EXPECT_EQ(font1.GetHeight(), font_list1.GetHeight()); |
| 305 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline()); | 306 EXPECT_EQ(font1.GetBaseline(), font_list1.GetBaseline()); |
| 306 | 307 |
| 307 // If there are two different fonts, the font list returns the max value | 308 // If there are two different fonts, the font list returns the max value |
| 308 // for the baseline (ascent) and height. | 309 // for the baseline (ascent) and height. |
| 309 Font font2("Symbol", 16); | 310 Font font2(symbol_font_name, 16); |
| 310 ASSERT_EQ("symbol", base::ToLowerASCII(font2.GetActualFontNameForTesting())); | 311 ASSERT_EQ(base::ToLowerASCII(symbol_font_name), |
| 312 base::ToLowerASCII(font2.GetActualFontNameForTesting())); |
| 311 EXPECT_NE(font1.GetBaseline(), font2.GetBaseline()); | 313 EXPECT_NE(font1.GetBaseline(), font2.GetBaseline()); |
| 312 // TODO(ananta): Find a size and font pair with reliably distinct descents. | 314 // TODO(ananta): Find a size and font pair with reliably distinct descents. |
| 313 EXPECT_NE(font1.GetHeight(), font2.GetHeight()); | 315 EXPECT_NE(font1.GetHeight(), font2.GetHeight()); |
| 314 std::vector<Font> fonts; | 316 std::vector<Font> fonts; |
| 315 fonts.push_back(font1); | 317 fonts.push_back(font1); |
| 316 fonts.push_back(font2); | 318 fonts.push_back(font2); |
| 317 FontList font_list_mix(fonts); | 319 FontList font_list_mix(fonts); |
| 318 // ascent of FontList == max(ascent of Fonts) | 320 // ascent of FontList == max(ascent of Fonts) |
| 319 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()), | 321 EXPECT_EQ(std::max(font1.GetBaseline(), font2.GetBaseline()), |
| 320 font_list_mix.GetBaseline()); | 322 font_list_mix.GetBaseline()); |
| 321 // descent of FontList == max(descent of Fonts) | 323 // descent of FontList == max(descent of Fonts) |
| 322 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(), | 324 EXPECT_EQ(std::max(font1.GetHeight() - font1.GetBaseline(), |
| 323 font2.GetHeight() - font2.GetBaseline()), | 325 font2.GetHeight() - font2.GetBaseline()), |
| 324 font_list_mix.GetHeight() - font_list_mix.GetBaseline()); | 326 font_list_mix.GetHeight() - font_list_mix.GetBaseline()); |
| 325 } | 327 } |
| 326 | 328 |
| 327 // TODO(489354): Enable this on android. | 329 // TODO(489354): Enable this on android. |
| 328 #if defined(OS_ANDROID) | 330 #if defined(OS_ANDROID) |
| 329 #define MAYBE_Fonts_DeriveWithHeightUpperBound \ | 331 #define MAYBE_Fonts_DeriveWithHeightUpperBound \ |
| 330 DISABLED_Fonts_DeriveWithHeightUpperBound | 332 DISABLED_Fonts_DeriveWithHeightUpperBound |
| 331 #else | 333 #else |
| 332 #define MAYBE_Fonts_DeriveWithHeightUpperBound Fonts_DeriveWithHeightUpperBound | 334 #define MAYBE_Fonts_DeriveWithHeightUpperBound Fonts_DeriveWithHeightUpperBound |
| 333 #endif | 335 #endif |
| 334 TEST(FontListTest, MAYBE_Fonts_DeriveWithHeightUpperBound) { | 336 TEST(FontListTest, MAYBE_Fonts_DeriveWithHeightUpperBound) { |
| 335 std::vector<Font> fonts; | 337 std::vector<Font> fonts; |
| 336 | 338 |
| 337 fonts.push_back(Font("Arial", 18)); | 339 fonts.push_back(Font("Arial", 18)); |
| 338 fonts.push_back(Font("Sans serif", 18)); | 340 fonts.push_back(Font("Sans serif", 18)); |
| 339 fonts.push_back(Font("Symbol", 18)); | 341 fonts.push_back(Font(symbol_font_name, 18)); |
| 340 FontList font_list = FontList(fonts); | 342 FontList font_list = FontList(fonts); |
| 341 | 343 |
| 342 // A smaller upper bound should derive a font list with a smaller height. | 344 // A smaller upper bound should derive a font list with a smaller height. |
| 343 const int height_1 = font_list.GetHeight() - 5; | 345 const int height_1 = font_list.GetHeight() - 5; |
| 344 FontList derived_1 = font_list.DeriveWithHeightUpperBound(height_1); | 346 FontList derived_1 = font_list.DeriveWithHeightUpperBound(height_1); |
| 345 EXPECT_LE(derived_1.GetHeight(), height_1); | 347 EXPECT_LE(derived_1.GetHeight(), height_1); |
| 346 EXPECT_LT(derived_1.GetHeight(), font_list.GetHeight()); | 348 EXPECT_LT(derived_1.GetHeight(), font_list.GetHeight()); |
| 347 EXPECT_LT(derived_1.GetFontSize(), font_list.GetFontSize()); | 349 EXPECT_LT(derived_1.GetFontSize(), font_list.GetFontSize()); |
| 348 | 350 |
| 349 // A larger upper bound should not change the height of the font list. | 351 // A larger upper bound should not change the height of the font list. |
| 350 const int height_2 = font_list.GetHeight() + 5; | 352 const int height_2 = font_list.GetHeight() + 5; |
| 351 FontList derived_2 = font_list.DeriveWithHeightUpperBound(height_2); | 353 FontList derived_2 = font_list.DeriveWithHeightUpperBound(height_2); |
| 352 EXPECT_LE(derived_2.GetHeight(), height_2); | 354 EXPECT_LE(derived_2.GetHeight(), height_2); |
| 353 EXPECT_EQ(font_list.GetHeight(), derived_2.GetHeight()); | 355 EXPECT_EQ(font_list.GetHeight(), derived_2.GetHeight()); |
| 354 EXPECT_EQ(font_list.GetFontSize(), derived_2.GetFontSize()); | 356 EXPECT_EQ(font_list.GetFontSize(), derived_2.GetFontSize()); |
| 355 } | 357 } |
| 356 | 358 |
| 357 } // namespace gfx | 359 } // namespace gfx |
| OLD | NEW |