• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ = new TestDragListener();
59 sptr<TestDragListener> WindowDragTest::secondWindowDragListener_ = new TestDragListener();
60 
SetUpTestCase()61 void WindowDragTest::SetUpTestCase() {}
62 
TearDownTestCase()63 void WindowDragTest::TearDownTestCase() {}
64 
SetUp()65 void WindowDragTest::SetUp()
66 {
67     dragWindowInfo_ = {
68         .name = "dragWindow",
69         .rect = { 200, 200, 380, 380 },
70         .type = WindowType::WINDOW_TYPE_DRAGGING_EFFECT,
71         .mode = WindowMode::WINDOW_MODE_FLOATING,
72         .needAvoid = false,
73         .parentLimit = false,
74         .showWhenLocked = true,
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         .showWhenLocked = true,
86         .parentId = INVALID_WINDOW_ID,
87     };
88 
89     secondWindowInfo_ = {
90         .name = "secondWindow",
91         .rect = Utils::customAppRect_,
92         .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
93         .mode = WindowMode::WINDOW_MODE_FLOATING,
94         .needAvoid = false,
95         .parentLimit = false,
96         .showWhenLocked = true,
97         .parentId = INVALID_WINDOW_ID,
98     };
99     activeWindows_.clear();
100 }
101 
TearDown()102 void WindowDragTest::TearDown()
103 {
104     while (!activeWindows_.empty()) {
105         ASSERT_EQ(WMError::WM_OK, activeWindows_.back()->Destroy());
106         activeWindows_.pop_back();
107     }
108 }
109 
110 namespace {
111 /**
112  * @tc.name: DragIn
113  * @tc.desc: Drag a window to another window
114  * @tc.type: FUNC
115  */
116 HWTEST_F(WindowDragTest, DragIn, TestSize.Level1)
117 {
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     EXPECT_EQ(300, firstWindowDragListener_->point_.x);
133     EXPECT_EQ(300, firstWindowDragListener_->point_.y);
134     EXPECT_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, TestSize.Level1)
145 {
146     const sptr<Window>& firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
147     ASSERT_NE(firstWindow, nullptr);
148     activeWindows_.push_back(firstWindow);
149     firstWindow->RegisterDragListener(firstWindowDragListener_);
150     firstWindow->SetTurnScreenOn(true);
151     firstWindow->Show();
152 
153     const sptr<Window>& dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
154     ASSERT_NE(dragWindow, nullptr);
155     activeWindows_.push_back(dragWindow);
156     dragWindow->Show();
157     dragWindow->MoveTo(300, 300);
158 
159     sleep(WAIT_CALLBACK_US);
160     EXPECT_EQ(300, firstWindowDragListener_->point_.x);
161     EXPECT_EQ(300, firstWindowDragListener_->point_.y);
162     EXPECT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
163 
164     dragWindow->MoveTo(400, 400);
165     sleep(WAIT_CALLBACK_US);
166     EXPECT_EQ(400, firstWindowDragListener_->point_.x);
167     EXPECT_EQ(400, firstWindowDragListener_->point_.y);
168     EXPECT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
169 
170     firstWindow->UnregisterDragListener(firstWindowDragListener_);
171 }
172 
173 /**
174  * @tc.name: DragOut
175  * @tc.desc: Drag the drag window out of the current window
176  * @tc.type: FUNC
177  */
178 HWTEST_F(WindowDragTest, DragOut, TestSize.Level1)
179 {
180     const sptr<Window>& firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
181     ASSERT_NE(firstWindow, nullptr);
182     activeWindows_.push_back(firstWindow);
183     firstWindow->RegisterDragListener(firstWindowDragListener_);
184     firstWindow->SetTurnScreenOn(true);
185     firstWindow->Show();
186 
187     secondWindowInfo_.rect = { 500, 500, 500, 500 };
188     const sptr<Window>& secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
189     ASSERT_NE(secondWindow, nullptr);
190     activeWindows_.push_back(secondWindow);
191     secondWindow->RegisterDragListener(secondWindowDragListener_);
192     secondWindow->Show();
193 
194     const sptr<Window>& dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
195     ASSERT_NE(secondWindow, nullptr);
196     activeWindows_.push_back(dragWindow);
197     dragWindow->Show();
198     dragWindow->MoveTo(300, 300);
199 
200     sleep(WAIT_CALLBACK_US);
201     EXPECT_EQ(300, firstWindowDragListener_->point_.x);
202     EXPECT_EQ(300, firstWindowDragListener_->point_.y);
203     EXPECT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
204 
205     dragWindow->MoveTo(400, 400);
206     sleep(WAIT_CALLBACK_US);
207     EXPECT_EQ(400, firstWindowDragListener_->point_.x);
208     EXPECT_EQ(400, firstWindowDragListener_->point_.y);
209     EXPECT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
210 
211     dragWindow->MoveTo(600, 600);
212     sleep(WAIT_CALLBACK_US);
213     EXPECT_EQ(100, secondWindowDragListener_->point_.x);
214     EXPECT_EQ(100, secondWindowDragListener_->point_.y);
215     EXPECT_EQ(DragEvent::DRAG_EVENT_IN, secondWindowDragListener_->event_);
216     EXPECT_EQ(DragEvent::DRAG_EVENT_OUT, firstWindowDragListener_->event_);
217 
218     firstWindow->UnregisterDragListener(firstWindowDragListener_);
219     secondWindow->UnregisterDragListener(secondWindowDragListener_);
220 }
221 
222 /**
223  * @tc.name: DragEnd
224  * @tc.desc: End window drag
225  * @tc.type: FUNC
226  */
227 HWTEST_F(WindowDragTest, DragEnd, TestSize.Level1)
228 {
229     const sptr<Window> firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
230     ASSERT_NE(nullptr, firstWindow);
231     firstWindow->RegisterDragListener(firstWindowDragListener_);
232     firstWindow->SetTurnScreenOn(true);
233     firstWindow->Show();
234 
235     const sptr<Window> dragWindow = Utils::CreateTestWindow(dragWindowInfo_);
236     ASSERT_NE(nullptr, dragWindow);
237     dragWindow->Show();
238     dragWindow->MoveTo(199, 199);
239 
240     sleep(WAIT_CALLBACK_US);
241     dragWindow->Destroy();
242     sleep(WAIT_CALLBACK_US);
243     ASSERT_EQ(DragEvent::DRAG_EVENT_END, firstWindowDragListener_->event_);
244 
245     firstWindow->UnregisterDragListener(firstWindowDragListener_);
246     firstWindow->Destroy();
247 }
248 } // namespace
249 } // namespace Rosen
250 } // namespace OHOS
251