summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/linux/fontconfig_util_unittest.cc
blob: 5c4d7d1f9267b512398511ab1d8ec26f4c866d85 (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
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
// Copyright 2019 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/linux/fontconfig_util.h"

#include <fontconfig/fontconfig.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace gfx {

TEST(FontConfigUtilTest, FcPatternAccessors) {
  ScopedFcPattern pattern(FcPatternCreate());

  const char kFamilyName[] = "sans";
  FcPatternAddString(pattern.get(), FC_FAMILY,
                     reinterpret_cast<const FcChar8*>(kFamilyName));
  const char kFileName[] = "/usr/share/fonts/arial.ttf";
  FcPatternAddString(pattern.get(), FC_FILE,
                     reinterpret_cast<const FcChar8*>(kFileName));
  const int kIndex = 42;
  FcPatternAddInteger(pattern.get(), FC_INDEX, kIndex);
  FcPatternAddInteger(pattern.get(), FC_WEIGHT, FC_WEIGHT_BOLD);
  FcPatternAddInteger(pattern.get(), FC_SLANT, FC_SLANT_ROMAN);
  FcPatternAddBool(pattern.get(), FC_SCALABLE, FcTrue);
  const char kFontFormat[] = "TrueType";
  FcPatternAddString(pattern.get(), FC_FONTFORMAT,
                     reinterpret_cast<const FcChar8*>(kFontFormat));

  EXPECT_EQ(kFamilyName, GetFontName(pattern.get()));
  EXPECT_EQ(kFileName, GetFilename(pattern.get()));
  EXPECT_EQ(kIndex, GetFontTtcIndex(pattern.get()));
  EXPECT_TRUE(IsFontBold(pattern.get()));
  EXPECT_FALSE(IsFontItalic(pattern.get()));
  EXPECT_TRUE(IsFontScalable(pattern.get()));
  EXPECT_EQ(kFontFormat, GetFontFormat(pattern.get()));
}

TEST(FontConfigUtilTest, GetFontPathWithSysRoot) {
  ScopedFcPattern pattern(FcPatternCreate());

  // Save the old sysroot, if specified.
  std::string old_sysroot;
  const FcChar8* old_sysroot_ptr = FcConfigGetSysRoot(nullptr);
  if (old_sysroot_ptr)
    old_sysroot = reinterpret_cast<const char*>(old_sysroot_ptr);

  // Override the sysroot.
  base::FilePath sysroot("/var/opt/fonts");
  FcConfigSetSysRoot(nullptr, reinterpret_cast<const FcChar8*>(
                                  sysroot.AsUTF8Unsafe().c_str()));

  // Validate that path are relative to sysroot.
  const char kFileName[] = "fonts/arial.ttf";
  FcPatternAddString(pattern.get(), FC_FILE,
                     reinterpret_cast<const FcChar8*>(kFileName));
  const char kExpectedFileName[] = "/var/opt/fonts/fonts/arial.ttf";
  EXPECT_EQ(base::FilePath(kExpectedFileName), GetFontPath(pattern.get()));

  // Restore the old sysroot, if specified.
  if (old_sysroot_ptr) {
    FcConfigSetSysRoot(nullptr,
                       reinterpret_cast<const FcChar8*>(old_sysroot.c_str()));
  }
}

TEST(FontConfigUtilTest, GetFontPathWithoutSysRoot) {
  ScopedFcPattern pattern(FcPatternCreate());

  // Save the old sysroot, if specified.
  std::string old_sysroot;
  const FcChar8* old_sysroot_ptr = FcConfigGetSysRoot(nullptr);
  if (old_sysroot_ptr)
    old_sysroot = reinterpret_cast<const char*>(old_sysroot_ptr);

  // Override (remove) the sysroot.
  FcConfigSetSysRoot(nullptr, nullptr);

  // Check that the filename is not changed without a sysroot present.
  const char kFileName[] = "/var/opt/font/fonts/arial.ttf";
  FcPatternAddString(pattern.get(), FC_FILE,
                     reinterpret_cast<const FcChar8*>(kFileName));
  EXPECT_EQ(base::FilePath(kFileName), GetFontPath(pattern.get()));

  // Restore the old sysroot, if specified.
  if (old_sysroot_ptr) {
    FcConfigSetSysRoot(nullptr,
                       reinterpret_cast<const FcChar8*>(old_sysroot.c_str()));
  }
}

TEST(FontConfigUtilTest, GetFontRenderParamsFromFcPatternWithEmptyPattern) {
  ScopedFcPattern pattern(FcPatternCreate());

  FontRenderParams params;
  GetFontRenderParamsFromFcPattern(pattern.get(), &params);

  FontRenderParams empty_params;
  EXPECT_EQ(params, empty_params);
}

TEST(FontConfigUtilTest, GetFontRenderParamsFromFcPatternWithFalseValues) {
  ScopedFcPattern pattern(FcPatternCreate());
  FcPatternAddBool(pattern.get(), FC_ANTIALIAS, FcFalse);
  FcPatternAddBool(pattern.get(), FC_AUTOHINT, FcFalse);
  FcPatternAddBool(pattern.get(), FC_EMBEDDED_BITMAP, FcFalse);

  FontRenderParams params;
  GetFontRenderParamsFromFcPattern(pattern.get(), &params);

  FontRenderParams expected_params;
  expected_params.antialiasing = false;
  expected_params.autohinter = false;
  expected_params.use_bitmaps = false;
  EXPECT_EQ(params, expected_params);
}

TEST(FontConfigUtilTest, GetFontRenderParamsFromFcPatternWithValues) {
  ScopedFcPattern pattern(FcPatternCreate());
  FcPatternAddBool(pattern.get(), FC_ANTIALIAS, FcTrue);
  FcPatternAddBool(pattern.get(), FC_AUTOHINT, FcTrue);
  FcPatternAddInteger(pattern.get(), FC_HINT_STYLE, FC_HINT_MEDIUM);
  FcPatternAddBool(pattern.get(), FC_EMBEDDED_BITMAP, FcTrue);
  FcPatternAddInteger(pattern.get(), FC_RGBA, FC_RGBA_RGB);

  FontRenderParams params;
  GetFontRenderParamsFromFcPattern(pattern.get(), &params);

  FontRenderParams expected_params;
  expected_params.antialiasing = true;
  expected_params.autohinter = true;
  expected_params.hinting = FontRenderParams::HINTING_MEDIUM;
  expected_params.use_bitmaps = true;
  expected_params.subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;

  EXPECT_EQ(params, expected_params);
}

}  // namespace gfx