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 "gtest/gtest.h"
17 #include "font_collection.h"
18 #include "paragraph_builder.h"
19 #include "paragraph_style.h"
20 #include "run_impl.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::Rosen;
25 using namespace OHOS::Rosen::Drawing;
26 using namespace OHOS::Rosen::SPText;
27
28 namespace txt {
29 class RunTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 static inline std::shared_ptr<Paragraph> paragraph_ = nullptr;
34 static inline std::vector<std::unique_ptr<SPText::Run>> runs_;
35 static inline std::unique_ptr<SPText::RunImpl> runImpl_ = nullptr;
36 };
37
SetUpTestCase()38 void RunTest::SetUpTestCase()
39 {
40 ParagraphStyle paragraphStyle;
41 std::shared_ptr<FontCollection> fontCollection = std::make_shared<FontCollection>();
42 if (!fontCollection) {
43 std::cout << "RunTest::SetUpTestCase error fontCollection is nullptr" << std::endl;
44 return;
45 }
46 fontCollection->SetupDefaultFontManager();
47 std::shared_ptr<ParagraphBuilder> paragraphBuilder = ParagraphBuilder::Create(paragraphStyle, fontCollection);
48 if (!paragraphBuilder) {
49 std::cout << "RunTest::SetUpTestCase error paragraphBuilder is nullptr" << std::endl;
50 return;
51 }
52 std::u16string text(u"RunTest");
53 paragraphBuilder->AddText(text);
54 paragraph_ = paragraphBuilder->Build();
55 if (!paragraph_) {
56 std::cout << "RunTest::SetUpTestCase error paragraph_ is nullptr" << std::endl;
57 return;
58 }
59 // 50 just for test
60 paragraph_->Layout(50);
61 Canvas canvas;
62 paragraph_->Paint(&canvas, 0.0, 0.0);
63 std::vector<std::unique_ptr<SPText::TextLineBase>> textLineBases = paragraph_->GetTextLines();
64 if (!textLineBases.size() || !textLineBases.at(0)) {
65 std::cout << "RunTest::SetUpTestCase error textLineBases variable acquisition exception " << std::endl;
66 return;
67 }
68 runs_ = textLineBases.at(0)->GetGlyphRuns();
69 if (!runs_.size() || !runs_.at(0)) {
70 std::cout << "RunTest::SetUpTestCase error runs_ variable acquisition exception" << std::endl;
71 }
72
73 std::vector<PaintRecord> paints;
74 std::unique_ptr<skia::textlayout::RunBase> runBase = nullptr;
75 runImpl_ = std::make_unique<SPText::RunImpl>(std::move(runBase), paints);
76 if (!runImpl_) {
77 std::cout << "RunTest::SetUpTestCase error runImpl_ variable acquisition exception" << std::endl;
78 }
79 }
80
TearDownTestCase()81 void RunTest::TearDownTestCase()
82 {
83 }
84
85 /*
86 * @tc.name: RunTest001
87 * @tc.desc: test for GetFont
88 * @tc.type: FUNC
89 */
90 HWTEST_F(RunTest, RunTest001, TestSize.Level1)
91 {
92 EXPECT_EQ(paragraph_ != nullptr, true);
93 EXPECT_EQ(runs_.size() != 0, true);
94 EXPECT_EQ(runs_.at(0) != nullptr, true);
95 EXPECT_EQ(runs_.at(0)->GetFont().GetSize() > 0, true);
96 }
97
98 /*
99 * @tc.name: RunTest002
100 * @tc.desc: test for GetGlyphCount
101 * @tc.type: FUNC
102 */
103 HWTEST_F(RunTest, RunTest002, TestSize.Level1)
104 {
105 EXPECT_EQ(paragraph_ != nullptr, true);
106 EXPECT_EQ(runs_.size() != 0, true);
107 EXPECT_EQ(runs_.at(0) != nullptr, true);
108 EXPECT_EQ(runs_.at(0)->GetGlyphCount() > 0, true);
109 }
110
111 /*
112 * @tc.name: RunTest003
113 * @tc.desc: test for GetGlyphs
114 * @tc.type: FUNC
115 */
116 HWTEST_F(RunTest, RunTest003, TestSize.Level1)
117 {
118 EXPECT_EQ(paragraph_ != nullptr, true);
119 EXPECT_EQ(runs_.size() != 0, true);
120 EXPECT_EQ(runs_.at(0) != nullptr, true);
121 EXPECT_EQ(runs_.at(0)->GetGlyphs().size() > 0, true);
122 }
123
124 /*
125 * @tc.name: RunTest004
126 * @tc.desc: test for GetPositions
127 * @tc.type: FUNC
128 */
129 HWTEST_F(RunTest, RunTest004, TestSize.Level1)
130 {
131 EXPECT_EQ(paragraph_ != nullptr, true);
132 EXPECT_EQ(runs_.size() != 0, true);
133 EXPECT_EQ(runs_.at(0) != nullptr, true);
134 EXPECT_EQ(runs_.at(0)->GetPositions().size() > 0, true);
135 }
136
137 /*
138 * @tc.name: RunTest005
139 * @tc.desc: test for GetOffsets
140 * @tc.type: FUNC
141 */
142 HWTEST_F(RunTest, RunTest005, TestSize.Level1)
143 {
144 EXPECT_EQ(paragraph_ != nullptr, true);
145 EXPECT_EQ(runs_.size() != 0, true);
146 EXPECT_EQ(runs_.at(0) != nullptr, true);
147 EXPECT_EQ(runs_.at(0)->GetOffsets().size() > 0, true);
148 }
149
150 /*
151 * @tc.name: RunTest006
152 * @tc.desc: test for Paint
153 * @tc.type: FUNC
154 */
155 HWTEST_F(RunTest, RunTest006, TestSize.Level1)
156 {
157 EXPECT_EQ(paragraph_ != nullptr, true);
158 EXPECT_EQ(runs_.size() != 0, true);
159 EXPECT_EQ(runs_.at(0) != nullptr, true);
160 Canvas canvas;
161 runs_.at(0)->Paint(&canvas, 0.0, 0.0);
162 }
163
164 /*
165 * @tc.name: RunTest007
166 * @tc.desc: test for GetFont
167 * @tc.type: FUNC
168 */
169 HWTEST_F(RunTest, RunTest007, TestSize.Level1)
170 {
171 EXPECT_EQ(runImpl_ != nullptr, true);
172 EXPECT_EQ(runImpl_->GetFont().GetSize() > 0, true);
173 }
174
175 /*
176 * @tc.name: RunTest008
177 * @tc.desc: test for GetGlyphCount
178 * @tc.type: FUNC
179 */
180 HWTEST_F(RunTest, RunTest008, TestSize.Level1)
181 {
182 EXPECT_EQ(runImpl_ != nullptr, true);
183 EXPECT_EQ(runImpl_->GetGlyphCount(), 0);
184 }
185
186 /*
187 * @tc.name: RunTest009
188 * @tc.desc: test for GetGlyphs
189 * @tc.type: FUNC
190 */
191 HWTEST_F(RunTest, RunTest009, TestSize.Level1)
192 {
193 EXPECT_EQ(runImpl_ != nullptr, true);
194 EXPECT_EQ(runImpl_->GetGlyphs().size(), 0);
195 }
196
197 /*
198 * @tc.name: RunTest010
199 * @tc.desc: test for GetPositions
200 * @tc.type: FUNC
201 */
202 HWTEST_F(RunTest, RunTest010, TestSize.Level1)
203 {
204 EXPECT_EQ(runImpl_ != nullptr, true);
205 EXPECT_EQ(runImpl_->GetPositions().size(), 0);
206 }
207
208 /*
209 * @tc.name: RunTest011
210 * @tc.desc: test for GetOffsets
211 * @tc.type: FUNC
212 */
213 HWTEST_F(RunTest, RunTest011, TestSize.Level1)
214 {
215 EXPECT_EQ(runImpl_ != nullptr, true);
216 EXPECT_EQ(runImpl_->GetOffsets().size(), 0);
217 }
218
219 /*
220 * @tc.name: RunTest012
221 * @tc.desc: test for Paint
222 * @tc.type: FUNC
223 */
224 HWTEST_F(RunTest, RunTest012, TestSize.Level1)
225 {
226 EXPECT_EQ(runImpl_ != nullptr, true);
227 runImpl_->Paint(nullptr, 0.0, 0.0);
228 }
229 } // namespace txt