• 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 int WAIT_SYANC_MS = 100000;
31 }
32 using Utils = WindowTestUtils;
33 class WindowMoveDragTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     virtual void SetUp() override;
38     virtual void TearDown() override;
39 
40 private:
41     std::shared_ptr<MMI::PointerEvent> CreatePointerEvent(int32_t posX,
42                                                           int32_t posY,
43                                                           uint32_t pointerId,
44                                                           int32_t pointerAction);
45     void DoMoveOrDrag(bool isMove, bool isDrag);
46     static inline std::vector<sptr<Window>> activeWindows_;
47     static inline uint32_t pointerId_ = 0;
48     static inline int32_t startPointX_ = 0;
49     static inline int32_t startPointY_ = 0;
50     static inline Rect startPointRect_  = {0, 0, 0, 0};
51     static inline Rect expectRect_ = {0, 0, 0, 0};
52     static inline sptr<WindowImpl> window_ = nullptr;
53     static inline float virtualPixelRatio_ = 0.0;
54     static inline uint32_t hotZone_ = 0;
55 };
56 
SetUpTestCase()57 void WindowMoveDragTest::SetUpTestCase()
58 {
59     startPointX_ = 0;
60     startPointY_ = 0;
61     startPointRect_ = {0, 0, 0, 0};
62     expectRect_     = {0, 0, 0, 0};
63     usleep(WAIT_SYANC_MS);
64 }
65 
TearDownTestCase()66 void WindowMoveDragTest::TearDownTestCase()
67 {
68 }
69 
SetUp()70 void WindowMoveDragTest::SetUp()
71 {
72     auto display = DisplayManager::GetInstance().GetDisplayById(0);
73     ASSERT_TRUE((display != nullptr));
74     WLOGFI("GetDefaultDisplay: id %{public}llu, w %{public}d, h %{public}d, fps %{public}u\n",
75         (unsigned long long)display->GetId(), display->GetWidth(), display->GetHeight(), 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     ASSERT_EQ(WMError::WM_OK, window_->Destroy());
95     window_ = nullptr;
96     usleep(WAIT_SYANC_MS);
97 }
98 
CreatePointerEvent(int32_t posX,int32_t posY,uint32_t pointerId,int32_t pointerAction)99 std::shared_ptr<MMI::PointerEvent> WindowMoveDragTest::CreatePointerEvent(int32_t posX,
100                                                                           int32_t posY,
101                                                                           uint32_t pointerId,
102                                                                           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     ASSERT_EQ(isDrag, window_->moveDragProperty_->startDragFlag_);
129 
130     pointerEvent = CreatePointerEvent(startPointX_, startPointY_, pointerId_, MMI::PointerEvent::POINTER_ACTION_UP);
131     window_->ConsumePointerEvent(pointerEvent);
132     ASSERT_EQ(false, window_->moveDragProperty_->startMoveFlag_);
133     ASSERT_EQ(false, window_->moveDragProperty_->startDragFlag_);
134 }
135 
136 namespace {
137 /**
138  * @tc.name: DragWindow01
139  * @tc.desc: drag left
140  * @tc.type: FUNC
141  * @tc.require: I5KYG1
142  */
143 HWTEST_F(WindowMoveDragTest, DragWindow01, Function | MediumTest | Level3)
144 {
145     ASSERT_EQ(WMError::WM_OK, window_->Show());
146     usleep(WAIT_SYANC_MS);
147     startPointRect_ = window_->GetRect();
148     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
149     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
150 
151     DoMoveOrDrag(false, true);
152     ASSERT_EQ(WMError::WM_OK, window_->Hide());
153 }
154 
155 /**
156  * @tc.name: DragWindow02
157  * @tc.desc: drag left top
158  * @tc.type: FUNC
159  * @tc.require: I5KYG1
160  */
161 HWTEST_F(WindowMoveDragTest, DragWindow02, Function | MediumTest | Level3)
162 {
163     ASSERT_EQ(WMError::WM_OK, window_->Show());
164     usleep(WAIT_SYANC_MS);
165     startPointRect_ = window_->GetRect();
166     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
167     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
168 
169     DoMoveOrDrag(false, true);
170     ASSERT_EQ(WMError::WM_OK, window_->Hide());
171 }
172 
173 /**
174  * @tc.name: DragWindow03
175  * @tc.desc: drag left bottom
176  * @tc.type: FUNC
177  * @tc.require: I5KYG1
178  */
179 HWTEST_F(WindowMoveDragTest, DragWindow03, Function | MediumTest | Level3)
180 {
181     ASSERT_EQ(WMError::WM_OK, window_->Show());
182     usleep(WAIT_SYANC_MS);
183     startPointRect_ = window_->GetRect();
184     startPointX_ = startPointRect_.posX_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
185     startPointY_ = startPointRect_.posY_ +
186                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
187 
188     DoMoveOrDrag(false, true);
189     ASSERT_EQ(WMError::WM_OK, window_->Hide());
190 }
191 
192 /**
193  * @tc.name: DragWindow04
194  * @tc.desc: drag right
195  * @tc.type: FUNC
196  * @tc.require: I5KYG1
197  */
198 HWTEST_F(WindowMoveDragTest, DragWindow04, Function | MediumTest | Level3)
199 {
200     ASSERT_EQ(WMError::WM_OK, window_->Show());
201     usleep(WAIT_SYANC_MS);
202     startPointRect_ = window_->GetRect();
203     startPointX_ = startPointRect_.posX_ +
204                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
205     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
206 
207     DoMoveOrDrag(false, true);
208     ASSERT_EQ(WMError::WM_OK, window_->Hide());
209 }
210 
211 /**
212  * @tc.name: DragWindow05
213  * @tc.desc: drag right top
214  * @tc.type: FUNC
215  * @tc.require: I5KYG1
216  */
217 HWTEST_F(WindowMoveDragTest, DragWindow05, Function | MediumTest | Level3)
218 {
219     ASSERT_EQ(WMError::WM_OK, window_->Show());
220     usleep(WAIT_SYANC_MS);
221     startPointRect_ = window_->GetRect();
222     startPointX_ = startPointRect_.posX_ +
223                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
224     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
225 
226     DoMoveOrDrag(false, true);
227     ASSERT_EQ(WMError::WM_OK, window_->Hide());
228 }
229 
230 /**
231  * @tc.name: DragWindow06
232  * @tc.desc: drag right bottom
233  * @tc.type: FUNC
234  */
235 HWTEST_F(WindowMoveDragTest, DragWindow06, Function | MediumTest | Level3)
236 {
237     ASSERT_EQ(WMError::WM_OK, window_->Show());
238     usleep(WAIT_SYANC_MS);
239     startPointRect_ = window_->GetRect();
240     startPointX_ = startPointRect_.posX_ +
241                    static_cast<int32_t>(startPointRect_.width_ + hotZone_ * POINT_HOTZONE_RATIO);
242     startPointY_ = startPointRect_.posY_ +
243                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
244 
245     DoMoveOrDrag(false, true);
246     ASSERT_EQ(WMError::WM_OK, window_->Hide());
247 }
248 
249 /**
250  * @tc.name: DragWindow07
251  * @tc.desc: drag top
252  * @tc.type: FUNC
253  */
254 HWTEST_F(WindowMoveDragTest, DragWindow07, Function | MediumTest | Level3)
255 {
256     ASSERT_EQ(WMError::WM_OK, window_->Show());
257     usleep(WAIT_SYANC_MS);
258     startPointRect_ = window_->GetRect();
259     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
260     startPointY_ = startPointRect_.posY_ - static_cast<int32_t>(hotZone_ * POINT_HOTZONE_RATIO);
261 
262     DoMoveOrDrag(false, true);
263     ASSERT_EQ(WMError::WM_OK, window_->Hide());
264 }
265 
266 /**
267  * @tc.name: DragWindow08
268  * @tc.desc: drag bottom
269  * @tc.type: FUNC
270  */
271 HWTEST_F(WindowMoveDragTest, DragWindow08, Function | MediumTest | Level3)
272 {
273     ASSERT_EQ(WMError::WM_OK, window_->Show());
274     usleep(WAIT_SYANC_MS);
275     startPointRect_ = window_->GetRect();
276     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
277     startPointY_ = startPointRect_.posY_ +
278                    static_cast<int32_t>(startPointRect_.height_ + hotZone_ * POINT_HOTZONE_RATIO);
279 
280     DoMoveOrDrag(false, true);
281     ASSERT_EQ(WMError::WM_OK, window_->Hide());
282 }
283 
284 /**
285  * @tc.name: DragWindow09
286  * @tc.desc: point in decorZone, uiContent is nullptr
287  * @tc.type: FUNC
288  */
289 HWTEST_F(WindowMoveDragTest, DragWindow09, Function | MediumTest | Level3)
290 {
291     ASSERT_EQ(WMError::WM_OK, window_->Show());
292     usleep(WAIT_SYANC_MS);
293     startPointRect_ = window_->GetRect();
294     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
295     startPointY_ = startPointRect_.posY_ +
296         static_cast<int32_t>(WINDOW_TITLE_BAR_HEIGHT * POINT_HOTZONE_RATIO * virtualPixelRatio_);
297 
298     DoMoveOrDrag(false, false);
299     ASSERT_EQ(WMError::WM_OK, window_->Hide());
300 }
301 
302 /**
303  * @tc.name: DragWindow10
304  * @tc.desc: drag inner
305  * @tc.type: FUNC
306  */
307 HWTEST_F(WindowMoveDragTest, DragWindow10, Function | MediumTest | Level3)
308 {
309     ASSERT_EQ(WMError::WM_OK, window_->Show());
310     usleep(WAIT_SYANC_MS);
311     startPointRect_ = window_->GetRect();
312     startPointX_ = startPointRect_.posX_ + static_cast<int32_t>(startPointRect_.width_ * POINT_HOTZONE_RATIO);
313     startPointY_ = startPointRect_.posY_ + static_cast<int32_t>(startPointRect_.height_ * POINT_HOTZONE_RATIO);
314 
315     DoMoveOrDrag(false, false);
316     ASSERT_EQ(WMError::WM_OK, window_->Hide());
317 }
318 }
319 } // namespace Rosen
320 } // namespace OHOS
321