1 /*
2 * Copyright (c) 2024-2025 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
18 #include "gtest/gtest.h"
19 #include "skia_adapter/skia_text_blob.h"
20
21 #include "text/rs_xform.h"
22 #include "text/text_blob.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class SkiaTextBlobTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void SkiaTextBlobTest::SetUpTestCase() {}
TearDownTestCase()39 void SkiaTextBlobTest::TearDownTestCase() {}
SetUp()40 void SkiaTextBlobTest::SetUp() {}
TearDown()41 void SkiaTextBlobTest::TearDown() {}
42
43 /**
44 * @tc.name: MakeFromText001
45 * @tc.desc: Test MakeFromText
46 * @tc.type: FUNC
47 * @tc.require:I91EDT
48 */
49 HWTEST_F(SkiaTextBlobTest, MakeFromText001, TestSize.Level1)
50 {
51 const char* str = "asdf";
52 Font font;
53 font.SetSize(100);
54 auto textblob = SkiaTextBlob::MakeFromText(str, strlen(str), font, TextEncoding::UTF8);
55 ASSERT_TRUE(textblob != nullptr);
56 }
57
58 /**
59 * @tc.name: MakeFromRSXform001
60 * @tc.desc: Test MakeFromRSXform
61 * @tc.type: FUNC
62 * @tc.require:I91EDT
63 */
64 HWTEST_F(SkiaTextBlobTest, MakeFromRSXform001, TestSize.Level1)
65 {
66 const char* str = "asdf";
67 Font font;
68 font.SetSize(100);
69 RSXform xform[] = { RSXform::Make(10, 10, 10, 10) }; // 10: cos, sin, tx, ty
70 auto textblob = SkiaTextBlob::MakeFromRSXform(str, strlen(str), xform, font, TextEncoding::UTF8);
71 ASSERT_TRUE(textblob != nullptr);
72 }
73
74 /**
75 * @tc.name: MakeFromPosText001
76 * @tc.desc: Test MakeFromPosText
77 * @tc.type: FUNC
78 * @tc.require:I91EDT
79 */
80 HWTEST_F(SkiaTextBlobTest, MakeFromPosText001, TestSize.Level1)
81 {
82 const char* str = "as";
83 Font font;
84 font.SetSize(100);
85 Point p1 { 0, 0 };
86 Point p2 { 1, 1 };
87 Point pos[] = { p1, p2 };
88 auto textblob = SkiaTextBlob::MakeFromPosText(str, strlen(str), pos, font, TextEncoding::UTF8);
89 ASSERT_TRUE(textblob != nullptr);
90 }
91
92 /**
93 * @tc.name: GetDrawingGlyphIDforTextBlob001
94 * @tc.desc: Test GetDrawingGlyphIDforTextBlob
95 * @tc.type: FUNC
96 * @tc.require:I91EDT
97 */
98 HWTEST_F(SkiaTextBlobTest, GetDrawingGlyphIDforTextBlob001, TestSize.Level1)
99 {
100 const char* str = "asdf";
101 Font font;
102 font.SetSize(100);
103 auto textblob = SkiaTextBlob::MakeFromText(str, strlen(str), font, TextEncoding::UTF8);
104 ASSERT_TRUE(textblob != nullptr);
105 std::vector<uint16_t> glyphIds { 0, 1 };
106 SkiaTextBlob::GetDrawingGlyphIDforTextBlob(textblob.get(), glyphIds);
107 }
108
109 /**
110 * @tc.name: GetDrawingPathforTextBlob001
111 * @tc.desc: Test GetDrawingPathforTextBlob
112 * @tc.type: FUNC
113 * @tc.require:I91EDT
114 */
115 HWTEST_F(SkiaTextBlobTest, GetDrawingPathforTextBlob001, TestSize.Level1)
116 {
117 uint16_t glyphId = 1;
118 const char* str = "asdf";
119 Font font;
120 font.SetSize(100);
121 RSXform xform[] = {RSXform::Make(10, 10, 10, 10)};
122 auto blob = SkiaTextBlob::MakeFromRSXform(str, strlen(str), xform, font, TextEncoding::UTF8);
123 ASSERT_TRUE(blob != nullptr);
124 auto path = SkiaTextBlob::GetDrawingPathforTextBlob(glyphId, blob.get());
125 ASSERT_TRUE(path.IsValid() == false);
126 }
127
128 /**
129 * @tc.name: GetDrawingPointsForTextBlob001
130 * @tc.desc: Test GetDrawingPointsForTextBlob
131 * @tc.type: FUNC
132 * @tc.require:I91EDT
133 */
134 HWTEST_F(SkiaTextBlobTest, GetDrawingPointsForTextBlob001, TestSize.Level1)
135 {
136 const char* str = "asdf";
137 Font font;
138 font.SetSize(100);
139 Point p1 { 0, 0 };
140 Point p2 { 1, 1 };
141 Point pos[] = { p1, p2 };
142 auto blob = SkiaTextBlob::MakeFromPosText(str, strlen(str), pos, font, TextEncoding::UTF8);
143 ASSERT_TRUE(blob != nullptr);
144 std::vector<Point> points;
145 SkiaTextBlob::GetDrawingPointsForTextBlob(blob.get(), points);
146 ASSERT_TRUE(points[0].GetX() == 0);
147 ASSERT_TRUE(points[0].GetY() == 0);
148 ASSERT_TRUE(points[1].GetX() == 1);
149 ASSERT_TRUE(points[1].GetY() == 1);
150 }
151
152 /**
153 * @tc.name: GetIntercepts001
154 * @tc.desc: Test GetIntercepts
155 * @tc.type: FUNC
156 * @tc.require:IAKGJ7
157 */
158 HWTEST_F(SkiaTextBlobTest, GetIntercepts001, TestSize.Level1)
159 {
160 const char* str = "asdf";
161 Font font;
162 font.SetSize(100);
163 Point p1 { 100, 100 }; // 100, 100 means point coordinate
164 Point p2 { 300, 300 }; // 300, 300 means point coordinate
165 Point pos[] = { p1, p2 };
166 auto textBlob = SkiaTextBlob::MakeFromPosText(str, strlen(str), pos, font, TextEncoding::UTF8);
167 ASSERT_TRUE(textBlob != nullptr);
168 auto skiaBlobImpl = textBlob->GetImpl<SkiaTextBlob>();
169 ASSERT_TRUE(skiaBlobImpl != nullptr);
170 float bounds[2] = {100, 300}; // 100, 300 means lower and upper line parallel to the advance
171 float intervals[5] = {0.f}; // 5 means intervals array size
172 Paint paint;
173 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
174 paint.SetPathEffect(PathEffect::CreateCornerPathEffect(10)); // 10 means radius
175 int retIntercepts = 0;
176 retIntercepts = skiaBlobImpl->GetIntercepts(bounds, intervals, &paint);
177 ASSERT_TRUE(retIntercepts != 0);
178 }
179 } // namespace Drawing
180 } // namespace Rosen
181 } // namespace OHOS