1 /*
2 * Copyright (c) 2023 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 "session_manager/include/screen_session_manager.h"
19 #include "display_manager_agent_default.h"
20 #include "iconsumer_surface.h"
21 #include <surface.h>
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 class ScreenSessionManagerTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34
35 static sptr<ScreenSessionManager> ssm_;
36
37 ScreenId DEFAULT_SCREEN_ID {0};
38 ScreenId VIRTUAL_SCREEN_ID {2};
39 };
40
41 sptr<ScreenSessionManager> ScreenSessionManagerTest::ssm_ = nullptr;
42
SetUpTestCase()43 void ScreenSessionManagerTest::SetUpTestCase()
44 {
45 ssm_ = new ScreenSessionManager();
46 }
47
TearDownTestCase()48 void ScreenSessionManagerTest::TearDownTestCase()
49 {
50 ssm_ = nullptr;
51 }
52
SetUp()53 void ScreenSessionManagerTest::SetUp()
54 {
55 }
56
TearDown()57 void ScreenSessionManagerTest::TearDown()
58 {
59 }
60
61 namespace {
62 /**
63 * @tc.name: RegisterDisplayManagerAgent
64 * @tc.desc: ScreenSesionManager rigister display manager agent
65 * @tc.type: FUNC
66 */
67 HWTEST_F(ScreenSessionManagerTest, RegisterDisplayManagerAgent, Function | SmallTest | Level3)
68 {
69 sptr<IDisplayManagerAgent> displayManagerAgent = new DisplayManagerAgentDefault();
70 DisplayManagerAgentType type = DisplayManagerAgentType::DISPLAY_STATE_LISTENER;
71
72 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ssm_->RegisterDisplayManagerAgent(nullptr, type));
73 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ssm_->UnregisterDisplayManagerAgent(nullptr, type));
74
75 ASSERT_EQ(DMError::DM_ERROR_NULLPTR, ssm_->UnregisterDisplayManagerAgent(displayManagerAgent, type));
76
77 ASSERT_EQ(DMError::DM_OK, ssm_->RegisterDisplayManagerAgent(displayManagerAgent, type));
78 ASSERT_EQ(DMError::DM_OK, ssm_->UnregisterDisplayManagerAgent(displayManagerAgent, type));
79 }
80
81 /**
82 * @tc.name: ScreenPower
83 * @tc.desc: ScreenSesionManager screen power
84 * @tc.type: FUNC
85 */
86 HWTEST_F(ScreenSessionManagerTest, ScreenPower, Function | SmallTest | Level3)
87 {
88 PowerStateChangeReason reason = PowerStateChangeReason::POWER_BUTTON;
89 ScreenPowerState state = ScreenPowerState::POWER_ON;
90 DisplayState displayState = DisplayState::ON;
91
92 ASSERT_EQ(false, ssm_->WakeUpBegin(reason));
93 ASSERT_EQ(false, ssm_->WakeUpEnd());
94
95 ASSERT_EQ(false, ssm_->SuspendBegin(reason));
96 ASSERT_EQ(false, ssm_->SuspendEnd());
97
98 ASSERT_EQ(false, ssm_->SetScreenPowerForAll(state, reason));
99
100 ASSERT_EQ(true, ssm_->SetDisplayState(displayState));
101 ASSERT_EQ(DisplayState::ON, ssm_->GetDisplayState(0));
102 }
103
104 /**
105 * @tc.name: GetDisplaySnapshot
106 * @tc.desc: ScreenSesionManager screen shot
107 * @tc.type: FUNC
108 */
109 HWTEST_F(ScreenSessionManagerTest, GetDisplaySnapshot, Function | SmallTest | Level3)
110 {
111 DisplayId displayId(0);
112 DmErrorCode* errorCode = nullptr;
113 ASSERT_EQ(nullptr, ssm_->GetDisplaySnapshot(displayId, errorCode));
114 }
115
116 /**
117 * @tc.name: VirtualScreen
118 * @tc.desc: ScreenSesionManager virtual screen
119 * @tc.type: FUNC
120 */
121 HWTEST_F(ScreenSessionManagerTest, VirtualScreen, Function | SmallTest | Level3)
122 {
123 sptr<IDisplayManagerAgent> displayManagerAgent = new DisplayManagerAgentDefault();
124 VirtualScreenOption virtualOption;
125 virtualOption.name_ = "testVirtualOption";
126 auto screenId = ssm_->CreateVirtualScreen(virtualOption, displayManagerAgent->AsObject());
127 if (screenId != VIRTUAL_SCREEN_ID) {
128 ASSERT_TRUE(screenId != VIRTUAL_SCREEN_ID);
129 }
130
131 std::vector<ScreenId> mirrorScreenIds;
132 ScreenId mainScreenId(DEFAULT_SCREEN_ID);
133 ScreenId screenGroupId{1};
134 ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, ssm_->MakeMirror(mainScreenId, mirrorScreenIds, screenGroupId));
135 mirrorScreenIds.push_back(VIRTUAL_SCREEN_ID);
136 ASSERT_NE(DMError::DM_OK, ssm_->MakeMirror(mainScreenId, mirrorScreenIds, screenGroupId));
137
138 auto result1 = ssm_->SetVirtualScreenSurface(VIRTUAL_SCREEN_ID, nullptr);
139 ASSERT_EQ(DMError::DM_ERROR_RENDER_SERVICE_FAILED, result1);
140 sptr<IConsumerSurface> surface = OHOS::IConsumerSurface::Create();
141 auto result2 = ssm_->SetVirtualScreenSurface(VIRTUAL_SCREEN_ID, surface->GetProducer());
142 if (DMError::DM_ERROR_RENDER_SERVICE_FAILED == result2) {
143 ASSERT_EQ(DMError::DM_ERROR_RENDER_SERVICE_FAILED, result2);
144 }
145 if (DMError::DM_OK != result2) {
146 ASSERT_NE(DMError::DM_OK, ssm_->DestroyVirtualScreen(VIRTUAL_SCREEN_ID));
147 }
148 }
149 }
150 } // namespace Rosen
151 } // namespace OHOS
152