summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/font_fallback_linux_unittest.cc
blob: a7c582d949907148bc42fa9479db823f91b38699 (plain)
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. All rights reserved.
// 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,
                              base::WideToUTF16(L"⻩"), &fallback_font_cjk));
  EXPECT_EQ(fallback_font_cjk.GetFontName(), "Noto Sans CJK JP");

  Font fallback_font_khmer;
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale,
                              base::WideToUTF16(L"ឨឮឡ"), &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,
                              base::WideToUTF16(L"⻩"), &fallback_font));
  EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());

  // Second call should not increase the cache size.
  EXPECT_TRUE(GetFallbackFont(base_font, kDefaultApplicationLocale,
                              base::WideToUTF16(L"⻩"), &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,
                              base::WideToUTF16(L"⻪"), &fallback_font));
  EXPECT_EQ(1U, GetFallbackFontEntriesCacheSizeForTesting());

  // A different locale should trigger an new cache entry.
  EXPECT_TRUE(GetFallbackFont(base_font, kFrenchApplicationLocale,
                              base::WideToUTF16(L"⻩"), &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