1 /*
2 * Copyright (c) 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 // gtest
17 #include <gtest/gtest.h>
18 #include "window_test_utils.h"
19 #include "wm_common.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 using Utils = WindowTestUtils;
27 const int WAIT_CALLBACK_US = 1; // 1s
28
29 class TestDragListener : public IWindowDragListener {
30 public:
31 PointInfo point_ { 0, 0 };
32 DragEvent event_ = DragEvent::DRAG_EVENT_END;
33 void OnDrag(int32_t x, int32_t y, DragEvent event) override;
34 };
35
OnDrag(int32_t x,int32_t y,DragEvent event)36 void TestDragListener::OnDrag(int32_t x, int32_t y, DragEvent event)
37 {
38 event_ = event;
39 point_.x = x;
40 point_.y = y;
41 }
42
43 class WindowDragTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 virtual void SetUp() override;
48 virtual void TearDown() override;
49
50 static sptr<TestDragListener> firstWindowDragListener_;
51 static sptr<TestDragListener> secondWindowDragListener_;
52 Utils::TestWindowInfo dragWindowInfo_;
53 Utils::TestWindowInfo firstWindowInfo_;
54 Utils::TestWindowInfo secondWindowInfo_;
55 std::vector<sptr<Window>> activeWindows_;
56 };
57
58 sptr<TestDragListener> WindowDragTest::firstWindowDragListener_ =
59 new TestDragListener();
60 sptr<TestDragListener> WindowDragTest::secondWindowDragListener_ =
61 new TestDragListener();
62
SetUpTestCase()63 void WindowDragTest::SetUpTestCase() {}
64
TearDownTestCase()65 void WindowDragTest::TearDownTestCase() {}
66
SetUp()67 void WindowDragTest::SetUp()
68 {
69 dragWindowInfo_ = {
70 .name = "dragWindow",
71 .rect = {200, 200, 380, 380},
72 .type = WindowType::WINDOW_TYPE_DRAGGING_EFFECT,
73 .mode = WindowMode::WINDOW_MODE_FLOATING,
74 .needAvoid = false,
75 .parentLimit = false,
76 .showWhenLocked = true,
77 .parentId = INVALID_WINDOW_ID,
78 };
79
80 firstWindowInfo_ = {
81 .name = "firstWindow",
82 .rect = Utils::customAppRect_,
83 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
84 .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
85 .needAvoid = false,
86 .parentLimit = false,
87 .showWhenLocked = true,
88 .parentId = INVALID_WINDOW_ID,
89 };
90
91 secondWindowInfo_ = {
92 .name = "secondWindow",
93 .rect = Utils::customAppRect_,
94 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
95 .mode = WindowMode::WINDOW_MODE_FLOATING,
96 .needAvoid = false,
97 .parentLimit = false,
98 .showWhenLocked = true,
99 .parentId = INVALID_WINDOW_ID,
100 };
101 activeWindows_.clear();
102 }
103
TearDown()104 void WindowDragTest::TearDown() {
105 while (!activeWindows_.empty()) {
106 ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
107 activeWindows_.pop_back();
108 }
109 }
110
111 namespace {
112 /**
113 * @tc.name: DragIn
114 * @tc.desc: Drag a window to another window
115 * @tc.type: FUNC
116 */
117 HWTEST_F(WindowDragTest, DragIn, Function | MediumTest | Level3) {
118 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
119 ASSERT_NE(firstWindow, nullptr);
120 activeWindows_.push_back(firstWindow);
121 firstWindow->RegisterDragListener(firstWindowDragListener_);
122 firstWindow->SetTurnScreenOn(true);
123 firstWindow->Show();
124
125 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
126 ASSERT_NE(dragWindow, nullptr);
127 activeWindows_.push_back(dragWindow);
128 dragWindow->Show();
129 dragWindow->MoveTo(300, 300);
130 sleep(WAIT_CALLBACK_US);
131
132 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
133 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
134 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
135
136 firstWindow->UnregisterDragListener(firstWindowDragListener_);
137 }
138
139 /**
140 * @tc.name: DragMove
141 * @tc.desc: Window Move
142 * @tc.type: FUNC
143 */
144 HWTEST_F(WindowDragTest, DragMove, Function | MediumTest | Level3) {
145 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
146 ASSERT_NE(firstWindow, nullptr);
147 activeWindows_.push_back(firstWindow);
148 firstWindow->RegisterDragListener(firstWindowDragListener_);
149 firstWindow->SetTurnScreenOn(true);
150 firstWindow->Show();
151
152 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
153 ASSERT_NE(dragWindow, nullptr);
154 activeWindows_.push_back(dragWindow);
155 dragWindow->Show();
156 dragWindow->MoveTo(300, 300);
157
158 sleep(WAIT_CALLBACK_US);
159 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
160 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
161 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
162
163 dragWindow->MoveTo(400, 400);
164 sleep(WAIT_CALLBACK_US);
165 ASSERT_EQ(400, firstWindowDragListener_->point_.x);
166 ASSERT_EQ(400, firstWindowDragListener_->point_.y);
167 ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
168
169 firstWindow->UnregisterDragListener(firstWindowDragListener_);
170 }
171
172 /**
173 * @tc.name: DragOut
174 * @tc.desc: Drag the drag window out of the current window
175 * @tc.type: FUNC
176 */
177 HWTEST_F(WindowDragTest, DragOut, Function | MediumTest | Level3) {
178 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
179 ASSERT_NE(firstWindow, nullptr);
180 activeWindows_.push_back(firstWindow);
181 firstWindow->RegisterDragListener(firstWindowDragListener_);
182 firstWindow->SetTurnScreenOn(true);
183 firstWindow->Show();
184
185 secondWindowInfo_.rect = {500, 500, 500, 500};
186 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
187 ASSERT_NE(secondWindow, nullptr);
188 activeWindows_.push_back(secondWindow);
189 secondWindow->RegisterDragListener(secondWindowDragListener_);
190 secondWindow->Show();
191
192 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
193 ASSERT_NE(secondWindow, nullptr);
194 activeWindows_.push_back(dragWindow);
195 dragWindow->Show();
196 dragWindow->MoveTo(300, 300);
197
198 sleep(WAIT_CALLBACK_US);
199 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
200 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
201 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
202
203 dragWindow->MoveTo(400, 400);
204 sleep(WAIT_CALLBACK_US);
205 ASSERT_EQ(400, firstWindowDragListener_->point_.x);
206 ASSERT_EQ(400, firstWindowDragListener_->point_.y);
207 ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
208
209 dragWindow->MoveTo(600, 600);
210 sleep(WAIT_CALLBACK_US);
211 ASSERT_EQ(100, secondWindowDragListener_->point_.x);
212 ASSERT_EQ(100, secondWindowDragListener_->point_.y);
213 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, secondWindowDragListener_->event_);
214 ASSERT_EQ(DragEvent::DRAG_EVENT_OUT, firstWindowDragListener_->event_);
215
216 firstWindow->UnregisterDragListener(firstWindowDragListener_);
217 secondWindow->UnregisterDragListener(secondWindowDragListener_);
218 }
219
220 /**
221 * @tc.name: DragEnd
222 * @tc.desc: End window drag
223 * @tc.type: FUNC
224 */
225 HWTEST_F(WindowDragTest, DragEnd, Function | MediumTest | Level3) {
226 const sptr<Window> firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
227 ASSERT_NE(nullptr, firstWindow);
228 firstWindow->RegisterDragListener(firstWindowDragListener_);
229 firstWindow->SetTurnScreenOn(true);
230 firstWindow->Show();
231
232 const sptr<Window> dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
233 ASSERT_NE(nullptr, dragWindow);
234 dragWindow->Show();
235 dragWindow->MoveTo(199, 199);
236
237 sleep(WAIT_CALLBACK_US);
238 dragWindow->Destroy();
239 sleep(WAIT_CALLBACK_US);
240 ASSERT_EQ(DragEvent::DRAG_EVENT_END, firstWindowDragListener_->event_);
241
242 firstWindow->UnregisterDragListener(firstWindowDragListener_);
243 firstWindow->Destroy();
244 }
245 } // namespace
246 } // namespace Rosen
247 } // namespace OHOS
248