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/Font.h"
18
19 #include <gtest/gtest.h>
20
21 #include "BufferUtils.h"
22 #include "FontTestUtils.h"
23 #include "FreeTypeMinikinFontForTest.h"
24
25 namespace minikin {
26
27 namespace {
28
getHeapSize()29 size_t getHeapSize() {
30 struct mallinfo info = mallinfo();
31 return info.uordblks;
32 }
33
34 } // namespace
35
TEST(FontTest,BufferTest)36 TEST(FontTest, BufferTest) {
37 FreeTypeMinikinFontForTestFactory::init();
38 auto minikinFont = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath("Ascii.ttf"));
39 std::shared_ptr<Font> original = Font::Builder(minikinFont).build();
40 std::vector<uint8_t> buffer = writeToBuffer<Font>(*original);
41
42 BufferReader reader(buffer.data());
43 Font font(&reader);
44 EXPECT_EQ(minikinFont->GetFontPath(), font.typeface()->GetFontPath());
45 EXPECT_EQ(original->style(), font.style());
46 EXPECT_EQ(original->getLocaleListId(), font.getLocaleListId());
47 // baseFont() should return the same non-null instance when called twice.
48 const auto& baseFont = font.baseFont();
49 EXPECT_NE(nullptr, baseFont);
50 EXPECT_EQ(baseFont, font.baseFont());
51 // typeface() should return the same non-null instance when called twice.
52 const auto& typeface = font.typeface();
53 EXPECT_NE(nullptr, typeface);
54 EXPECT_EQ(typeface, font.typeface());
55 std::vector<uint8_t> newBuffer = writeToBuffer<Font>(font);
56 EXPECT_EQ(buffer, newBuffer);
57 }
58
TEST(FontTest,MoveConstructorTest)59 TEST(FontTest, MoveConstructorTest) {
60 FreeTypeMinikinFontForTestFactory::init();
61 // Note: by definition, only BufferReader-based Font can be moved.
62 auto minikinFont = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath("Ascii.ttf"));
63 std::shared_ptr<Font> original = Font::Builder(minikinFont).build();
64 std::vector<uint8_t> buffer = writeToBuffer<Font>(*original);
65
66 size_t baseHeapSize = getHeapSize();
67 {
68 BufferReader reader(buffer.data());
69 Font moveFrom(&reader);
70 Font moveTo(std::move(moveFrom));
71 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
72 EXPECT_EQ(nullptr, moveTo.mExternalRefsHolder.load());
73 }
74 EXPECT_EQ(baseHeapSize, getHeapSize());
75 {
76 BufferReader reader(buffer.data());
77 Font moveFrom(&reader);
78 std::shared_ptr<MinikinFont> typeface = moveFrom.typeface();
79 Font moveTo(std::move(moveFrom));
80 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
81 EXPECT_EQ(typeface, moveTo.typeface());
82 }
83 EXPECT_EQ(baseHeapSize, getHeapSize());
84 }
85
TEST(FontTest,MoveAssignmentTest)86 TEST(FontTest, MoveAssignmentTest) {
87 FreeTypeMinikinFontForTestFactory::init();
88 // Note: by definition, only BufferReader-based Font can be moved.
89 auto minikinFont = std::make_shared<FreeTypeMinikinFontForTest>(getTestFontPath("Ascii.ttf"));
90 std::shared_ptr<Font> original = Font::Builder(minikinFont).build();
91 std::vector<uint8_t> buffer = writeToBuffer<Font>(*original);
92
93 size_t baseHeapSize = getHeapSize();
94 {
95 // mExternalRefsHolder: null -> null
96 BufferReader reader(buffer.data());
97 Font moveFrom(&reader);
98 BufferReader reader2(buffer.data());
99 Font moveTo(&reader2);
100 moveTo = std::move(moveFrom);
101 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
102 EXPECT_EQ(nullptr, moveTo.mExternalRefsHolder.load());
103 }
104 EXPECT_EQ(baseHeapSize, getHeapSize());
105 {
106 // mExternalRefsHolder: non-null -> null
107 BufferReader reader(buffer.data());
108 Font moveFrom(&reader);
109 std::shared_ptr<MinikinFont> typeface = moveFrom.typeface();
110 BufferReader reader2(buffer.data());
111 Font moveTo(&reader2);
112 moveTo = std::move(moveFrom);
113 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
114 EXPECT_EQ(typeface, moveTo.typeface());
115 }
116 EXPECT_EQ(baseHeapSize, getHeapSize());
117 {
118 // mExternalRefsHolder: null -> non-null
119 BufferReader reader(buffer.data());
120 Font moveFrom(&reader);
121 BufferReader reader2(buffer.data());
122 Font moveTo(&reader2);
123 moveTo.typeface();
124 moveTo = std::move(moveFrom);
125 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
126 EXPECT_EQ(nullptr, moveTo.mExternalRefsHolder.load());
127 }
128 EXPECT_EQ(baseHeapSize, getHeapSize());
129 {
130 // mExternalRefsHolder: non-null -> non-null
131 BufferReader reader(buffer.data());
132 Font moveFrom(&reader);
133 std::shared_ptr<MinikinFont> typeface = moveFrom.typeface();
134 BufferReader reader2(buffer.data());
135 Font moveTo(&reader2);
136 moveTo.typeface();
137 moveTo = std::move(moveFrom);
138 EXPECT_EQ(nullptr, moveFrom.mExternalRefsHolder.load());
139 EXPECT_EQ(typeface, moveTo.typeface());
140 }
141 EXPECT_EQ(baseHeapSize, getHeapSize());
142 }
143
144 } // namespace minikin
145