1 /*
2 * Copyright (c) 2020-2021 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 "components/ui_label.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
24 const int16_t INIT_WIDTH = 100;
25 const int16_t INIT_HEIGHT = 150;
26 }
27
28 class UILabelTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 static UILabel* label_;
33 };
34
35 UILabel* UILabelTest::label_ = nullptr;
36
SetUpTestCase(void)37 void UILabelTest::SetUpTestCase(void)
38 {
39 if (label_ == nullptr) {
40 label_ = new UILabel();
41 }
42 }
43
TearDownTestCase(void)44 void UILabelTest::TearDownTestCase(void)
45 {
46 if (label_ != nullptr) {
47 delete label_;
48 label_ = nullptr;
49 }
50 }
51
52 /**
53 * @tc.name: UILabelGetViewType_001
54 * @tc.desc: Verify GetViewType and GetHeight function.
55 * @tc.type: FUNC
56 * @tc.require: AR000DSMQ1
57 */
58 HWTEST_F(UILabelTest, UILabelGetViewType_001, TestSize.Level1)
59 {
60 if (label_ == nullptr) {
61 EXPECT_EQ(1, 0);
62 return;
63 }
64 EXPECT_EQ(label_->GetViewType(), UI_LABEL);
65 }
66
67 /**
68 * @tc.name: UILabelResize_001
69 * @tc.desc: Verify Resize function.
70 * @tc.type: FUNC
71 * @tc.require: AR000DSMQ1
72 */
73 HWTEST_F(UILabelTest, UILabelResize_001, TestSize.Level1)
74 {
75 if (label_ == nullptr) {
76 EXPECT_EQ(1, 0);
77 return;
78 }
79 label_->Resize(INIT_WIDTH, INIT_HEIGHT);
80 EXPECT_EQ(label_->GetWidth(), INIT_WIDTH);
81 EXPECT_EQ(label_->GetHeight(), INIT_HEIGHT);
82 }
83
84 /**
85 * @tc.name: UILabelSetText_001
86 * @tc.desc: Verify SetText function.
87 * @tc.type: FUNC
88 * @tc.require: AR000DSMQ1
89 */
90 HWTEST_F(UILabelTest, UILabelSetText_001, TestSize.Level1)
91 {
92 if (label_ == nullptr) {
93 EXPECT_EQ(1, 0);
94 return;
95 }
96 const char* text = "abc";
97 label_->Resize(INIT_WIDTH, INIT_HEIGHT);
98 label_->SetText(text);
99
100 const char* text0 = label_->GetText();
101 ASSERT_TRUE(text0);
102 EXPECT_EQ(strcmp(text0, text), 0);
103 }
104
105 /**
106 * @tc.name: UILabelSetLineBreakMode_001
107 * @tc.desc: Verify SetLineBreakMode function.
108 * @tc.type: FUNC
109 * @tc.require: AR000DSMQ1
110 */
111 HWTEST_F(UILabelTest, UILabelSetLineBreakMode_001, TestSize.Level0)
112 {
113 if (label_ == nullptr) {
114 EXPECT_EQ(1, 0);
115 return;
116 }
117 const uint8_t lineBreakMode = UILabel::LINE_BREAK_ELLIPSIS;
118
119 label_->SetLineBreakMode(lineBreakMode);
120 EXPECT_EQ(label_->GetLineBreakMode(), lineBreakMode);
121 }
122
123 /**
124 * @tc.name: UILabelSetTextColor_001
125 * @tc.desc: Verify SetTextColor function.
126 * @tc.type: FUNC
127 * @tc.require: AR000DSMQ1
128 */
129 HWTEST_F(UILabelTest, UILabelSetTextColor_001, TestSize.Level1)
130 {
131 if (label_ == nullptr) {
132 EXPECT_EQ(1, 0);
133 return;
134 }
135 ColorType color = Color::White();
136
137 label_->SetTextColor(color);
138 EXPECT_EQ(label_->GetTextColor().full, color.full);
139 }
140
141 /**
142 * @tc.name: UILabelSetLongMode_001
143 * @tc.desc: Verify SetLongMode function.
144 * @tc.type: FUNC
145 * @tc.require: AR000DSMQ1
146 */
147 HWTEST_F(UILabelTest, UILabelSetLongMode_001, TestSize.Level0)
148 {
149 if (label_ == nullptr) {
150 EXPECT_EQ(1, 0);
151 return;
152 }
153 label_->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
154 EXPECT_EQ(label_->GetLineBreakMode(), UILabel::LINE_BREAK_ADAPT);
155 }
156
157 /**
158 * @tc.name: UILabelSetAlign_001
159 * @tc.desc: Verify SetAlign function.
160 * @tc.type: FUNC
161 * @tc.require: AR000DSMQ1
162 */
163 HWTEST_F(UILabelTest, UILabelSetAlign_001, TestSize.Level0)
164 {
165 if (label_ == nullptr) {
166 EXPECT_EQ(1, 0);
167 return;
168 }
169 label_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT, UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
170 EXPECT_EQ(label_->GetHorAlign(), UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
171 EXPECT_EQ(label_->GetVerAlign(), UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
172 }
173
174 /**
175 * @tc.name: UILabelSetDirect_001
176 * @tc.desc: Verify SetDirect function.
177 * @tc.type: FUNC
178 * @tc.require: AR000DSMQ1
179 */
180 HWTEST_F(UILabelTest, UILabelSetDirect_001, TestSize.Level1)
181 {
182 if (label_ == nullptr) {
183 EXPECT_EQ(1, 0);
184 return;
185 }
186 label_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_RTL);
187 EXPECT_EQ(label_->GetDirect(), UITextLanguageDirect::TEXT_DIRECT_RTL);
188 }
189
190 /**
191 * @tc.name: UILabelSetRollStartPos_001
192 * @tc.desc: Verify SetRollStartPos function.
193 * @tc.type: FUNC
194 * @tc.require: AR000DSMQ1
195 */
196 HWTEST_F(UILabelTest, UILabelSetRollStartPos_001, TestSize.Level0)
197 {
198 if (label_ == nullptr) {
199 EXPECT_EQ(1, 0);
200 return;
201 }
202 const int16_t rollStartPos = 50;
203
204 label_->SetRollStartPos(rollStartPos);
205 EXPECT_EQ(label_->GetRollStartPos(), rollStartPos);
206 }
207
208 /**
209 * @tc.name: UILabelSetFont_001
210 * @tc.desc: Verify SetFont function.
211 * @tc.type: FUNC
212 * @tc.require: AR000DSMQ1
213 */
214 HWTEST_F(UILabelTest, UILabelSetFont_001, TestSize.Level1)
215 {
216 if (label_ == nullptr) {
217 EXPECT_EQ(1, 0);
218 return;
219 }
220 uint8_t fontId = label_->GetFontId();
221
222 const uint8_t fontSize = 20; // 20: font size for test
223 label_->SetFont("error_font_name", fontSize);
224
225 EXPECT_EQ(label_->GetFontId(), fontId);
226 }
227 } // namespace OHOS