OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_render_params.h" | 5 #include "ui/gfx/font_render_params.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "ui/gfx/font.h" | 12 #include "ui/gfx/font.h" |
| 13 #include "ui/gfx/linux_font_delegate.h" |
| 14 #include "ui/gfx/pango_util.h" |
13 #include "ui/gfx/test/fontconfig_util_linux.h" | 15 #include "ui/gfx/test/fontconfig_util_linux.h" |
14 | 16 |
15 namespace gfx { | 17 namespace gfx { |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
| 21 // Implementation of LinuxFontDelegate that returns a canned FontRenderParams |
| 22 // struct. This is used to isolate tests from the system's local configuration. |
| 23 class TestFontDelegate : public LinuxFontDelegate { |
| 24 public: |
| 25 TestFontDelegate() {} |
| 26 virtual ~TestFontDelegate() {} |
| 27 |
| 28 void set_params(const FontRenderParams& params) { params_ = params; } |
| 29 |
| 30 virtual FontRenderParams GetDefaultFontRenderParams() const OVERRIDE { |
| 31 return params_; |
| 32 } |
| 33 virtual scoped_ptr<ScopedPangoFontDescription> |
| 34 GetDefaultPangoFontDescription() const OVERRIDE { |
| 35 NOTIMPLEMENTED(); |
| 36 return scoped_ptr<ScopedPangoFontDescription>(); |
| 37 } |
| 38 virtual double GetFontDPI() const OVERRIDE { |
| 39 NOTIMPLEMENTED(); |
| 40 return 96.0; |
| 41 } |
| 42 |
| 43 private: |
| 44 FontRenderParams params_; |
| 45 |
| 46 DISALLOW_COPY_AND_ASSIGN(TestFontDelegate); |
| 47 }; |
| 48 |
19 // Loads the first system font defined by fontconfig_util_linux.h with a base | 49 // Loads the first system font defined by fontconfig_util_linux.h with a base |
20 // filename of |basename|. Case is ignored. | 50 // filename of |basename|. Case is ignored. |
21 bool LoadSystemFont(const std::string& basename) { | 51 bool LoadSystemFont(const std::string& basename) { |
22 for (size_t i = 0; i < kNumSystemFontsForFontconfig; ++i) { | 52 for (size_t i = 0; i < kNumSystemFontsForFontconfig; ++i) { |
23 base::FilePath path(gfx::kSystemFontsForFontconfig[i]); | 53 base::FilePath path(gfx::kSystemFontsForFontconfig[i]); |
24 if (strcasecmp(path.BaseName().value().c_str(), basename.c_str()) == 0) | 54 if (strcasecmp(path.BaseName().value().c_str(), basename.c_str()) == 0) |
25 return LoadFontIntoFontconfig(path); | 55 return LoadFontIntoFontconfig(path); |
26 } | 56 } |
27 LOG(ERROR) << "Unable to find system font named " << basename; | 57 LOG(ERROR) << "Unable to find system font named " << basename; |
28 return false; | 58 return false; |
29 } | 59 } |
30 | 60 |
31 } // namespace | 61 } // namespace |
32 | 62 |
33 class FontRenderParamsTest : public testing::Test { | 63 class FontRenderParamsTest : public testing::Test { |
34 public: | 64 public: |
35 FontRenderParamsTest() { | 65 FontRenderParamsTest() { |
36 SetUpFontconfig(); | 66 SetUpFontconfig(); |
37 CHECK(temp_dir_.CreateUniqueTempDir()); | 67 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 68 original_font_delegate_ = LinuxFontDelegate::instance(); |
| 69 LinuxFontDelegate::SetInstance(&test_font_delegate_); |
38 } | 70 } |
39 | 71 |
40 virtual ~FontRenderParamsTest() { | 72 virtual ~FontRenderParamsTest() { |
| 73 LinuxFontDelegate::SetInstance( |
| 74 const_cast<LinuxFontDelegate*>(original_font_delegate_)); |
41 TearDownFontconfig(); | 75 TearDownFontconfig(); |
42 } | 76 } |
43 | 77 |
44 protected: | 78 protected: |
45 base::ScopedTempDir temp_dir_; | 79 base::ScopedTempDir temp_dir_; |
| 80 const LinuxFontDelegate* original_font_delegate_; |
| 81 TestFontDelegate test_font_delegate_; |
46 | 82 |
47 private: | 83 private: |
48 DISALLOW_COPY_AND_ASSIGN(FontRenderParamsTest); | 84 DISALLOW_COPY_AND_ASSIGN(FontRenderParamsTest); |
49 }; | 85 }; |
50 | 86 |
51 TEST_F(FontRenderParamsTest, Default) { | 87 TEST_F(FontRenderParamsTest, Default) { |
52 // Fontconfig needs to know about at least one font to return a match. | 88 // Fontconfig needs to know about at least one font to return a match. |
53 ASSERT_TRUE(LoadSystemFont("arial.ttf")); | 89 ASSERT_TRUE(LoadSystemFont("arial.ttf")); |
54 ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(), | 90 ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(), |
55 std::string(kFontconfigFileHeader) + | 91 std::string(kFontconfigFileHeader) + |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 FontRenderParams params = GetCustomFontRenderParams( | 239 FontRenderParams params = GetCustomFontRenderParams( |
204 false, NULL, NULL, NULL, NULL, NULL); | 240 false, NULL, NULL, NULL, NULL, NULL); |
205 EXPECT_FALSE(params.use_bitmaps); | 241 EXPECT_FALSE(params.use_bitmaps); |
206 | 242 |
207 const int pixel_size = 5; | 243 const int pixel_size = 5; |
208 params = GetCustomFontRenderParams( | 244 params = GetCustomFontRenderParams( |
209 false, NULL, &pixel_size, NULL, NULL, NULL); | 245 false, NULL, &pixel_size, NULL, NULL, NULL); |
210 EXPECT_TRUE(params.use_bitmaps); | 246 EXPECT_TRUE(params.use_bitmaps); |
211 } | 247 } |
212 | 248 |
| 249 TEST_F(FontRenderParamsTest, OnlySetConfiguredValues) { |
| 250 // Configure the LinuxFontDelegate (which queries GtkSettings on desktop |
| 251 // Linux) to request subpixel rendering. |
| 252 FontRenderParams system_params; |
| 253 system_params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB; |
| 254 test_font_delegate_.set_params(system_params); |
| 255 |
| 256 // Load a Fontconfig config that enables antialiasing but doesn't say anything |
| 257 // about subpixel rendering. |
| 258 ASSERT_TRUE(LoadSystemFont("arial.ttf")); |
| 259 ASSERT_TRUE(LoadConfigDataIntoFontconfig(temp_dir_.path(), |
| 260 std::string(kFontconfigFileHeader) + |
| 261 kFontconfigMatchHeader + |
| 262 CreateFontconfigEditStanza("antialias", "bool", "true") + |
| 263 kFontconfigMatchFooter + |
| 264 kFontconfigFileFooter)); |
| 265 |
| 266 // The subpixel rendering setting from the delegate should make it through. |
| 267 FontRenderParams params = GetCustomFontRenderParams( |
| 268 false, NULL, NULL, NULL, NULL, NULL); |
| 269 EXPECT_EQ(system_params.subpixel_rendering, params.subpixel_rendering); |
| 270 } |
| 271 |
213 } // namespace gfx | 272 } // namespace gfx |
OLD | NEW |