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_scroll_view.h"
17 #include <climits>
18 #include <gtest/gtest.h>
19 
20 using namespace testing::ext;
21 namespace OHOS {
22 namespace {
23     const uint16_t size = 0;
24     const EasingFunc func(EasingEquation::CubicEaseOut);
25     const uint8_t value = 0;
26     const uint8_t threshold = 2;
27     const uint8_t side = 0;
28     const Point center = {0, 0};
29 }
30 
31 class ScrollViewTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     static UIScrollView* scrollView_;
36 };
37 
38 UIScrollView* ScrollViewTest::scrollView_ = nullptr;
39 
SetUpTestCase()40 void ScrollViewTest::SetUpTestCase()
41 {
42     if (scrollView_ == nullptr) {
43         scrollView_ = new UIScrollView();
44     }
45 }
46 
TearDownTestCase()47 void ScrollViewTest::TearDownTestCase()
48 {
49     if (scrollView_ != nullptr) {
50         delete scrollView_;
51         scrollView_ = nullptr;
52     }
53 }
54 
55 /**
56  * @tc.name: UIScrollViewSetScrollState_001
57  * @tc.desc: Verify SetScrollState function, equal.
58  * @tc.type: FUNC
59  * @tc.require: AR000DSMQE
60  */
61 HWTEST_F(ScrollViewTest, UIScrollViewSetScrollState_001, TestSize.Level0)
62 {
63     UIScrollView::OnScrollListener* scrollListener = new UIScrollView::OnScrollListener();
64     if (scrollListener == nullptr) {
65         EXPECT_NE(0, 0);
66         return;
67     }
68     uint8_t state = UIScrollView::OnScrollListener::SCROLL_STATE_MOVE;
69     scrollListener->SetScrollState(state);
70     EXPECT_EQ(scrollListener->GetScrollState(), state);
71 
72     state = UIScrollView::OnScrollListener::SCROLL_STATE_STOP;
73     scrollListener->SetScrollState(state);
74     EXPECT_EQ(scrollListener->GetScrollState(), state);
75     delete scrollListener;
76 }
77 
78 /**
79  * @tc.name: UIScrollViewGetViewType_001
80  * @tc.desc: Verify GetViewType function, equal.
81  * @tc.type: FUNC
82  * @tc.require: AR000DSMQE
83  */
84 HWTEST_F(ScrollViewTest, UIScrollViewGetViewType_001, TestSize.Level1)
85 {
86     if (scrollView_ == nullptr) {
87         EXPECT_NE(0, 0);
88         return;
89     }
90     EXPECT_EQ(scrollView_->GetViewType(), UI_SCROLL_VIEW);
91 }
92 
93 /**
94  * @tc.name: UIScrollViewSetHorizontalScrollState_001
95  * @tc.desc: Verify SetHorizontalScrollState function, equal.
96  * @tc.type: FUNC
97  * @tc.require: AR000DSMQE
98  */
99 HWTEST_F(ScrollViewTest, UIScrollViewSetHorizontalScrollState_001, TestSize.Level1)
100 {
101     if (scrollView_ == nullptr) {
102         EXPECT_NE(0, 0);
103         return;
104     }
105     scrollView_->SetHorizontalScrollState(false);
106     EXPECT_EQ(scrollView_->GetHorizontalScrollState(), false);
107 
108     scrollView_->SetHorizontalScrollState(true);
109     EXPECT_EQ(scrollView_->GetHorizontalScrollState(), true);
110 }
111 
112 /**
113  * @tc.name: UIScrollViewSetVerticalScrollState_001
114  * @tc.desc: Verify SetVerticalScrollState function, equal.
115  * @tc.type: FUNC
116  * @tc.require: AR000DSMQE
117  */
118 HWTEST_F(ScrollViewTest, UIScrollViewSetVerticalScrollState_001, TestSize.Level1)
119 {
120     if (scrollView_ == nullptr) {
121         EXPECT_NE(0, 0);
122         return;
123     }
124     scrollView_->SetVerticalScrollState(false);
125     EXPECT_EQ(scrollView_->GetVerticalScrollState(), false);
126 
127     scrollView_->SetVerticalScrollState(true);
128     EXPECT_EQ(scrollView_->GetVerticalScrollState(), true);
129 }
130 
131 /**
132  * @tc.name: UIScrollViewSetMaxScrollDistance_001
133  * @tc.desc: Verify SetMaxScrollDistance function, equal.
134  * @tc.type: FUNC
135  * @tc.require: AR000E2RJK
136  */
137 HWTEST_F(ScrollViewTest, UIScrollViewSetMaxScrollDistance_001, TestSize.Level1)
138 {
139     if (scrollView_ == nullptr) {
140         EXPECT_NE(0, 0);
141         return;
142     }
143     const uint16_t distance = 10;
144     scrollView_->SetMaxScrollDistance(distance);
145     EXPECT_EQ(scrollView_->GetMaxScrollDistance(), distance);
146 }
147 
148 /**
149  * @tc.name: UIScrollViewSetDragACCLevel_001
150  * @tc.desc: Verify SetDragACCLevel function, equal.
151  * @tc.type: FUNC
152  * @tc.require: SR000E2RJJ
153  */
154 HWTEST_F(ScrollViewTest, UIScrollViewSetDragACCLevel_001, TestSize.Level1)
155 {
156     if (scrollView_ == nullptr) {
157         EXPECT_NE(0, 0);
158         return;
159     }
160     const uint16_t value = 10;
161     scrollView_->SetDragACCLevel(value);
162     EXPECT_EQ(scrollView_->GetDragACCLevel(), value);
163 }
164 
165 /**
166  * @tc.name: UIScrollViewSetSwipeACCLevel_001
167  * @tc.desc: Verify SetSwipeACCLevel function, equal.
168  * @tc.type: FUNC
169  * @tc.require: AR000F4E5P
170  */
171 HWTEST_F(ScrollViewTest, UIScrollViewSetSwipeACCLevel_001, TestSize.Level1)
172 {
173     if (scrollView_ == nullptr) {
174         EXPECT_NE(0, 0);
175         return;
176     }
177     const uint16_t value = 10;
178     scrollView_->SetSwipeACCLevel(value);
179     EXPECT_EQ(scrollView_->GetSwipeACCLevel(), value);
180 }
181 
182 /**
183  * @tc.name: UIScrollViewMoveChildByOffset_001
184  * @tc.desc: Verify MoveChildByOffset function, equal.
185  * @tc.type: FUNC
186  * @tc.require: SR000F3PEM
187  */
188 HWTEST_F(ScrollViewTest, UIScrollViewMoveChildByOffset_001, TestSize.Level0)
189 {
190     if (scrollView_ == nullptr) {
191         EXPECT_NE(0, 0);
192         return;
193     }
194     UIView* view = new UIView();
195     EXPECT_EQ(view->GetX(), 0);
196     EXPECT_EQ(view->GetY(), 0);
197 
198     scrollView_->Add(view);
199 
200     const int16_t offsetX = 10;
201     const int16_t offsetY = 20;
202     scrollView_->SetThrowDrag(true);
203     scrollView_->MoveChildByOffset(offsetX, offsetY);
204     EXPECT_EQ(view->GetX(), offsetX);
205     EXPECT_EQ(view->GetY(), offsetY);
206 
207     scrollView_->Remove(view);
208     delete view;
209 }
210 
211 /**
212  * @tc.name: UIAbstractClockSetScrollBlankSize_001
213  * @tc.desc: Verify SetScrollBlankSize function.
214  */
215 HWTEST_F(ScrollViewTest, UIAbstractScrollSetScrollBlankSize_001, TestSize.Level1)
216 {
217     if (scrollView_ == nullptr) {
218         EXPECT_NE(0, 0);
219         return;
220     }
221     scrollView_->SetScrollBlankSize(size);
222     EXPECT_EQ(scrollView_->GetScrollBlankSize(), size);
223 }
224 
225 /**
226  * @tc.name: UIAbstractClockSetReboundSize_001
227  * @tc.desc: Verify SetReboundSize function.
228  */
229 HWTEST_F(ScrollViewTest, UIAbstractScrollSetReboundSize_001, TestSize.Level1)
230 {
231     if (scrollView_ == nullptr) {
232         EXPECT_NE(0, 0);
233         return;
234     }
235     scrollView_->SetReboundSize(size);
236     EXPECT_EQ(scrollView_->GetReboundSize(), size);
237 }
238 
239 /**
240  * @tc.name: UIAbstractClockSetDragFunc_001
241  * @tc.desc: Verify SetDragFunc function.
242  */
243 HWTEST_F(ScrollViewTest, UIAbstractScrollSetDragFunc_001, TestSize.Level1)
244 {
245     if (scrollView_ == nullptr) {
246         EXPECT_NE(0, 0);
247         return;
248     }
249     scrollView_->SetDragFunc(func);
250     EXPECT_EQ(scrollView_->GetDragFunc(), func);
251 }
252 
253 #if defined(ENABLE_ROTATE_INPUT) && ENABLE_ROTATE_INPUT
254 /**
255  * @tc.name: UIAbstractClockSetRotateACCLevel_001
256  * @tc.desc: Verify SetRotateACCLevel function.
257  */
258 HWTEST_F(ScrollViewTest, UIAbstractScrollSetRotateACCLevel_001, TestSize.Level1)
259 {
260     if (scrollView_ == nullptr) {
261         EXPECT_NE(0, 0);
262         return;
263     }
264     scrollView_->SetRotateACCLevel(value);
265     EXPECT_EQ(scrollView_->GetRotateACCLevel(), value);
266 }
267 
268 /**
269  * @tc.name: UIAbstractClockSetRotateThrowThreshold_001
270  * @tc.desc: Verify SetRotateThrowThreshold function.
271  */
272 HWTEST_F(ScrollViewTest, UIAbstractScrollSetRotateThrowThreshold_001, TestSize.Level1)
273 {
274     if (scrollView_ == nullptr) {
275         EXPECT_NE(0, 0);
276         return;
277     }
278     scrollView_->SetRotateThrowThreshold(threshold);
279     EXPECT_EQ(scrollView_->GetRotateThrowThreshold(), threshold);
280 }
281 
282 /**
283  * @tc.name: UIAbstractClockRotateEvent_001
284  * @tc.desc: Verify OnRotateStartEvent OnRotateEvent OnRotateEndEvent function.
285  */
286 HWTEST_F(ScrollViewTest, UIAbstractScrollRotateEvent_001, TestSize.Level1)
287 {
288     if (scrollView_ == nullptr) {
289         EXPECT_NE(0, 0);
290         return;
291     }
292     RotateEvent* event = new RotateEvent(1);
293     EXPECT_EQ(scrollView_->OnRotateStartEvent(*event), false);
294     EXPECT_EQ(scrollView_->OnRotateEvent(*event), false);
295     EXPECT_EQ(scrollView_->OnRotateEndEvent(*event), false);
296     delete event;
297     event = nullptr;
298 }
299 #endif
300 
301 /**
302  * @tc.name: UIAbstractClockSetScrollBarSide_001
303  * @tc.desc: Verify SetScrollBarSide function.
304  */
305 HWTEST_F(ScrollViewTest, UIAbstractScrollSetScrollBarSide_001, TestSize.Level1)
306 {
307     if (scrollView_ == nullptr) {
308         EXPECT_NE(0, 0);
309         return;
310     }
311     scrollView_->SetScrollBarSide(side);
312     EXPECT_EQ(scrollView_->GetScrollBarSide(), side);
313 }
314 
315 /**
316  * @tc.name: UIAbstractClockSetScrollBarCenter_001
317  * @tc.desc: Verify SetScrollBarCenter function.
318  */
319 HWTEST_F(ScrollViewTest, UIAbstractScrollSetScrollBarCenter_001, TestSize.Level1)
320 {
321     if (scrollView_ == nullptr) {
322         EXPECT_NE(0, 0);
323         return;
324     }
325     scrollView_->SetScrollBarCenter(center);
326     EXPECT_EQ(scrollView_->GetScrollBarCenter().x, center.x);
327     EXPECT_EQ(scrollView_->GetScrollBarCenter().y, center.y);
328 }
329 
330 /**
331  * @tc.name: UIAbstractClockSetDirection_001
332  * @tc.desc: Verify SetDirection function.
333  */
334 HWTEST_F(ScrollViewTest, UIAbstractScrollSetDirection_001, TestSize.Level1)
335 {
336     if (scrollView_ == nullptr) {
337         EXPECT_NE(0, 0);
338         return;
339     }
340     scrollView_->SetDirection(2);
341     EXPECT_EQ(scrollView_->GetDirection(), 2);
342 }
343 } // namespace OHOS
344