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 #include "session_manager.h"
18
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include <ipc_skeleton.h>
22
23 #include "scene_board_judgement.h"
24 #include "session_manager_service_recover_interface.h"
25 #include "singleton_delegator.h"
26 #include "window_manager_hilog.h"
27 #include "session_manager_lite.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Rosen {
34 class SessionManagerTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40
41 private:
42 std::shared_ptr<SessionManager> sm_;
43 };
44
SetUpTestCase()45 void SessionManagerTest::SetUpTestCase() {}
46
TearDownTestCase()47 void SessionManagerTest::TearDownTestCase() {}
48
SetUp()49 void SessionManagerTest::SetUp()
50 {
51 sm_ = std::make_shared<SessionManager>();
52 ASSERT_NE(nullptr, sm_);
53 }
54
TearDown()55 void SessionManagerTest::TearDown()
56 {
57 sm_ = nullptr;
58 }
59
60 namespace {
61 /**
62 * @tc.name: OnRemoteRequest
63 * @tc.desc: normal function
64 * @tc.type: FUNC
65 */
66 HWTEST_F(SessionManagerTest, OnRemoteRequest, TestSize.Level1)
67 {
68 OHOS::MessageParcel data;
69 OHOS::MessageParcel reply;
70 OHOS::MessageOption option;
71 IPCObjectStub iPCObjectStub;
72
73 uint32_t code =
74 static_cast<uint32_t>(OHOS::Rosen::ISessionManagerServiceRecoverListener::SessionManagerServiceRecoverMessage::
75 TRANS_ID_ON_SESSION_MANAGER_SERVICE_RECOVER);
76 auto ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
77 ASSERT_NE(ret, 0);
78
79 code = static_cast<uint32_t>(OHOS::Rosen::ISessionManagerServiceRecoverListener::
80 SessionManagerServiceRecoverMessage::TRANS_ID_ON_WMS_CONNECTION_CHANGED);
81 ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
82 ASSERT_NE(ret, 0);
83
84 code = 10;
85 ret = iPCObjectStub.OnRemoteRequest(code, data, reply, option);
86 ASSERT_NE(0, ret);
87 }
88
89 /**
90 * @tc.name: OnRemoteDied1
91 * @tc.desc: foundation died
92 * @tc.type: FUNC
93 */
94 HWTEST_F(SessionManagerTest, OnRemoteDied1, TestSize.Level1)
95 {
96 ASSERT_NE(nullptr, sm_);
97 FoundationDeathRecipient foundationDeathRecipient;
98 wptr<IRemoteObject> wptrDeath;
99 foundationDeathRecipient.OnRemoteDied(wptrDeath);
100 ASSERT_EQ(false, sm_->isWMSConnected_);
101 ASSERT_EQ(false, sm_->isFoundationListenerRegistered_);
102 ASSERT_EQ(false, sm_->isRecoverListenerRegistered_);
103 ASSERT_EQ(nullptr, sm_->mockSessionManagerServiceProxy_);
104 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
105 ASSERT_EQ(nullptr, sm_->sceneSessionManagerProxy_);
106 }
107
108 /**
109 * @tc.name: OnRemoteDied2
110 * @tc.desc: scb died
111 * @tc.type: FUNC
112 */
113 HWTEST_F(SessionManagerTest, OnRemoteDied2, TestSize.Level1)
114 {
115 ASSERT_NE(nullptr, sm_);
116 SSMDeathRecipient sSMDeathRecipient;
117 wptr<IRemoteObject> wptrDeath;
118 sSMDeathRecipient.OnRemoteDied(wptrDeath);
119 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
120 }
121
122 /**
123 * @tc.name: OnFoundationDied
124 * @tc.desc: normal function
125 * @tc.type: FUNC
126 */
127 HWTEST_F(SessionManagerTest, OnFoundationDied, TestSize.Level1)
128 {
129 ASSERT_NE(nullptr, sm_);
130 sm_->OnFoundationDied();
131 ASSERT_EQ(false, sm_->isWMSConnected_);
132 ASSERT_EQ(false, sm_->isFoundationListenerRegistered_);
133 ASSERT_EQ(false, sm_->isRecoverListenerRegistered_);
134 ASSERT_EQ(nullptr, sm_->mockSessionManagerServiceProxy_);
135 ASSERT_EQ(nullptr, sm_->sessionManagerServiceProxy_);
136 ASSERT_EQ(nullptr, sm_->sceneSessionManagerProxy_);
137 }
138
139 /**
140 * @tc.name: InitMockSMSProxy
141 * @tc.desc: normal function
142 * @tc.type: FUNC
143 */
144 HWTEST_F(SessionManagerTest, InitMockSMSProxy, TestSize.Level1)
145 {
146 ASSERT_NE(nullptr, sm_);
147 sm_->InitMockSMSProxy();
148 ASSERT_NE(sm_->foundationDeath_, nullptr);
149 }
150
151 /**
152 * @tc.name: RegisterWindowManagerRecoverCallbackFunc
153 * @tc.desc: normal function
154 * @tc.type: FUNC
155 */
156 HWTEST_F(SessionManagerTest, RegisterWindowManagerRecoverCallbackFunc, TestSize.Level1)
157 {
158 ASSERT_NE(nullptr, sm_);
__anon13e500990202() 159 auto testFunc = []() { return; };
160 sm_->RegisterWindowManagerRecoverCallbackFunc(testFunc);
161 ASSERT_NE(sm_->windowManagerRecoverFunc_, nullptr);
162 }
163 } // namespace
164 } // namespace Rosen
165 } // namespace OHOS