1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstddef>
17 #include "gtest/gtest.h"
18 #include "skia_adapter/skia_font.h"
19 #include "text/font.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class SkiaFontTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp() override;
32 void TearDown() override;
33 };
34
SetUpTestCase()35 void SkiaFontTest::SetUpTestCase() {}
TearDownTestCase()36 void SkiaFontTest::TearDownTestCase() {}
SetUp()37 void SkiaFontTest::SetUp() {}
TearDown()38 void SkiaFontTest::TearDown() {}
39
40 /**
41 * @tc.name: SkiaFont001
42 * @tc.desc: Test SkiaFont's constructor
43 * @tc.type: FUNC
44 * @tc.require: I9120P
45 */
46 HWTEST_F(SkiaFontTest, SkiaFont001, TestSize.Level1)
47 {
48 std::shared_ptr<SkiaFont> skiaFont = std::make_shared<SkiaFont>(nullptr, 1, 1, 1);
49 ASSERT_TRUE(skiaFont != nullptr);
50 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
51 skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
52 ASSERT_TRUE(skiaFont != nullptr);
53 }
54
55 /**
56 * @tc.name: SetEdging001
57 * @tc.desc: Test SetEdging
58 * @tc.type: FUNC
59 * @tc.require: I9120P
60 */
61 HWTEST_F(SkiaFontTest, SetEdging001, TestSize.Level1)
62 {
63 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
64 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
65 skiaFont->SetEdging(FontEdging::ALIAS);
66 ASSERT_TRUE(skiaFont->GetFont().getEdging() == SkFont::Edging::kAlias);
67 }
68
69 /**
70 * @tc.name: SetSubpixel001
71 * @tc.desc: Test SetSubpixel
72 * @tc.type: FUNC
73 * @tc.require: I9120P
74 */
75 HWTEST_F(SkiaFontTest, SetSubpixel001, TestSize.Level1)
76 {
77 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
78 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
79 skiaFont->SetSubpixel(true);
80 ASSERT_TRUE(skiaFont->GetFont().isSubpixel());
81 }
82
83 /**
84 * @tc.name: SetHinting001
85 * @tc.desc: Test SetHinting
86 * @tc.type: FUNC
87 * @tc.require: I9120P
88 */
89 HWTEST_F(SkiaFontTest, SetHinting001, TestSize.Level1)
90 {
91 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
92 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
93 skiaFont->SetHinting(FontHinting::NONE);
94 ASSERT_TRUE(skiaFont->GetFont().getHinting() == SkFontHinting::kNone);
95 }
96
97 /**
98 * @tc.name: SetTypeface001
99 * @tc.desc: Test SetTypeface
100 * @tc.type: FUNC
101 * @tc.require: I9120P
102 */
103 HWTEST_F(SkiaFontTest, SetTypeface001, TestSize.Level1)
104 {
105 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
106 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
107 skiaFont->SetTypeface(nullptr);
108 ASSERT_TRUE(skiaFont->GetTypeface() != nullptr);
109 }
110
111 /**
112 * @tc.name: SetScaleX001
113 * @tc.desc: Test SetScaleX
114 * @tc.type: FUNC
115 * @tc.require: I9120P
116 */
117 HWTEST_F(SkiaFontTest, SetScaleX001, TestSize.Level1)
118 {
119 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
120 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
121 skiaFont->SetScaleX(1);
122 ASSERT_TRUE(skiaFont->GetFont().getScaleX() == 1);
123 }
124
125 /**
126 * @tc.name: SetLinearMetrics001
127 * @tc.desc: Test SetLinearMetrics
128 * @tc.type: FUNC
129 * @tc.require: I9120P
130 */
131 HWTEST_F(SkiaFontTest, SetLinearMetrics001, TestSize.Level1)
132 {
133 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
134 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
135 skiaFont->SetLinearMetrics(true);
136 ASSERT_TRUE(skiaFont->GetFont().isLinearMetrics());
137 }
138
139 /**
140 * @tc.name: GetMetrics001
141 * @tc.desc: Test GetMetrics
142 * @tc.type: FUNC
143 * @tc.require: I9120P
144 */
145 HWTEST_F(SkiaFontTest, GetMetrics001, TestSize.Level1)
146 {
147 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
148 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
149 auto metrics = skiaFont->GetMetrics(nullptr);
150 ASSERT_TRUE(metrics > 0);
151 }
152
153 /**
154 * @tc.name: GetWidths001
155 * @tc.desc: Test GetWidths
156 * @tc.type: FUNC
157 * @tc.require: I9120P
158 */
159 HWTEST_F(SkiaFontTest, GetWidths001, TestSize.Level1)
160 {
161 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
162 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
163 uint16_t glyphs[] = { 0, 0 };
164 scalar widths[] = { 0, 0 };
165 skiaFont->GetWidths(glyphs, 2, widths); // 2:count
166 skiaFont->GetWidths(glyphs, 2, widths, nullptr); // 2:count
167 Rect rect1;
168 Rect rect2;
169 Rect bounds[] = { rect1, rect2 };
170 skiaFont->GetWidths(glyphs, 2, widths, bounds);
171 ASSERT_TRUE(rect1.GetWidth() >= 0);
172 }
173
174 /**
175 * @tc.name: GetSize001
176 * @tc.desc: Test GetSize
177 * @tc.type: FUNC
178 * @tc.require: I9120P
179 */
180 HWTEST_F(SkiaFontTest, GetSize001, TestSize.Level1)
181 {
182 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
183 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
184 auto size = skiaFont->GetSize();
185 ASSERT_TRUE(size > 0);
186 }
187
188 /**
189 * @tc.name: GetTypeface001
190 * @tc.desc: Test GetTypeface
191 * @tc.type: FUNC
192 * @tc.require: I9120P
193 */
194 HWTEST_F(SkiaFontTest, GetTypeface001, TestSize.Level1)
195 {
196 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
197 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
198 auto typeface2 = skiaFont->GetTypeface();
199 ASSERT_TRUE(typeface2 != nullptr);
200 }
201
202 /**
203 * @tc.name: MeasureText001
204 * @tc.desc: Test MeasureText
205 * @tc.type: FUNC
206 * @tc.require: I9120P
207 */
208 HWTEST_F(SkiaFontTest, MeasureText001, TestSize.Level1)
209 {
210 std::shared_ptr<Typeface> typeface = Typeface::MakeDefault();
211 auto skiaFont = std::make_shared<SkiaFont>(typeface, 1, 1, 1);
212 auto size = skiaFont->MeasureText("11", 2, TextEncoding::UTF8, nullptr); // 2:byteLength
213 ASSERT_TRUE(size > 0);
214 }
215 } // namespace Drawing
216 } // namespace Rosen
217 } // namespace OHOS