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 #define private public
18 #include "display_manager_adapter_impl.h"
19 #include "screen_manager.h"
20 #include "screen_manager/rs_screen_mode_info.h"
21 #undef private
22
23 #include "syspara/parameters.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::Rosen;
29 using namespace OHOS::NWeb;
30
31 namespace OHOS {
32 namespace {
33 DMError g_unregister = DMError::DM_ERROR_NULLPTR;
34 constexpr uint64_t DISPLAY_ID_INVALID = -1ULL;
35 }
36
37 namespace Rosen {
UnregisterDisplayListener(sptr<IDisplayListener> listener)38 DMError DisplayManager::UnregisterDisplayListener(sptr<IDisplayListener> listener)
39 {
40 return g_unregister;
41 }
42 }
43
44 std::string g_deviceType = "";
45 namespace system {
GetDeviceType()46 std::string GetDeviceType()
47 {
48 return g_deviceType;
49 }
50 }
51
52 namespace NWeb {
53 class DisplayListenerAdapterTest : public DisplayListenerAdapter {
54 public:
55 DisplayListenerAdapterTest() = default;
56
57 virtual ~DisplayListenerAdapterTest() = default;
58
OnCreate(DisplayId)59 void OnCreate(DisplayId) override {}
OnDestroy(DisplayId)60 void OnDestroy(DisplayId) override {}
OnChange(DisplayId)61 void OnChange(DisplayId) override {}
62 };
63
64 class DisplayManagerAdapterImplTest : public testing::Test {
65 public:
66 static void SetUpTestCase(void);
67 static void TearDownTestCase(void);
68 void SetUp();
69 void TearDown();
70 };
71
SetUpTestCase(void)72 void DisplayManagerAdapterImplTest::SetUpTestCase(void)
73 {}
74
TearDownTestCase(void)75 void DisplayManagerAdapterImplTest::TearDownTestCase(void)
76 {}
77
SetUp(void)78 void DisplayManagerAdapterImplTest::SetUp(void)
79 {}
80
TearDown(void)81 void DisplayManagerAdapterImplTest::TearDown(void)
82 {}
83
84 /**
85 * @tc.name: DisplayManagerAdapterImplTest_001.
86 * @tc.desc: test lock type.
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_001, TestSize.Level1)
91 {
92 std::unique_ptr<DisplayAdapterImpl> displayAdapterImpl = std::make_unique<DisplayAdapterImpl>(nullptr);
93 EXPECT_NE(displayAdapterImpl, nullptr);
94 EXPECT_EQ(displayAdapterImpl->GetId(), static_cast<DisplayId>(-1));
95 EXPECT_EQ(displayAdapterImpl->GetWidth(), -1);
96 EXPECT_EQ(displayAdapterImpl->GetHeight(), -1);
97 EXPECT_EQ(displayAdapterImpl->GetVirtualPixelRatio(), -1);
98 EXPECT_EQ(displayAdapterImpl->GetRotation(), RotationType::ROTATION_BUTT);
99 EXPECT_EQ(displayAdapterImpl->GetOrientation(), OrientationType::BUTT);
100 }
101
102 /**
103 * @tc.name: DisplayManagerAdapterImplTest_002.
104 * @tc.desc: test lock type.
105 * @tc.type: FUNC
106 * @tc.require:
107 */
108 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_002, TestSize.Level1)
109 {
110 std::shared_ptr<DisplayListenerAdapter> listener = std::make_shared<DisplayListenerAdapterTest>();
111 std::unique_ptr<DisplayListenerAdapterImpl> displayListenerAdapterImpl =
112 std::make_unique<DisplayListenerAdapterImpl>(listener);
113 displayListenerAdapterImpl->OnCreate(static_cast<DisplayId>(1));
114 displayListenerAdapterImpl->OnDestroy(static_cast<DisplayId>(1));
115 displayListenerAdapterImpl->OnChange(static_cast<DisplayId>(1));
116 EXPECT_NE(displayListenerAdapterImpl, nullptr);
117 std::unique_ptr<DisplayListenerAdapterImpl> displayListenerAdapterImpl1 =
118 std::make_unique<DisplayListenerAdapterImpl>(nullptr);
119 displayListenerAdapterImpl1->OnCreate(static_cast<DisplayId>(1));
120 displayListenerAdapterImpl1->OnDestroy(static_cast<DisplayId>(1));
121 displayListenerAdapterImpl1->OnChange(static_cast<DisplayId>(1));
122 }
123
124 /**
125 * @tc.name: DisplayManagerAdapterImplTest_003.
126 * @tc.desc: test ConvertRotationType.
127 * @tc.type: FUNC
128 * @tc.require:
129 */
130 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_003, TestSize.Level1)
131 {
132 std::unique_ptr<DisplayAdapterImpl> displayAdapterImpl = std::make_unique<DisplayAdapterImpl>(nullptr);
133 EXPECT_NE(displayAdapterImpl, nullptr);
134
135 OHOS::Rosen::Rotation type = OHOS::Rosen::Rotation::ROTATION_0;
136 OHOS::NWeb::RotationType retType;
137 retType = displayAdapterImpl->ConvertRotationType(type);
138 EXPECT_EQ(retType, OHOS::NWeb::RotationType::ROTATION_0);
139
140 type = OHOS::Rosen::Rotation::ROTATION_90;
141 retType = displayAdapterImpl->ConvertRotationType(type);
142 EXPECT_EQ(retType, OHOS::NWeb::RotationType::ROTATION_90);
143
144 type = OHOS::Rosen::Rotation::ROTATION_180;
145 retType = displayAdapterImpl->ConvertRotationType(type);
146 EXPECT_EQ(retType, OHOS::NWeb::RotationType::ROTATION_180);
147
148 type = OHOS::Rosen::Rotation::ROTATION_270;
149 retType = displayAdapterImpl->ConvertRotationType(type);
150 EXPECT_EQ(retType, OHOS::NWeb::RotationType::ROTATION_270);
151
152 const uint32_t ROTATION_butt = 4;
153 type = static_cast<OHOS::Rosen::Rotation>(ROTATION_butt);
154 retType = displayAdapterImpl->ConvertRotationType(type);
155 EXPECT_EQ(retType, OHOS::NWeb::RotationType::ROTATION_BUTT);
156 }
157
158 /**
159 * @tc.name: DisplayManagerAdapterImplTest_004.
160 * @tc.desc: test ConvertRotationType.
161 * @tc.type: FUNC
162 * @tc.require:
163 */
164 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_004, TestSize.Level1)
165 {
166 std::unique_ptr<DisplayAdapterImpl> displayAdapterImpl = std::make_unique<DisplayAdapterImpl>(nullptr);
167 EXPECT_NE(displayAdapterImpl, nullptr);
168
169 OHOS::Rosen::Orientation type = OHOS::Rosen::Orientation::UNSPECIFIED;
170 OHOS::NWeb::OrientationType retType;
171 retType = displayAdapterImpl->ConvertOrientationType(type);
172 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::UNSPECIFIED);
173
174 type = OHOS::Rosen::Orientation::VERTICAL;
175 retType = displayAdapterImpl->ConvertOrientationType(type);
176 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::VERTICAL);
177
178 type = OHOS::Rosen::Orientation::HORIZONTAL;
179 retType = displayAdapterImpl->ConvertOrientationType(type);
180 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::HORIZONTAL);
181
182 type = OHOS::Rosen::Orientation::REVERSE_VERTICAL;
183 retType = displayAdapterImpl->ConvertOrientationType(type);
184 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::REVERSE_VERTICAL);
185
186 type = OHOS::Rosen::Orientation::REVERSE_HORIZONTAL;
187 retType = displayAdapterImpl->ConvertOrientationType(type);
188 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::REVERSE_HORIZONTAL);
189
190 type = OHOS::Rosen::Orientation::SENSOR;
191 retType = displayAdapterImpl->ConvertOrientationType(type);
192 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::SENSOR);
193
194 type = OHOS::Rosen::Orientation::SENSOR_VERTICAL;
195 retType = displayAdapterImpl->ConvertOrientationType(type);
196 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::SENSOR_VERTICAL);
197
198 type = OHOS::Rosen::Orientation::SENSOR_HORIZONTAL;
199 retType = displayAdapterImpl->ConvertOrientationType(type);
200 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::SENSOR_HORIZONTAL);
201
202 type = OHOS::Rosen::Orientation::AUTO_ROTATION_RESTRICTED;
203 retType = displayAdapterImpl->ConvertOrientationType(type);
204 EXPECT_EQ(retType, OHOS::NWeb::OrientationType::BUTT);
205 }
206
207 /**
208 * @tc.name: DisplayManagerAdapterImplTest_005.
209 * @tc.desc: test lock type.
210 * @tc.type: FUNC
211 * @tc.require:
212 */
213 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_005, TestSize.Level1)
214 {
215 std::shared_ptr<DisplayManagerAdapterImpl> displayManagerAdapterImpl =
216 std::make_shared<DisplayManagerAdapterImpl>();
217 std::shared_ptr<DisplayListenerAdapter> listener = std::make_shared<DisplayListenerAdapterTest>();
218 EXPECT_NE(displayManagerAdapterImpl, nullptr);
219 EXPECT_NE(displayManagerAdapterImpl->GetDefaultDisplayId(), DISPLAY_ID_INVALID);
220 EXPECT_NE(displayManagerAdapterImpl->GetDefaultDisplay(), nullptr);
221 EXPECT_EQ(displayManagerAdapterImpl->RegisterDisplayListener(listener), true);
222 EXPECT_FALSE(displayManagerAdapterImpl->UnregisterDisplayListener(listener));
223 std::shared_ptr<DisplayListenerAdapter> listener1 = std::make_shared<DisplayListenerAdapterTest>();
224 EXPECT_FALSE(displayManagerAdapterImpl->UnregisterDisplayListener(listener1));
225 g_unregister = DMError::DM_OK;
226 }
227
228 /**
229 * @tc.name: DisplayManagerAdapterImplTest_006.
230 * @tc.desc: test lock type.
231 * @tc.type: FUNC
232 * @tc.require:
233 */
234 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_006, TestSize.Level1)
235 {
236 sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplay();
237 ScreenId defaultScreenId = display->GetScreenId();
238 auto defaultScreen = ScreenManager::GetInstance().GetScreenById(defaultScreenId);
239 defaultScreen->SetOrientation(Orientation::REVERSE_HORIZONTAL);
240 std::unique_ptr<DisplayAdapterImpl> displayAdapterImpl = std::make_unique<DisplayAdapterImpl>(display);
241 EXPECT_NE(displayAdapterImpl, nullptr);
242 EXPECT_NE(displayAdapterImpl->GetId(), static_cast<DisplayId>(-1));
243 EXPECT_NE(displayAdapterImpl->GetWidth(), -1);
244 EXPECT_NE(displayAdapterImpl->GetHeight(), -1);
245 EXPECT_NE(displayAdapterImpl->GetVirtualPixelRatio(), -1);
246 EXPECT_NE(displayAdapterImpl->GetRotation(), RotationType::ROTATION_BUTT);
247 EXPECT_NE(displayAdapterImpl->GetOrientation(), OrientationType::BUTT);
248 }
249
250 /**
251 * @tc.name: DisplayManagerAdapterImplTest_007.
252 * @tc.desc: test IsDefaultPortrait.
253 * @tc.type: FUNC
254 * @tc.require:
255 */
256 HWTEST_F(DisplayManagerAdapterImplTest, DisplayManagerAdapterImplTest_007, TestSize.Level1)
257 {
258 std::shared_ptr<DisplayManagerAdapterImpl> displayManagerAdapterImpl =
259 std::make_shared<DisplayManagerAdapterImpl>();
260 EXPECT_NE(displayManagerAdapterImpl, nullptr);
261 bool result = displayManagerAdapterImpl->IsDefaultPortrait();
262 std::string deviceType = OHOS::system::GetDeviceType();
263 if (deviceType == "phone" || deviceType == "default") {
264 EXPECT_TRUE(result);
265 } else {
266 EXPECT_FALSE(result);
267 }
268 }
269 }
270 }
271