1 /*
2 * Copyright (c) 2022-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 "display_manager.h"
18 #include "window.h"
19
20 #include "mock_display_manager_adapter.h"
21 #include "singleton_mocker.h"
22 #include "display_manager.cpp"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29 using Mocker = SingletonMocker<DisplayManagerAdapter, MockDisplayManagerAdapter>;
30 class DmMockScreenshotListener : public DisplayManager::IScreenshotListener {
31 public:
OnScreenshot(const ScreenshotInfo info)32 void OnScreenshot(const ScreenshotInfo info) override {}
33 };
34 class DmMockDisplayListener : public DisplayManager::IDisplayListener {
35 public:
OnCreate(DisplayId)36 void OnCreate(DisplayId) override {}
OnDestroy(DisplayId)37 void OnDestroy(DisplayId) override {}
OnChange(DisplayId)38 void OnChange(DisplayId) override {}
39 };
40 class DisplayManagerTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 virtual void SetUp() override;
45 virtual void TearDown() override;
46 };
47
SetUpTestCase()48 void DisplayManagerTest::SetUpTestCase()
49 {
50 }
51
TearDownTestCase()52 void DisplayManagerTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void DisplayManagerTest::SetUp()
57 {
58 }
59
TearDown()60 void DisplayManagerTest::TearDown()
61 {
62 }
63
64 namespace {
65 /**
66 * @tc.name: Freeze01
67 * @tc.desc: success
68 * @tc.type: FUNC
69 */
70 HWTEST_F(DisplayManagerTest, Freeze01, Function | SmallTest | Level1)
71 {
72 std::vector<DisplayId> displayIds;
73 displayIds.push_back(0);
74 bool ret = DisplayManager::GetInstance().Freeze(displayIds);
75 ASSERT_TRUE(ret);
76 }
77
78 /**
79 * @tc.name: Freeze02
80 * @tc.desc: test Freeze displayIds exceed the maximum
81 * @tc.type: FUNC
82 */
83 HWTEST_F(DisplayManagerTest, Freeze02, Function | SmallTest | Level1)
84 {
85 std::vector<DisplayId> displayIds;
86 for (uint32_t i = 0; i < 33; i++) { // MAX_DISPLAY_SIZE + 1
87 displayIds.push_back(i);
88 }
89
90 bool ret = DisplayManager::GetInstance().Freeze(displayIds);
91 ASSERT_FALSE(ret);
92 }
93
94 /**
95 * @tc.name: Freeze03
96 * @tc.desc: test Freeze displayIds empty
97 * @tc.type: FUNC
98 */
99 HWTEST_F(DisplayManagerTest, Freeze03, Function | SmallTest | Level1)
100 {
101 std::vector<DisplayId> displayIds;
102 bool ret = DisplayManager::GetInstance().Freeze(displayIds);
103 ASSERT_FALSE(ret);
104 }
105
106 /**
107 * @tc.name: Unfreeze01
108 * @tc.desc: success
109 * @tc.type: FUNC
110 */
111 HWTEST_F(DisplayManagerTest, Unfreeze01, Function | SmallTest | Level1)
112 {
113 std::vector<DisplayId> displayIds;
114 displayIds.push_back(0);
115 bool ret = DisplayManager::GetInstance().Unfreeze(displayIds);
116 ASSERT_TRUE(ret);
117 }
118
119 /**
120 * @tc.name: Unfreeze02
121 * @tc.desc: test Freeze displayIds exceed the maximum
122 * @tc.type: FUNC
123 */
124 HWTEST_F(DisplayManagerTest, Unfreeze02, Function | SmallTest | Level1)
125 {
126 std::vector<DisplayId> displayIds;
127 for (uint32_t i = 0; i < 33; i++) { // MAX_DISPLAY_SIZE + 1
128 displayIds.push_back(i);
129 }
130
131 bool ret = DisplayManager::GetInstance().Unfreeze(displayIds);
132 ASSERT_FALSE(ret);
133 }
134
135 /**
136 * @tc.name: Unfreeze03
137 * @tc.desc: test Freeze displayIds empty
138 * @tc.type: FUNC
139 */
140 HWTEST_F(DisplayManagerTest, Unfreeze03, Function | SmallTest | Level1)
141 {
142 std::vector<DisplayId> displayIds;
143 bool ret = DisplayManager::GetInstance().Unfreeze(displayIds);
144 ASSERT_FALSE(ret);
145 }
146
147 /**
148 * @tc.name: RegisterScreenshotListener01
149 * @tc.desc: test RegisterScreenshotListener with null listener
150 * @tc.type: FUNC
151 */
152 HWTEST_F(DisplayManagerTest, RegisterScreenshotListener01, Function | SmallTest | Level1)
153 {
154 bool ret = DisplayManager::GetInstance().RegisterScreenshotListener(nullptr);
155 ASSERT_FALSE(ret);
156 }
157
158 /**
159 * @tc.name: RegisterScreenshotListener02
160 * @tc.desc: test RegisterScreenshotListener with null listener
161 * @tc.type: FUNC
162 */
163 HWTEST_F(DisplayManagerTest, RegisterScreenshotListener02, Function | SmallTest | Level1)
164 {
165 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
166 EXPECT_CALL(m->Mock(), RegisterDisplayManagerAgent(_, _)).Times(1).WillOnce(Return(false));
167 sptr<DisplayManager::IScreenshotListener> listener = new DmMockScreenshotListener();
168 bool ret = DisplayManager::GetInstance().RegisterScreenshotListener(listener);
169 ASSERT_FALSE(ret);
170 }
171
172 /**
173 * @tc.name: UnregisterScreenshotListener01
174 * @tc.desc: test UnregisterScreenshotListener with null listener
175 * @tc.type: FUNC
176 */
177 HWTEST_F(DisplayManagerTest, UnregisterScreenshotListener01, Function | SmallTest | Level1)
178 {
179 bool ret = DisplayManager::GetInstance().UnregisterScreenshotListener(nullptr);
180 ASSERT_FALSE(ret);
181 }
182
183 /**
184 * @tc.name: OnDisplayCreate01
185 * @tc.desc: OnDisplayCreate
186 * @tc.type: FUNC
187 */
188 HWTEST_F(DisplayManagerTest, OnDisplayCreate01, Function | SmallTest | Level1)
189 {
190 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
191 EXPECT_CALL(m->Mock(), RegisterDisplayManagerAgent(_, _)).Times(1).WillOnce(Return(true));
192 sptr<DisplayManager::IDisplayListener> listener = new DmMockDisplayListener();
193 DisplayManager::GetInstance().RegisterDisplayListener(listener);
194 auto displayManagerListener = DisplayManager::GetInstance().pImpl_->displayManagerListener_;
195 ASSERT_NE(displayManagerListener, nullptr);
196 displayManagerListener->OnDisplayCreate(nullptr);
197 sptr<DisplayInfo> displayInfo = new DisplayInfo();
198 displayInfo->SetDisplayId(DISPLAY_ID_INVALID);
199 displayManagerListener->OnDisplayCreate(displayInfo);
200 displayInfo->SetDisplayId(0);
201 displayManagerListener->OnDisplayCreate(displayInfo);
202 ASSERT_NE(displayManagerListener->pImpl_, nullptr);
203 displayManagerListener->pImpl_ = nullptr;
204 displayManagerListener->OnDisplayCreate(displayInfo);
205 DisplayManager::GetInstance().pImpl_->displayManagerListener_ = nullptr;
206 }
207
208 /**
209 * @tc.name: OnDisplayDestroy
210 * @tc.desc: OnDisplayDestroy
211 * @tc.type: FUNC
212 */
213 HWTEST_F(DisplayManagerTest, OnDisplayDestroy, Function | SmallTest | Level1)
214 {
215 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
216 EXPECT_CALL(m->Mock(), RegisterDisplayManagerAgent(_, _)).Times(1).WillOnce(Return(true));
217 sptr<DisplayManager::IDisplayListener> listener = new DmMockDisplayListener();
218 DisplayManager::GetInstance().RegisterDisplayListener(listener);
219 auto displayManagerListener = DisplayManager::GetInstance().pImpl_->displayManagerListener_;
220 ASSERT_NE(displayManagerListener, nullptr);
221 displayManagerListener->OnDisplayDestroy(DISPLAY_ID_INVALID);
222 displayManagerListener->OnDisplayDestroy(0);
223 displayManagerListener->pImpl_ = nullptr;
224 displayManagerListener->OnDisplayDestroy(1);
225 DisplayManager::GetInstance().pImpl_->displayManagerListener_ = nullptr;
226 }
227
228 /**
229 * @tc.name: OnDisplayChange
230 * @tc.desc: OnDisplayChange
231 * @tc.type: FUNC
232 */
233 HWTEST_F(DisplayManagerTest, OnDisplayChange, Function | SmallTest | Level1)
234 {
235 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
236 EXPECT_CALL(m->Mock(), RegisterDisplayManagerAgent(_, _)).Times(1).WillOnce(Return(true));
237 sptr<DisplayManager::IDisplayListener> listener = new DmMockDisplayListener();
238 DisplayManager::GetInstance().RegisterDisplayListener(listener);
239 auto displayManagerListener = DisplayManager::GetInstance().pImpl_->displayManagerListener_;
240 ASSERT_NE(displayManagerListener, nullptr);
241 DisplayChangeEvent event = DisplayChangeEvent::DISPLAY_SIZE_CHANGED;
242 displayManagerListener->OnDisplayChange(nullptr, event);
243 sptr<DisplayInfo> displayInfo = new DisplayInfo();
244 displayInfo->SetDisplayId(DISPLAY_ID_INVALID);
245 displayManagerListener->OnDisplayChange(displayInfo, event);
246 displayInfo->SetDisplayId(0);
247 displayManagerListener->OnDisplayChange(displayInfo, event);
248 ASSERT_NE(displayManagerListener->pImpl_, nullptr);
249 displayManagerListener->pImpl_ = nullptr;
250 displayManagerListener->OnDisplayChange(displayInfo, event);
251 DisplayManager::GetInstance().pImpl_->displayManagerListener_ = nullptr;
252 }
253
254 /**
255 * @tc.name: CheckRectValid
256 * @tc.desc: CheckRectValid all
257 * @tc.type: FUNC
258 */
259 HWTEST_F(DisplayManagerTest, CheckRectValid, Function | SmallTest | Level1)
260 {
261 int32_t oriHeight = 500;
262 int32_t oriWidth = 500;
263 Media::Rect rect = {.left = 1, .top = 1, .width = 1, .height = 1};
264 bool ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
265 ASSERT_TRUE(ret);
266 rect.left = -1;
267 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
268 ASSERT_FALSE(ret);
269 rect.left = 1;
270 rect.top = -1;
271 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
272 ASSERT_FALSE(ret);
273 rect.top = 1;
274 rect.width = -1;
275 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
276 ASSERT_FALSE(ret);
277 rect.width = 1;
278 rect.height = -1;
279 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
280 ASSERT_FALSE(ret);
281 rect.width = 500;
282 rect.height = 1;
283 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
284 ASSERT_FALSE(ret);
285 rect.width = 1;
286 rect.height = 500;
287 ret = DisplayManager::GetInstance().pImpl_->CheckRectValid(rect, oriHeight, oriWidth);
288 ASSERT_FALSE(ret);
289 }
290
291 /**
292 * @tc.name: CheckSizeValid
293 * @tc.desc: CheckSizeValid all
294 * @tc.type: FUNC
295 */
296 HWTEST_F(DisplayManagerTest, CheckSizeValid, Function | SmallTest | Level1)
297 {
298 int32_t oriHeight = 500;
299 int32_t oriWidth = 500;
300 Media::Size size = {.width = 1, .height = 1};
301 bool ret = DisplayManager::GetInstance().pImpl_->CheckSizeValid(size, oriHeight, oriWidth);
302 ASSERT_TRUE(ret);
303 size.width = -1;
304 ret = DisplayManager::GetInstance().pImpl_->CheckSizeValid(size, oriHeight, oriWidth);
305 ASSERT_FALSE(ret);
306 size.width = 1;
307 size.height = -1;
308 ret = DisplayManager::GetInstance().pImpl_->CheckSizeValid(size, oriHeight, oriWidth);
309 ASSERT_FALSE(ret);
310 size.width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1;
311 size.height = 1;
312 ret = DisplayManager::GetInstance().pImpl_->CheckSizeValid(size, oriHeight, oriWidth);
313 ASSERT_FALSE(ret);
314 size.width = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT;
315 size.height = DisplayManager::MAX_RESOLUTION_SIZE_SCREENSHOT + 1;
316 ret = DisplayManager::GetInstance().pImpl_->CheckSizeValid(size, oriHeight, oriWidth);
317 ASSERT_FALSE(ret);
318 }
319
320 /**
321 * @tc.name: ImplGetDefaultDisplay01
322 * @tc.desc: Impl GetDefaultDisplay nullptr
323 * @tc.type: FUNC
324 */
325 HWTEST_F(DisplayManagerTest, ImplGetDefaultDisplay01, Function | SmallTest | Level1)
326 {
327 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
328 EXPECT_CALL(m->Mock(), GetDefaultDisplayInfo()).Times(1).WillOnce(Return(nullptr));
329 sptr<Display> display = DisplayManager::GetInstance().pImpl_->GetDefaultDisplay();
330 ASSERT_EQ(display, nullptr);
331 }
332
333 /**
334 * @tc.name: GetDisplayByScreen
335 * @tc.desc: for interface coverage & check GetDisplayByScreen
336 * @tc.type: FUNC
337 */
338 HWTEST_F(DisplayManagerTest, GetDisplayByScreen, Function | SmallTest | Level1)
339 {
340 auto& displayManager = DisplayManager::GetInstance();
341 sptr<Display> display = displayManager.GetDisplayByScreen(SCREEN_ID_INVALID);
342 ASSERT_EQ(display, nullptr);
343
344 sptr<DisplayInfo> displayInfo = new DisplayInfo();
345 displayInfo->SetDisplayId(DISPLAY_ID_INVALID);
346 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
347 EXPECT_CALL(m->Mock(), GetDisplayInfoByScreenId(_)).Times(1).WillOnce(Return(displayInfo));
348 display = displayManager.GetDisplayByScreen(1);
349 ASSERT_EQ(display, nullptr);
350 }
351
352 /**
353 * @tc.name: ImplGetDefaultDisplaySync
354 * @tc.desc: Impl GetDefaultDisplaySync nullptr
355 * @tc.type: FUNC
356 */
357 HWTEST_F(DisplayManagerTest, ImplGetDefaultDisplaySync, Function | SmallTest | Level1)
358 {
359 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
360 EXPECT_CALL(m->Mock(), GetDefaultDisplayInfo()).Times(1).WillOnce(Return(nullptr));
361 sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplaySync();
362 ASSERT_EQ(display, nullptr);
363 }
364 }
365 } // namespace Rosen
366 } // namespace OHOS