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_button.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20 #include "font/ui_font.h"
21
22 using namespace testing::ext;
23 namespace OHOS {
24 class UILabelButtonTest : public testing::Test {
25 public:
26 static void SetUpTestCase();
27 static void TearDownTestCase();
28 static UILabelButton* labelBtn_;
29 };
30
31 UILabelButton* UILabelButtonTest::labelBtn_ = nullptr;
32
SetUpTestCase()33 void UILabelButtonTest::SetUpTestCase()
34 {
35 if (labelBtn_ == nullptr) {
36 labelBtn_ = new UILabelButton();
37 }
38 }
39
TearDownTestCase()40 void UILabelButtonTest::TearDownTestCase()
41 {
42 if (labelBtn_ != nullptr) {
43 delete labelBtn_;
44 labelBtn_ = nullptr;
45 }
46 }
47
48 /**
49 * @tc.name: UILabelButtonGetViewType_001
50 * @tc.desc: Verify GetViewType function.
51 * @tc.type: FUNC
52 * @tc.require: AR000EEMQ5
53 */
54 HWTEST_F(UILabelButtonTest, UILabelButtonGetViewType_001, TestSize.Level1)
55 {
56 if (labelBtn_ == nullptr) {
57 EXPECT_NE(0, 0);
58 return;
59 }
60 EXPECT_EQ(labelBtn_->GetViewType(), UI_LABEL_BUTTON);
61 }
62
63 /**
64 * @tc.name: UILabelButtonSetText_001
65 * @tc.desc: Verify SetText function.
66 * @tc.type: FUNC
67 * @tc.require: AR000EEMQ5
68 */
69 HWTEST_F(UILabelButtonTest, UILabelButtonSetText_001, TestSize.Level1)
70 {
71 if (labelBtn_ == nullptr) {
72 EXPECT_NE(0, 0);
73 return;
74 }
75 const char* text = "abc";
76
77 labelBtn_->SetText(text);
78 if (labelBtn_->GetText() == nullptr) {
79 EXPECT_NE(0, 0);
80 return;
81 }
82 EXPECT_EQ(strcmp(labelBtn_->GetText(), text), 0);
83 }
84 /**
85 * @tc.name: UILabelButtonSetLablePosition_001
86 * @tc.desc: Verify SetLabelPosition function.
87 * @tc.type: FUNC
88 * @tc.require: AR000EEMQ5
89 */
90 HWTEST_F(UILabelButtonTest, UILabelButtonSetLablePosition_001, TestSize.Level1)
91 {
92 if (labelBtn_ == nullptr) {
93 EXPECT_NE(0, 0);
94 return;
95 }
96 const int16_t posX = 10;
97 const int16_t posY = 20;
98
99 labelBtn_->SetLabelPosition(posX, posY);
100 EXPECT_EQ(labelBtn_->GetLabelPosition().x, posX);
101 EXPECT_EQ(labelBtn_->GetLabelPosition().y, posY);
102 }
103
104 /**
105 * @tc.name: UILabelButtonSetAlign_001
106 * @tc.desc: Verify SetAlign function.
107 * @tc.type: FUNC
108 * @tc.require: AR000EEMQ5
109 */
110 HWTEST_F(UILabelButtonTest, UILabelButtonSetAlign_001, TestSize.Level0)
111 {
112 if (labelBtn_ == nullptr) {
113 EXPECT_NE(0, 0);
114 return;
115 }
116 labelBtn_->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
117 EXPECT_EQ(labelBtn_->GetAlign(), UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
118 }
119
120 /**
121 * @tc.name: UILabelButtonSetDirect_001
122 * @tc.desc: Verify SetDirect function.
123 * @tc.type: FUNC
124 * @tc.require: AR000EEMQ5
125 */
126 HWTEST_F(UILabelButtonTest, UILabelButtonSetDirect_001, TestSize.Level0)
127 {
128 if (labelBtn_ == nullptr) {
129 EXPECT_NE(0, 0);
130 return;
131 }
132 labelBtn_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_RTL);
133 EXPECT_EQ(labelBtn_->GetDirect(), UITextLanguageDirect::TEXT_DIRECT_RTL);
134 }
135
136 /**
137 * @tc.name: UILabelButtonSetLabelStyle_001
138 * @tc.desc: Verify SetLabelStyle function.
139 * @tc.type: FUNC
140 * @tc.require: AR000EEMQ5
141 */
142 HWTEST_F(UILabelButtonTest, UILabelButtonSetLabelStyle_001, TestSize.Level1)
143 {
144 if (labelBtn_ == nullptr) {
145 EXPECT_NE(0, 0);
146 return;
147 }
148 Style style;
149 style.borderWidth_ = 1;
150
151 labelBtn_->SetLabelStyle(style);
152 EXPECT_EQ(labelBtn_->GetLabelStyle().borderWidth_, style.borderWidth_);
153 }
154
155 /**
156 * @tc.name: UILabelButtonSetLabelStyle_002
157 * @tc.desc: Verify SetLabelStyle function.
158 * @tc.type: FUNC
159 * @tc.require: AR000EEMQ5
160 */
161 HWTEST_F(UILabelButtonTest, UILabelButtonSetLabelStyle_002, TestSize.Level1)
162 {
163 if (labelBtn_ == nullptr) {
164 EXPECT_NE(0, 0);
165 return;
166 }
167 labelBtn_->SetLabelStyle(STYLE_BORDER_OPA, OPA_TRANSPARENT);
168 EXPECT_EQ(labelBtn_->GetLabelStyle(STYLE_BORDER_OPA), OPA_TRANSPARENT);
169 }
170
171 /**
172 * @tc.name: UILabelButtonSetTextColor_001
173 * @tc.desc: Verify SetTextColor function.
174 * @tc.type: FUNC
175 * @tc.require: AR000EEMQ5
176 */
177 HWTEST_F(UILabelButtonTest, UILabelButtonSetTextColor_001, TestSize.Level1)
178 {
179 if (labelBtn_ == nullptr) {
180 EXPECT_NE(0, 0);
181 return;
182 }
183 labelBtn_->SetTextColor(Color::Gray());
184 EXPECT_EQ(labelBtn_->GetLabelStyle().textColor_.full, Color::Gray().full);
185 }
186
187 /**
188 * @tc.name: UILabelButtonSetFont_001
189 * @tc.desc: Verify SetFontId function.
190 * @tc.type: FUNC
191 * @tc.require: AR000EEMQ5
192 */
193 HWTEST_F(UILabelButtonTest, UILabelButtonSetFontId_001, TestSize.Level0)
194 {
195 if (labelBtn_ == nullptr) {
196 EXPECT_NE(0, 0);
197 return;
198 }
199 const uint16_t fontId = 16;
200 labelBtn_->SetFontId(fontId);
201 if (!UIFont::GetInstance()->IsVectorFont()) {
202 EXPECT_EQ(labelBtn_->GetFontId(), fontId);
203 } else {
204 EXPECT_EQ(labelBtn_->GetFontId(), 0);
205 }
206 }
207 } // namespace OHOS
208