• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 #include <gtest/gtest.h>
17 #include <map>
18 #include "window_adapter_lite.h"
19 #include "window_manager_hilog.h"
20 #include "wm_common.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 namespace {
25     std::string g_logMsg;
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)26     void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char* tag,
27         const char* msg)
28     {
29         g_logMsg = msg;
30     }
31 }
32 
33 namespace OHOS {
34 namespace Rosen {
35 class WindowAdapterLiteTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 };
42 
SetUpTestCase()43 void WindowAdapterLiteTest::SetUpTestCase() {}
44 
TearDownTestCase()45 void WindowAdapterLiteTest::TearDownTestCase() {}
46 
SetUp()47 void WindowAdapterLiteTest::SetUp() {}
48 
TearDown()49 void WindowAdapterLiteTest::TearDown() {}
50 
51 namespace {
52 /**
53  * @tc.name: UnregisterWindowManagerAgent
54  * @tc.desc: WindowAdapterLite/UnregisterWindowManagerAgent
55  * @tc.type: FUNC
56  */
57 HWTEST_F(WindowAdapterLiteTest, UnregisterWindowManagerAgent, TestSize.Level1)
58 {
59     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
60     ASSERT_NE(windowAdapterLite_, nullptr);
61     sptr<IWindowManagerAgent> windowManagerAgent = nullptr;
62 
63     std::set<sptr<IWindowManagerAgent>> iWindowManagerAgent = { nullptr };
64     windowAdapterLite_->windowManagerLiteAgentMap_.insert(
65         std::make_pair(WindowManagerAgentType::WINDOW_MANAGER_AGENT_TYPE_CAMERA_FLOAT, iWindowManagerAgent));
66     int32_t pid = 0;
67     auto ret = windowAdapterLite_->CheckWindowId(0, pid);
68 
69     ASSERT_NE(WMError::WM_OK, ret);
70 }
71 
72 /**
73  * @tc.name: UnregisterWindowManagerAgent01
74  * @tc.desc: WindowAdapterLite/UnregisterWindowManagerAgent
75  * @tc.type: FUNC
76  */
77 HWTEST_F(WindowAdapterLiteTest, UnregisterWindowManagerAgent01, TestSize.Level1)
78 {
79     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
80     ASSERT_NE(windowAdapterLite_, nullptr);
81     windowAdapterLite_->ReregisterWindowManagerLiteAgent();
82     windowAdapterLite_->OnUserSwitch();
83     windowAdapterLite_->ClearWindowAdapter();
84 
85     sptr<WMSDeathRecipient> wmSDeathRecipient = sptr<WMSDeathRecipient>::MakeSptr();
86     ASSERT_NE(wmSDeathRecipient, nullptr);
87     wptr<IRemoteObject> wptrDeath;
88     wmSDeathRecipient->OnRemoteDied(wptrDeath);
89 
90     FocusChangeInfo focusInfo;
91     windowAdapterLite_->GetFocusWindowInfo(focusInfo);
92     WindowModeType windowModeType;
93     windowAdapterLite_->GetWindowModeType(windowModeType);
94     int32_t pid = 0;
95     auto ret = windowAdapterLite_->CheckWindowId(0, pid);
96 
97     ASSERT_NE(WMError::WM_OK, ret);
98 }
99 
100 /**
101  * @tc.name: OnRemoteDied
102  * @tc.desc: WindowAdapterLite/OnRemoteDied
103  * @tc.type: FUNC
104  */
105 HWTEST_F(WindowAdapterLiteTest, OnRemoteDied, TestSize.Level1)
106 {
107     g_logMsg.clear();
108     LOG_SetCallback(MyLogCallback);
109     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
110     ASSERT_NE(windowAdapterLite_, nullptr);
111     windowAdapterLite_->OnUserSwitch();
112 
113     sptr<WMSDeathRecipient> wmSDeathRecipient = sptr<WMSDeathRecipient>::MakeSptr();
114     ASSERT_NE(wmSDeathRecipient, nullptr);
115     wptr<IRemoteObject> wptrDeath = nullptr;
116     wmSDeathRecipient->OnRemoteDied(wptrDeath);
117     EXPECT_TRUE(g_logMsg.find("wptrDeath is null") != std::string::npos);
118     LOG_SetCallback(nullptr);
119 }
120 
121 /**
122  * @tc.name: GetAllMainWindowInfos
123  * @tc.desc: WindowAdapterLite/GetAllMainWindowInfos
124  * @tc.type: FUNC
125  */
126 HWTEST_F(WindowAdapterLiteTest, GetAllMainWindowInfos, TestSize.Level1)
127 {
128     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
129     ASSERT_NE(windowAdapterLite_, nullptr);
130     windowAdapterLite_->OnUserSwitch();
131 
132     MainWindowInfo info;
133     std::vector<MainWindowInfo> infos;
134     infos.push_back(info);
135     ASSERT_EQ(WMError::WM_OK, windowAdapterLite_->GetAllMainWindowInfos(infos));
136 }
137 
138 /**
139  * @tc.name: ClearMainSessions
140  * @tc.desc: WindowAdapterLite/ClearMainSessions
141  * @tc.type: FUNC
142  */
143 HWTEST_F(WindowAdapterLiteTest, ClearMainSessions, TestSize.Level1)
144 {
145     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
146     ASSERT_NE(windowAdapterLite_, nullptr);
147     windowAdapterLite_->OnUserSwitch();
148 
149     const std::vector<int32_t> persistentIds;
150     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, windowAdapterLite_->ClearMainSessions(persistentIds));
151 }
152 
153 /**
154  * @tc.name: ClearMainSessions01
155  * @tc.desc: WindowAdapterLite/ClearMainSessions
156  * @tc.type: FUNC
157  */
158 HWTEST_F(WindowAdapterLiteTest, ClearMainSessions01, TestSize.Level1)
159 {
160     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
161     ASSERT_NE(windowAdapterLite_, nullptr);
162     windowAdapterLite_->OnUserSwitch();
163 
164     std::vector<int32_t> persistentIds;
165     std::vector<int32_t> clearFailedIds;
166     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION,
167         windowAdapterLite_->ClearMainSessions(persistentIds, clearFailedIds));
168 }
169 
170 /**
171  * @tc.name: RaiseWindowToTop
172  * @tc.desc: WindowAdapterLite/RaiseWindowToTop
173  * @tc.type: FUNC
174  */
175 HWTEST_F(WindowAdapterLiteTest, RaiseWindowToTop, TestSize.Level1)
176 {
177     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
178     ASSERT_NE(windowAdapterLite_, nullptr);
179     windowAdapterLite_->OnUserSwitch();
180 
181     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, windowAdapterLite_->RaiseWindowToTop(0));
182 }
183 
184 /**
185  * @tc.name: GetWindowStyleType
186  * @tc.desc: WindowAdapterLite/GetWindowStyleType
187  * @tc.type: FUNC
188  */
189 HWTEST_F(WindowAdapterLiteTest, GetWindowStyleType, TestSize.Level1)
190 {
191     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
192     WindowStyleType windowStyleType = Rosen::WindowStyleType::WINDOW_STYLE_DEFAULT;
193     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, windowAdapterLite_->GetWindowStyleType(windowStyleType));
194 }
195 
196 /**
197  * @tc.name: TerminateSessionByPersistentId
198  * @tc.desc: WindowAdapterLite/TerminateSessionByPersistentId
199  * @tc.type: FUNC
200  */
201 HWTEST_F(WindowAdapterLiteTest, TerminateSessionByPersistentId, TestSize.Level1)
202 {
203     g_logMsg.clear();
204     LOG_SetCallback(MyLogCallback);
205     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
206     ASSERT_NE(windowAdapterLite_, nullptr);
207     windowAdapterLite_->OnUserSwitch();
208 
209     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, windowAdapterLite_->TerminateSessionByPersistentId(0));
210     LOG_SetCallback(nullptr);
211 }
212 
213 /**
214  * @tc.name: CloseTargetFloatWindow
215  * @tc.desc: WindowAdapterLite/CloseTargetFloatWindow
216  * @tc.type: FUNC
217  */
218 HWTEST_F(WindowAdapterLiteTest, CloseTargetFloatWindow, TestSize.Level1)
219 {
220     g_logMsg.clear();
221     LOG_SetCallback(MyLogCallback);
222     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
223     ASSERT_NE(windowAdapterLite_, nullptr);
224     windowAdapterLite_->OnUserSwitch();
225 
226     const std::string& bundleName = "test";
227     ASSERT_EQ(WMError::WM_OK, windowAdapterLite_->CloseTargetFloatWindow(bundleName));
228     LOG_SetCallback(nullptr);
229 }
230 
231 /**
232  * @tc.name: CloseTargetPiPWindow
233  * @tc.desc: WindowAdapterLite/CloseTargetPiPWindow
234  * @tc.type: FUNC
235  */
236 HWTEST_F(WindowAdapterLiteTest, CloseTargetPiPWindow, TestSize.Level1)
237 {
238     g_logMsg.clear();
239     LOG_SetCallback(MyLogCallback);
240     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
241     ASSERT_NE(windowAdapterLite_, nullptr);
242     windowAdapterLite_->OnUserSwitch();
243 
244     const std::string& bundleName = "test";
245     ASSERT_EQ(WMError::WM_OK, windowAdapterLite_->CloseTargetPiPWindow(bundleName));
246     LOG_SetCallback(nullptr);
247 }
248 
249 /**
250  * @tc.name: GetCurrentPiPWindowInfo
251  * @tc.desc: WindowAdapterLite/GetCurrentPiPWindowInfo
252  * @tc.type: FUNC
253  */
254 HWTEST_F(WindowAdapterLiteTest, GetCurrentPiPWindowInfo, TestSize.Level1)
255 {
256     g_logMsg.clear();
257     LOG_SetCallback(MyLogCallback);
258     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
259     ASSERT_NE(windowAdapterLite_, nullptr);
260     windowAdapterLite_->OnUserSwitch();
261 
262     std::string bundleName = "test";
263     ASSERT_EQ(WMError::WM_OK, windowAdapterLite_->GetCurrentPiPWindowInfo(bundleName));
264     LOG_SetCallback(nullptr);
265 }
266 
267 /**
268  * @tc.name: ListWindowInfo01
269  * @tc.desc: WindowAdapter/ListWindowInfo
270  * @tc.type: FUNC
271  */
272 HWTEST_F(WindowAdapterLiteTest, ListWindowInfo01, Function | SmallTest | Level2)
273 {
274     WindowAdapterLite WindowAdapterLite;
275     WindowInfoOption windowInfoOption;
276     std::vector<sptr<WindowInfo>> infos;
277     auto err = WindowAdapterLite.ListWindowInfo(windowInfoOption, infos);
278     ASSERT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, err);
279 }
280 
281 /**
282  * @tc.name: SendPointerEventForHover
283  * @tc.desc: SendPointerEventForHover
284  * @tc.type: FUNC
285  */
286 HWTEST_F(WindowAdapterLiteTest, SendPointerEventForHover, Function | SmallTest | Level2)
287 {
288     std::shared_ptr<WindowAdapterLite> windowAdapterLite_ = std::make_shared<WindowAdapterLite>();
289     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
290     auto err = windowAdapterLite_->SendPointerEventForHover(pointerEvent);
291     EXPECT_EQ(WMError::WM_ERROR_INVALID_PERMISSION, err);
292 }
293 } // namespace
294 } // namespace Rosen
295 } // namespace OHOS