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 #include <gtest/gtest.h>
17 #include <transaction/rs_sync_transaction_controller.h>
18
19 #include "modifier_render_thread/rs_modifiers_draw_thread.h"
20 #include "rs_adapter.h"
21 #include "window_agent.h"
22 #include "window_manager_hilog.h"
23 #include "window_stub.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 std::string g_errLog;
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)32 void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
33 const char *msg)
34 {
35 g_errLog = msg;
36 }
37 class WindowAgentTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp() override;
42 void TearDown() override;
43 sptr<WindowAgent> windowAgent_;
44 };
45
SetUpTestCase()46 void WindowAgentTest::SetUpTestCase() {}
47
TearDownTestCase()48 void WindowAgentTest::TearDownTestCase()
49 {
50 #ifdef RS_ENABLE_VK
51 RSModifiersDrawThread::Destroy();
52 #endif
53 }
54
SetUp()55 void WindowAgentTest::SetUp()
56 {
57 sptr<WindowOption> option = new WindowOption();
58 sptr<WindowImpl> window = new WindowImpl(option);
59 windowAgent_ = new WindowAgent(window);
60 }
61
TearDown()62 void WindowAgentTest::TearDown()
63 {
64 windowAgent_ = nullptr;
65 }
66
67 namespace {
68 /**
69 * @tc.name: UpdateWindowRect
70 * @tc.desc: UpdateWindowRect
71 * @tc.type: FUNC
72 */
73 HWTEST_F(WindowAgentTest, UpdateWindowRect, TestSize.Level1)
74 {
75 Rect rect = Rect{ 0, 0, 0, 0 };
76 bool status = true;
77 WMError err = windowAgent_->UpdateWindowRect(rect, status, WindowSizeChangeReason::HIDE);
78 ASSERT_EQ(err, WMError::WM_OK);
79
80 windowAgent_->window_ = nullptr;
81 err = windowAgent_->UpdateWindowRect(rect, status, WindowSizeChangeReason::HIDE);
82 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
83 }
84
85 /**
86 * @tc.name: UpdateWindowMode
87 * @tc.desc: UpdateWindowMode
88 * @tc.type: FUNC
89 */
90 HWTEST_F(WindowAgentTest, UpdateWindowMode, TestSize.Level1)
91 {
92 WMError err = windowAgent_->UpdateWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
93 ASSERT_EQ(err, WMError::WM_OK);
94
95 windowAgent_->window_ = nullptr;
96 err = windowAgent_->UpdateWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
97 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
98 }
99
100 /**
101 * @tc.name: UpdateWindowModeSupportType
102 * @tc.desc: UpdateWindowModeSupportType
103 * @tc.type: FUNC
104 */
105 HWTEST_F(WindowAgentTest, UpdateWindowModeSupportType, TestSize.Level1)
106 {
107 WMError err = windowAgent_->UpdateWindowModeSupportType(WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN);
108 ASSERT_EQ(err, WMError::WM_OK);
109
110 windowAgent_->window_ = nullptr;
111 err = windowAgent_->UpdateWindowModeSupportType(WindowModeSupport::WINDOW_MODE_SUPPORT_FULLSCREEN);
112 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
113 }
114
115 /**
116 * @tc.name: UpdateFocusStatus
117 * @tc.desc: UpdateFocusStatus
118 * @tc.type: FUNC
119 */
120 HWTEST_F(WindowAgentTest, UpdateFocusStatus, TestSize.Level1)
121 {
122 WMError err = windowAgent_->UpdateFocusStatus(true);
123 ASSERT_EQ(err, WMError::WM_OK);
124
125 windowAgent_->window_ = nullptr;
126 err = windowAgent_->UpdateFocusStatus(true);
127 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
128 }
129
130 /**
131 * @tc.name: UpdateAvoidArea
132 * @tc.desc: UpdateAvoidArea
133 * @tc.type: FUNC
134 */
135 HWTEST_F(WindowAgentTest, UpdateAvoidArea, TestSize.Level1)
136 {
137 const sptr<AvoidArea>& avoidArea = new AvoidArea();
138 WMError err = windowAgent_->UpdateAvoidArea(avoidArea, AvoidAreaType::TYPE_SYSTEM);
139 ASSERT_EQ(err, WMError::WM_OK);
140
141 windowAgent_->window_ = nullptr;
142 err = windowAgent_->UpdateAvoidArea(avoidArea, AvoidAreaType::TYPE_SYSTEM);
143 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
144 }
145
146 /**
147 * @tc.name: UpdateWindowState
148 * @tc.desc: UpdateWindowState
149 * @tc.type: FUNC
150 */
151 HWTEST_F(WindowAgentTest, UpdateWindowState, TestSize.Level1)
152 {
153 WMError err = windowAgent_->UpdateWindowState(WindowState::STATE_BOTTOM);
154 ASSERT_EQ(err, WMError::WM_OK);
155
156 windowAgent_->window_ = nullptr;
157 err = windowAgent_->UpdateWindowState(WindowState::STATE_SHOWN);
158 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
159 }
160
161 /**
162 * @tc.name: UpdateWindowDragInfo
163 * @tc.desc: UpdateWindowDragInfo
164 * @tc.type: FUNC
165 */
166 HWTEST_F(WindowAgentTest, UpdateWindowDragInfo, TestSize.Level1)
167 {
168 PointInfo point;
169 point.x = 1;
170 point.y = 2;
171 WMError err = windowAgent_->UpdateWindowDragInfo(point, DragEvent::DRAG_EVENT_MOVE);
172 ASSERT_EQ(err, WMError::WM_OK);
173
174 windowAgent_->window_ = nullptr;
175 err = windowAgent_->UpdateWindowDragInfo(point, DragEvent::DRAG_EVENT_MOVE);
176 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
177 }
178
179 /**
180 * @tc.name: UpdateDisplayId
181 * @tc.desc: UpdateDisplayId
182 * @tc.type: FUNC
183 */
184 HWTEST_F(WindowAgentTest, UpdateDisplayId, TestSize.Level1)
185 {
186 WMError err = windowAgent_->UpdateDisplayId(0, 1);
187 ASSERT_EQ(err, WMError::WM_OK);
188
189 windowAgent_->window_ = nullptr;
190 err = windowAgent_->UpdateDisplayId(0, 1);
191 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
192 }
193
194 /**
195 * @tc.name: UpdateOccupiedAreaChangeInfo
196 * @tc.desc: UpdateOccupiedAreaChangeInfo
197 * @tc.type: FUNC
198 */
199 HWTEST_F(WindowAgentTest, UpdateOccupiedAreaChangeInfo, TestSize.Level1)
200 {
201 Rect overlapRect = { 0, 0, 0, 0 };
202 sptr<OccupiedAreaChangeInfo> info = new OccupiedAreaChangeInfo(OccupiedAreaType::TYPE_INPUT, overlapRect);
203 WMError err = windowAgent_->UpdateOccupiedAreaChangeInfo(info);
204 ASSERT_EQ(err, WMError::WM_OK);
205
206 windowAgent_->window_ = nullptr;
207 err = windowAgent_->UpdateOccupiedAreaChangeInfo(info);
208 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
209 }
210
211 /**
212 * @tc.name: UpdateOccupiedAreaAndRect
213 * @tc.desc: UpdateOccupiedAreaAndRect
214 * @tc.type: FUNC
215 */
216 HWTEST_F(WindowAgentTest, UpdateOccupiedAreaAndRect, TestSize.Level1)
217 {
218 Rect overlapRect = { 0, 0, 0, 0 };
219 sptr<OccupiedAreaChangeInfo> info = new OccupiedAreaChangeInfo(OccupiedAreaType::TYPE_INPUT, overlapRect);
220 std::shared_ptr<RSUIContext> rsUIContext;
221 if (windowAgent_->window_) {
222 rsUIContext = windowAgent_->window_->GetRSUIContext();
223 }
224 auto rsTransaction = RSSyncTransactionAdapter::GetRSTransaction(rsUIContext);
225
226 WMError err = windowAgent_->UpdateOccupiedAreaAndRect(info, overlapRect, rsTransaction);
227 ASSERT_EQ(err, WMError::WM_OK);
228
229 windowAgent_->window_ = nullptr;
230 err = windowAgent_->UpdateOccupiedAreaAndRect(info, overlapRect, rsTransaction);
231 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
232 }
233
234 /**
235 * @tc.name: UpdateActiveStatus
236 * @tc.desc: UpdateActiveStatus
237 * @tc.type: FUNC
238 */
239 HWTEST_F(WindowAgentTest, UpdateActiveStatus, TestSize.Level1)
240 {
241 WMError err = windowAgent_->UpdateActiveStatus(false);
242 ASSERT_EQ(err, WMError::WM_OK);
243
244 windowAgent_->window_ = nullptr;
245 err = windowAgent_->UpdateActiveStatus(false);
246 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
247 }
248
249 /**
250 * @tc.name: GetWindowProperty
251 * @tc.desc: GetWindowProperty
252 * @tc.type: FUNC
253 */
254 HWTEST_F(WindowAgentTest, GetWindowProperty, TestSize.Level1)
255 {
256 auto windowProperty = windowAgent_->GetWindowProperty();
257 ASSERT_NE(windowProperty, nullptr);
258
259 windowAgent_->window_ = nullptr;
260 windowProperty = windowAgent_->GetWindowProperty();
261 ASSERT_EQ(windowProperty, nullptr);
262 }
263
264 /**
265 * @tc.name: RestoreSplitWindowMode
266 * @tc.desc: RestoreSplitWindowMode
267 * @tc.type: FUNC
268 */
269 HWTEST_F(WindowAgentTest, RestoreSplitWindowMode, TestSize.Level1)
270 {
271 WMError err = windowAgent_->RestoreSplitWindowMode(static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY));
272 ASSERT_EQ(err, WMError::WM_OK);
273
274 windowAgent_->window_ = nullptr;
275 err = windowAgent_->RestoreSplitWindowMode(static_cast<uint32_t>(WindowMode::WINDOW_MODE_SPLIT_PRIMARY));
276 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
277 }
278
279 /**
280 * @tc.name: NotifyTouchOutside
281 * @tc.desc: NotifyTouchOutside
282 * @tc.type: FUNC
283 */
284 HWTEST_F(WindowAgentTest, NotifyTouchOutside, TestSize.Level1)
285 {
286 WMError err = windowAgent_->NotifyTouchOutside();
287 ASSERT_EQ(err, WMError::WM_OK);
288
289 windowAgent_->window_ = nullptr;
290 err = windowAgent_->NotifyTouchOutside();
291 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
292 }
293
294 /**
295 * @tc.name: NotifyScreenshot
296 * @tc.desc: NotifyScreenshot
297 * @tc.type: FUNC
298 */
299 HWTEST_F(WindowAgentTest, NotifyScreenshot, TestSize.Level1)
300 {
301 WMError err = windowAgent_->NotifyScreenshot();
302 ASSERT_EQ(err, WMError::WM_OK);
303
304 windowAgent_->window_ = nullptr;
305 err = windowAgent_->NotifyScreenshot();
306 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
307 }
308
309 /**
310 * @tc.name: NotifyScreenshotAppEvent
311 * @tc.desc: NotifyScreenshotAppEvent
312 * @tc.type: FUNC
313 */
314 HWTEST_F(WindowAgentTest, NotifyScreenshotAppEvent, TestSize.Level1)
315 {
316 WMError err = windowAgent_->NotifyScreenshotAppEvent(ScreenshotEventType::SCROLL_SHOT_START);
317 EXPECT_EQ(err, WMError::WM_OK);
318
319 windowAgent_->window_ = nullptr;
320 err = windowAgent_->NotifyScreenshotAppEvent(ScreenshotEventType::SCROLL_SHOT_START);
321 EXPECT_EQ(err, WMError::WM_ERROR_NULLPTR);
322 }
323
324 /**
325 * @tc.name: DumpInfo
326 * @tc.desc: DumpInfo
327 * @tc.type: FUNC
328 */
329 HWTEST_F(WindowAgentTest, DumpInfo, TestSize.Level1)
330 {
331 std::vector<std::string> params;
332 WMError err = windowAgent_->DumpInfo(params);
333 ASSERT_EQ(err, WMError::WM_OK);
334
335 windowAgent_->window_ = nullptr;
336 err = windowAgent_->DumpInfo(params);
337 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
338 }
339
340 /**
341 * @tc.name: UpdateZoomTransform
342 * @tc.desc: UpdateZoomTransform
343 * @tc.type: FUNC
344 */
345 HWTEST_F(WindowAgentTest, UpdateZoomTransform, TestSize.Level1)
346 {
347 Transform transform;
348 WMError err = windowAgent_->UpdateZoomTransform(transform, false);
349 ASSERT_EQ(err, WMError::WM_OK);
350
351 windowAgent_->window_ = nullptr;
352 err = windowAgent_->UpdateZoomTransform(transform, false);
353 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
354 }
355
356 /**
357 * @tc.name: NotifyDestroy
358 * @tc.desc: NotifyDestroy
359 * @tc.type: FUNC
360 */
361 HWTEST_F(WindowAgentTest, NotifyDestroy, TestSize.Level1)
362 {
363 WMError err = windowAgent_->NotifyDestroy();
364 ASSERT_EQ(err, WMError::WM_OK);
365
366 windowAgent_->window_ = nullptr;
367 err = windowAgent_->NotifyDestroy();
368 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
369 }
370
371 /**
372 * @tc.name: NotifyForeground
373 * @tc.desc: NotifyForeground
374 * @tc.type: FUNC
375 */
376 HWTEST_F(WindowAgentTest, NotifyForeground, TestSize.Level1)
377 {
378 WMError err = windowAgent_->NotifyForeground();
379 ASSERT_EQ(err, WMError::WM_OK);
380
381 windowAgent_->window_ = nullptr;
382 err = windowAgent_->NotifyForeground();
383 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
384 }
385
386 /**
387 * @tc.name: NotifyBackground
388 * @tc.desc: NotifyBackground
389 * @tc.type: FUNC
390 */
391 HWTEST_F(WindowAgentTest, NotifyBackground, TestSize.Level1)
392 {
393 WMError err = windowAgent_->NotifyBackground();
394 ASSERT_EQ(err, WMError::WM_OK);
395
396 windowAgent_->window_ = nullptr;
397 err = windowAgent_->NotifyBackground();
398 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
399 }
400
401 /**
402 * @tc.name: NotifyWindowClientPointUp
403 * @tc.desc: NotifyWindowClientPointUp
404 * @tc.type: FUNC
405 */
406 HWTEST_F(WindowAgentTest, NotifyWindowClientPointUp, TestSize.Level1)
407 {
408 auto pointerEvent = MMI::PointerEvent::Create();
409 WMError err = windowAgent_->NotifyWindowClientPointUp(pointerEvent);
410 ASSERT_EQ(err, WMError::WM_OK);
411
412 windowAgent_->window_ = nullptr;
413 err = windowAgent_->NotifyWindowClientPointUp(pointerEvent);
414 ASSERT_EQ(err, WMError::WM_ERROR_NULLPTR);
415 }
416
417 /**
418 * @tc.name: ConsumeKeyEvent
419 * @tc.desc: ConsumeKeyEvent
420 * @tc.type: FUNC
421 */
422 HWTEST_F(WindowAgentTest, ConsumeKeyEvent, TestSize.Level1)
423 {
424 g_errLog.clear();
425 LOG_SetCallback(MyLogCallback);
426 auto keyEvent = MMI::KeyEvent::Create();
427 windowAgent_->ConsumeKeyEvent(keyEvent);
428
429 windowAgent_->window_ = nullptr;
430 windowAgent_->ConsumeKeyEvent(keyEvent);
431 EXPECT_TRUE(g_errLog.find("window_ is nullptr") != std::string::npos);
432 LOG_SetCallback(nullptr);
433 }
434
435 /**
436 * @tc.name: NotifyForegroundInteractiveStatus
437 * @tc.desc: NotifyForegroundInteractiveStatus
438 * @tc.type: FUNC
439 */
440 HWTEST_F(WindowAgentTest, NotifyForegroundInteractiveStatus, TestSize.Level1)
441 {
442 g_errLog.clear();
443 LOG_SetCallback(MyLogCallback);
444 bool interactive = false;
445 windowAgent_->NotifyForegroundInteractiveStatus(interactive);
446
447 interactive = true;
448 windowAgent_->window_ = nullptr;
449 windowAgent_->NotifyForegroundInteractiveStatus(interactive);
450 EXPECT_TRUE(g_errLog.find("window_ is nullptr") != std::string::npos);
451 LOG_SetCallback(nullptr);
452 }
453 }
454 } // namespace
455 } // namespace Rosen
456 } // namespace OHOS