1 /*
2 * Copyright (c) 2022-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 "screen_client_window_adapter_test.h"
17 #include "accesstoken_kit.h"
18 #include "display_manager.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace DistributedHardware {
26 constexpr static uint32_t VIDEO_DATA_NUM = 480;
27
SetUpTestCase(void)28 void ScreenClientWindowAdapterTest::SetUpTestCase(void) {}
29
TearDownTestCase(void)30 void ScreenClientWindowAdapterTest::TearDownTestCase(void) {}
31
SetUp()32 void ScreenClientWindowAdapterTest::SetUp()
33 {
34 const char **perms = new const char *[1];
35 perms[0] = "ohos.permission.SYSTEM_FLOAT_WINDOW";
36 NativeTokenInfoParams infoInstance = {
37 .dcapsNum = 0,
38 .permsNum = 1,
39 .aclsNum = 0,
40 .dcaps = nullptr,
41 .perms = perms,
42 .acls = nullptr,
43 .processName = "screen_client_unittest",
44 .aplStr = "system_core",
45 };
46 uint64_t tokenId = GetAccessTokenId(&infoInstance);
47 SetSelfTokenID(tokenId);
48 Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
49 delete[] perms;
50
51 windowProperty_ = std::make_shared<WindowProperty>();
52 windowProperty_->width = VIDEO_DATA_NUM;
53 windowProperty_->height = VIDEO_DATA_NUM;
54 windowProperty_->displayId = Rosen::DisplayManager::GetInstance().GetDefaultDisplay()->GetId();
55 }
56
TearDown()57 void ScreenClientWindowAdapterTest::TearDown()
58 {
59 windowProperty_ = nullptr;
60 }
61
62 /**
63 * @tc.name: CreateWindow_001
64 * @tc.desc: Verify the CreateWindow function.
65 * @tc.type: FUNC
66 * @tc.require: Issue Number
67 */
68 HWTEST_F(ScreenClientWindowAdapterTest, CreateWindow_001, TestSize.Level1)
69 {
70 windowProperty_ = nullptr;
71 EXPECT_EQ(nullptr, ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, 0));
72 }
73
74 /**
75 * @tc.name: CreateWindow_002
76 * @tc.desc: Verify the CreateWindow function.
77 * @tc.type: FUNC
78 * @tc.require: Issue Number
79 */
80 HWTEST_F(ScreenClientWindowAdapterTest, CreateWindow_002, TestSize.Level1)
81 {
82 int32_t windowId = 0;
83 sptr<Surface> actualSurface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, windowId);
84 EXPECT_NE(nullptr, actualSurface);
85 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
86 EXPECT_EQ(DH_SUCCESS, ret);
87 }
88
89 /**
90 * @tc.name: ShowWindow_001
91 * @tc.desc: Verify the ShowWindow function.
92 * @tc.type: FUNC
93 * @tc.require: Issue Number
94 */
95 HWTEST_F(ScreenClientWindowAdapterTest, ShowWindow_001, TestSize.Level1)
96 {
97 int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(100);
98 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR, ret);
99 }
100
101 /**
102 * @tc.name: ShowWindow_002
103 * @tc.desc: Verify the ShowWindow function.
104 * @tc.type: FUNC
105 * @tc.require: Issue Number
106 */
107 HWTEST_F(ScreenClientWindowAdapterTest, ShowWindow_002, TestSize.Level1)
108 {
109 int32_t windowId = 100;
110 ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, windowId);
111 int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId);
112 EXPECT_EQ(DH_SUCCESS, ret);
113 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
114 EXPECT_EQ(DH_SUCCESS, ret);
115 }
116
117 /**
118 * @tc.name: ShowWindow_003
119 * @tc.desc: Verify the ShowWindow function.
120 * @tc.type: FUNC
121 * @tc.require: Issue Number
122 */
123 HWTEST_F(ScreenClientWindowAdapterTest, ShowWindow_003, TestSize.Level1)
124 {
125 int32_t windowId = 0;
126 ScreenClientWindowAdapter::GetInstance().windowIdMap_.emplace(windowId, nullptr);
127 int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId);
128 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR, ret);
129 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
130 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, ret);
131 }
132 /**
133 * @tc.name: HideWindow_001
134 * @tc.desc: Verify the HideWindow function.
135 * @tc.type: FUNC
136 * @tc.require: Issue Number
137 */
138 HWTEST_F(ScreenClientWindowAdapterTest, HideWindow_001, TestSize.Level1)
139 {
140 int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(0);
141 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR, ret);
142 }
143
144 /**
145 * @tc.name: HideWindow_001
146 * @tc.desc: Verify the HideWindow function.
147 * @tc.type: FUNC
148 * @tc.require: Issue Number
149 */
150 HWTEST_F(ScreenClientWindowAdapterTest, HideWindow_002, TestSize.Level1)
151 {
152 int32_t windowId = 0;
153 ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, windowId);
154 int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(windowId);
155 EXPECT_EQ(DH_SUCCESS, ret);
156 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
157 EXPECT_EQ(DH_SUCCESS, ret);
158 }
159
160 /**
161 * @tc.name: HideWindow_003
162 * @tc.desc: Verify the HideWindow function.
163 * @tc.type: FUNC
164 * @tc.require: Issue Number
165 */
166 HWTEST_F(ScreenClientWindowAdapterTest, HideWindow_003, TestSize.Level1)
167 {
168 int32_t windowId = 0;
169 ScreenClientWindowAdapter::GetInstance().windowIdMap_.emplace(windowId, nullptr);
170 int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(windowId);
171 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR, ret);
172 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
173 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, ret);
174 }
175
176 /**
177 * @tc.name: MoveWindow_001
178 * @tc.desc: Verify the MoveWindow function.
179 * @tc.type: FUNC
180 * @tc.require: Issue Number
181 */
182 HWTEST_F(ScreenClientWindowAdapterTest, MoveWindow_001, TestSize.Level1)
183 {
184 int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(0, 0, 0);
185 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR, ret);
186 }
187
188 /**
189 * @tc.name: MoveWindow_002
190 * @tc.desc: Verify the MoveWindow function.
191 * @tc.type: FUNC
192 * @tc.require: Issue Number
193 */
194 HWTEST_F(ScreenClientWindowAdapterTest, MoveWindow_002, TestSize.Level1)
195 {
196 int32_t windowId = 0;
197 ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, windowId);
198 int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, 0, 0);
199 EXPECT_EQ(DH_SUCCESS, ret);
200 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
201 EXPECT_EQ(DH_SUCCESS, ret);
202 }
203
204 /**
205 * @tc.name: MoveWindow_003
206 * @tc.desc: Verify the MoveWindow function.
207 * @tc.type: FUNC
208 * @tc.require: Issue Number
209 */
210 HWTEST_F(ScreenClientWindowAdapterTest, MoveWindow_003, TestSize.Level1)
211 {
212 int32_t windowId = 0;
213 ScreenClientWindowAdapter::GetInstance().windowIdMap_.emplace(windowId, nullptr);
214 int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, 0, 0);
215 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR, ret);
216 ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
217 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, ret);
218 }
219
220 /**
221 * @tc.name: RemoveWindow_001
222 * @tc.desc: Verify the RemoveWindow function.
223 * @tc.type: FUNC
224 * @tc.require: Issue Number
225 */
226 HWTEST_F(ScreenClientWindowAdapterTest, RemoveWindow_001, TestSize.Level1)
227 {
228 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(0);
229 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, ret);
230 }
231
232 /**
233 * @tc.name: RemoveWindow_002
234 * @tc.desc: Verify the RemoveWindow function.
235 * @tc.type: FUNC
236 * @tc.require: Issue Number
237 */
238 HWTEST_F(ScreenClientWindowAdapterTest, RemoveWindow_002, TestSize.Level1)
239 {
240 int32_t windowId = 0;
241 ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty_, windowId);
242 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
243 EXPECT_EQ(DH_SUCCESS, ret);
244 }
245
246 /**
247 * @tc.name: RemoveWindow_003
248 * @tc.desc: Verify the RemoveWindow function.
249 * @tc.type: FUNC
250 * @tc.require: Issue Number
251 */
252 HWTEST_F(ScreenClientWindowAdapterTest, RemoveWindow_003, TestSize.Level1)
253 {
254 int32_t windowId = 0;
255 ScreenClientWindowAdapter::GetInstance().windowIdMap_.emplace(windowId, nullptr);
256 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
257 EXPECT_EQ(ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR, ret);
258 }
259
260 /**
261 * @tc.name: DestroyAllWindow_001
262 * @tc.desc: Verify the DestroyAllWindow function.
263 * @tc.type: FUNC
264 * @tc.require: Issue Number
265 */
266 HWTEST_F(ScreenClientWindowAdapterTest, DestroyAllWindow_001, TestSize.Level1)
267 {
268 ScreenClientWindowAdapter::GetInstance().windowIdMap_.emplace(0, nullptr);
269 int32_t ret = ScreenClientWindowAdapter::GetInstance().DestroyAllWindow();
270 EXPECT_EQ(DH_SUCCESS, ret);
271 }
272
273 /**
274 * @tc.name: ScreenClientInputEventListener_OnInputEvent_001
275 * @tc.desc: Verify the OnInputEvent function.
276 * @tc.type: FUNC
277 * @tc.require: Issue Number
278 */
279 HWTEST_F(ScreenClientWindowAdapterTest, ScreenClientInputEventListener_OnInputEvent_001, TestSize.Level1)
280 {
281 std::shared_ptr<MMI::PointerEvent> pointerEvent = nullptr;
282 std::shared_ptr<ScreenClientInputEventListener> listener = std::make_shared<ScreenClientInputEventListener>();
283 EXPECT_FALSE(listener->OnInputEvent(pointerEvent));
284
285 pointerEvent = MMI::PointerEvent::Create();
286 EXPECT_TRUE(pointerEvent != nullptr);
287 EXPECT_TRUE(listener->OnInputEvent(pointerEvent));
288 }
289
290 /**
291 * @tc.name: ScreenClientInputEventListener_OnInputEvent_002
292 * @tc.desc: Verify the OnInputEvent function.
293 * @tc.type: FUNC
294 * @tc.require: Issue Number
295 */
296 HWTEST_F(ScreenClientWindowAdapterTest, ScreenClientInputEventListener_OnInputEvent_002, TestSize.Level1)
297 {
298 std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
299 std::shared_ptr<ScreenClientInputEventListener> listener = std::make_shared<ScreenClientInputEventListener>();
300 EXPECT_FALSE(listener->OnInputEvent(keyEvent));
301
302 keyEvent = MMI::KeyEvent::Create();
303 EXPECT_TRUE(keyEvent != nullptr);
304 EXPECT_TRUE(listener->OnInputEvent(keyEvent));
305 }
306
307 /**
308 * @tc.name: ScreenClientInputEventListener_OnInputEvent_003
309 * @tc.desc: Verify the OnInputEvent function.
310 * @tc.type: FUNC
311 * @tc.require: Issue Number
312 */
313 HWTEST_F(ScreenClientWindowAdapterTest, ScreenClientInputEventListener_OnInputEvent_003, TestSize.Level1)
314 {
315 std::shared_ptr<MMI::AxisEvent> axisEvent = nullptr;
316 std::shared_ptr<ScreenClientInputEventListener> listener = std::make_shared<ScreenClientInputEventListener>();
317 EXPECT_FALSE(listener->OnInputEvent(axisEvent));
318
319 axisEvent = MMI::AxisEvent::Create();
320 EXPECT_TRUE(axisEvent != nullptr);
321 EXPECT_TRUE(listener->OnInputEvent(axisEvent));
322 }
323 } // DistributedHardware
324 } // OHOS