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
19 #include "singleton_container.h"
20 #include "wm_common.h"
21 #include "window_adapter.h"
22 #include "window_test_utils.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 using Utils = WindowTestUtils;
30 const int WAIT_CALLBACK_US = 10000; // 10000 us
31
32 class WindowTouchOutsideTestListener : public ITouchOutsideListener {
33 public:
OnTouchOutside() const34 void OnTouchOutside() const override
35 {
36 isTouchOutside_ = true;
37 }
38 mutable bool isTouchOutside_ { false };
39 };
40
41 class WindowTouchOutsideTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 virtual void SetUp() override;
46 virtual void TearDown() override;
47
48 static sptr<WindowTouchOutsideTestListener> windowlistener1_;
49 static sptr<WindowTouchOutsideTestListener> windowlistener2_;
50 Utils::TestWindowInfo firstWindowInfo_;
51 Utils::TestWindowInfo secondWindowInfo_;
52 Utils::TestWindowInfo thirdWindowInfo_;
53 };
54
55 sptr<WindowTouchOutsideTestListener> WindowTouchOutsideTest::windowlistener1_ =
56 new WindowTouchOutsideTestListener();
57 sptr<WindowTouchOutsideTestListener> WindowTouchOutsideTest::windowlistener2_ =
58 new WindowTouchOutsideTestListener();
59
SetUp()60 void WindowTouchOutsideTest::SetUp()
61 {
62 firstWindowInfo_ = {
63 .name = "firstWindow",
64 .rect = { 100, 100, 200, 200 },
65 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
66 .mode = WindowMode::WINDOW_MODE_FLOATING,
67 .needAvoid = false,
68 .parentLimit = false,
69 .parentId = INVALID_WINDOW_ID,
70 };
71
72 secondWindowInfo_ = {
73 .name = "secondWindow",
74 .rect = { 400, 400, 200, 200 },
75 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
76 .mode = WindowMode::WINDOW_MODE_FLOATING,
77 .needAvoid = false,
78 .parentLimit = false,
79 .parentId = INVALID_WINDOW_ID,
80 };
81
82 thirdWindowInfo_ = {
83 .name = "thirdWindow",
84 .rect = { 400, 400, 200, 200 },
85 .type = WindowType::WINDOW_TYPE_APP_MAIN_WINDOW,
86 .mode = WindowMode::WINDOW_MODE_FLOATING,
87 .needAvoid = false,
88 .parentLimit = false,
89 .parentId = INVALID_WINDOW_ID,
90 };
91 }
92
TearDown()93 void WindowTouchOutsideTest::TearDown()
94 {
95 windowlistener1_->isTouchOutside_ = false;
96 windowlistener2_->isTouchOutside_ = false;
97 }
98
SetUpTestCase()99 void WindowTouchOutsideTest::SetUpTestCase()
100 {
101 }
102
TearDownTestCase()103 void WindowTouchOutsideTest::TearDownTestCase()
104 {
105 }
106
107 namespace {
108 /**
109 * @tc.name: onTouchInside
110 * @tc.desc: can't not receive a inside touch event
111 * @tc.type: FUNC
112 */
113 HWTEST_F(WindowTouchOutsideTest, onTouchInside, Function | MediumTest | Level3)
114 {
115 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
116 if (firstWindow == nullptr) {
117 return;
118 }
119 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
120 firstWindow->Show();
121 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(firstWindow->GetWindowId());
122 usleep(WAIT_CALLBACK_US);
123 ASSERT_TRUE(!windowlistener1_->isTouchOutside_);
124 firstWindow->Destroy();
125 }
126
127 /**
128 * @tc.name: onTouchOutside
129 * @tc.desc: received an outside touch event when window state is show
130 * @tc.type: FUNC
131 */
132 HWTEST_F(WindowTouchOutsideTest, onTouchOutside, Function | MediumTest | Level3)
133 {
134 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
135 if (firstWindow == nullptr) {
136 return;
137 }
138 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
139 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
140 firstWindow->Show();
141 secondWindow->Show();
142 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
143 usleep(WAIT_CALLBACK_US);
144 ASSERT_TRUE(windowlistener1_->isTouchOutside_);
145 firstWindow->Destroy();
146 secondWindow->Destroy();
147 }
148
149 /**
150 * @tc.name: onTouchOutsideNotShow
151 * @tc.desc: If the window is not in the show state, the touch outside event cannot be received
152 * @tc.type: FUNC
153 */
154 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideNotShow, Function | MediumTest | Level3)
155 {
156 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
157 if (firstWindow == nullptr) {
158 return;
159 }
160 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
161 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
162 secondWindow->Show();
163 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
164 usleep(WAIT_CALLBACK_US);
165 ASSERT_TRUE(!windowlistener1_->isTouchOutside_);
166 firstWindow->Destroy();
167 secondWindow->Destroy();
168 }
169
170 /**
171 * @tc.name: onTouchOutsideForAllWindow
172 * @tc.desc: All windows can receive the touch outside event
173 * @tc.type: FUNC
174 */
175 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideForAllWindow, Function | MediumTest | Level3)
176 {
177 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
178 if (firstWindow == nullptr) {
179 return;
180 }
181 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
182 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
183 firstWindow->RegisterTouchOutsideListener(windowlistener2_);
184
185 firstWindow->Show();
186 secondWindow->Show();
187
188 const sptr<Window> &thirdWindow = Utils::CreateTestWindow(thirdWindowInfo_);
189 thirdWindow->Show();
190 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(thirdWindow->GetWindowId());
191 usleep(WAIT_CALLBACK_US);
192 ASSERT_TRUE(windowlistener1_->isTouchOutside_);
193 ASSERT_TRUE(windowlistener2_->isTouchOutside_);
194 firstWindow->Destroy();
195 secondWindow->Destroy();
196 thirdWindow->Destroy();
197 }
198 } // namespace
199 } // Rosen
200 } // OHOS