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_circle_progress.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
24 constexpr uint16_t MAX_ANGLE_VALUE = 360;
25 constexpr uint16_t MIN_ANGLE_VALUE = 0;
26 }
27 class UICircleProgressTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 static UICircleProgress* circleProgress_;
32 };
33
34 UICircleProgress* UICircleProgressTest::circleProgress_ = nullptr;
35
SetUpTestCase()36 void UICircleProgressTest::SetUpTestCase()
37 {
38 if (circleProgress_ == nullptr) {
39 circleProgress_ = new UICircleProgress();
40 }
41 }
42
TearDownTestCase()43 void UICircleProgressTest::TearDownTestCase()
44 {
45 if (circleProgress_ != nullptr) {
46 delete circleProgress_;
47 circleProgress_ = nullptr;
48 }
49 }
50
51 /**
52 * @tc.name: UICircleProgressGetViewType_001
53 * @tc.desc: Verify GetViewType function, equal.
54 * @tc.type: FUNC
55 * @tc.require: AR000DSMQG
56 */
57 HWTEST_F(UICircleProgressTest, UICircleProgressGetViewType_001, TestSize.Level1)
58 {
59 if (circleProgress_ == nullptr) {
60 EXPECT_NE(0, 0);
61 return;
62 }
63 EXPECT_EQ(circleProgress_->GetViewType(), UI_CIRCLE_PROGRESS);
64 }
65
66 /**
67 * @tc.name: UICircleProgressSetRadius_001
68 * @tc.desc: Verify SetRadius function, equal.
69 * @tc.type: FUNC
70 * @tc.require: AR000DSMQG
71 */
72 HWTEST_F(UICircleProgressTest, UICircleProgressSetRadius_001, TestSize.Level1)
73 {
74 if (circleProgress_ == nullptr) {
75 EXPECT_NE(0, 0);
76 return;
77 }
78 const uint16_t radius = 1;
79
80 circleProgress_->SetRadius(radius);
81 EXPECT_EQ(circleProgress_->GetRadius(), radius);
82 }
83
84 /**
85 * @tc.name: UICircleProgressSetCenterPosition_001
86 * @tc.desc: Verify SetCenterPosition function, equal.
87 * @tc.type: FUNC
88 * @tc.require: AR000DSMQG
89 */
90 HWTEST_F(UICircleProgressTest, UICircleProgressSetCenterPosition_001, TestSize.Level1)
91 {
92 if (circleProgress_ == nullptr) {
93 EXPECT_NE(0, 0);
94 return;
95 }
96 const int16_t presetLength = 100;
97 const int16_t centerX = 50;
98 const int16_t centerY = 50;
99
100 circleProgress_->Resize(presetLength, presetLength);
101 circleProgress_->SetCenterPosition(centerX, centerY);
102 EXPECT_EQ(circleProgress_->GetCenterPosition().x, centerX);
103 EXPECT_EQ(circleProgress_->GetCenterPosition().y, centerY);
104 }
105
106 /**
107 * @tc.name: UICircleProgressSetLineColor_001
108 * @tc.desc: Verify SetLineColor function, equal.
109 * @tc.type: FUNC
110 * @tc.require: AR000DSMQG
111 */
112 HWTEST_F(UICircleProgressTest, UICircleProgressSetLineColor_001, TestSize.Level1)
113 {
114 if (circleProgress_ == nullptr) {
115 EXPECT_NE(0, 0);
116 return;
117 }
118 circleProgress_->SetLineColor(Color::Red());
119 EXPECT_EQ(circleProgress_->GetForegroundStyle(STYLE_LINE_COLOR), Color::Red().full);
120 }
121
122 /**
123 * @tc.name: UICircleProgressSetAngle_001
124 * @tc.desc: Verify SetAngle function, equal.
125 * @tc.type: FUNC
126 * @tc.require: AR000DSMQG
127 */
128 HWTEST_F(UICircleProgressTest, UICircleProgressSetStartAngle_001, TestSize.Level0)
129 {
130 if (circleProgress_ == nullptr) {
131 EXPECT_NE(0, 0);
132 return;
133 }
134 const int16_t startAngle = 0;
135 const int16_t endAngle = 90;
136
137 EXPECT_EQ(circleProgress_->GetStartAngle(), MIN_ANGLE_VALUE);
138 circleProgress_->SetStartAngle(startAngle);
139 EXPECT_EQ(circleProgress_->GetStartAngle(), startAngle);
140
141 EXPECT_EQ(circleProgress_->GetEndAngle(), MAX_ANGLE_VALUE);
142 circleProgress_->SetEndAngle(endAngle);
143 EXPECT_EQ(circleProgress_->GetEndAngle(), endAngle);
144 }
145
146 /**
147 * @tc.name: UICircleProgressOnPreDraw_001
148 * @tc.desc: Verify OnPreDraw function, equal.
149 */
150 HWTEST_F(UICircleProgressTest, UICircleProgressOnPreDraw_001, TestSize.Level0)
151 {
152 if (circleProgress_ == nullptr) {
153 EXPECT_NE(0, 0);
154 return;
155 }
156 Rect* invalidatedArea = new Rect();
157 EXPECT_EQ(circleProgress_->OnPreDraw(*invalidatedArea), false);
158 delete invalidatedArea;
159 invalidatedArea = nullptr;
160 }
161
162 /**
163 * @tc.name: UICircleProgressSetProgressImagePosition_001
164 * @tc.desc: Verify SetProgressImagePosition function, equal.
165 */
166 HWTEST_F(UICircleProgressTest, UICircleProgressSetProgressImagePosition_001, TestSize.Level0)
167 {
168 if (circleProgress_ == nullptr) {
169 EXPECT_NE(0, 0);
170 return;
171 }
172 int16_t x = 1;
173 int16_t y = 1;
174 UIView* view = new UIView();
175 circleProgress_->SetCenterPosition(x, y);
176 int16_t x1 = x + view->GetStyleConst().paddingLeft_ + view->GetStyleConst().borderWidth_;
177 int16_t y1 = y + view->GetStyleConst().paddingTop_ + view->GetStyleConst().borderWidth_;
178 circleProgress_->SetProgressImagePosition(x, y);
179 EXPECT_EQ(circleProgress_->GetCenterPosition().x, x1);
180 EXPECT_EQ(circleProgress_->GetCenterPosition().y, y1);
181 delete view;
182 view = nullptr;
183 }
184
185 /**
186 * @tc.name: UICircleProgressSetBackgroundImagePosition_001
187 * @tc.desc: Verify SetBackgroundImagePosition function, equal.
188 */
189 HWTEST_F(UICircleProgressTest, UICircleProgressSetBackgroundImagePosition_001, TestSize.Level0)
190 {
191 if (circleProgress_ == nullptr) {
192 EXPECT_NE(0, 0);
193 return;
194 }
195 int16_t x = 1;
196 int16_t y = 1;
197 UIView* view = new UIView();
198 circleProgress_->SetCenterPosition(x, y);
199 int16_t x1 = x + view->GetStyleConst().paddingLeft_ + view->GetStyleConst().borderWidth_;
200 int16_t y1 = y + view->GetStyleConst().paddingTop_ + view->GetStyleConst().borderWidth_;
201 circleProgress_->SetBackgroundImagePosition(x, y);
202 EXPECT_EQ(circleProgress_->GetCenterPosition().x, x1);
203 EXPECT_EQ(circleProgress_->GetCenterPosition().y, y1);
204 delete view;
205 view = nullptr;
206 }
207 } // namespace OHOS
208