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.
5 #include "ui/gfx/platform_font_linux.h"
9 #include "base/memory/ref_counted.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/font.h"
12 #include "ui/gfx/font_render_params.h"
13 #include "ui/gfx/linux_font_delegate.h"
14 #include "ui/gfx/test/fontconfig_util_linux.h"
18 // Implementation of LinuxFontDelegate used to control the default font
19 // description on Linux.
20 class TestFontDelegate
: public LinuxFontDelegate
{
22 TestFontDelegate() : size_pixels_(0), style_(gfx::Font::NORMAL
) {}
23 ~TestFontDelegate() override
{}
25 void set_family(const std::string
& family
) { family_
= family
; }
26 void set_size_pixels(int size_pixels
) { size_pixels_
= size_pixels
; }
27 void set_style(int style
) { style_
= style
; }
28 void set_params(const FontRenderParams
& params
) { params_
= params
; }
30 FontRenderParams
GetDefaultFontRenderParams() const override
{
32 return FontRenderParams();
34 void GetDefaultFontDescription(
35 std::string
* family_out
,
38 FontRenderParams
* params_out
) const override
{
39 *family_out
= family_
;
40 *size_pixels_out
= size_pixels_
;
42 *params_out
= params_
;
46 // Default values to be returned.
50 FontRenderParams params_
;
52 DISALLOW_COPY_AND_ASSIGN(TestFontDelegate
);
55 class PlatformFontLinuxTest
: public testing::Test
{
57 PlatformFontLinuxTest() {
59 original_font_delegate_
= LinuxFontDelegate::instance();
60 LinuxFontDelegate::SetInstance(&test_font_delegate_
);
63 ~PlatformFontLinuxTest() override
{
64 LinuxFontDelegate::SetInstance(
65 const_cast<LinuxFontDelegate
*>(original_font_delegate_
));
70 TestFontDelegate test_font_delegate_
;
73 // Originally-registered delegate.
74 const LinuxFontDelegate
* original_font_delegate_
;
76 DISALLOW_COPY_AND_ASSIGN(PlatformFontLinuxTest
);
79 // Test that PlatformFontLinux's default constructor initializes the instance
80 // with the correct parameters.
81 TEST_F(PlatformFontLinuxTest
, DefaultFont
) {
82 ASSERT_TRUE(LoadSystemFontIntoFontconfig("arial.ttf"));
83 ASSERT_TRUE(LoadSystemFontIntoFontconfig("times_new_roman.ttf"));
85 #if defined(OS_CHROMEOS)
86 PlatformFontLinux::SetDefaultFontDescription("Arial,Times New Roman,13px");
88 test_font_delegate_
.set_family("Arial");
89 test_font_delegate_
.set_size_pixels(13);
90 test_font_delegate_
.set_style(gfx::Font::NORMAL
);
91 FontRenderParams params
;
92 params
.antialiasing
= false;
93 params
.hinting
= FontRenderParams::HINTING_FULL
;
94 test_font_delegate_
.set_params(params
);
96 scoped_refptr
<gfx::PlatformFontLinux
> font(new gfx::PlatformFontLinux());
97 EXPECT_EQ("Arial", font
->GetFontName());
98 EXPECT_EQ(13, font
->GetFontSize());
99 EXPECT_EQ(gfx::Font::NORMAL
, font
->GetStyle());
100 #if !defined(OS_CHROMEOS)
101 // On Linux, the FontRenderParams returned by the the delegate should be used.
102 EXPECT_EQ(params
.antialiasing
, font
->GetFontRenderParams().antialiasing
);
103 EXPECT_EQ(params
.hinting
, font
->GetFontRenderParams().hinting
);
106 // Drop the old default font and check that new settings are loaded.
107 #if defined(OS_CHROMEOS)
108 PlatformFontLinux::SetDefaultFontDescription(
109 "Times New Roman,Arial,Bold 15px");
111 test_font_delegate_
.set_family("Times New Roman");
112 test_font_delegate_
.set_size_pixels(15);
113 test_font_delegate_
.set_style(gfx::Font::BOLD
);
115 PlatformFontLinux::ReloadDefaultFont();
116 scoped_refptr
<gfx::PlatformFontLinux
> font2(new gfx::PlatformFontLinux());
117 EXPECT_EQ("Times New Roman", font2
->GetFontName());
118 EXPECT_EQ(15, font2
->GetFontSize());
119 EXPECT_EQ(gfx::Font::BOLD
, font2
->GetStyle());