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"; |
| 16 | } // namespace |
| 17 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 18 | class FontFallbackLinuxTest : public testing::Test { |
| 19 | public: |
| 20 | void SetUp() override { |
| 21 | // Clear the font fallback cache. |
| 22 | GetFallbackFontsCacheInstance()->Clear(); |
| 23 | } |
| 24 | }; |
| 25 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 26 | // If the Type 1 Symbol.pfb font is installed, it is returned as fallback font |
| 27 | // for the PUA character 0xf6db. This test ensures we're not returning Type 1 |
| 28 | // fonts as fallback. |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 29 | TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) { |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 30 | FallbackFontData font_fallback_data = |
| 31 | GetFallbackFontForChar(0xf6db, std::string()); |
| 32 | if (font_fallback_data.filename.length() >= 3u) { |
| 33 | std::string extension = font_fallback_data.filename.substr( |
| 34 | font_fallback_data.filename.length() - 3); |
| 35 | EXPECT_NE(extension, "pfb"); |
| 36 | } else { |
| 37 | EXPECT_EQ(font_fallback_data.filename.length(), 0u); |
| 38 | } |
| 39 | } |
| 40 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 41 | TEST_F(FontFallbackLinuxTest, GetFallbackFont) { |
Etienne Bergeron | 5a32abe | 2019-07-19 19:14:40 | [diff] [blame] | 42 | Font base_font; |
| 43 | |
| 44 | Font fallback_font_cjk; |
| 45 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 46 | base::WideToUTF16(L"⻩"), &fallback_font_cjk)); |
| 47 | EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP"); |
| 48 | |
| 49 | Font fallback_font_khmer; |
| 50 | EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, |
| 51 | base::WideToUTF16(L"ឨឮឡ"), &fallback_font_khmer)); |
| 52 | EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer"); |
| 53 | } |
| 54 | |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 55 | TEST_F(FontFallbackLinuxTest, Fallbacks) { |
| 56 | EXPECT_EQ(0U, GetFallbackFontsCacheInstance()->size()); |
| 57 | |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 58 | Font default_font("sans", 13); |
| 59 | std::vector<Font> fallbacks = GetFallbackFonts(default_font); |
| 60 | EXPECT_FALSE(fallbacks.empty()); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 61 | EXPECT_EQ(1U, GetFallbackFontsCacheInstance()->size()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 62 | |
| 63 | // The first fallback should be 'DejaVu Sans' which is the default linux |
| 64 | // fonts. The fonts on linux are mock with test_fonts (see |
| 65 | // third_party/tests_font). |
| 66 | if (!fallbacks.empty()) |
| 67 | EXPECT_EQ(fallbacks[0].GetFontName(), "DejaVu Sans"); |
Etienne Bergeron | b27aab5 | 2019-09-30 16:51:42 | [diff] [blame] | 68 | |
| 69 | // Second lookup should not increase the cache size. |
| 70 | fallbacks = GetFallbackFonts(default_font); |
| 71 | EXPECT_FALSE(fallbacks.empty()); |
| 72 | EXPECT_EQ(1U, GetFallbackFontsCacheInstance()->size()); |
Etienne Bergeron | 4a5f549 | 2019-07-19 17:32:36 | [diff] [blame] | 73 | } |
| 74 | |
Dominik Röttsches | 8b49ef6 | 2017-11-21 20:04:23 | [diff] [blame] | 75 | } // namespace gfx |