platform_font_skia_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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
  3. // found in the LICENSE file.
  4. #include "ui/gfx/platform_font_skia.h"
  5. #include <string>
  6. #include "base/check_op.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/notreached.h"
  10. #include "build/build_config.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/gfx/font.h"
  13. #include "ui/gfx/font_names_testing.h"
  14. #include "ui/gfx/font_render_params.h"
  15. #if BUILDFLAG(IS_WIN)
  16. #include "ui/gfx/system_fonts_win.h"
  17. #endif
  18. #if BUILDFLAG(IS_LINUX)
  19. #include "ui/linux/fake_linux_ui.h"
  20. #endif
  21. namespace gfx {
  22. #if BUILDFLAG(IS_LINUX)
  23. // Implementation of LinuxUi used to control the default font description.
  24. class TestFontDelegate : public ui::FakeLinuxUi {
  25. public:
  26. TestFontDelegate() = default;
  27. TestFontDelegate(const TestFontDelegate&) = delete;
  28. TestFontDelegate& operator=(const TestFontDelegate&) = delete;
  29. ~TestFontDelegate() override = default;
  30. void set_family(const std::string& family) { family_ = family; }
  31. void set_size_pixels(int size_pixels) { size_pixels_ = size_pixels; }
  32. void set_style(int style) { style_ = style; }
  33. void set_weight(gfx::Font::Weight weight) { weight_ = weight; }
  34. void set_params(const FontRenderParams& params) { params_ = params; }
  35. FontRenderParams GetDefaultFontRenderParams() const override {
  36. NOTIMPLEMENTED();
  37. return FontRenderParams();
  38. }
  39. void GetDefaultFontDescription(std::string* family_out,
  40. int* size_pixels_out,
  41. int* style_out,
  42. int* weight_out,
  43. FontRenderParams* params_out) const override {
  44. *family_out = family_;
  45. *size_pixels_out = size_pixels_;
  46. *style_out = style_;
  47. *weight_out = static_cast<int>(weight_);
  48. *params_out = params_;
  49. }
  50. private:
  51. // Default values to be returned.
  52. std::string family_;
  53. int size_pixels_ = 0;
  54. int style_ = Font::NORMAL;
  55. gfx::Font::Weight weight_ = Font::Weight::NORMAL;
  56. FontRenderParams params_;
  57. };
  58. class PlatformFontSkiaTest : public testing::Test {
  59. public:
  60. PlatformFontSkiaTest() = default;
  61. PlatformFontSkiaTest(const PlatformFontSkiaTest&) = delete;
  62. PlatformFontSkiaTest& operator=(const PlatformFontSkiaTest&) = delete;
  63. ~PlatformFontSkiaTest() override = default;
  64. void SetUp() override {
  65. DCHECK_EQ(ui::LinuxUi::instance(), nullptr);
  66. auto test_font_delegate = std::make_unique<TestFontDelegate>();
  67. test_font_delegate_ = test_font_delegate.get();
  68. ui::LinuxUi::SetInstance(std::move(test_font_delegate));
  69. PlatformFontSkia::ReloadDefaultFont();
  70. }
  71. void TearDown() override {
  72. DCHECK_EQ(test_font_delegate_, ui::LinuxUi::instance());
  73. ui::LinuxUi::SetInstance(nullptr);
  74. PlatformFontSkia::ReloadDefaultFont();
  75. }
  76. protected:
  77. TestFontDelegate* test_font_delegate_ = nullptr;
  78. };
  79. // Test that PlatformFontSkia's default constructor initializes the instance
  80. // with the correct parameters.
  81. TEST_F(PlatformFontSkiaTest, DefaultFont) {
  82. test_font_delegate_->set_family(kTestFontName);
  83. test_font_delegate_->set_size_pixels(13);
  84. test_font_delegate_->set_style(Font::NORMAL);
  85. FontRenderParams params;
  86. params.antialiasing = false;
  87. params.hinting = FontRenderParams::HINTING_FULL;
  88. test_font_delegate_->set_params(params);
  89. scoped_refptr<gfx::PlatformFontSkia> font(new gfx::PlatformFontSkia());
  90. EXPECT_EQ(kTestFontName, font->GetFontName());
  91. EXPECT_EQ(13, font->GetFontSize());
  92. EXPECT_EQ(gfx::Font::NORMAL, font->GetStyle());
  93. EXPECT_EQ(params.antialiasing, font->GetFontRenderParams().antialiasing);
  94. EXPECT_EQ(params.hinting, font->GetFontRenderParams().hinting);
  95. // Drop the old default font and check that new settings are loaded.
  96. test_font_delegate_->set_family(kSymbolFontName);
  97. test_font_delegate_->set_size_pixels(15);
  98. test_font_delegate_->set_style(gfx::Font::ITALIC);
  99. test_font_delegate_->set_weight(gfx::Font::Weight::BOLD);
  100. PlatformFontSkia::ReloadDefaultFont();
  101. scoped_refptr<gfx::PlatformFontSkia> font2(new gfx::PlatformFontSkia());
  102. EXPECT_EQ(kSymbolFontName, font2->GetFontName());
  103. EXPECT_EQ(15, font2->GetFontSize());
  104. EXPECT_NE(font2->GetStyle() & Font::ITALIC, 0);
  105. EXPECT_EQ(gfx::Font::Weight::BOLD, font2->GetWeight());
  106. }
  107. #endif // BUILDFLAG(IS_LINUX)
  108. TEST(PlatformFontSkiaRenderParamsTest, DefaultFontRenderParams) {
  109. scoped_refptr<PlatformFontSkia> default_font(new PlatformFontSkia());
  110. scoped_refptr<PlatformFontSkia> named_font(new PlatformFontSkia(
  111. default_font->GetFontName(), default_font->GetFontSize()));
  112. // Ensures that both constructors are producing fonts with the same render
  113. // params.
  114. EXPECT_EQ(default_font->GetFontRenderParams(),
  115. named_font->GetFontRenderParams());
  116. }
  117. #if BUILDFLAG(IS_WIN)
  118. TEST(PlatformFontSkiaOnWindowsTest, SystemFont) {
  119. // Ensures that the font styles are kept while creating the default font.
  120. gfx::Font system_font = win::GetDefaultSystemFont();
  121. gfx::Font default_font;
  122. EXPECT_EQ(system_font.GetFontName(), default_font.GetFontName());
  123. EXPECT_EQ(system_font.GetFontSize(), default_font.GetFontSize());
  124. EXPECT_EQ(system_font.GetStyle(), default_font.GetStyle());
  125. EXPECT_EQ(system_font.GetWeight(), default_font.GetWeight());
  126. EXPECT_EQ(system_font.GetHeight(), default_font.GetHeight());
  127. EXPECT_EQ(system_font.GetBaseline(), default_font.GetBaseline());
  128. EXPECT_EQ(system_font.GetBaseline(), default_font.GetBaseline());
  129. EXPECT_EQ(system_font.GetFontRenderParams(),
  130. default_font.GetFontRenderParams());
  131. }
  132. #endif // BUILDFLAG(IS_WIN)
  133. } // namespace gfx