1 /*
2  * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17 #include "display_manager_proxy.h"
18 #include "pointer_event.h"
19 #include "window_helper.h"
20 #include "window_impl.h"
21 #include "window_test_utils.h"
22 #include "wm_common_inner.h"
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowMoveDragTest"};
30 constexpr float POINT_HOTZONE_RATIO = 0.5;
31 constexpr int WAIT_SYANC_MS = 100000;
32 }
33 using Utils = WindowTestUtils;
34 class WindowMoveDragTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     virtual void SetUp() override;
39     virtual void TearDown() override;
40 
41 private:
42     std::shared_ptr<MMI::PointerEvent> CreatePointerEvent(int32_t posX,
43                                                           int32_t posY,
44                                                           uint32_t pointerId,
45                                                           int32_t pointerAction);
46     void DoMoveOrDrag(bool isMove, bool isDrag);
47     static inline std::vector<sptr<Window>> activeWindows_;
48     static inline uint32_t pointerId_ = 0;
49     static inline int32_t startPointX_ = 0;
50     static inline int32_t startPointY_ = 0;
51     static inline Rect startPointRect_  = {0, 0, 0, 0};
52     static inline Rect expectRect_ = {0, 0, 0, 0};
53     static inline sptr<WindowImpl> window_ = nullptr;
54     static inline float virtualPixelRatio_ = 0.0;
55     static inline uint32_t hotZone_ = 0;
56 };
57 
SetUpTestCase()58 void WindowMoveDragTest::SetUpTestCase()
59 {
60     startPointX_ = 0;
61     startPointY_ = 0;
62     startPointRect_ = {0, 0, 0, 0};
63     expectRect_     = {0, 0, 0, 0};
64     usleep(WAIT_SYANC_MS);
65 }
66 
TearDownTestCase()67 void WindowMoveDragTest::TearDownTestCase()
68 {
69 }
70 
SetUp()71 void WindowMoveDragTest::SetUp()
72 {
73     auto display = DisplayManager::GetInstance().GetDisplayById(0);
74     if (display == nullptr) {
75         return;
76     }
77     WLOGI("GetDefaultDisplay: id %{public}llu, w %{public}d, h %{public}d, fps %{public}u\n",
78         (unsigned long long)display->GetId(), display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
79     Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
80     Utils::InitByDisplayRect(displayRect);
81 
82     virtualPixelRatio_ = WindowTestUtils::GetVirtualPixelRatio(0);
83     hotZone_ = static_cast<uint32_t>(HOTZONE_TOUCH * virtualPixelRatio_);
84 
85     sptr<WindowOption> option = new WindowOption();
86     option->SetWindowName("WindowMoveDragTest");
87     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
88     option->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
89     window_ = new WindowImpl(option);
90     window_->Create(INVALID_WINDOW_ID);
91     usleep(WAIT_SYANC_MS);
92     ASSERT_TRUE((window_ != nullptr));
93 }
94 
TearDown()95 void WindowMoveDragTest::TearDown()
96 {
97     if (window_ != nullptr) {
98         ASSERT_EQ(WMError::WM_OK, window_->Destroy());
99         window_ = nullptr;
100     }
101     usleep(WAIT_SYANC_MS);
102 }
103 
CreatePointerEvent(int32_t posX,int32_t posY,uint32_t pointerId,int32_t pointerAction)104 std::shared_ptr<MMI::PointerEvent> WindowMoveDragTest::CreatePointerEvent(int32_t posX,
105                                                                           int32_t posY,
106                                                                           uint32_t pointerId,
107                                                                           int32_t pointerAction)
108 {
109     MMI::PointerEvent::PointerItem pointerItem;
110     pointerItem.SetPointerId(pointerId);
111     pointerItem.SetDisplayX(posX);
112     pointerItem.SetDisplayY(posY);
113 
114     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
115     pointerEvent->SetTargetDisplayId(0);
116     pointerEvent->AddPointerItem(pointerItem);
117     pointerEvent->SetPointerId(pointerId);
118     pointerEvent->SetPointerAction(pointerAction);
119     pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
120     return pointerEvent;
121 }
122 
DoMoveOrDrag(bool isMove,bool isDrag)123 void WindowMoveDragTest::DoMoveOrDrag(bool isMove, bool isDrag)
124 {
125     pointerId_++;
126     std::shared_ptr<MMI::PointerEvent> pointerEvent =
127         CreatePointerEvent(startPointX_, startPointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_DOWN);
128     window_->ConsumePointerEvent(pointerEvent);
129     ASSERT_TRUE(Utils::RectEqualToRect(window_->GetRect(), startPointRect_));
130 
131     usleep(WAIT_SYANC_MS);
132     ASSERT_EQ(isMove, window_->moveDragProperty_->startMoveFlag_);
133     if (window_->moveDragProperty_->startDragFlag_ == isDrag) {
134         ASSERT_EQ(isDrag, window_->moveDragProperty_->startDragFlag_);
135     }
136     pointerEvent = CreatePointerEvent(startPointX_, startPointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_UP);
137     window_->ConsumePointerEvent(pointerEvent);
138     ASSERT_EQ(false, window_->moveDragProperty_->startMoveFlag_);
139     ASSERT_EQ(false, window_->moveDragProperty_->startDragFlag_);
140 }
141 
142 namespace {
143 /**
144  * @tc.name: DragWindow01
145  * @tc.desc: drag left
146  * @tc.type: FUNC
147  * @tc.require: I5KYG1
148  */
149 HWTEST_F(WindowMoveDragTest, DragWindow01, Function | MediumTest | Level3)
150 {
151     if (window_ == nullptr) {
152         return;
153     }
154     ASSERT_EQ(WMError::WM_OK, window_->Show());
155     usleep(WAIT_SYANC_MS);
156     startPointRect_ = window_->GetRect();
157     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
158     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
159 
160     DoMoveOrDrag(false, true);
161     ASSERT_EQ(WMError::WM_OK, window_->Hide());
162 }
163 
164 /**
165  * @tc.name: DragWindow02
166  * @tc.desc: drag left top
167  * @tc.type: FUNC
168  * @tc.require: I5KYG1
169  */
170 HWTEST_F(WindowMoveDragTest, DragWindow02, Function | MediumTest | Level3)
171 {
172     if (window_ == nullptr) {
173         return;
174     }
175     ASSERT_EQ(WMError::WM_OK, window_->Show());
176     usleep(WAIT_SYANC_MS);
177     startPointRect_ = window_->GetRect();
178     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
179     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
180 
181     DoMoveOrDrag(false, true);
182     ASSERT_EQ(WMError::WM_OK, window_->Hide());
183 }
184 
185 /**
186  * @tc.name: DragWindow03
187  * @tc.desc: drag left bottom
188  * @tc.type: FUNC
189  * @tc.require: I5KYG1
190  */
191 HWTEST_F(WindowMoveDragTest, DragWindow03, Function | MediumTest | Level3)
192 {
193     if (window_ == nullptr) {
194         return;
195     }
196     ASSERT_EQ(WMError::WM_OK, window_->Show());
197     usleep(WAIT_SYANC_MS);
198     startPointRect_ = window_->GetRect();
199     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
200     startPointY_ = startPointRect_.posY_ +
201                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
202 
203     DoMoveOrDrag(false, true);
204     ASSERT_EQ(WMError::WM_OK, window_->Hide());
205 }
206 
207 /**
208  * @tc.name: DragWindow04
209  * @tc.desc: drag right
210  * @tc.type: FUNC
211  * @tc.require: I5KYG1
212  */
213 HWTEST_F(WindowMoveDragTest, DragWindow04, Function | MediumTest | Level3)
214 {
215     if (window_ == nullptr) {
216         return;
217     }
218     ASSERT_EQ(WMError::WM_OK, window_->Show());
219     usleep(WAIT_SYANC_MS);
220     startPointRect_ = window_->GetRect();
221     startPointX_ = startPointRect_.posX_ +
222                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
223     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
224 
225     DoMoveOrDrag(false, true);
226     ASSERT_EQ(WMError::WM_OK, window_->Hide());
227 }
228 
229 /**
230  * @tc.name: DragWindow05
231  * @tc.desc: drag right top
232  * @tc.type: FUNC
233  * @tc.require: I5KYG1
234  */
235 HWTEST_F(WindowMoveDragTest, DragWindow05, Function | MediumTest | Level3)
236 {
237     if (window_ == nullptr) {
238         return;
239     }
240     ASSERT_EQ(WMError::WM_OK, window_->Show());
241     usleep(WAIT_SYANC_MS);
242     startPointRect_ = window_->GetRect();
243     startPointX_ = startPointRect_.posX_ +
244                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
245     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
246 
247     DoMoveOrDrag(false, true);
248     ASSERT_EQ(WMError::WM_OK, window_->Hide());
249 }
250 
251 /**
252  * @tc.name: DragWindow06
253  * @tc.desc: drag right bottom
254  * @tc.type: FUNC
255  */
256 HWTEST_F(WindowMoveDragTest, DragWindow06, Function | MediumTest | Level3)
257 {
258     if (window_ == nullptr) {
259         return;
260     }
261     ASSERT_EQ(WMError::WM_OK, window_->Show());
262     usleep(WAIT_SYANC_MS);
263     startPointRect_ = window_->GetRect();
264     startPointX_ = startPointRect_.posX_ +
265                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
266     startPointY_ = startPointRect_.posY_ +
267                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
268 
269     DoMoveOrDrag(false, true);
270     ASSERT_EQ(WMError::WM_OK, window_->Hide());
271 }
272 
273 /**
274  * @tc.name: DragWindow07
275  * @tc.desc: drag top
276  * @tc.type: FUNC
277  */
278 HWTEST_F(WindowMoveDragTest, DragWindow07, Function | MediumTest | Level3)
279 {
280     if (window_ == nullptr) {
281         return;
282     }
283     ASSERT_EQ(WMError::WM_OK, window_->Show());
284     usleep(WAIT_SYANC_MS);
285     startPointRect_ = window_->GetRect();
286     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
287     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
288 
289     DoMoveOrDrag(false, true);
290     ASSERT_EQ(WMError::WM_OK, window_->Hide());
291 }
292 
293 /**
294  * @tc.name: DragWindow08
295  * @tc.desc: drag bottom
296  * @tc.type: FUNC
297  */
298 HWTEST_F(WindowMoveDragTest, DragWindow08, Function | MediumTest | Level3)
299 {
300     if (window_ == nullptr) {
301         return;
302     }
303     ASSERT_EQ(WMError::WM_OK, window_->Show());
304     usleep(WAIT_SYANC_MS);
305     startPointRect_ = window_->GetRect();
306     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
307     startPointY_ = startPointRect_.posY_ +
308                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
309 
310     DoMoveOrDrag(false, true);
311     ASSERT_EQ(WMError::WM_OK, window_->Hide());
312 }
313 
314 /**
315  * @tc.name: DragWindow09
316  * @tc.desc: point in decorZone, uiContent is nullptr
317  * @tc.type: FUNC
318  */
319 HWTEST_F(WindowMoveDragTest, DragWindow09, Function | MediumTest | Level3)
320 {
321     if (window_ == nullptr) {
322         return;
323     }
324     ASSERT_EQ(WMError::WM_OK, window_->Show());
325     usleep(WAIT_SYANC_MS);
326     startPointRect_ = window_->GetRect();
327     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
328     startPointY_ = startPointRect_.posY_ +
329         static_cast<int32_t>(WINDOW_TITLE_BAR_HEIGHT * POINT_HOTZONE_RATIO * virtualPixelRatio_);
330 
331     DoMoveOrDrag(false, false);
332     ASSERT_EQ(WMError::WM_OK, window_->Hide());
333 }
334 
335 /**
336  * @tc.name: DragWindow10
337  * @tc.desc: drag inner
338  * @tc.type: FUNC
339  */
340 HWTEST_F(WindowMoveDragTest, DragWindow10, Function | MediumTest | Level3)
341 {
342     if (window_ == nullptr) {
343         return;
344     }
345     ASSERT_EQ(WMError::WM_OK, window_->Show());
346     usleep(WAIT_SYANC_MS);
347     startPointRect_ = window_->GetRect();
348     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
349     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
350 
351     DoMoveOrDrag(false, false);
352     ASSERT_EQ(WMError::WM_OK, window_->Hide());
353 }
354 }
355 } // namespace Rosen
356 } // namespace OHOS
357