Dominik Röttsches | eae454fe | 2017-11-22 11:16:57 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
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 | |
| 7 | #include "base/strings/utf_string_conversions.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "ui/gfx/font.h" |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 10 | #include "ui/gfx/font_fallback.h" |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 11 | |
| 12 | namespace gfx { |
| 13 | |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 14 | namespace { |
| 15 | const char kDefaultApplicationLocale[] = "us-en"; |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 16 | const char kFrenchApplicationLocale[] = "ca-fr"; |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 17 | } // namespace |
| 18 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 19 | class FontFallbackLinuxTest : public testing::Test { |
| 20 | public: |
| 21 | void SetUp() override { |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 22 | // Clear the font fallback caches. |
| 23 | ClearAllFontFallbackCachesForTesting(); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 24 | } |
| 25 | }; |
| 26 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 27 | // If the Type 1 Symbol.pfb font is installed, it is returned as fallback font |
| 28 | // for the PUA character 0xf6db. This test ensures we're not returning Type 1 |
| 29 | // fonts as fallback. |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 30 | TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) { |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 31 | FallbackFontData font_fallback_data = |
| 32 | GetFallbackFontForChar(0xf6db, std::string()); |
| 33 | if (font_fallback_data.filename.length() >= 3u) { |
| 34 | std::string extension = font_fallback_data.filename.substr( |
| 35 | font_fallback_data.filename.length() - 3); |
| 36 | EXPECT_NE(extension, "pfb"); |
| 37 | } else { |
| 38 | EXPECT_EQ(font_fallback_data.filename.length(), 0u); |
| 39 | } |
| 40 | } |
| 41 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 42 | TEST_F(FontFallbackLinuxTest, GetFallbackFont) { |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 43 | Font base_font; |
| 44 | |
| 45 | Font fallback_font_cjk; |
| 46 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 47 | base::WideToUTF16(L"⻩"), &fallback_font_cjk)); |
| 48 | EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP"); |
| 49 | |
| 50 | Font fallback_font_khmer; |
| 51 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 52 | base::WideToUTF16(L"ឨឮឡ"), &fallback_font_khmer)); |
| 53 | EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer"); |
| 54 | } |
| 55 | |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 56 | TEST_F(FontFallbackLinuxTest, GetFallbackFontCache) { |
| 57 | EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 58 | |
| 59 | Font base_font; |
| 60 | Font fallback_font; |
| 61 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 62 | base::WideToUTF16(L"⻩"), &fallback_font)); |
| 63 | EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 64 | |
| 65 | // Second call should not increase the cache size. |
| 66 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 67 | base::WideToUTF16(L"⻩"), &fallback_font)); |
| 68 | EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 69 | |
| 70 | // Third call with a different code point in the same font, should not |
| 71 | // increase the cache size. |
| 72 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 73 | base::WideToUTF16(L"⻪"), &fallback_font)); |
| 74 | EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 75 | |
| 76 | // A different locale should trigger an new cache entry. |
| 77 | EXPECT_TRUE(GetFallbackFont(base_font, kFrenchApplicationLocale, |
| 78 | base::WideToUTF16(L"⻩"), &fallback_font)); |
| 79 | EXPECT_EQ(2U, GetFallbackFontEntriesCacheSizeForTesting()); |
| 80 | |
| 81 | // The fallbackfonts cache should not be affected. |
| 82 | EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting()); |
| 83 | } |
| 84 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 85 | TEST_F(FontFallbackLinuxTest, Fallbacks) { |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 86 | EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting()); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 87 | |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 88 | Font default_font("sans", 13); |
| 89 | std::vector<Font> fallbacks = GetFallbackFonts(default_font); |
| 90 | EXPECT_FALSE(fallbacks.empty()); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 91 | EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 92 | |
| 93 | // The first fallback should be 'DejaVu Sans' which is the default linux |
| 94 | // fonts. The fonts on linux are mock with test_fonts (see |
| 95 | // third_party/tests_font). |
| 96 | if (!fallbacks.empty()) |
| 97 | EXPECT_EQ(fallbacks[0].GetFontName(), "DejaVu Sans"); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 98 | |
| 99 | // Second lookup should not increase the cache size. |
| 100 | fallbacks = GetFallbackFonts(default_font); |
| 101 | EXPECT_FALSE(fallbacks.empty()); |
Etienne Bergeron | ee1a599 | 2019-10-08 16:01:22 | [diff] [blame^] | 102 | EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting()); |
| 103 | |
| 104 | // The fallbackfont cache should not be affected. |
| 105 | EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 106 | } |
| 107 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 108 | } // namespace gfx |