Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 2 | // 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 Ferreiro | f6ba3bf | 2019-11-21 01:27:43 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "ui/gfx/font.h" |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 11 | #include "ui/gfx/font_fallback.h" |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 12 | |
| 13 | namespace gfx { |
| 14 | |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 15 | namespace { |
| 16 | const char kDefaultApplicationLocale[] = "us-en"; |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 17 | const char kFrenchApplicationLocale[] = "ca-fr"; |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 18 | } // namespace |
| 19 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 20 | class FontFallbackLinuxTest : public testing::Test { |
| 21 | public: |
| 22 | void SetUp() override { |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 23 | // Clear the font fallback caches. |
| 24 | ClearAllFontFallbackCachesForTesting(); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 25 | } |
| 26 | }; |
| 27 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 28 | // 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 Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 31 | TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) { |
Henrique Ferreiro | f5571080 | 2019-11-25 10:01:16 | [diff] [blame] | 32 | 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öttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 40 | TEST_F(FontFallbackLinuxTest, GetFallbackFont) { |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 41 | Font base_font; |
| 42 | |
| 43 | Font fallback_font_cjk; |
Jan Wilken Dörrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 44 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩", |
| 45 | &fallback_font_cjk)); |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 46 | EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP"); |
| 47 | |
| 48 | Font fallback_font_khmer; |
Jan Wilken Dörrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 49 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"ឨឮឡ", |
| 50 | &fallback_font_khmer)); |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 51 | EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer"); |
| 52 | } |
| 53 | |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 54 | TEST_F(FontFallbackLinuxTest, GetFallbackFontCache) { |
| 55 | EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 56 | |
| 57 | Font base_font; |
| 58 | Font fallback_font; |
Jan Wilken Dörrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 59 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩", |
| 60 | &fallback_font)); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 61 | EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 62 | |
| 63 | // Second call should not increase the cache size. |
Jan Wilken Dörrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 64 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩", |
| 65 | &fallback_font)); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 66 | 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örrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 70 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻪", |
| 71 | &fallback_font)); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 72 | EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 73 | |
| 74 | // A different locale should trigger an new cache entry. |
Jan Wilken Dörrie | 414bbf5 | 2021-03-19 19:06:26 | [diff] [blame] | 75 | EXPECT_TRUE(GetFallbackFont(base_font, kFrenchApplicationLocale, u"⻩", |
| 76 | &fallback_font)); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 77 | EXPECT_EQ(2U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 78 | |
| 79 | // The fallbackfonts cache should not be affected. |
| 80 | EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting()); |
| 81 | } |
| 82 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 83 | TEST_F(FontFallbackLinuxTest, Fallbacks) { |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 84 | EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting()); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 85 | |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 86 | Font default_font("sans", 13); |
| 87 | std::vector<Font> fallbacks = GetFallbackFonts(default_font); |
| 88 | EXPECT_FALSE(fallbacks.empty()); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 89 | EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 90 | |
| 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 Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 96 | |
| 97 | // Second lookup should not increase the cache size. |
| 98 | fallbacks = GetFallbackFonts(default_font); |
| 99 | EXPECT_FALSE(fallbacks.empty()); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame] | 100 | EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting()); |
| 101 | |
| 102 | // The fallbackfont cache should not be affected. |
| 103 | EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 104 | } |
| 105 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 106 | } // namespace gfx |