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