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