• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pointer_event.h"
18 #include "window_helper.h"
19 #include "window_impl.h"
20 #include "window_test_utils.h"
21 #include "wm_common_inner.h"
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowMoveDragTest"};
29     constexpr float POINT_HOTZONE_RATIO = 0.5;
30     constexpr float DRAG_HOTZONE_RATIO = 0.6;
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 CalExpectRects();
47     void DoMoveOrDrag(bool isMove, bool isDrag);
48     static inline std::vector<sptr<Window>> activeWindows_;
49     static inline uint32_t pointerId_ = 0;
50     static inline int32_t pointX_ = 0;
51     static inline int32_t pointY_ = 0;
52     static inline int32_t startPointX_ = 0;
53     static inline int32_t startPointY_ = 0;
54     static inline bool hasStartMove_ = false;
55     static inline Rect startPointRect_  = {0, 0, 0, 0};
56     static inline Rect expectRect_ = {0, 0, 0, 0};
57     static inline sptr<WindowImpl> window_ = nullptr;
58     static inline float virtualPixelRatio_ = 0.0;
59     static inline uint32_t hotZone_ = 0;
60 };
61 
SetUpTestCase()62 void WindowMoveDragTest::SetUpTestCase()
63 {
64     startPointX_ = 0;
65     startPointY_ = 0;
66     pointX_ = 0;
67     pointY_ = 0;
68     startPointRect_ = {0, 0, 0, 0};
69     expectRect_     = {0, 0, 0, 0};
70     usleep(WAIT_SYANC_MS);
71 }
72 
TearDownTestCase()73 void WindowMoveDragTest::TearDownTestCase()
74 {
75 }
76 
SetUp()77 void WindowMoveDragTest::SetUp()
78 {
79     auto display = DisplayManager::GetInstance().GetDisplayById(0);
80     ASSERT_TRUE((display != nullptr));
81     WLOGFI("GetDefaultDisplay: id %{public}llu, w %{public}d, h %{public}d, fps %{public}u\n",
82         display->GetId(), display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
83     Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
84     utils::InitByDisplayRect(displayRect);
85 
86     virtualPixelRatio_ = WindowTestUtils::GetVirtualPixelRatio(0);
87     hotZone_ = static_cast<uint32_t>(HOTZONE * virtualPixelRatio_);
88 
89     sptr<WindowOption> option = new WindowOption();
90     option->SetWindowName("WindowMoveDragTest");
91     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
92     option->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
93     window_ = new WindowImpl(option);
94     window_->Create("");
95     usleep(WAIT_SYANC_MS);
96     ASSERT_TRUE((window_ != nullptr));
97 }
98 
TearDown()99 void WindowMoveDragTest::TearDown()
100 {
101     ASSERT_EQ(WMError::WM_OK, window_->Destroy());
102     usleep(WAIT_SYANC_MS);
103 }
104 
CreatePointerEvent(int32_t posX,int32_t posY,uint32_t pointerId,int32_t pointerAction)105 std::shared_ptr<MMI::PointerEvent> WindowMoveDragTest::CreatePointerEvent(int32_t posX,
106                                                                           int32_t posY,
107                                                                           uint32_t pointerId,
108                                                                           int32_t pointerAction)
109 {
110     MMI::PointerEvent::PointerItem pointerItem;
111     pointerItem.SetPointerId(pointerId);
112     pointerItem.SetGlobalX(posX);
113     pointerItem.SetGlobalY(posY);
114 
115     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
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     pointerEvent = CreatePointerEvent(pointX_, pointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_MOVE);
132     window_->ConsumePointerEvent(pointerEvent);
133     CalExpectRects();
134     usleep(WAIT_SYANC_MS);
135     ASSERT_TRUE(utils::RectEqualToRect(window_->GetRect(), expectRect_));
136     ASSERT_EQ(isMove, window_->startMoveFlag_);
137     ASSERT_EQ(isDrag, window_->startDragFlag_);
138 
139     pointerEvent = CreatePointerEvent(pointX_, pointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_UP);
140     window_->ConsumePointerEvent(pointerEvent);
141     ASSERT_TRUE(utils::RectEqualToRect(window_->GetRect(), expectRect_));
142 }
143 
CalExpectRects()144 void WindowMoveDragTest::CalExpectRects()
145 {
146     Rect startRectExceptFrame;
147     // calculate window inner rect except frame
148     startRectExceptFrame.posX_ = startPointRect_.posX_ +
149         static_cast<int32_t>(WINDOW_FRAME_WIDTH * virtualPixelRatio_);
150     startRectExceptFrame.posY_ = startPointRect_.posY_ +
151         static_cast<int32_t>(WINDOW_FRAME_WIDTH * virtualPixelRatio_);
152     startRectExceptFrame.width_ = startPointRect_.width_ -
153         static_cast<uint32_t>((WINDOW_FRAME_WIDTH + WINDOW_FRAME_WIDTH) * virtualPixelRatio_);
154     startRectExceptFrame.height_ = startPointRect_.height_ -
155         static_cast<uint32_t>((WINDOW_FRAME_WIDTH + WINDOW_FRAME_WIDTH) * virtualPixelRatio_);
156 
157     bool isPointInWindow = WindowHelper::IsPointInTargetRect(startPointX_, startPointY_, startRectExceptFrame);
158     int32_t diffX = pointX_ - startPointX_;
159     int32_t diffY = pointY_ - startPointY_;
160     Rect oriRect = startPointRect_;
161     if (!isPointInWindow) {
162         if (startPointX_ <= startRectExceptFrame.posX_) {
163             oriRect.posX_ += diffX;
164             oriRect.width_ -= diffX;
165         } else if (startPointX_ >= startRectExceptFrame.posX_ + static_cast<int32_t>(startRectExceptFrame.width_)) {
166             oriRect.width_ += diffX;
167         }
168 
169         if (startPointY_ <= startRectExceptFrame.posY_) {
170             oriRect.posY_   += diffY;
171             oriRect.height_ -= diffY;
172         } else if (startPointY_ >= startRectExceptFrame.posY_ + static_cast<int32_t>(startRectExceptFrame.height_)) {
173             oriRect.height_ += diffY;
174         }
175     } else {
176         if (hasStartMove_) {
177             oriRect.posX_ += diffX;
178             oriRect.posY_ += diffY;
179             hasStartMove_ = false;
180         }
181     }
182     bool isVertical = (utils::displayRect_.width_ < utils::displayRect_.height_) ? true : false;
183     expectRect_ = WindowHelper::GetFixedWindowRectByLimitSize(oriRect, startPointRect_, isVertical, virtualPixelRatio_);
184 }
185 
186 namespace {
187 /**
188  * @tc.name: DragWindow01
189  * @tc.desc: drag left
190  * @tc.type: FUNC
191  */
192 HWTEST_F(WindowMoveDragTest, DragWindow01, Function | MediumTest | Level3)
193 {
194     ASSERT_EQ(WMError::WM_OK, window_->Show());
195     usleep(WAIT_SYANC_MS);
196     startPointRect_ = window_->GetRect();
197     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
198     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
199     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
200     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
201     DoMoveOrDrag(false, true);
202     ASSERT_EQ(WMError::WM_OK, window_->Hide());
203 }
204 
205 /**
206  * @tc.name: DragWindow02
207  * @tc.desc: drag left top
208  * @tc.type: FUNC
209  */
210 HWTEST_F(WindowMoveDragTest, DragWindow02, Function | MediumTest | Level3)
211 {
212     ASSERT_EQ(WMError::WM_OK, window_->Show());
213     usleep(WAIT_SYANC_MS);
214     startPointRect_ = window_->GetRect();
215     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
216     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
217 
218     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
219     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
220     DoMoveOrDrag(false, true);
221     ASSERT_EQ(WMError::WM_OK, window_->Hide());
222 }
223 
224 /**
225  * @tc.name: DragWindow03
226  * @tc.desc: drag left bottom
227  * @tc.type: FUNC
228  */
229 HWTEST_F(WindowMoveDragTest, DragWindow03, Function | MediumTest | Level3)
230 {
231     ASSERT_EQ(WMError::WM_OK, window_->Show());
232     usleep(WAIT_SYANC_MS);
233     startPointRect_ = window_->GetRect();
234     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
235     startPointY_ = startPointRect_.posY_ +
236                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
237 
238     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
239     pointY_ = startPointRect_.posY_ +
240               static_cast<int32_t>(startPointRect_.height_ + hotZone_ * DRAG_HOTZONE_RATIO);
241     DoMoveOrDrag(false, true);
242     ASSERT_EQ(WMError::WM_OK, window_->Hide());
243 }
244 
245 /**
246  * @tc.name: DragWindow04
247  * @tc.desc: drag right
248  * @tc.type: FUNC
249  */
250 HWTEST_F(WindowMoveDragTest, DragWindow04, Function | MediumTest | Level3)
251 {
252     ASSERT_EQ(WMError::WM_OK, window_->Show());
253     usleep(WAIT_SYANC_MS);
254     startPointRect_ = window_->GetRect();
255     startPointX_ = startPointRect_.posX_ +
256                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
257     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
258 
259     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ + hotZone_ * DRAG_HOTZONE_RATIO);
260     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * DRAG_HOTZONE_RATIO);
261     DoMoveOrDrag(false, true);
262     ASSERT_EQ(WMError::WM_OK, window_->Hide());
263 }
264 
265 /**
266  * @tc.name: DragWindow05
267  * @tc.desc: drag right top
268  * @tc.type: FUNC
269  */
270 HWTEST_F(WindowMoveDragTest, DragWindow05, Function | MediumTest | Level3)
271 {
272     ASSERT_EQ(WMError::WM_OK, window_->Show());
273     usleep(WAIT_SYANC_MS);
274     startPointRect_ = window_->GetRect();
275     startPointX_ = startPointRect_.posX_ +
276                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
277     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
278 
279     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ + hotZone_ * DRAG_HOTZONE_RATIO);
280     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
281     DoMoveOrDrag(false, true);
282     ASSERT_EQ(WMError::WM_OK, window_->Hide());
283 }
284 
285 /**
286  * @tc.name: DragWindow06
287  * @tc.desc: drag right bottom
288  * @tc.type: FUNC
289  */
290 HWTEST_F(WindowMoveDragTest, DragWindow06, Function | MediumTest | Level3)
291 {
292     ASSERT_EQ(WMError::WM_OK, window_->Show());
293     usleep(WAIT_SYANC_MS);
294     startPointRect_ = window_->GetRect();
295     startPointX_ = startPointRect_.posX_ +
296                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
297     startPointY_ = startPointRect_.posY_ +
298                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
299 
300     pointX_ = startPointRect_.posX_ +
301               static_cast<int32_t>(startPointRect_.width_ + hotZone_ * DRAG_HOTZONE_RATIO);
302     pointY_ = startPointRect_.posY_ +
303               static_cast<int32_t>(startPointRect_.height_ + hotZone_ * DRAG_HOTZONE_RATIO);
304     DoMoveOrDrag(false, true);
305     ASSERT_EQ(WMError::WM_OK, window_->Hide());
306 }
307 
308 /**
309  * @tc.name: DragWindow07
310  * @tc.desc: drag top
311  * @tc.type: FUNC
312  */
313 HWTEST_F(WindowMoveDragTest, DragWindow07, Function | MediumTest | Level3)
314 {
315     ASSERT_EQ(WMError::WM_OK, window_->Show());
316     usleep(WAIT_SYANC_MS);
317     startPointRect_ = window_->GetRect();
318     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
319     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
320 
321     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * DRAG_HOTZONE_RATIO);
322     pointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
323     DoMoveOrDrag(false, true);
324     ASSERT_EQ(WMError::WM_OK, window_->Hide());
325 }
326 
327 /**
328  * @tc.name: DragWindow08
329  * @tc.desc: drag bottom
330  * @tc.type: FUNC
331  */
332 HWTEST_F(WindowMoveDragTest, DragWindow08, Function | MediumTest | Level3)
333 {
334     ASSERT_EQ(WMError::WM_OK, window_->Show());
335     usleep(WAIT_SYANC_MS);
336     startPointRect_ = window_->GetRect();
337     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
338     startPointY_ = startPointRect_.posY_ +
339                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
340 
341     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * DRAG_HOTZONE_RATIO);
342     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ + hotZone_ * DRAG_HOTZONE_RATIO);
343     DoMoveOrDrag(false, true);
344     ASSERT_EQ(WMError::WM_OK, window_->Hide());
345 }
346 
347 /**
348  * @tc.name: DragWindow09
349  * @tc.desc: point in decorZone, uiContent is nullptr
350  * @tc.type: FUNC
351  */
352 HWTEST_F(WindowMoveDragTest, DragWindow09, Function | MediumTest | Level3)
353 {
354     ASSERT_EQ(WMError::WM_OK, window_->Show());
355     usleep(WAIT_SYANC_MS);
356     startPointRect_ = window_->GetRect();
357     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
358     startPointY_ = startPointRect_.posY_ +
359         static_cast<int32_t>(WINDOW_TITLE_BAR_HEIGHT * POINT_HOTZONE_RATIO * virtualPixelRatio_);
360     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * DRAG_HOTZONE_RATIO);
361     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ + hotZone_ * DRAG_HOTZONE_RATIO);
362     DoMoveOrDrag(false, false);
363     ASSERT_EQ(WMError::WM_OK, window_->Hide());
364 }
365 
366 /**
367  * @tc.name: DragWindow10
368  * @tc.desc: point in decorZone, start move
369  * @tc.type: FUNC
370  */
371 HWTEST_F(WindowMoveDragTest, DragWindow10, Function | MediumTest | Level3)
372 {
373     ASSERT_EQ(WMError::WM_OK, window_->Show());
374     usleep(WAIT_SYANC_MS);
375     startPointRect_ = window_->GetRect();
376     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
377     startPointY_ = startPointRect_.posY_ +
378         static_cast<int32_t>(WINDOW_TITLE_BAR_HEIGHT * POINT_HOTZONE_RATIO * virtualPixelRatio_);
379 
380     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * DRAG_HOTZONE_RATIO);
381     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(hotZone_ * DRAG_HOTZONE_RATIO);
382     window_->StartMove();
383     hasStartMove_ = true;
384     DoMoveOrDrag(true, false);
385     ASSERT_EQ(WMError::WM_OK, window_->Hide());
386 }
387 
388 /**
389  * @tc.name: DragWindow11
390  * @tc.desc: drag inner
391  * @tc.type: FUNC
392  */
393 HWTEST_F(WindowMoveDragTest, DragWindow11, Function | MediumTest | Level3)
394 {
395     ASSERT_EQ(WMError::WM_OK, window_->Show());
396     usleep(WAIT_SYANC_MS);
397     startPointRect_ = window_->GetRect();
398     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
399     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
400 
401     pointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * DRAG_HOTZONE_RATIO);
402     pointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * DRAG_HOTZONE_RATIO);
403     DoMoveOrDrag(false, false);
404     ASSERT_EQ(WMError::WM_OK, window_->Hide());
405 }
406 }
407 } // namespace Rosen
408 } // namespace OHOS
409