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 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
117 firstWindow->Show();
118 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(firstWindow->GetWindowId());
119 usleep(WAIT_CALLBACK_US);
120 ASSERT_TRUE(!windowlistener1_->isTouchOutside_);
121 firstWindow->Destroy();
122 }
123
124 /**
125 * @tc.name: onTouchOutside
126 * @tc.desc: received an outside touch event when window state is show
127 * @tc.type: FUNC
128 */
129 HWTEST_F(WindowTouchOutsideTest, onTouchOutside, Function | MediumTest | Level3)
130 {
131 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
132 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
133 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
134 firstWindow->Show();
135 secondWindow->Show();
136 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
137 usleep(WAIT_CALLBACK_US);
138 ASSERT_TRUE(windowlistener1_->isTouchOutside_);
139 firstWindow->Destroy();
140 secondWindow->Destroy();
141 }
142
143 /**
144 * @tc.name: onTouchOutsideNotShow
145 * @tc.desc: If the window is not in the show state, the touch outside event cannot be received
146 * @tc.type: FUNC
147 */
148 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideNotShow, Function | MediumTest | Level3)
149 {
150 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
151 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
152 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
153 secondWindow->Show();
154 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
155 usleep(WAIT_CALLBACK_US);
156 ASSERT_TRUE(!windowlistener1_->isTouchOutside_);
157 firstWindow->Destroy();
158 secondWindow->Destroy();
159 }
160
161 /**
162 * @tc.name: onTouchOutsideForAllWindow
163 * @tc.desc: All windows can receive the touch outside event
164 * @tc.type: FUNC
165 */
166 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideForAllWindow, Function | MediumTest | Level3)
167 {
168 const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
169 firstWindow->RegisterTouchOutsideListener(windowlistener1_);
170 const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
171 firstWindow->RegisterTouchOutsideListener(windowlistener2_);
172
173 firstWindow->Show();
174 secondWindow->Show();
175
176 const sptr<Window> &thirdWindow = Utils::CreateTestWindow(thirdWindowInfo_);
177 thirdWindow->Show();
178 SingletonContainer::Get<WindowAdapter>().ProcessPointDown(thirdWindow->GetWindowId());
179 usleep(WAIT_CALLBACK_US);
180 ASSERT_TRUE(windowlistener1_->isTouchOutside_);
181 ASSERT_TRUE(windowlistener2_->isTouchOutside_);
182 firstWindow->Destroy();
183 secondWindow->Destroy();
184 thirdWindow->Destroy();
185 }
186 } // namespace
187 } // Rosen
188 } // OHOS