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     ASSERT_NE(nullptr, secondWindow);
141     firstWindow->Show();
142     secondWindow->Show();
143     SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
144     usleep(WAIT_CALLBACK_US);
145     ASSERT_TRUE(windowlistener1_->isTouchOutside_);
146     firstWindow->Destroy();
147     secondWindow->Destroy();
148 }
149 
150 /**
151  * @tc.name: onTouchOutsideNotShow
152  * @tc.desc: If the window is not in the show state, the touch outside event cannot be received
153  * @tc.type: FUNC
154  */
155 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideNotShow, Function | MediumTest | Level3)
156 {
157     const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
158     if (firstWindow == nullptr) {
159         return;
160     }
161     firstWindow->RegisterTouchOutsideListener(windowlistener1_);
162     const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
163     ASSERT_NE(nullptr, secondWindow);
164     secondWindow->Show();
165     SingletonContainer::Get<WindowAdapter>().ProcessPointDown(secondWindow->GetWindowId());
166     usleep(WAIT_CALLBACK_US);
167     ASSERT_TRUE(!windowlistener1_->isTouchOutside_);
168     firstWindow->Destroy();
169     secondWindow->Destroy();
170 }
171 
172 /**
173  * @tc.name: onTouchOutsideForAllWindow
174  * @tc.desc: All windows can receive the touch outside event
175  * @tc.type: FUNC
176  */
177 HWTEST_F(WindowTouchOutsideTest, onTouchOutsideForAllWindow, Function | MediumTest | Level3)
178 {
179     const sptr<Window> &firstWindow = Utils::CreateTestWindow(firstWindowInfo_);
180     if (firstWindow == nullptr) {
181         return;
182     }
183     firstWindow->RegisterTouchOutsideListener(windowlistener1_);
184     const sptr<Window> &secondWindow = Utils::CreateTestWindow(secondWindowInfo_);
185     ASSERT_NE(nullptr, secondWindow);
186     firstWindow->RegisterTouchOutsideListener(windowlistener2_);
187 
188     firstWindow->Show();
189     secondWindow->Show();
190 
191     const sptr<Window> &thirdWindow = Utils::CreateTestWindow(thirdWindowInfo_);
192     ASSERT_NE(nullptr, thirdWindow);
193     thirdWindow->Show();
194     SingletonContainer::Get<WindowAdapter>().ProcessPointDown(thirdWindow->GetWindowId());
195     usleep(WAIT_CALLBACK_US);
196     ASSERT_TRUE(windowlistener1_->isTouchOutside_);
197     ASSERT_TRUE(windowlistener2_->isTouchOutside_);
198     firstWindow->Destroy();
199     secondWindow->Destroy();
200     thirdWindow->Destroy();
201 }
202 } // namespace
203 } // Rosen
204 } // OHOS