1 /*
2  * Copyright (c) 2020-2022 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 <climits>
17 #include <codecvt>
18 #include <gtest/gtest.h>
19 #include <locale>
20 #include <string>
21 #include "common/text.h"
22 #include "common/spannable_string.h"
23 #if !(defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT)
24 #include "common/ui_text_language.h"
25 #endif
26 #include "gfx_utils/color.h"
27 #include "gfx_utils/list.h"
28 #include "gfx_utils/vector.h"
29 
30 using namespace testing::ext;
31 namespace OHOS {
32 class TextTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     static Text* text_;
37 };
38 
39 Text* TextTest::text_ = nullptr;
40 
SetUpTestCase(void)41 void TextTest::SetUpTestCase(void)
42 {
43     if (text_ == nullptr) {
44         text_ = new Text();
45     }
46 }
47 
TearDownTestCase(void)48 void TextTest::TearDownTestCase(void)
49 {
50     if (text_ != nullptr) {
51         delete text_;
52         text_ = nullptr;
53     }
54 }
55 
56 /**
57  * @tc.name: TextSetText_001
58  * @tc.desc: Verify SetText function, equal.
59  * @tc.type: FUNC
60  * @tc.require: AR000DSMQ1
61  */
62 HWTEST_F(TextTest, TextSetText_001, TestSize.Level0)
63 {
64     if (text_ == nullptr) {
65         EXPECT_NE(0, 0);
66         return;
67     }
68     const char* text = "unit test text";
69     text_->SetText(text);
70     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
71 }
72 
73 /**
74  * @tc.name: TextSetDirect_001
75  * @tc.desc: Verify SetDirect function, equal.
76  * @tc.type: FUNC
77  * @tc.require: AR000DSMQ1
78  */
79 HWTEST_F(TextTest, TextSetDirect_001, TestSize.Level0)
80 {
81     if (text_ == nullptr) {
82         EXPECT_NE(0, 0);
83         return;
84     }
85     UITextLanguageDirect direct = UITextLanguageDirect::TEXT_DIRECT_LTR;
86     text_->SetDirect(direct);
87     EXPECT_EQ(text_->GetDirect(), direct);
88     direct = UITextLanguageDirect::TEXT_DIRECT_RTL;
89     text_->SetDirect(direct);
90     EXPECT_EQ(text_->GetDirect(), direct);
91 }
92 
93 /**
94  * @tc.name: TextSetAlign_001
95  * @tc.desc: Verify SetAlign function, equal.
96  * @tc.type: FUNC
97  * @tc.require: AR000DSMQ1
98  */
99 HWTEST_F(TextTest, TextSetAlign_001, TestSize.Level1)
100 {
101     if (text_ == nullptr) {
102         EXPECT_NE(0, 0);
103         return;
104     }
105     text_->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_TOP);
106     EXPECT_EQ(text_->IsNeedRefresh(), true);
107     EXPECT_EQ(text_->GetHorAlign(), TEXT_ALIGNMENT_LEFT);
108     EXPECT_EQ(text_->GetVerAlign(), TEXT_ALIGNMENT_TOP);
109 }
110 
111 /**
112  * @tc.name: TextSetExpand_001
113  * @tc.desc: Verify SetExpand function, equal.
114  * @tc.type: FUNC
115  * @tc.require: AR000DSMQ1
116  */
117 HWTEST_F(TextTest, TextSetExpand_001, TestSize.Level1)
118 {
119     if (text_ == nullptr) {
120         EXPECT_NE(0, 0);
121         return;
122     }
123     EXPECT_EQ(text_->IsExpandWidth(), false);
124     text_->SetExpandWidth(true);
125     EXPECT_EQ(text_->IsExpandWidth(), true);
126 
127     EXPECT_EQ(text_->IsExpandHeight(), false);
128     text_->SetExpandHeight(true);
129     EXPECT_EQ(text_->IsExpandHeight(), true);
130 }
131 
132 HWTEST_F(TextTest, TextSetBackgroundColorSpan_001, TestSize.Level1)
133 {
134     if (text_ == nullptr) {
135         EXPECT_NE(0, 0);
136         return;
137     }
138     EXPECT_EQ(text_->GetBackgroundColorSpan().Size(), 0);
139     text_->SetBackgroundColorSpan(Color::Red(), 0, 2);
140     EXPECT_EQ(text_->GetBackgroundColorSpan().Size(), 1);
141 }
142 
143 HWTEST_F(TextTest, TextSetForegroundColorSpan_001, TestSize.Level1)
144 {
145     if (text_ == nullptr) {
146         EXPECT_NE(0, 0);
147         return;
148     }
149     EXPECT_EQ(text_->GetForegroundColorSpan().Size(), 0);
150     text_->SetForegroundColorSpan(Color::Blue(), 1, 3);
151     EXPECT_EQ(text_->GetForegroundColorSpan().Size(), 1);
152 }
153 
154 HWTEST_F(TextTest, TextSetLineBackgroundSpan_001, TestSize.Level1)
155 {
156     if (text_ == nullptr) {
157         EXPECT_NE(0, 0);
158         return;
159     }
160     EXPECT_EQ(text_->GetLineBackgroundSpan().Size(), 0);
161     text_->SetLineBackgroundSpan(Color::Blue(), 1, 3);
162     EXPECT_EQ(text_->GetLineBackgroundSpan().Size(), 1);
163 }
164 
165 HWTEST_F(TextTest, TextSetAbsoluteSizeSpan_001, TestSize.Level1)
166 {
167     Text* text = new Text();
168 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
169 #else
170     text->SetFontId(16);
171 #endif
172     text->SetAbsoluteSizeSpan(1, 2, 38);
173     EXPECT_EQ(text->GetSizeSpan(), 0);
174     delete text;
175     text = nullptr;
176 }
177 
178 HWTEST_F(TextTest, TextSetRelativeSpan_001, TestSize.Level1)
179 {
180     Text* text = new Text();
181     text->SetRelativeSizeSpan(1, 2, 1.9f);
182     EXPECT_EQ(text->GetSizeSpan(), 0);
183     delete text;
184     text = nullptr;
185 }
186 
187 #if defined(ENABLE_TEXT_STYLE) && ENABLE_TEXT_STYLE
188 HWTEST_F(TextTest, TextSetStyleSpan_001, TestSize.Level1)
189 {
190     SpannableString spannableString("图形子系统测试正常粗体斜体粗斜体");
191     spannableString.SetTextStyle(TEXT_STYLE_ITALIC, 11, 13);
192     spannableString.SetTextStyle(TEXT_STYLE_BOLD, 9, 11);
193     spannableString.SetTextStyle(TEXT_STYLE_BOLD_ITALIC, 13, 16);
194     EXPECT_EQ(spannableString.styleList_.Size(), 3);
195 }
196 #endif
197 
198 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
199 HWTEST_F(TextTest, TextSetText_002, TestSize.Level0)
200 {
201     if (text_ == nullptr) {
202         EXPECT_NE(0, 0);
203         return;
204     }
205     const char* text = "abcd";
206     text_->SetText(text);
207     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
208 }
209 #else
210 HWTEST_F(TextTest, TextSetText_002, TestSize.Level0)
211 {
212     if (text_ == nullptr) {
213         EXPECT_NE(0, 0);
214         return;
215     }
216 
217     const char* text = "\xEF\x80\x80\xEF\x80\x81\xEF\x80\x82";
218     text_->SetText(text);
219     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
220 }
221 #endif
222 
223 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
224 HWTEST_F(TextTest, TextSetText_003, TestSize.Level0)
225 {
226     if (text_ == nullptr) {
227         EXPECT_NE(0, 0);
228         return;
229     }
230     const char* text = "鸿蒙操作系统";
231     text_->SetText(text);
232     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
233 }
234 #else
235 HWTEST_F(TextTest, TextSetText_003, TestSize.Level0)
236 {
237     if (text_ == nullptr) {
238         EXPECT_NE(0, 0);
239         return;
240     }
241 
242     const char* text = "轻量图形子系统\xEF\x80\x80\xEF\x80\x81\xEF\x80\x82鸿蒙操作系統";
243     text_->SetText(text);
244     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
245 }
246 #endif
247 
248 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
249 HWTEST_F(TextTest, TextSetText_004, TestSize.Level1)
250 {
251     if (text_ == nullptr) {
252         EXPECT_NE(0, 0);
253         return;
254     }
255     const char* text = "鸿蒙操作系统abcd";
256     text_->SetText(text);
257     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
258 }
259 #else
260 HWTEST_F(TextTest, TextSetText_004, TestSize.Level1)
261 {
262     if (text_ == nullptr) {
263         EXPECT_NE(0, 0);
264         return;
265     }
266 
267     const char* text = "鸿蒙操作系統轻量图形子系统TDD测试用例\xEF\x80\x80\xEF\x80\x81\xEF\x80\x82";
268     text_->SetText(text);
269     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
270 }
271 #endif
272 
273 
274 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
275 HWTEST_F(TextTest, TextSetText_005, TestSize.Level1)
276 {
277     if (text_ == nullptr) {
278         EXPECT_NE(0, 0);
279         return;
280     }
281     const char* text = "鸿蒙abcd操作系统";
282     text_->SetText(text);
283     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
284 }
285 #else
286 HWTEST_F(TextTest, TextSetText_005, TestSize.Level1)
287 {
288     if (text_ == nullptr) {
289         EXPECT_NE(0, 0);
290         return;
291     }
292 
293     const char* text = "\xEF\x80\x80鸿蒙操作系統\xEF\x80\x81\xEF\x80\x82轻量图形子系统TDD测试用例";
294     text_->SetText(text);
295     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
296 }
297 #endif
298 
299 #if defined(ENABLE_VECTOR_FONT) && ENABLE_VECTOR_FONT
300 HWTEST_F(TextTest, TextSetText_006, TestSize.Level1)
301 {
302     if (text_ == nullptr) {
303         EXPECT_NE(0, 0);
304         return;
305     }
306     const char* text = "鸿蒙ab轻量图形子系统cd操作系统";
307     text_->SetText(text);
308     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
309 }
310 #else
311 HWTEST_F(TextTest, TextSetText_006, TestSize.Level1)
312 {
313     if (text_ == nullptr) {
314         EXPECT_NE(0, 0);
315         return;
316     }
317 
318     const char* text = "轻量图形子系统TDD测试用例\xEF\x80\x80鸿蒙操作系統\xEF\x80\x81测试符号\xEF\x80\x82";
319     text_->SetText(text);
320     EXPECT_EQ(strcmp(text_->GetText(), text), 0);
321 }
322 #endif
323 
324 /**
325  * @tc.name: TextGetShapingFontId_001
326  * @tc.desc: Verify GetShapingFontId function, equal.
327  * @tc.type: FUNC
328  * @tc.require: AR000H8BB3
329  */
330 HWTEST_F(TextTest, TextGetShapingFontId_001, TestSize.Level1)
331 {
332     if (text_ == nullptr) {
333         EXPECT_NE(0, 0);
334         return;
335     }
336     EXPECT_EQ(text_->GetShapingFontId(), 0);
337 }
338 
339 /**
340  * @tc.name: TextGetCodePointNum_001
341  * @tc.desc: Verify GetCodePointNum function, equal.
342  * @tc.type: FUNC
343  * @tc.require: AR000H8BB3
344  */
345 HWTEST_F(TextTest, TextGetCodePointNum_001, TestSize.Level1)
346 {
347     if (text_ == nullptr) {
348         EXPECT_NE(0, 0);
349         return;
350     }
351     EXPECT_EQ(text_->GetCodePointNum(), 0);
352 }
353 
354 /**
355  * @tc.name: TextGetCodePoints_001
356  * @tc.desc: Verify GetCodePoints function, equal.
357  * @tc.type: FUNC
358  * @tc.require: AR000H8BB3
359  */
360 HWTEST_F(TextTest, TextGetCodePoints_001, TestSize.Level1)
361 {
362     if (text_ == nullptr) {
363         EXPECT_NE(0, 0);
364         return;
365     }
366     EXPECT_EQ(text_->GetCodePoints(), nullptr);
367 }
368 } // namespace OHOS
369