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