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 = 100000; // 100000 us
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 };
56
57 sptr<TestDragListener> WindowDragTest::firstWindowDragListener_ =
58 new TestDragListener();
59 sptr<TestDragListener> WindowDragTest::secondWindowDragListener_ =
60 new TestDragListener();
61
SetUpTestCase()62 void WindowDragTest::SetUpTestCase() {}
63
TearDownTestCase()64 void WindowDragTest::TearDownTestCase() {}
65
SetUp()66 void WindowDragTest::SetUp()
67 {
68 dragWindowInfo_ = {
69 .name = "dragWindow",
70 .rect = {200, 200, 380, 380},
71 .type = WindowType::WINDOW_TYPE_DRAGGING_EFFECT,
72 .mode = WindowMode::WINDOW_MODE_FLOATING,
73 .needAvoid = false,
74 .parentLimit = false,
75 .parentId = INVALID_WINDOW_ID,
76 };
77
78 firstWindowInfo_ = {
79 .name = "firstWindow",
80 .rect = Utils::customAppRect_,
81 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
82 .mode = WindowMode::WINDOW_MODE_FULLSCREEN,
83 .needAvoid = false,
84 .parentLimit = false,
85 .parentId = INVALID_WINDOW_ID,
86 };
87
88 secondWindowInfo_ = {
89 .name = "secondWindow",
90 .rect = Utils::customAppRect_,
91 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
92 .mode = WindowMode::WINDOW_MODE_FLOATING,
93 .needAvoid = false,
94 .parentLimit = false,
95 .parentId = INVALID_WINDOW_ID,
96 };
97 }
98
TearDown()99 void WindowDragTest::TearDown() {}
100
101 namespace {
102 /**
103 * @tc.name: DragIn
104 * @tc.desc: Drag a window to another window
105 * @tc.type: FUNC
106 */
107 HWTEST_F(WindowDragTest, DragIn, Function | MediumTest | Level3) {
108 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
109 firstWindow->RegisterDragListener(firstWindowDragListener_);
110 firstWindow->SetTurnScreenOn(true);
111 firstWindow->Show();
112
113 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
114 dragWindow->Show();
115 dragWindow->MoveTo(300, 300);
116 usleep(WAIT_CALLBACK_US);
117 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
118 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
119 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
120
121 dragWindow->Destroy();
122 firstWindow->UnregisterDragListener(firstWindowDragListener_);
123 firstWindow->Destroy();
124 }
125
126 /**
127 * @tc.name: DragMove
128 * @tc.desc: Window Move
129 * @tc.type: FUNC
130 */
131 HWTEST_F(WindowDragTest, DragMove, Function | MediumTest | Level3) {
132 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
133 firstWindow->RegisterDragListener(firstWindowDragListener_);
134 firstWindow->SetTurnScreenOn(true);
135 firstWindow->Show();
136
137 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
138 dragWindow->Show();
139 dragWindow->MoveTo(300, 300);
140 usleep(WAIT_CALLBACK_US);
141 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
142 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
143 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
144
145 dragWindow->MoveTo(400, 400);
146 usleep(WAIT_CALLBACK_US);
147 ASSERT_EQ(400, firstWindowDragListener_->point_.x);
148 ASSERT_EQ(400, firstWindowDragListener_->point_.y);
149 ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
150
151 dragWindow->Destroy();
152 firstWindow->UnregisterDragListener(firstWindowDragListener_);
153 firstWindow->Destroy();
154 }
155
156 /**
157 * @tc.name: DragOut
158 * @tc.desc: Drag the drag window out of the current window
159 * @tc.type: FUNC
160 */
161 HWTEST_F(WindowDragTest, DragOut, Function | MediumTest | Level3) {
162 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
163 firstWindow->RegisterDragListener(firstWindowDragListener_);
164 firstWindow->SetTurnScreenOn(true);
165 firstWindow->Show();
166
167 secondWindowInfo_.rect = {500, 500, 500, 500};
168 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
169 secondWindow->RegisterDragListener(secondWindowDragListener_);
170 secondWindow->Show();
171
172 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
173 dragWindow->Show();
174 dragWindow->MoveTo(300, 300);
175 usleep(WAIT_CALLBACK_US);
176 ASSERT_EQ(300, firstWindowDragListener_->point_.x);
177 ASSERT_EQ(300, firstWindowDragListener_->point_.y);
178 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
179
180 dragWindow->MoveTo(400, 400);
181 usleep(WAIT_CALLBACK_US);
182 ASSERT_EQ(400, firstWindowDragListener_->point_.x);
183 ASSERT_EQ(400, firstWindowDragListener_->point_.y);
184 ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
185
186 dragWindow->MoveTo(600, 600);
187 usleep(WAIT_CALLBACK_US);
188 ASSERT_EQ(100, secondWindowDragListener_->point_.x);
189 ASSERT_EQ(100, secondWindowDragListener_->point_.y);
190 ASSERT_EQ(DragEvent::DRAG_EVENT_IN, secondWindowDragListener_->event_);
191 ASSERT_EQ(DragEvent::DRAG_EVENT_OUT, firstWindowDragListener_->event_);
192
193 dragWindow->Destroy();
194 firstWindow->UnregisterDragListener(firstWindowDragListener_);
195 secondWindow->UnregisterDragListener(secondWindowDragListener_);
196 firstWindow->Destroy();
197 secondWindow->Destroy();
198 }
199
200 /**
201 * @tc.name: DragEnd
202 * @tc.desc: End window drag
203 * @tc.type: FUNC
204 */
205 HWTEST_F(WindowDragTest, DragEnd, Function | MediumTest | Level3) {
206 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
207 firstWindow->RegisterDragListener(firstWindowDragListener_);
208 firstWindow->SetTurnScreenOn(true);
209 firstWindow->Show();
210
211 const sptr<Window> &dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
212 dragWindow->Show();
213 dragWindow->MoveTo(199, 199);
214 usleep(WAIT_CALLBACK_US);
215 dragWindow->Destroy();
216 usleep(WAIT_CALLBACK_US);
217 ASSERT_EQ(DragEvent::DRAG_EVENT_END, firstWindowDragListener_->event_);
218 firstWindow->UnregisterDragListener(firstWindowDragListener_);
219 firstWindow->Destroy();
220 }
221 } // namespace
222 } // namespace Rosen
223 } // namespace OHOS
224