1 /*
2 * Copyright (c) 2021-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
18 #include "display_info.h"
19 #include "display_manager.h"
20 #include "mock_display_manager_adapter.h"
21 #include "screen_manager.h"
22 #include "screen_manager/rs_screen_mode_info.h"
23 #include "singleton_mocker.h"
24 #include "window_manager_hilog.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayChangeUnitTest"};
32 using Mocker = SingletonMocker<DisplayManagerAdapter, MockDisplayManagerAdapter>;
33
34 class DisplayChangeEventListener : public DisplayManager::IDisplayListener {
35 public:
OnCreate(DisplayId displayId)36 virtual void OnCreate(DisplayId displayId) {}
37
OnDestroy(DisplayId displayId)38 virtual void OnDestroy(DisplayId displayId) {}
39
OnChange(DisplayId displayId)40 virtual void OnChange(DisplayId displayId) {}
41 };
42
43 class DisplayChangeUnitTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 virtual void SetUp() override;
48 virtual void TearDown() override;
49 bool ScreenSizeEqual(const sptr<Screen> screen, const sptr<SupportedScreenModes> curInfo);
50 bool DisplaySizeEqual(const sptr<Display> display, const sptr<SupportedScreenModes> curInfo);
51 static DisplayId defaultDisplayId_;
52 static ScreenId defaultScreenId_;
53 sptr<DisplayChangeEventListener> listener_ = new DisplayChangeEventListener();
54 };
55 DisplayId DisplayChangeUnitTest::defaultDisplayId_ = DISPLAY_ID_INVALID;
56 ScreenId DisplayChangeUnitTest::defaultScreenId_ = SCREEN_ID_INVALID;
57
SetUpTestCase()58 void DisplayChangeUnitTest::SetUpTestCase()
59 {
60 defaultDisplayId_ = DisplayManager::GetInstance().GetDefaultDisplayId();
61 sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDisplayById(defaultDisplayId_);
62 if (defaultDisplay != nullptr) {
63 defaultScreenId_ = defaultDisplay->GetScreenId();
64 }
65 }
66
TearDownTestCase()67 void DisplayChangeUnitTest::TearDownTestCase()
68 {
69 }
70
SetUp()71 void DisplayChangeUnitTest::SetUp()
72 {
73 }
74
TearDown()75 void DisplayChangeUnitTest::TearDown()
76 {
77 }
78
DisplaySizeEqual(const sptr<Display> display,const sptr<SupportedScreenModes> curInfo)79 bool DisplayChangeUnitTest::DisplaySizeEqual(const sptr<Display> display, const sptr<SupportedScreenModes> curInfo)
80 {
81 uint32_t dWidth = static_cast<uint32_t>(display->GetWidth());
82 uint32_t dHeight = static_cast<uint32_t>(display->GetHeight());
83 WLOGI("DisplaySizeEqual:: DisplaySize: %{public}u %{public}u, ActiveModeInfoSize: %{public}u %{public}u",
84 dWidth, dHeight, curInfo->width_, curInfo->height_);
85 return ((curInfo->width_ == dWidth) && (curInfo->height_ == dHeight));
86 }
87
88 namespace {
89 /**
90 * @tc.name: RegisterDisplayChangeListener01
91 * @tc.desc: Register and Unregister displayChangeListener with valid listener and check return true
92 * @tc.type: FUNC
93 */
94 HWTEST_F(DisplayChangeUnitTest, RegisterDisplayChangeListener01, Function | SmallTest | Level2)
95 {
96 Mocker m;
97 EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
98 .Times(1).WillOnce(Return(DMError::DM_OK));
99 DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(listener_);
100 ASSERT_EQ(DMError::DM_OK, ret);
101
102 EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
103 .Times(1).WillOnce(Return(DMError::DM_OK));
104 ret = DisplayManager::GetInstance().UnregisterDisplayListener(listener_);
105 ASSERT_EQ(DMError::DM_OK, ret);
106 }
107
108 /**
109 * @tc.name: RegisterDisplayChangeListener02
110 * @tc.desc: Register and Unregister displayChangeListener with nullptr and check return false
111 * @tc.type: FUNC
112 */
113 HWTEST_F(DisplayChangeUnitTest, RegisterDisplayChangeListener02, Function | SmallTest | Level2)
114 {
115 Mocker m;
116 EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
117 .Times(0);
118 DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(nullptr);
119 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
120
121 EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
122 .Times(0);
123 ret = DisplayManager::GetInstance().UnregisterDisplayListener(nullptr);
124 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
125 }
126
127 /**
128 * @tc.name: UnregisterDisplayChangeListener03
129 * @tc.desc: Register and Unregister displayChangeListener when ipc fails and check return false
130 * @tc.type: FUNC
131 */
132 HWTEST_F(DisplayChangeUnitTest, RegisterDisplayChangeListener03, Function | SmallTest | Level2)
133 {
134 Mocker m;
135 EXPECT_CALL(m.Mock(), RegisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
136 .Times(1).WillOnce(Return(DMError::DM_ERROR_NULLPTR));
137 DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(listener_);
138 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
139
140 EXPECT_CALL(m.Mock(), UnregisterDisplayManagerAgent(_, DisplayManagerAgentType::DISPLAY_EVENT_LISTENER))
141 .Times(0);
142 ret = DisplayManager::GetInstance().UnregisterDisplayListener(listener_);
143 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ret);
144 }
145 }
146 } // namespace Rosen
147 } // namespace OHOS