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