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 class ScrollViewTest : public testing::Test {
23 public:
24 static void SetUpTestCase();
25 static void TearDownTestCase();
26 static UIScrollView* scrollView_;
27 };
28
29 UIScrollView* ScrollViewTest::scrollView_ = nullptr;
30
SetUpTestCase()31 void ScrollViewTest::SetUpTestCase()
32 {
33 if (scrollView_ == nullptr) {
34 scrollView_ = new UIScrollView();
35 }
36 }
37
TearDownTestCase()38 void ScrollViewTest::TearDownTestCase()
39 {
40 if (scrollView_ != nullptr) {
41 delete scrollView_;
42 scrollView_ = nullptr;
43 }
44 }
45
46 /**
47 * @tc.name: UIScrollViewSetScrollState_001
48 * @tc.desc: Verify SetScrollState function, equal.
49 * @tc.type: FUNC
50 * @tc.require: AR000DSMQE
51 */
52 HWTEST_F(ScrollViewTest, UIScrollViewSetScrollState_001, TestSize.Level0)
53 {
54 UIScrollView::OnScrollListener* scrollListener = new UIScrollView::OnScrollListener();
55 if (scrollListener == nullptr) {
56 EXPECT_NE(0, 0);
57 return;
58 }
59 uint8_t state = UIScrollView::OnScrollListener::SCROLL_STATE_MOVE;
60 scrollListener->SetScrollState(state);
61 EXPECT_EQ(scrollListener->GetScrollState(), state);
62
63 state = UIScrollView::OnScrollListener::SCROLL_STATE_STOP;
64 scrollListener->SetScrollState(state);
65 EXPECT_EQ(scrollListener->GetScrollState(), state);
66 delete scrollListener;
67 }
68
69 /**
70 * @tc.name: UIScrollViewGetViewType_001
71 * @tc.desc: Verify GetViewType function, equal.
72 * @tc.type: FUNC
73 * @tc.require: AR000DSMQE
74 */
75 HWTEST_F(ScrollViewTest, UIScrollViewGetViewType_001, TestSize.Level1)
76 {
77 if (scrollView_ == nullptr) {
78 EXPECT_NE(0, 0);
79 return;
80 }
81 EXPECT_EQ(scrollView_->GetViewType(), UI_SCROLL_VIEW);
82 }
83
84 /**
85 * @tc.name: UIScrollViewSetHorizontalScrollState_001
86 * @tc.desc: Verify SetHorizontalScrollState function, equal.
87 * @tc.type: FUNC
88 * @tc.require: AR000DSMQE
89 */
90 HWTEST_F(ScrollViewTest, UIScrollViewSetHorizontalScrollState_001, TestSize.Level1)
91 {
92 if (scrollView_ == nullptr) {
93 EXPECT_NE(0, 0);
94 return;
95 }
96 scrollView_->SetHorizontalScrollState(false);
97 EXPECT_EQ(scrollView_->GetHorizontalScrollState(), false);
98
99 scrollView_->SetHorizontalScrollState(true);
100 EXPECT_EQ(scrollView_->GetHorizontalScrollState(), true);
101 }
102
103 /**
104 * @tc.name: UIScrollViewSetVerticalScrollState_001
105 * @tc.desc: Verify SetVerticalScrollState function, equal.
106 * @tc.type: FUNC
107 * @tc.require: AR000DSMQE
108 */
109 HWTEST_F(ScrollViewTest, UIScrollViewSetVerticalScrollState_001, TestSize.Level1)
110 {
111 if (scrollView_ == nullptr) {
112 EXPECT_NE(0, 0);
113 return;
114 }
115 scrollView_->SetVerticalScrollState(false);
116 EXPECT_EQ(scrollView_->GetVerticalScrollState(), false);
117
118 scrollView_->SetVerticalScrollState(true);
119 EXPECT_EQ(scrollView_->GetVerticalScrollState(), true);
120 }
121
122 /**
123 * @tc.name: UIScrollViewSetMaxScrollDistance_001
124 * @tc.desc: Verify SetMaxScrollDistance function, equal.
125 * @tc.type: FUNC
126 * @tc.require: AR000E2RJK
127 */
128 HWTEST_F(ScrollViewTest, UIScrollViewSetMaxScrollDistance_001, TestSize.Level1)
129 {
130 if (scrollView_ == nullptr) {
131 EXPECT_NE(0, 0);
132 return;
133 }
134 const uint16_t distance = 10;
135 scrollView_->SetMaxScrollDistance(distance);
136 EXPECT_EQ(scrollView_->GetMaxScrollDistance(), distance);
137 }
138
139 /**
140 * @tc.name: UIScrollViewSetDragACCLevel_001
141 * @tc.desc: Verify SetDragACCLevel function, equal.
142 * @tc.type: FUNC
143 * @tc.require: SR000E2RJJ
144 */
145 HWTEST_F(ScrollViewTest, UIScrollViewSetDragACCLevel_001, TestSize.Level1)
146 {
147 if (scrollView_ == nullptr) {
148 EXPECT_NE(0, 0);
149 return;
150 }
151 const uint16_t value = 10;
152 scrollView_->SetDragACCLevel(value);
153 EXPECT_EQ(scrollView_->GetDragACCLevel(), value);
154 }
155
156 /**
157 * @tc.name: UIScrollViewSetSwipeACCLevel_001
158 * @tc.desc: Verify SetSwipeACCLevel function, equal.
159 * @tc.type: FUNC
160 * @tc.require: AR000F4E5P
161 */
162 HWTEST_F(ScrollViewTest, UIScrollViewSetSwipeACCLevel_001, TestSize.Level1)
163 {
164 if (scrollView_ == nullptr) {
165 EXPECT_NE(0, 0);
166 return;
167 }
168 const uint16_t value = 10;
169 scrollView_->SetSwipeACCLevel(value);
170 EXPECT_EQ(scrollView_->GetSwipeACCLevel(), value);
171 }
172
173 /**
174 * @tc.name: UIScrollViewMoveChildByOffset_001
175 * @tc.desc: Verify MoveChildByOffset function, equal.
176 * @tc.type: FUNC
177 * @tc.require: SR000F3PEM
178 */
179 HWTEST_F(ScrollViewTest, UIScrollViewMoveChildByOffset_001, TestSize.Level0)
180 {
181 if (scrollView_ == nullptr) {
182 EXPECT_NE(0, 0);
183 return;
184 }
185 UIView* view = new UIView();
186 EXPECT_EQ(view->GetX(), 0);
187 EXPECT_EQ(view->GetY(), 0);
188
189 scrollView_->Add(view);
190
191 const int16_t offsetX = 10;
192 const int16_t offsetY = 20;
193 scrollView_->SetThrowDrag(true);
194 scrollView_->MoveChildByOffset(offsetX, offsetY);
195 EXPECT_EQ(view->GetX(), offsetX);
196 EXPECT_EQ(view->GetY(), offsetY);
197
198 scrollView_->Remove(view);
199 delete view;
200 }
201 } // namespace OHOS
202