blob: a887f1f11f0fcaa5e6e414740caab128d27be358 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2017 The Chromium Authors
Dominik Röttsches8b49ef62017-11-21 20:04:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gfx/font_fallback_linux.h"
6
Henrique Ferreirof6ba3bf2019-11-21 01:27:437#include "base/files/file_path.h"
Dominik Röttsches8b49ef62017-11-21 20:04:238#include "base/strings/utf_string_conversions.h"
9#include "testing/gtest/include/gtest/gtest.h"
10#include "ui/gfx/font.h"
Etienne Bergeron5a32abe2019-07-19 19:14:4011#include "ui/gfx/font_fallback.h"
Dominik Röttsches8b49ef62017-11-21 20:04:2312
13namespace gfx {
14
Etienne Bergeron5a32abe2019-07-19 19:14:4015namespace {
16const char kDefaultApplicationLocale[] = "us-en";
Etienne Bergeronee1a5992019-10-08 16:01:2217const char kFrenchApplicationLocale[] = "ca-fr";
Etienne Bergeron5a32abe2019-07-19 19:14:4018} // namespace
19
Etienne Bergeronb27aab52019-09-30 16:51:4220class FontFallbackLinuxTest : public testing::Test {
21 public:
22 void SetUp() override {
Etienne Bergeronee1a5992019-10-08 16:01:2223 // Clear the font fallback caches.
24 ClearAllFontFallbackCachesForTesting();
Etienne Bergeronb27aab52019-09-30 16:51:4225 }
26};
27
Dominik Röttsches8b49ef62017-11-21 20:04:2328// If the Type 1 Symbol.pfb font is installed, it is returned as fallback font
29// for the PUA character 0xf6db. This test ensures we're not returning Type 1
30// fonts as fallback.
Etienne Bergeronb27aab52019-09-30 16:51:4231TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) {
Henrique Ferreirof55710802019-11-25 10:01:1632 FallbackFontData font_fallback_data;
33 if (GetFallbackFontForChar(0xf6db, std::string(), &font_fallback_data)) {
34 std::string extension = font_fallback_data.filepath.Extension();
35 if (!extension.empty())
36 EXPECT_NE(extension, ".pfb");
Dominik Röttsches8b49ef62017-11-21 20:04:2337 }
38}
39
Etienne Bergeronb27aab52019-09-30 16:51:4240TEST_F(FontFallbackLinuxTest, GetFallbackFont) {
Etienne Bergeron5a32abe2019-07-19 19:14:4041 Font base_font;
42
43 Font fallback_font_cjk;
Jan Wilken Dörrie414bbf52021-03-19 19:06:2644 EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
45 &fallback_font_cjk));
Etienne Bergeron5a32abe2019-07-19 19:14:4046 EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP");
47
48 Font fallback_font_khmer;
Jan Wilken Dörrie414bbf52021-03-19 19:06:2649 EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"ឨឮឡ",
50 &fallback_font_khmer));
Etienne Bergeron5a32abe2019-07-19 19:14:4051 EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer");
52}
53
Etienne Bergeronee1a5992019-10-08 16:01:2254TEST_F(FontFallbackLinuxTest, GetFallbackFontCache) {
55 EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting());
56
57 Font base_font;
58 Font fallback_font;
Jan Wilken Dörrie414bbf52021-03-19 19:06:2659 EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
60 &fallback_font));
Etienne Bergeronee1a5992019-10-08 16:01:2261 EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());
62
63 // Second call should not increase the cache size.
Jan Wilken Dörrie414bbf52021-03-19 19:06:2664 EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
65 &fallback_font));
Etienne Bergeronee1a5992019-10-08 16:01:2266 EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());
67
68 // Third call with a different code point in the same font, should not
69 // increase the cache size.
Jan Wilken Dörrie414bbf52021-03-19 19:06:2670 EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻪",
71 &fallback_font));
Etienne Bergeronee1a5992019-10-08 16:01:2272 EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());
73
74 // A different locale should trigger an new cache entry.
Jan Wilken Dörrie414bbf52021-03-19 19:06:2675 EXPECT_TRUE(GetFallbackFont(base_font, kFrenchApplicationLocale, u"⻩",
76 &fallback_font));
Etienne Bergeronee1a5992019-10-08 16:01:2277 EXPECT_EQ(2U, GetFallbackFontEntriesCacheSizeForTesting());
78
79 // The fallbackfonts cache should not be affected.
80 EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting());
81}
82
Etienne Bergeronb27aab52019-09-30 16:51:4283TEST_F(FontFallbackLinuxTest, Fallbacks) {
Etienne Bergeronee1a5992019-10-08 16:01:2284 EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting());
Etienne Bergeronb27aab52019-09-30 16:51:4285
Etienne Bergeron4a5f5492019-07-19 17:32:3686 Font default_font("sans", 13);
87 std::vector<Font> fallbacks = GetFallbackFonts(default_font);
88 EXPECT_FALSE(fallbacks.empty());
Etienne Bergeronee1a5992019-10-08 16:01:2289 EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());
Etienne Bergeron4a5f5492019-07-19 17:32:3690
91 // The first fallback should be 'DejaVu Sans' which is the default linux
92 // fonts. The fonts on linux are mock with test_fonts (see
93 // third_party/tests_font).
94 if (!fallbacks.empty())
95 EXPECT_EQ(fallbacks[0].GetFontName(), "DejaVu Sans");
Etienne Bergeronb27aab52019-09-30 16:51:4296
97 // Second lookup should not increase the cache size.
98 fallbacks = GetFallbackFonts(default_font);
99 EXPECT_FALSE(fallbacks.empty());
Etienne Bergeronee1a5992019-10-08 16:01:22100 EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());
101
102 // The fallbackfont cache should not be affected.
103 EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting());
Etienne Bergeron4a5f5492019-07-19 17:32:36104}
105
Dominik Röttsches8b49ef62017-11-21 20:04:23106} // namespace gfx