1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "minikin/SystemFonts.h"
18
19 #include <gtest/gtest.h>
20
21 #include "minikin/FontCollection.h"
22
23 #include "FontTestUtils.h"
24 #include "PathUtils.h"
25
26 namespace minikin {
27 namespace {
28
29 class TestableSystemFonts : public SystemFonts {
30 public:
TestableSystemFonts()31 TestableSystemFonts() : SystemFonts() {}
~TestableSystemFonts()32 virtual ~TestableSystemFonts() {}
33
findFontCollection(const std::string & familyName)34 std::shared_ptr<FontCollection> findFontCollection(const std::string& familyName) {
35 return findFontCollectionInternal(familyName);
36 }
37
addFontMap(std::shared_ptr<FontCollection> && collections)38 void addFontMap(std::shared_ptr<FontCollection>&& collections) {
39 addFontMapInternal(std::move(collections));
40 }
41
getFontSet(std::function<void (const std::vector<std::shared_ptr<Font>> &)> func)42 void getFontSet(std::function<void(const std::vector<std::shared_ptr<Font>>&)> func) {
43 getFontSetInternal(func);
44 }
45
registerFallback(const std::string & familyName,const std::shared_ptr<FontCollection> & fc)46 void registerFallback(const std::string& familyName,
47 const std::shared_ptr<FontCollection>& fc) {
48 registerFallbackInternal(familyName, fc);
49 }
50
registerDefault(const std::shared_ptr<FontCollection> & fc)51 void registerDefault(const std::shared_ptr<FontCollection>& fc) { registerDefaultInternal(fc); }
52 };
53
TEST(SystemFontsTest,registerAndLookup)54 TEST(SystemFontsTest, registerAndLookup) {
55 TestableSystemFonts systemFonts;
56 auto fc = buildFontCollection("Ascii.ttf");
57 systemFonts.registerFallback("sans", fc);
58 EXPECT_EQ(fc, systemFonts.findFontCollection("sans"));
59 }
60
TEST(SystemFontsTest,registerDefaultAndLookup)61 TEST(SystemFontsTest, registerDefaultAndLookup) {
62 TestableSystemFonts systemFonts;
63 auto fc = buildFontCollection("Ascii.ttf");
64 systemFonts.registerDefault(fc);
65 EXPECT_EQ(fc, systemFonts.findFontCollection("unknown-name"));
66 }
67
TEST(SystemFontsTest,registerDefaultAndFallback)68 TEST(SystemFontsTest, registerDefaultAndFallback) {
69 TestableSystemFonts systemFonts;
70 auto fc1 = buildFontCollection("Ascii.ttf");
71 auto fc2 = buildFontCollection("Bold.ttf");
72 systemFonts.registerDefault(fc1);
73 systemFonts.registerFallback("sans", fc2);
74 EXPECT_EQ(fc1, systemFonts.findFontCollection("unknown-name"));
75 EXPECT_EQ(fc2, systemFonts.findFontCollection("sans"));
76 }
77
TEST(SystemFontsTest,updateDefaultAndFallback)78 TEST(SystemFontsTest, updateDefaultAndFallback) {
79 TestableSystemFonts systemFonts;
80 auto fc1 = buildFontCollection("Ascii.ttf");
81 auto fc2 = buildFontCollection("Bold.ttf");
82 systemFonts.registerDefault(fc1);
83 systemFonts.registerFallback("sans", fc2);
84 systemFonts.registerDefault(fc2);
85 systemFonts.registerFallback("sans", fc1);
86 EXPECT_EQ(fc2, systemFonts.findFontCollection("unknown-name"));
87 EXPECT_EQ(fc1, systemFonts.findFontCollection("sans"));
88 }
89
TEST(SystemFontTest,getAvailableFont_dedupFonts)90 TEST(SystemFontTest, getAvailableFont_dedupFonts) {
91 TestableSystemFonts systemFonts;
92 auto asciiFamily = buildFontFamily("Ascii.ttf");
93 auto boldFamily = buildFontFamily("Bold.ttf");
94 auto boldItalicFamily = buildFontFamily("BoldItalic.ttf");
95
96 auto fc1Families = std::vector<std::shared_ptr<FontFamily>>{asciiFamily, boldItalicFamily};
97 auto fc2Families = std::vector<std::shared_ptr<FontFamily>>{boldFamily, boldItalicFamily};
98 auto fc1 = std::make_shared<FontCollection>(std::move(fc1Families));
99 auto fc2 = std::make_shared<FontCollection>(std::move(fc2Families));
100
101 systemFonts.addFontMap(std::move(fc1));
102 systemFonts.addFontMap(std::move(fc2));
103
104 systemFonts.getFontSet([](const std::vector<std::shared_ptr<Font>>& fonts) {
105 EXPECT_EQ(3u, fonts.size()); // Ascii, Bold and BoldItalic
106 std::unordered_set<std::string> fontPaths;
107 for (const auto& font : fonts) {
108 fontPaths.insert(getBasename(font->typeface()->GetFontPath()));
109 }
110
111 EXPECT_TRUE(fontPaths.find("Ascii.ttf") != fontPaths.end());
112 EXPECT_TRUE(fontPaths.find("Bold.ttf") != fontPaths.end());
113 EXPECT_TRUE(fontPaths.find("BoldItalic.ttf") != fontPaths.end());
114 });
115 }
116
117 } // namespace
118 } // namespace minikin
119