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     swipeView_->SetCurrentPage(1);
210     EXPECT_EQ(swipeView_->GetCurrentPage(), 1);
211 
212     swipeView_->Remove(view);
213     swipeView_->Remove(view2);
214     delete view;
215     delete view2;
216 }
217 
218 class OnTestSwipeListener : public UISwipeView::OnSwipeListener {
219 public:
OnTestSwipeListener()220     OnTestSwipeListener() {}
~OnTestSwipeListener()221     virtual ~OnTestSwipeListener() {}
OnSwipe(UISwipeView & view)222     void OnSwipe(UISwipeView& view) override
223     {
224         view.SetWidth(DEFAULT_WIDTH);
225         return;
226     }
227 };
228 
229 class TestUISwipeView : public UISwipeView {
230 public:
TriggerStopAnimator()231     void TriggerStopAnimator()
232     {
233         StopAnimator();
234     }
235 };
236 
237 /**
238  * @tc.name: UISwipeViewSetOnSwipeListener_001
239  * @tc.desc: Verify SetOnSwipeListener function, equal.
240  * @tc.type: FUNC
241  * @tc.require: AR000EEMQF
242  */
243 HWTEST_F(UISwipeViewTest, UISwipeViewSetOnSwipeListener_001, TestSize.Level1)
244 {
245     if (swipeView_ == nullptr) {
246         EXPECT_NE(0, 0);
247         return;
248     }
249     auto testSwipeLis = new OnTestSwipeListener();
250 
251     EXPECT_EQ(swipeView_->GetOnSwipeListener(), nullptr);
252     swipeView_->SetOnSwipeListener(testSwipeLis);
253     EXPECT_EQ(swipeView_->GetOnSwipeListener(), testSwipeLis);
254 
255     EXPECT_NE(swipeView_->GetWidth(), DEFAULT_WIDTH);
256     static_cast<TestUISwipeView *>(swipeView_)->TriggerStopAnimator();
257     EXPECT_EQ(swipeView_->GetWidth(), DEFAULT_WIDTH);
258     delete testSwipeLis;
259 }
260 
261 /**
262  * @tc.name: UISwipeViewGetViewByIndex_001
263  * @tc.desc: Verify GetViewByIndex function, equal.
264  * @tc.type: FUNC
265  * @tc.require: AR000EEMQF
266  */
267 HWTEST_F(UISwipeViewTest, UISwipeViewGetViewByIndex_001, TestSize.Level0)
268 {
269     if (swipeView_ == nullptr) {
270         EXPECT_NE(0, 0);
271         return;
272     }
273     swipeView_->SetDirection(HORIZONTAL);
274     const int16_t initWidth = 50;
275     const int16_t initHeight = 30;
276 
277     swipeView_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
278     swipeView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
279     swipeView_->SetLoopState(true);
280     UIView* view1 = new UIView();
281     view1->SetPosition(0, 0, initWidth, initHeight);
282     swipeView_->Add(view1);
283     UIView* view2 = new UIView();
284     view2->SetPosition(0, 0, initWidth, initHeight);
285     swipeView_->Add(view2);
286     UIView* view3 = new UIView();
287     view3->SetPosition(0, 0, initWidth, initHeight);
288     swipeView_->Add(view3);
289 
290     UIView* view = swipeView_->GetViewByIndex(1);
291     EXPECT_EQ(view2, view);
292     swipeView_->Remove(view1);
293     swipeView_->Remove(view2);
294     swipeView_->Remove(view3);
295     delete view1;
296     delete view2;
297     delete view3;
298 }
299 
300 /**
301  * @tc.name: Graphic_UISwipeView_Test_SetAlignMode_001
302  * @tc.desc: Verify SetAlignMode function, equal.
303  * @tc.type: FUNC
304  * @tc.require: AR000EVTV4
305  */
306 HWTEST_F(UISwipeViewTest, Graphic_UISwipeView_Test_SetAlignMode_001, TestSize.Level1)
307 {
308     if (swipeView_ == nullptr) {
309         EXPECT_NE(0, 0);
310         return;
311     }
312     swipeView_->SetAlignMode(UISwipeView::ALIGN_LEFT);
313     EXPECT_EQ(swipeView_->GetAlignMode(), UISwipeView::ALIGN_LEFT);
314 }
315 } // namespace OHOS
316