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_swipe_view.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20 #include "common/screen.h"
21
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace {
25 constexpr uint8_t HORIZONTAL = 0;
26 constexpr uint8_t VERTICAL = 1;
27 const uint16_t DEFAULT_WIDTH = 100;
28 }
29 class UISwipeViewTest : public testing::Test {
30 public:
UISwipeViewTest()31 UISwipeViewTest() : swipeView_(nullptr) {}
~UISwipeViewTest()32 virtual ~UISwipeViewTest() {}
SetUpTestCase()33 static void SetUpTestCase() {}
TearDownTestCase()34 static void TearDownTestCase() {}
35 void SetUp();
36 void TearDown();
37 UISwipeView* swipeView_;
38 };
39
SetUp()40 void UISwipeViewTest::SetUp()
41 {
42 if (swipeView_ == nullptr) {
43 swipeView_ = new UISwipeView();
44 }
45 }
46
TearDown()47 void UISwipeViewTest::TearDown()
48 {
49 if (swipeView_ != nullptr) {
50 delete swipeView_;
51 swipeView_ = nullptr;
52 }
53 }
54 /**
55 * @tc.name: UISwipeViewGetViewType_001
56 * @tc.desc: Verify GetViewType function, equal.
57 * @tc.type: FUNC
58 * @tc.require: AR000EEMQF
59 */
60 HWTEST_F(UISwipeViewTest, UISwipeViewGetViewType_001, TestSize.Level1)
61 {
62 if (swipeView_ == nullptr) {
63 EXPECT_NE(0, 0);
64 return;
65 }
66 EXPECT_EQ(swipeView_->GetViewType(), UI_SWIPE_VIEW);
67 }
68
69 /**
70 * @tc.name: UISwipeViewSetDirection_001
71 * @tc.desc: Verify SetDirection function, equal.
72 * @tc.type: FUNC
73 * @tc.require: AR000EEMQF
74 */
75 HWTEST_F(UISwipeViewTest, UISwipeViewSetDirection_001, TestSize.Level1)
76 {
77 if (swipeView_ == nullptr) {
78 EXPECT_NE(0, 0);
79 return;
80 }
81 swipeView_->SetDirection(HORIZONTAL);
82 EXPECT_EQ(swipeView_->GetDirection(), HORIZONTAL);
83
84 swipeView_->SetDirection(VERTICAL);
85 EXPECT_EQ(swipeView_->GetDirection(), VERTICAL);
86 }
87
88 /**
89 * @tc.name: UISwipeViewAdd_001
90 * @tc.desc: Verify Add function, equal.
91 * @tc.type: FUNC
92 * @tc.require: AR000EEMQF
93 */
94 HWTEST_F(UISwipeViewTest, UISwipeViewAdd_001, TestSize.Level1)
95 {
96 if (swipeView_ == nullptr) {
97 EXPECT_NE(0, 0);
98 return;
99 }
100 UIView* view = new UIView();
101 if (view == nullptr) {
102 EXPECT_NE(0, 0);
103 return;
104 }
105 swipeView_->Add(view);
106 EXPECT_EQ(view, swipeView_->GetChildrenHead());
107 EXPECT_EQ(view->GetParent(), swipeView_);
108 swipeView_->Remove(view);
109 delete view;
110 }
111
112 /**
113 * @tc.name: UISwipeViewInsert_001
114 * @tc.desc: Verify Insert function, equal.
115 * @tc.type: FUNC
116 * @tc.require: AR000EEMQF
117 */
118 HWTEST_F(UISwipeViewTest, UISwipeViewInsert_001, TestSize.Level1)
119 {
120 if (swipeView_ == nullptr) {
121 EXPECT_NE(0, 0);
122 return;
123 }
124 UIView* preView = new UIView();
125 if (preView == nullptr) {
126 EXPECT_NE(0, 0);
127 return;
128 }
129 UIView* view = new UIView();
130 if (view == nullptr) {
131 delete preView;
132 EXPECT_NE(0, 0);
133 return;
134 }
135 swipeView_->Add(preView);
136 swipeView_->Insert(preView, view);
137 EXPECT_EQ(view, preView->GetNextSibling());
138
139 swipeView_->Remove(preView);
140 swipeView_->Remove(view);
141 delete view;
142 delete preView;
143 }
144
145 /**
146 * @tc.name: UISwipeViewRemove_001
147 * @tc.desc: Verify Remove function, equal.
148 * @tc.type: FUNC
149 * @tc.require: AR000EEMQF
150 */
151 HWTEST_F(UISwipeViewTest, UISwipeViewRemove_001, TestSize.Level1)
152 {
153 if (swipeView_ == nullptr) {
154 EXPECT_NE(0, 0);
155 return;
156 }
157 UIView* view = new UIView();
158 if (view == nullptr) {
159 EXPECT_NE(0, 0);
160 return;
161 }
162 swipeView_->Add(view);
163 swipeView_->Remove(view);
164 EXPECT_EQ(nullptr, swipeView_->GetChildrenHead());
165 EXPECT_EQ(nullptr, view->GetParent());
166
167 delete view;
168 }
169
170 /**
171 * @tc.name: UISwipeViewSetCurrentPage_001
172 * @tc.desc: Verify SetCurrentPage function, equal.
173 * @tc.type: FUNC
174 * @tc.require: AR000EEMQF
175 */
176 HWTEST_F(UISwipeViewTest, UISwipeViewSetCurrentPage_001, TestSize.Level0)
177 {
178 if (swipeView_ == nullptr) {
179 EXPECT_NE(0, 0);
180 return;
181 }
182 const int16_t initPosX = 10;
183 const int16_t initPosY = 20;
184 const int16_t initWidth = 50;
185 const int16_t initHeight = 30;
186 const uint16_t size = 100;
187 const uint16_t time = 100;
188
189 swipeView_->SetPosition(initPosX, initPosY, initWidth, initHeight);
190 swipeView_->SetLoopState(true);
191 swipeView_->SetAnimatorTime(time);
192 swipeView_->SetBlankSize(size);
193 UIViewGroup* view = new UIViewGroup();
194 if (view == nullptr) {
195 EXPECT_NE(0, 0);
196 return;
197 }
198 UIViewGroup* view2 = new UIViewGroup();
199 if (view2 == nullptr) {
200 delete view;
201 EXPECT_NE(0, 0);
202 return;
203 }
204
205 view->SetPosition(initPosX, initPosY, initWidth, initHeight);
206 swipeView_->Add(view);
207 view2->SetPosition(initPosX, initPosY, initWidth, initHeight);
208 swipeView_->Add(view2);
209 uint16_t index = 1;
210 swipeView_->SetCurrentPage(1);
211 EXPECT_EQ(swipeView_->GetCurrentPage(), 1);
212
213 swipeView_->Remove(view);
214 swipeView_->Remove(view2);
215 delete view;
216 delete view2;
217 }
218
219 class OnTestSwipeListener : public UISwipeView::OnSwipeListener {
220 public:
OnTestSwipeListener()221 OnTestSwipeListener() {}
~OnTestSwipeListener()222 virtual ~OnTestSwipeListener() {}
OnSwipe(UISwipeView & view)223 void OnSwipe(UISwipeView& view) override
224 {
225 view.SetWidth(DEFAULT_WIDTH);
226 return;
227 }
228 };
229
230 class TestUISwipeView : public UISwipeView {
231 public:
TriggerStopAnimator()232 void TriggerStopAnimator()
233 {
234 StopAnimator();
235 }
236 };
237
238 /**
239 * @tc.name: UISwipeViewSetOnSwipeListener_001
240 * @tc.desc: Verify SetOnSwipeListener function, equal.
241 * @tc.type: FUNC
242 * @tc.require: AR000EEMQF
243 */
244 HWTEST_F(UISwipeViewTest, UISwipeViewSetOnSwipeListener_001, TestSize.Level1)
245 {
246 if (swipeView_ == nullptr) {
247 EXPECT_NE(0, 0);
248 return;
249 }
250 auto testSwipeLis = new OnTestSwipeListener();
251
252 EXPECT_EQ(swipeView_->GetOnSwipeListener(), nullptr);
253 swipeView_->SetOnSwipeListener(testSwipeLis);
254 EXPECT_EQ(swipeView_->GetOnSwipeListener(), testSwipeLis);
255
256 EXPECT_NE(swipeView_->GetWidth(), DEFAULT_WIDTH);
257 static_cast<TestUISwipeView *>(swipeView_)->TriggerStopAnimator();
258 EXPECT_EQ(swipeView_->GetWidth(), DEFAULT_WIDTH);
259 delete testSwipeLis;
260 }
261
262 /**
263 * @tc.name: UISwipeViewGetViewByIndex_001
264 * @tc.desc: Verify GetViewByIndex function, equal.
265 * @tc.type: FUNC
266 * @tc.require: AR000EEMQF
267 */
268 HWTEST_F(UISwipeViewTest, UISwipeViewGetViewByIndex_001, TestSize.Level0)
269 {
270 if (swipeView_ == nullptr) {
271 EXPECT_NE(0, 0);
272 return;
273 }
274 swipeView_->SetDirection(HORIZONTAL);
275 const int16_t initWidth = 50;
276 const int16_t initHeight = 30;
277
278 swipeView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
279 swipeView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
280 swipeView_->SetLoopState(true);
281 UIView* view1 = new UIView();
282 view1->SetPosition(0, 0, initWidth, initHeight);
283 swipeView_->Add(view1);
284 UIView* view2 = new UIView();
285 view2->SetPosition(0, 0, initWidth, initHeight);
286 swipeView_->Add(view2);
287 UIView* view3 = new UIView();
288 view3->SetPosition(0, 0, initWidth, initHeight);
289 swipeView_->Add(view3);
290
291 UIView* view = swipeView_->GetViewByIndex(1);
292 EXPECT_EQ(view2, view);
293 swipeView_->Remove(view1);
294 swipeView_->Remove(view2);
295 swipeView_->Remove(view3);
296 delete view1;
297 delete view2;
298 delete view3;
299 }
300
301 /**
302 * @tc.name: Graphic_UISwipeView_Test_SetAlignMode_001
303 * @tc.desc: Verify SetAlignMode function, equal.
304 * @tc.type: FUNC
305 * @tc.require: AR000EVTV4
306 */
307 HWTEST_F(UISwipeViewTest, Graphic_UISwipeView_Test_SetAlignMode_001, TestSize.Level1)
308 {
309 if (swipeView_ == nullptr) {
310 EXPECT_NE(0, 0);
311 return;
312 }
313 swipeView_->SetAlignMode(UISwipeView::ALIGN_LEFT);
314 EXPECT_EQ(swipeView_->GetAlignMode(), UISwipeView::ALIGN_LEFT);
315 }
316 } // namespace OHOS