File: font_cache_test.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 (222 lines) | stat: -rw-r--r-- 8,773 bytes parent folder | download | duplicates (5)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/fonts/font_cache.h"

#include <unicode/unistr.h>

#include <string>
#include <tuple>

#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/testing/font_test_base.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"

namespace blink {

namespace {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
StringView MaybeStripFontationsSuffix(const String& font_name) {
  wtf_size_t found_index = font_name.ReverseFind(" (Fontations)");
  if (found_index != WTF::kNotFound) {
    return StringView(font_name, 0, found_index);
  } else {
    return font_name;
  }
}
#endif
}  // namespace

class FontCacheTest : public FontTestBase {};

TEST_F(FontCacheTest, getLastResortFallbackFont) {
  FontCache& font_cache = FontCache::Get();

  // Perform the test for the default font family (kStandardFamily) and the
  // -webkit-body font family (kWebkitBodyFamily) since they behave the same in
  // term of font/glyph selection.
  // TODO(crbug.com/1065468): Remove the test for kWebkitBodyFamily when
  // -webkit-body in unshipped.
  for (FontDescription::GenericFamilyType family_type :
       {FontDescription::kStandardFamily, FontDescription::kWebkitBodyFamily,
        FontDescription::kSansSerifFamily}) {
    FontDescription font_description;
    font_description.SetGenericFamily(family_type);
    const SimpleFontData* font_data =
        font_cache.GetLastResortFallbackFont(font_description);
    EXPECT_TRUE(font_data);
  }
}

TEST_F(FontCacheTest, NoFallbackForPrivateUseArea) {
  FontCache& font_cache = FontCache::Get();

  // Perform the test for the default font family (kStandardFamily) and the
  // -webkit-body font family (kWebkitBodyFamily) since they behave the same in
  // term of font/glyph selection.
  // TODO(crbug.com/1065468): Remove the test for kWebkitBodyFamily when
  // -webkit-body in unshipped.
  for (FontDescription::GenericFamilyType family_type :
       {FontDescription::kStandardFamily, FontDescription::kWebkitBodyFamily}) {
    FontDescription font_description;
    font_description.SetGenericFamily(family_type);
    for (UChar32 character : {0xE000, 0xE401, 0xE402, 0xE403, 0xF8FF, 0xF0000,
                              0xFAAAA, 0x100000, 0x10AAAA}) {
      const SimpleFontData* font_data = font_cache.FallbackFontForCharacter(
          font_description, character, nullptr);
      EXPECT_EQ(font_data, nullptr);
    }
  }
}

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
TEST_F(FontCacheTest, FallbackForEmojis) {
  FontCache& font_cache = FontCache::Get();
  FontCachePurgePreventer purge_preventer;

  // Perform the test for the default font family (kStandardFamily) and the
  // -webkit-body font family (kWebkitBodyFamily) since they behave the same in
  // term of font/glyph selection.
  // TODO(crbug.com/1065468): Remove the test for kWebkitBodyFamily when
  // -webkit-body in unshipped.
  for (FontDescription::GenericFamilyType family_type :
       {FontDescription::kStandardFamily, FontDescription::kWebkitBodyFamily}) {
    FontDescription font_description;
    font_description.SetGenericFamily(family_type);

    static constexpr char kNotoColorEmoji[] = "Noto Color Emoji";

    // We should use structured binding when it becomes available...
    for (auto info : {
             std::pair<UChar32, bool>{U'☺', true},
             {U'👪', true},
             {U'🤣', false},
         }) {
      UChar32 character = info.first;
      // Set to true if the installed contour fonts support this glyph.
      bool available_in_contour_font = info.second;
      std::string character_utf8;
      icu::UnicodeString(character).toUTF8String(character_utf8);

      {
        const SimpleFontData* font_data = font_cache.FallbackFontForCharacter(
            font_description, character, nullptr,
            FontFallbackPriority::kEmojiEmoji);
        EXPECT_EQ(MaybeStripFontationsSuffix(
                      font_data->PlatformData().FontFamilyName()),
                  kNotoColorEmoji)
            << "Character " << character_utf8
            << " doesn't match what we expected for kEmojiEmoji.";
      }
      {
        const SimpleFontData* font_data = font_cache.FallbackFontForCharacter(
            font_description, character, nullptr,
            FontFallbackPriority::kEmojiText);
        if (available_in_contour_font) {
          EXPECT_NE(MaybeStripFontationsSuffix(
                        font_data->PlatformData().FontFamilyName()),
                    kNotoColorEmoji)
              << "Character " << character_utf8
              << " doesn't match what we expected for kEmojiText.";
        } else {
          EXPECT_EQ(MaybeStripFontationsSuffix(
                        font_data->PlatformData().FontFamilyName()),
                    kNotoColorEmoji)
              << "Character " << character_utf8
              << " doesn't match what we expected for kEmojiText.";
        }
      }
    }
  }
}
#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

TEST_F(FontCacheTest, firstAvailableOrFirst) {
  EXPECT_TRUE(FontCache::FirstAvailableOrFirst("").empty());
  EXPECT_TRUE(FontCache::FirstAvailableOrFirst(String()).empty());

  EXPECT_EQ("Arial", FontCache::FirstAvailableOrFirst("Arial"));
  EXPECT_EQ("not exist", FontCache::FirstAvailableOrFirst("not exist"));

  EXPECT_EQ("Arial", FontCache::FirstAvailableOrFirst("Arial, not exist"));
  EXPECT_EQ("Arial", FontCache::FirstAvailableOrFirst("not exist, Arial"));
  EXPECT_EQ("Arial",
            FontCache::FirstAvailableOrFirst("not exist, Arial, not exist"));

  EXPECT_EQ("not exist",
            FontCache::FirstAvailableOrFirst("not exist, not exist 2"));

  EXPECT_EQ("Arial", FontCache::FirstAvailableOrFirst(", not exist, Arial"));
  EXPECT_EQ("not exist",
            FontCache::FirstAvailableOrFirst(", not exist, not exist"));
}

TEST_F(FontCacheTest, FontUniqueNameMatchAvailable) {
  FontCache& font_cache = FontCache::Get();

  FontDescription font_description;
  font_description.SetGenericFamily(FontDescription::kStandardFamily);
  font_description.SetComputedSize(12.f);
  FontFaceCreationParams creation_params;
  EXPECT_FALSE(font_cache.IsPlatformFontUniqueNameMatchAvailable(
      font_description, AtomicString()));
  EXPECT_TRUE(font_cache.IsPlatformFontUniqueNameMatchAvailable(
      font_description, AtomicString("Arial")));
  EXPECT_FALSE(font_cache.IsPlatformFontUniqueNameMatchAvailable(
      font_description, AtomicString("INVALID_FONT_NAME")));
}

// Unfortunately, we can't ensure a font here since on Android and Mac the
// unittests can't access the font configuration. However, this test passes
// when it's not crashing in FontCache.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_GetLargerThanMaxUnsignedFont DISABLED_GetLargerThanMaxUnsignedFont
#else
#define MAYBE_GetLargerThanMaxUnsignedFont GetLargerThanMaxUnsignedFont
#endif
// https://crbug.com/969402
TEST_F(FontCacheTest, MAYBE_GetLargerThanMaxUnsignedFont) {
  FontCache& font_cache = FontCache::Get();

  FontDescription font_description;
  font_description.SetGenericFamily(FontDescription::kStandardFamily);
  font_description.SetComputedSize(
      static_cast<float>(std::numeric_limits<unsigned>::max()) + 1.f);
  FontFaceCreationParams creation_params;
  const blink::SimpleFontData* font_data =
      font_cache.GetFontData(font_description, AtomicString());
  EXPECT_TRUE(font_data);
}

#if !BUILDFLAG(IS_MAC)
TEST_F(FontCacheTest, systemFont) {
  FontCache::SystemFontFamily();
  // Test the function does not crash. Return value varies by system and config.
}
#endif

#if BUILDFLAG(IS_ANDROID)
TEST_F(FontCacheTest, Locale) {
  FontCacheKey key1(FontFaceCreationParams(), /* font_size */ 16,
                    /* options */ 0, /* device_scale_factor */ 1.0f,
                    /* size_adjust */ FontSizeAdjust(),
                    /* variation_settings */ nullptr,
                    /* palette */ nullptr,
                    /* variant_alternates */ nullptr,
                    /* is_unique_match */ false);
  FontCacheKey key2 = key1;
  EXPECT_EQ(key1.GetHash(), key2.GetHash());
  EXPECT_EQ(key1, key2);

  key2.SetLocale(AtomicString("ja"));
  EXPECT_NE(key1.GetHash(), key2.GetHash());
  EXPECT_NE(key1, key2);
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace blink