• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define private public
19 #include "ability_manager_service.h"
20 #include "sa_interceptor_manager.h"
21 #undef private
22 #include "mock_sa_interceptor_manager.h"
23 #include "singleton.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::AAFwk;
28 namespace OHOS {
29 namespace AbilityRuntime {
30 class SAInterceptorManagerTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp();
35     void TearDown();
36 };
37 
SetUpTestCase()38 void SAInterceptorManagerTest::SetUpTestCase()
39 {
40 }
41 
TearDownTestCase()42 void SAInterceptorManagerTest::TearDownTestCase()
43 {
44 }
45 
SetUp()46 void SAInterceptorManagerTest::SetUp()
47 {
48 }
49 
TearDown()50 void SAInterceptorManagerTest::TearDown()
51 {
52 }
53 
54 /*
55  * @tc.number: GetInstance_0100
56  * @tc.name: GetInstance
57  * @tc.desc: Verify GetInstance returns the singleton instance
58  */
59 HWTEST_F(SAInterceptorManagerTest, GetInstance_0100, TestSize.Level1)
60 {
61     SAInterceptorManager& instance1 = SAInterceptorManager::GetInstance();
62     SAInterceptorManager& instance2 = SAInterceptorManager::GetInstance();
63 
64     // Verify we're getting the same instance
65     EXPECT_EQ(&instance1, &instance2);
66 }
67 
68 /*
69  * @tc.number: AddSAInterceptor_0100
70  * @tc.name: AddSAInterceptor
71  * @tc.desc: Verify AddSAInterceptor with null interceptor
72  */
73 HWTEST_F(SAInterceptorManagerTest, AddSAInterceptor_0100, TestSize.Level1)
74 {
75     int32_t result = SAInterceptorManager::GetInstance().AddSAInterceptor(nullptr);
76     EXPECT_EQ(result, ERR_NULL_SA_INTERCEPTOR_EXECUTER);
77     sptr<ISAInterceptor> interceptor = new MockSAInterceptor();
78     EXPECT_EQ(SAInterceptorManager::GetInstance().saInterceptors_.size(), 0);
79     result = SAInterceptorManager::GetInstance().AddSAInterceptor(interceptor);
80     EXPECT_EQ(result, ERR_OK);
81     EXPECT_EQ(SAInterceptorManager::GetInstance().saInterceptors_.size(), 1);
82     result = SAInterceptorManager::GetInstance().AddSAInterceptor(interceptor);
83     EXPECT_EQ(SAInterceptorManager::GetInstance().saInterceptors_.size(), 1);
84     EXPECT_EQ(result, ERR_OK);
85 }
86 
87 /*
88  * @tc.number: ExecuteSAInterceptor_0100
89  * @tc.name: ExecuteSAInterceptor
90  * @tc.desc: Verify ExecuteSAInterceptor functionality
91  */
92 HWTEST_F(SAInterceptorManagerTest, ExecuteSAInterceptor_0100, TestSize.Level1)
93 {
94     std::string params = "";
95     OHOS::AAFwk::Rule rule;
96     int32_t result = SAInterceptorManager::GetInstance().ExecuteSAInterceptor(params, rule);
97     EXPECT_EQ(result, ERR_OK);
98     sptr<ISAInterceptor> interceptor = new MockSAInterceptorRetFalse();
99     SAInterceptorManager::GetInstance().saInterceptors_.emplace_back(interceptor);
100     result = SAInterceptorManager::GetInstance().ExecuteSAInterceptor(params, rule);
101     EXPECT_NE(result, ERR_OK);
102 }
103 
104 /*
105  * @tc.number: SAInterceptorListIsEmpty_0100
106  * @tc.name: SAInterceptorListIsEmpty
107  * @tc.desc: Verify SAInterceptorListIsEmpty functionality
108  */
109 HWTEST_F(SAInterceptorManagerTest, SAInterceptorListIsEmpty_0100, TestSize.Level1)
110 {
111     SAInterceptorManager::GetInstance().saInterceptors_.clear();
112     bool result = SAInterceptorManager::GetInstance().SAInterceptorListIsEmpty();
113     EXPECT_EQ(result, true);
114     sptr<MockSAInterceptor> interceptor = new MockSAInterceptor();
115     SAInterceptorManager::GetInstance().saInterceptors_.emplace_back(interceptor);
116     result = SAInterceptorManager::GetInstance().SAInterceptorListIsEmpty();
117     EXPECT_EQ(result, false);
118 }
119 
120 /*
121  * @tc.number: ObserverExist_0100
122  * @tc.name: ObserverExist
123  * @tc.desc: Verify ObserverExist functionality
124  */
125 HWTEST_F(SAInterceptorManagerTest, ObserverExist_0100, TestSize.Level1)
126 {
127     SAInterceptorManager::GetInstance().saInterceptors_.clear();
128     sptr<MockSAInterceptor> interceptor = new MockSAInterceptor();
129     bool result = SAInterceptorManager::GetInstance().ObserverExist(interceptor);
130     EXPECT_EQ(result, false);
131     SAInterceptorManager::GetInstance().saInterceptors_.emplace_back(interceptor);
132     result = SAInterceptorManager::GetInstance().ObserverExist(interceptor);
133     EXPECT_EQ(result, true);
134 }
135 
136 /*
137  * @tc.number: GenerateSAInterceptorParams_0100
138  * @tc.name: GenerateSAInterceptorParams
139  * @tc.desc: Verify GenerateSAInterceptorParams functionality
140  */
141 HWTEST_F(SAInterceptorManagerTest, GenerateSAInterceptorParams_0100, TestSize.Level1)
142 {
143     std::string dialogSessionId = "10001";
144     AppExecFwk::AbilityInfo abilityInfo;
145     Want want;
146     auto result = SAInterceptorManager::GetInstance().GenerateSAInterceptorParams(want, nullptr,
147         abilityInfo, dialogSessionId);
148     EXPECT_NE(result, "");
149 }
150 
151 /*
152  * @tc.number: OnObserverDied_0100
153  * @tc.name: OnObserverDied
154  * @tc.desc: Verify OnObserverDied
155  */
156 HWTEST_F(SAInterceptorManagerTest, OnObserverDied_0100, TestSize.Level1)
157 {
158     wptr<IRemoteObject> remote = nullptr;
159     SAInterceptorManager::GetInstance().OnObserverDied(remote);
160 
161     remote = sptr<MockIRemoteObject>::MakeSptr();
162     sptr<ISAInterceptor> interceptor = sptr<MockSAInterceptor>::MakeSptr();
163     SAInterceptorManager::GetInstance().saInterceptors_.emplace_back(interceptor);
164     EXPECT_NE(SAInterceptorManager::GetInstance().saInterceptors_.size(), 0);
165     SAInterceptorManager::GetInstance().OnObserverDied(remote);
166     EXPECT_NE(&SAInterceptorManager::GetInstance(), nullptr);
167 }
168 } // namespace AbilityRuntime
169 } // namespace OHOS