blob: eb86ca07e8655e00eaccb5426b070ea5ecd49339 [file] [log] [blame]
Dominik Röttscheseae454fe2017-11-22 11:16:571// Copyright 2017 The Chromium Authors. All rights reserved.
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
7#include "base/strings/utf_string_conversions.h"
8#include "testing/gtest/include/gtest/gtest.h"
9#include "ui/gfx/font.h"
Etienne Bergeron5a32abe2019-07-19 19:14:4010#include "ui/gfx/font_fallback.h"
Dominik Röttsches8b49ef62017-11-21 20:04:2311
12namespace gfx {
13
Etienne Bergeron5a32abe2019-07-19 19:14:4014namespace {
15const char kDefaultApplicationLocale[] = "us-en";
Etienne Bergeronee1a5992019-10-08 16:01:2216const char kFrenchApplicationLocale[] = "ca-fr";
Etienne Bergeron5a32abe2019-07-19 19:14:4017} // namespace
18
Etienne Bergeronb27aab52019-09-30 16:51:4219class FontFallbackLinuxTest : public testing::Test {
20 public:
21 void SetUp() override {
Etienne Bergeronee1a5992019-10-08 16:01:2222 // Clear the font fallback caches.
23 ClearAllFontFallbackCachesForTesting();
Etienne Bergeronb27aab52019-09-30 16:51:4224 }
25};
26
Dominik Röttsches8b49ef62017-11-21 20:04:2327// 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 Bergeronb27aab52019-09-30 16:51:4230TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) {
Dominik Röttsches8b49ef62017-11-21 20:04:2331 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 Bergeronb27aab52019-09-30 16:51:4242TEST_F(FontFallbackLinuxTest, GetFallbackFont) {
Etienne Bergeron5a32abe2019-07-19 19:14:4043 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 Bergeronee1a5992019-10-08 16:01:2256TEST_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 Bergeronb27aab52019-09-30 16:51:4285TEST_F(FontFallbackLinuxTest, Fallbacks) {
Etienne Bergeronee1a5992019-10-08 16:01:2286 EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting());
Etienne Bergeronb27aab52019-09-30 16:51:4287
Etienne Bergeron4a5f5492019-07-19 17:32:3688 Font default_font("sans", 13);
89 std::vector<Font> fallbacks = GetFallbackFonts(default_font);
90 EXPECT_FALSE(fallbacks.empty());
Etienne Bergeronee1a5992019-10-08 16:01:2291 EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());
Etienne Bergeron4a5f5492019-07-19 17:32:3692
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 Bergeronb27aab52019-09-30 16:51:4298
99 // Second lookup should not increase the cache size.
100 fallbacks = GetFallbackFonts(default_font);
101 EXPECT_FALSE(fallbacks.empty());
Etienne Bergeronee1a5992019-10-08 16:01:22102 EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());
103
104 // The fallbackfont cache should not be affected.
105 EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting());
Etienne Bergeron4a5f5492019-07-19 17:32:36106}
107
Dominik Röttsches8b49ef62017-11-21 20:04:23108} // namespace gfx