• 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 = 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         .parentName = "",
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         .parentName = "",
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         .parentName = "",
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->Show();
111 
112     const sptr<Window> &dragWindow = utils::CreateTestWindow(dragWindowInfo_);
113     dragWindow->Show();
114     dragWindow->MoveTo(300, 300);
115     usleep(WAIT_CALLBACK_US);
116     ASSERT_EQ(300, firstWindowDragListener_->point_.x);
117     ASSERT_EQ(300, firstWindowDragListener_->point_.y);
118     ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
119 
120     dragWindow->Destroy();
121     firstWindow->UnregisterDragListener(firstWindowDragListener_);
122     firstWindow->Destroy();
123 }
124 
125 /**
126  * @tc.name: DragMove
127  * @tc.desc: Window Move
128  * @tc.type: FUNC
129  */
130 HWTEST_F(WindowDragTest, DragMove, Function | MediumTest | Level3) {
131     const sptr<Window> &firstWindow = utils::CreateTestWindow(firstWindowInfo_);
132     firstWindow->RegisterDragListener(firstWindowDragListener_);
133     firstWindow->Show();
134 
135     const sptr<Window> &dragWindow = utils::CreateTestWindow(dragWindowInfo_);
136     dragWindow->Show();
137     dragWindow->MoveTo(300, 300);
138     usleep(WAIT_CALLBACK_US);
139     ASSERT_EQ(300, firstWindowDragListener_->point_.x);
140     ASSERT_EQ(300, firstWindowDragListener_->point_.y);
141     ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
142 
143     dragWindow->MoveTo(400, 400);
144     usleep(WAIT_CALLBACK_US);
145     ASSERT_EQ(400, firstWindowDragListener_->point_.x);
146     ASSERT_EQ(400, firstWindowDragListener_->point_.y);
147     ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
148 
149     dragWindow->Destroy();
150     firstWindow->UnregisterDragListener(firstWindowDragListener_);
151     firstWindow->Destroy();
152 }
153 
154 /**
155  * @tc.name: DragOut
156  * @tc.desc: Drag the drag window out of the current window
157  * @tc.type: FUNC
158  */
159 HWTEST_F(WindowDragTest, DragOut, Function | MediumTest | Level3) {
160     const sptr<Window> &firstWindow = utils::CreateTestWindow(firstWindowInfo_);
161     firstWindow->RegisterDragListener(firstWindowDragListener_);
162     firstWindow->Show();
163 
164     secondWindowInfo_.rect = {500, 500, 500, 500};
165     const sptr<Window> &secondWindow = utils::CreateTestWindow(secondWindowInfo_);
166     secondWindow->RegisterDragListener(secondWindowDragListener_);
167     secondWindow->Show();
168 
169     const sptr<Window> &dragWindow = utils::CreateTestWindow(dragWindowInfo_);
170     dragWindow->Show();
171     dragWindow->MoveTo(300, 300);
172     usleep(WAIT_CALLBACK_US);
173     ASSERT_EQ(300, firstWindowDragListener_->point_.x);
174     ASSERT_EQ(300, firstWindowDragListener_->point_.y);
175     ASSERT_EQ(DragEvent::DRAG_EVENT_IN, firstWindowDragListener_->event_);
176 
177     dragWindow->MoveTo(400, 400);
178     usleep(WAIT_CALLBACK_US);
179     ASSERT_EQ(400, firstWindowDragListener_->point_.x);
180     ASSERT_EQ(400, firstWindowDragListener_->point_.y);
181     ASSERT_EQ(DragEvent::DRAG_EVENT_MOVE, firstWindowDragListener_->event_);
182 
183     dragWindow->MoveTo(600, 600);
184     usleep(WAIT_CALLBACK_US);
185     ASSERT_EQ(100, secondWindowDragListener_->point_.x);
186     ASSERT_EQ(100, secondWindowDragListener_->point_.y);
187     ASSERT_EQ(DragEvent::DRAG_EVENT_IN, secondWindowDragListener_->event_);
188     ASSERT_EQ(DragEvent::DRAG_EVENT_OUT, firstWindowDragListener_->event_);
189 
190     dragWindow->Destroy();
191     firstWindow->UnregisterDragListener(firstWindowDragListener_);
192     secondWindow->UnregisterDragListener(secondWindowDragListener_);
193     firstWindow->Destroy();
194     secondWindow->Destroy();
195 }
196 
197 /**
198  * @tc.name: DragEnd
199  * @tc.desc: End window drag
200  * @tc.type: FUNC
201  */
202 HWTEST_F(WindowDragTest, DragEnd, Function | MediumTest | Level3) {
203     const sptr<Window> &firstWindow = utils::CreateTestWindow(firstWindowInfo_);
204     firstWindow->RegisterDragListener(firstWindowDragListener_);
205     firstWindow->Show();
206 
207     const sptr<Window> &dragWindow = utils::CreateTestWindow(dragWindowInfo_);
208     dragWindow->Show();
209     dragWindow->MoveTo(199, 199);
210     usleep(WAIT_CALLBACK_US);
211     dragWindow->Destroy();
212     usleep(WAIT_CALLBACK_US);
213     ASSERT_EQ(DragEvent::DRAG_EVENT_END, firstWindowDragListener_->event_);
214     firstWindow->UnregisterDragListener(firstWindowDragListener_);
215     firstWindow->Destroy();
216 }
217 } // namespace
218 } // namespace Rosen
219 } // namespace OHOS