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 "events/drag_event.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
24     const Point NEW_POS = { 100, 200 };
25     const Point LAST_POS = { 150, 250 };
26     const Point TOTAL_LEN = { 50, 150 };
27     constexpr uint8_t DIRECTION_LEFT_TO_RIGHT = 0;
28     constexpr uint8_t DIRECTION_RIGHT_TO_LEFT = 1;
29     constexpr uint8_t DIRECTION_TOP_TO_BOTTOM = 2;
30     constexpr uint8_t DIRECTION_BOTTOM_TO_TOP = 3;
31 }
32 
33 class DragEventTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     static DragEvent* dragEvent_;
38 };
39 
40 DragEvent* DragEventTest::dragEvent_ = nullptr;
41 
SetUpTestCase(void)42 void DragEventTest::SetUpTestCase(void)
43 {
44     if (dragEvent_ == nullptr) {
45         dragEvent_ = new DragEvent(NEW_POS, LAST_POS, TOTAL_LEN);
46     }
47 }
48 
TearDownTestCase(void)49 void DragEventTest::TearDownTestCase(void)
50 {
51     if (dragEvent_ != nullptr) {
52         delete dragEvent_;
53         dragEvent_ = nullptr;
54     }
55 }
56 /**
57  * @tc.name: DragEventConstructor_001
58  * @tc.desc: Verify Constructor function, equal.
59  * @tc.type: FUNC
60  * @tc.require: SR000DRSH4
61  */
62 HWTEST_F(DragEventTest, DragEventConstructor_001, TestSize.Level0)
63 {
64     if (dragEvent_ == nullptr) {
65         EXPECT_EQ(1, 0);
66         return;
67     }
68     EXPECT_EQ(dragEvent_->GetCurrentPos().x, NEW_POS.x);
69     EXPECT_EQ(dragEvent_->GetCurrentPos().y, NEW_POS.y);
70 }
71 
72 /**
73  * @tc.name: DragEventGetLastPoint_001
74  * @tc.desc: Verify GetLastPoint function, equal.
75  * @tc.type: FUNC
76  * @tc.require: SR000DRSH4
77  */
78 HWTEST_F(DragEventTest, DragEventGetLastPoint_001, TestSize.Level0)
79 {
80     if (dragEvent_ == nullptr) {
81         EXPECT_EQ(1, 0);
82         return;
83     }
84     EXPECT_EQ(dragEvent_->GetLastPoint().x, LAST_POS.x);
85     EXPECT_EQ(dragEvent_->GetLastPoint().y, LAST_POS.y);
86 }
87 
88 /**
89  * @tc.name: DragEventGetStartPoint_001
90  * @tc.desc: Verify GetStartPoint function, equal.
91  * @tc.type: FUNC
92  * @tc.require: SR000DRSH4
93  */
94 HWTEST_F(DragEventTest, DragEventGetStartPoint_001, TestSize.Level0)
95 {
96     if (dragEvent_ == nullptr) {
97         EXPECT_EQ(1, 0);
98         return;
99     }
100     EXPECT_EQ(dragEvent_->GetStartPoint().x, NEW_POS.x - TOTAL_LEN.x);
101     EXPECT_EQ(dragEvent_->GetStartPoint().y, NEW_POS.y - TOTAL_LEN.y);
102 }
103 
104 /**
105  * @tc.name: DragEventSetPreLastPoint_001
106  * @tc.desc: Verify SetPreLastPoint function, equal.
107  * @tc.type: FUNC
108  * @tc.require: SR000DRSH4
109  */
110 HWTEST_F(DragEventTest, DragEventSetPreLastPoint_001, TestSize.Level1)
111 {
112     if (dragEvent_ == nullptr) {
113         EXPECT_EQ(1, 0);
114         return;
115     }
116     const Point preLastPos = { 20, 40 };
117     dragEvent_->SetPreLastPoint(preLastPos);
118     EXPECT_EQ(dragEvent_->GetPreLastPoint().x, preLastPos.x);
119     EXPECT_EQ(dragEvent_->GetPreLastPoint().y, preLastPos.y);
120 }
121 
122 /**
123  * @tc.name: DragEventGetDragDirection_001
124  * @tc.desc: Verify GetDragDirection function, equal.
125  * @tc.type: FUNC
126  * @tc.require: SR000DRSH4
127  */
128 HWTEST_F(DragEventTest, DragEventGetDragDirection_001, TestSize.Level0)
129 {
130     if (dragEvent_ == nullptr) {
131         EXPECT_EQ(1, 0);
132         return;
133     }
134     Point startPos = dragEvent_->GetStartPoint();
135     Point currentPos = dragEvent_->GetCurrentPos();
136     if (MATH_ABS(currentPos.x - startPos.x) >= MATH_ABS(currentPos.y - startPos.y)) {
137         if (currentPos.x > startPos.x) {
138             EXPECT_EQ(dragEvent_->GetDragDirection(), DIRECTION_LEFT_TO_RIGHT);
139         } else {
140             EXPECT_EQ(dragEvent_->GetDragDirection(), DIRECTION_RIGHT_TO_LEFT);
141         }
142     } else {
143         if (currentPos.y > startPos.y) {
144             EXPECT_EQ(dragEvent_->GetDragDirection(), DIRECTION_TOP_TO_BOTTOM);
145         } else {
146             EXPECT_EQ(dragEvent_->GetDragDirection(), DIRECTION_BOTTOM_TO_TOP);
147         }
148     }
149 }
150 
151 /**
152  * @tc.name: DragEventGetDeltaX_001
153  * @tc.desc: Verify GetDeltaX function, equal.
154  * @tc.type: FUNC
155  * @tc.require: SR000DRSH4
156  */
157 HWTEST_F(DragEventTest, DragEventGetDeltaX_001, TestSize.Level1)
158 {
159     if (dragEvent_ == nullptr) {
160         EXPECT_EQ(1, 0);
161         return;
162     }
163     EXPECT_EQ(dragEvent_->GetDeltaX(), NEW_POS.x - LAST_POS.x);
164 }
165 
166 /**
167  * @tc.name: DragEventGetDeltaY_001
168  * @tc.desc: Verify GetDeltaY function, equal.
169  * @tc.type: FUNC
170  * @tc.require: SR000DRSH4
171  */
172 HWTEST_F(DragEventTest, DragEventGetDeltaY_001, TestSize.Level1)
173 {
174     if (dragEvent_ == nullptr) {
175         EXPECT_EQ(1, 0);
176         return;
177     }
178     EXPECT_EQ(dragEvent_->GetDeltaY(), NEW_POS.y - LAST_POS.y);
179 }
180 } // namespace OHOS
181