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