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