File: font_fallback_linux_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,825 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/font_fallback_linux.h"

#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_fallback.h"

namespace gfx {

namespace {
const char kDefaultApplicationLocale[] = "us-en";
const char kFrenchApplicationLocale[] = "ca-fr";
}  // namespace

class FontFallbackLinuxTest : public testing::Test {
 public:
  void SetUp() override {
    // Clear the font fallback caches.
    ClearAllFontFallbackCachesForTesting();
  }
};

// If the Type 1 Symbol.pfb font is installed, it is returned as fallback font
// for the PUA character 0xf6db. This test ensures we're not returning Type 1
// fonts as fallback.
TEST_F(FontFallbackLinuxTest, NoType1InFallbackFonts) {
  FallbackFontData font_fallback_data;
  if (GetFallbackFontForChar(0xf6db, std::string(), &font_fallback_data)) {
    std::string extension = font_fallback_data.filepath.Extension();
    if (!extension.empty())
      EXPECT_NE(extension, ".pfb");
  }
}

TEST_F(FontFallbackLinuxTest, GetFallbackFont) {
  Font base_font;

  Font fallback_font_cjk;
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
                              &fallback_font_cjk));
  EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP");

  Font fallback_font_khmer;
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"ឨឮឡ",
                              &fallback_font_khmer));
  EXPECT_EQ(fallback_font_khmer.GetFontName(), "Noto Sans Khmer");
}

TEST_F(FontFallbackLinuxTest, GetFallbackFontCache) {
  EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting());

  Font base_font;
  Font fallback_font;
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
                              &fallback_font));
  EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());

  // Second call should not increase the cache size.
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻩",
                              &fallback_font));
  EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());

  // Third call with a different code point in the same font, should not
  // increase the cache size.
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale, u"⻪",
                              &fallback_font));
  EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());

  // A different locale should trigger an new cache entry.
  EXPECT_TRUE(GetFallbackFont(base_font, kFrenchApplicationLocale, u"⻩",
                              &fallback_font));
  EXPECT_EQ(2U, GetFallbackFontEntriesCacheSizeForTesting());

  // The fallbackfonts cache should not be affected.
  EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting());
}

TEST_F(FontFallbackLinuxTest, Fallbacks) {
  EXPECT_EQ(0U, GetFallbackFontListCacheSizeForTesting());

  Font default_font("sans", 13);
  std::vector<Font> fallbacks = GetFallbackFonts(default_font);
  EXPECT_FALSE(fallbacks.empty());
  EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());

  // The first fallback should be 'DejaVu Sans' which is the default linux
  // fonts. The fonts on linux are mock with test_fonts (see
  // third_party/tests_font).
  if (!fallbacks.empty())
    EXPECT_EQ(fallbacks[0].GetFontName(), "DejaVu Sans");

  // Second lookup should not increase the cache size.
  fallbacks = GetFallbackFonts(default_font);
  EXPECT_FALSE(fallbacks.empty());
  EXPECT_EQ(1U, GetFallbackFontListCacheSizeForTesting());

  // The fallbackfont cache should not be affected.
  EXPECT_EQ(0U, GetFallbackFontEntriesCacheSizeForTesting());
}

}  // namespace gfx