• 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 "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     ASSERT_TRUE((display != nullptr));
75     WLOGI("GetDefaultDisplay: id %{public}llu, w %{public}d, h %{public}d, fps %{public}u\n",
76         (unsigned long long)display->GetId(), display->GetWidth(), display->GetHeight(), display->GetRefreshRate());
77     Rect displayRect = {0, 0, display->GetWidth(), display->GetHeight()};
78     Utils::InitByDisplayRect(displayRect);
79 
80     virtualPixelRatio_ = WindowTestUtils::GetVirtualPixelRatio(0);
81     hotZone_ = static_cast<uint32_t>(HOTZONE_TOUCH * virtualPixelRatio_);
82 
83     sptr<WindowOption> option = new WindowOption();
84     option->SetWindowName("WindowMoveDragTest");
85     option->SetWindowMode(WindowMode::WINDOW_MODE_FLOATING);
86     option->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
87     window_ = new WindowImpl(option);
88     window_->Create(INVALID_WINDOW_ID);
89     usleep(WAIT_SYANC_MS);
90     ASSERT_TRUE((window_ != nullptr));
91 }
92 
TearDown()93 void WindowMoveDragTest::TearDown()
94 {
95     ASSERT_EQ(WMError::WM_OK, window_->Destroy());
96     window_ = nullptr;
97     usleep(WAIT_SYANC_MS);
98 }
99 
CreatePointerEvent(int32_t posX,int32_t posY,uint32_t pointerId,int32_t pointerAction)100 std::shared_ptr<MMI::PointerEvent> WindowMoveDragTest::CreatePointerEvent(int32_t posX,
101                                                                           int32_t posY,
102                                                                           uint32_t pointerId,
103                                                                           int32_t pointerAction)
104 {
105     MMI::PointerEvent::PointerItem pointerItem;
106     pointerItem.SetPointerId(pointerId);
107     pointerItem.SetDisplayX(posX);
108     pointerItem.SetDisplayY(posY);
109 
110     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
111     pointerEvent->SetTargetDisplayId(0);
112     pointerEvent->AddPointerItem(pointerItem);
113     pointerEvent->SetPointerId(pointerId);
114     pointerEvent->SetPointerAction(pointerAction);
115     pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
116     return pointerEvent;
117 }
118 
DoMoveOrDrag(bool isMove,bool isDrag)119 void WindowMoveDragTest::DoMoveOrDrag(bool isMove, bool isDrag)
120 {
121     pointerId_++;
122     std::shared_ptr<MMI::PointerEvent> pointerEvent =
123         CreatePointerEvent(startPointX_, startPointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_DOWN);
124     window_->ConsumePointerEvent(pointerEvent);
125     ASSERT_TRUE(Utils::RectEqualToRect(window_->GetRect(), startPointRect_));
126 
127     usleep(WAIT_SYANC_MS);
128     ASSERT_EQ(isMove, window_->moveDragProperty_->startMoveFlag_);
129     if (window_->moveDragProperty_->startDragFlag_ == isDrag) {
130         ASSERT_EQ(isDrag, window_->moveDragProperty_->startDragFlag_);
131     }
132     pointerEvent = CreatePointerEvent(startPointX_, startPointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_UP);
133     window_->ConsumePointerEvent(pointerEvent);
134     ASSERT_EQ(false, window_->moveDragProperty_->startMoveFlag_);
135     ASSERT_EQ(false, window_->moveDragProperty_->startDragFlag_);
136 }
137 
138 namespace {
139 /**
140  * @tc.name: DragWindow01
141  * @tc.desc: drag left
142  * @tc.type: FUNC
143  * @tc.require: I5KYG1
144  */
145 HWTEST_F(WindowMoveDragTest, DragWindow01, Function | MediumTest | Level3)
146 {
147     ASSERT_EQ(WMError::WM_OK, window_->Show());
148     usleep(WAIT_SYANC_MS);
149     startPointRect_ = window_->GetRect();
150     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
151     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
152 
153     DoMoveOrDrag(false, true);
154     ASSERT_EQ(WMError::WM_OK, window_->Hide());
155 }
156 
157 /**
158  * @tc.name: DragWindow02
159  * @tc.desc: drag left top
160  * @tc.type: FUNC
161  * @tc.require: I5KYG1
162  */
163 HWTEST_F(WindowMoveDragTest, DragWindow02, Function | MediumTest | Level3)
164 {
165     ASSERT_EQ(WMError::WM_OK, window_->Show());
166     usleep(WAIT_SYANC_MS);
167     startPointRect_ = window_->GetRect();
168     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
169     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
170 
171     DoMoveOrDrag(false, true);
172     ASSERT_EQ(WMError::WM_OK, window_->Hide());
173 }
174 
175 /**
176  * @tc.name: DragWindow03
177  * @tc.desc: drag left bottom
178  * @tc.type: FUNC
179  * @tc.require: I5KYG1
180  */
181 HWTEST_F(WindowMoveDragTest, DragWindow03, Function | MediumTest | Level3)
182 {
183     ASSERT_EQ(WMError::WM_OK, window_->Show());
184     usleep(WAIT_SYANC_MS);
185     startPointRect_ = window_->GetRect();
186     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
187     startPointY_ = startPointRect_.posY_ +
188                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
189 
190     DoMoveOrDrag(false, true);
191     ASSERT_EQ(WMError::WM_OK, window_->Hide());
192 }
193 
194 /**
195  * @tc.name: DragWindow04
196  * @tc.desc: drag right
197  * @tc.type: FUNC
198  * @tc.require: I5KYG1
199  */
200 HWTEST_F(WindowMoveDragTest, DragWindow04, Function | MediumTest | Level3)
201 {
202     ASSERT_EQ(WMError::WM_OK, window_->Show());
203     usleep(WAIT_SYANC_MS);
204     startPointRect_ = window_->GetRect();
205     startPointX_ = startPointRect_.posX_ +
206                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
207     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
208 
209     DoMoveOrDrag(false, true);
210     ASSERT_EQ(WMError::WM_OK, window_->Hide());
211 }
212 
213 /**
214  * @tc.name: DragWindow05
215  * @tc.desc: drag right top
216  * @tc.type: FUNC
217  * @tc.require: I5KYG1
218  */
219 HWTEST_F(WindowMoveDragTest, DragWindow05, Function | MediumTest | Level3)
220 {
221     ASSERT_EQ(WMError::WM_OK, window_->Show());
222     usleep(WAIT_SYANC_MS);
223     startPointRect_ = window_->GetRect();
224     startPointX_ = startPointRect_.posX_ +
225                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
226     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
227 
228     DoMoveOrDrag(false, true);
229     ASSERT_EQ(WMError::WM_OK, window_->Hide());
230 }
231 
232 /**
233  * @tc.name: DragWindow06
234  * @tc.desc: drag right bottom
235  * @tc.type: FUNC
236  */
237 HWTEST_F(WindowMoveDragTest, DragWindow06, Function | MediumTest | Level3)
238 {
239     ASSERT_EQ(WMError::WM_OK, window_->Show());
240     usleep(WAIT_SYANC_MS);
241     startPointRect_ = window_->GetRect();
242     startPointX_ = startPointRect_.posX_ +
243                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
244     startPointY_ = startPointRect_.posY_ +
245                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
246 
247     DoMoveOrDrag(false, true);
248     ASSERT_EQ(WMError::WM_OK, window_->Hide());
249 }
250 
251 /**
252  * @tc.name: DragWindow07
253  * @tc.desc: drag top
254  * @tc.type: FUNC
255  */
256 HWTEST_F(WindowMoveDragTest, DragWindow07, Function | MediumTest | Level3)
257 {
258     ASSERT_EQ(WMError::WM_OK, window_->Show());
259     usleep(WAIT_SYANC_MS);
260     startPointRect_ = window_->GetRect();
261     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
262     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
263 
264     DoMoveOrDrag(false, true);
265     ASSERT_EQ(WMError::WM_OK, window_->Hide());
266 }
267 
268 /**
269  * @tc.name: DragWindow08
270  * @tc.desc: drag bottom
271  * @tc.type: FUNC
272  */
273 HWTEST_F(WindowMoveDragTest, DragWindow08, Function | MediumTest | Level3)
274 {
275     ASSERT_EQ(WMError::WM_OK, window_->Show());
276     usleep(WAIT_SYANC_MS);
277     startPointRect_ = window_->GetRect();
278     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
279     startPointY_ = startPointRect_.posY_ +
280                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
281 
282     DoMoveOrDrag(false, true);
283     ASSERT_EQ(WMError::WM_OK, window_->Hide());
284 }
285 
286 /**
287  * @tc.name: DragWindow09
288  * @tc.desc: point in decorZone, uiContent is nullptr
289  * @tc.type: FUNC
290  */
291 HWTEST_F(WindowMoveDragTest, DragWindow09, Function | MediumTest | Level3)
292 {
293     ASSERT_EQ(WMError::WM_OK, window_->Show());
294     usleep(WAIT_SYANC_MS);
295     startPointRect_ = window_->GetRect();
296     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
297     startPointY_ = startPointRect_.posY_ +
298         static_cast<int32_t>(WINDOW_TITLE_BAR_HEIGHT * POINT_HOTZONE_RATIO * virtualPixelRatio_);
299 
300     DoMoveOrDrag(false, false);
301     ASSERT_EQ(WMError::WM_OK, window_->Hide());
302 }
303 
304 /**
305  * @tc.name: DragWindow10
306  * @tc.desc: drag inner
307  * @tc.type: FUNC
308  */
309 HWTEST_F(WindowMoveDragTest, DragWindow10, Function | MediumTest | Level3)
310 {
311     ASSERT_EQ(WMError::WM_OK, window_->Show());
312     usleep(WAIT_SYANC_MS);
313     startPointRect_ = window_->GetRect();
314     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
315     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
316 
317     DoMoveOrDrag(false, false);
318     ASSERT_EQ(WMError::WM_OK, window_->Hide());
319 }
320 }
321 } // namespace Rosen
322 } // namespace OHOS
323