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 #include "gtest/gtest.h"
16 #include "iservice_registry.h"
17 #include "local_ability_manager_stub.h"
18 #include "memory"
19 #include "sa_mock_permission.h"
20 #include "test_log.h"
21
22 #define private public
23 #define protected public
24 #include "local_ability_manager.h"
25 #include "mock_sa_realize.h"
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace SAFWK {
31 namespace {
32 constexpr int32_t SAID = 1489;
33 const std::string TEST_RESOURCE_PATH = "/data/test/resource/samgr/profile/";
34 constexpr int32_t LISTENER_ID = 1488;
35 constexpr int32_t MOCK_DEPEND_TIMEOUT = 1000;
36 }
37
38 class MockLocalAbilityManager : public LocalAbilityManagerStub {
39 public:
40 MockLocalAbilityManager() = default;
41 ~MockLocalAbilityManager() = default;
42
43 bool StartAbility(int32_t systemAbilityId) override;
44 };
45 class SystemAbilityTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 };
52
SetUpTestCase()53 void SystemAbilityTest::SetUpTestCase()
54 {
55 DTEST_LOG << "SetUpTestCase" << std::endl;
56 }
57
TearDownTestCase()58 void SystemAbilityTest::TearDownTestCase()
59 {
60 DTEST_LOG << "TearDownTestCase" << std::endl;
61 }
62
SetUp()63 void SystemAbilityTest::SetUp()
64 {
65 SaMockPermission::MockPermission();
66 DTEST_LOG << "SetUp" << std::endl;
67 }
68
TearDown()69 void SystemAbilityTest::TearDown()
70 {
71 DTEST_LOG << "TearDown" << std::endl;
72 }
73
StartAbility(int32_t systemAbilityId)74 bool MockLocalAbilityManager::StartAbility(int32_t systemAbilityId)
75 {
76 DTEST_LOG << "said : " << systemAbilityId <<std::endl;
77 return true;
78 }
79
80 /**
81 * @tc.name: RemoveSystemAbilityListener001
82 * @tc.desc: Check RemoveSystemAbilityListener
83 * @tc.type: FUNC
84 * @tc.require: I5KMF7
85 */
86 HWTEST_F(SystemAbilityTest, RemoveSystemAbilityListener001, TestSize.Level2)
87 {
88 std::shared_ptr<SystemAbility> sysAby = std::make_shared<MockSaRealize>(SAID, false);
89 sysAby->AddSystemAbilityListener(LISTENER_ID);
90 bool res = sysAby->RemoveSystemAbilityListener(LISTENER_ID);
91 EXPECT_EQ(res, true);
92 }
93
94 /**
95 * @tc.name: MakeAndRegisterAbility001
96 * @tc.desc: Check MakeAndRegisterAbility
97 * @tc.type: FUNC
98 * @tc.require: I5KMF7
99 */
100 HWTEST_F(SystemAbilityTest, MakeAndRegisterAbility001, TestSize.Level2)
101 {
102 bool res = SystemAbility::MakeAndRegisterAbility(new MockSaRealize(SAID, false));
103 EXPECT_EQ(res, false);
104 }
105
106 /**
107 * @tc.name: MakeAndRegisterAbility002
108 * @tc.desc: Check MakeAndRegisterAbility
109 * @tc.type: FUNC
110 * @tc.require: I5KMF7
111 */
112 HWTEST_F(SystemAbilityTest, MakeAndRegisterAbility002, TestSize.Level2)
113 {
114 bool ret = LocalAbilityManager::GetInstance().profileParser_->ParseSaProfiles(TEST_RESOURCE_PATH + "1489.xml");
115 EXPECT_EQ(ret, true);
116 bool res = SystemAbility::MakeAndRegisterAbility(new MockSaRealize(SAID, false));
117 EXPECT_EQ(res, true);
118 }
119
120 /**
121 * @tc.name: Publish001
122 * @tc.desc: Verify Publish when systemabitly is nullptr
123 * @tc.type: FUNC
124 * @tc.require: I5KMF7
125 */
126 HWTEST_F(SystemAbilityTest, Publish001, TestSize.Level2)
127 {
128 std::shared_ptr<SystemAbility> sysAby = std::make_shared<MockSaRealize>(SAID, false);
129 bool ret = sysAby->Publish(nullptr);
130 EXPECT_FALSE(ret);
131 }
132
133 /**
134 * @tc.name: Publish002
135 * @tc.desc: Verify Publish function
136 * @tc.type: FUNC
137 * @tc.require: I5KMF7
138 */
139 HWTEST_F(SystemAbilityTest, Publish002, TestSize.Level2)
140 {
141 std::shared_ptr<SystemAbility> sysAby = std::make_shared<MockSaRealize>(SAID, false);
142 sptr<IRemoteObject> obj(new MockLocalAbilityManager());
143 bool ret = sysAby->Publish(obj);
144 ASSERT_TRUE(ret);
145 sysAby->Stop();
146 sysAby->StopAbility(-1);
147 sysAby->Start();
148 sysAby->Stop();
149 EXPECT_FALSE(sysAby->isRunning_);
150 }
151
152 /**
153 * @tc.name: SetDependTimeout001
154 * @tc.desc: Verify SetDependTimeout
155 * @tc.type: FUNC
156 * @tc.require: I5KMF7
157 */
158 HWTEST_F(SystemAbilityTest, SetDependTimeout001, TestSize.Level2)
159 {
160 std::shared_ptr<SystemAbility> sysAby = std::make_shared<MockSaRealize>(SAID, false);
161 sysAby->SetDependTimeout(0);
162 sysAby->SetDependTimeout(MOCK_DEPEND_TIMEOUT);
163 EXPECT_EQ(sysAby->GetDependTimeout(), MOCK_DEPEND_TIMEOUT);
164 }
165
166 /**
167 * @tc.name: GetSystemAbility001
168 * @tc.desc: Check GetSystemAbility
169 * @tc.type: FUNC
170 * @tc.require: I5KMF7
171 */
172 HWTEST_F(SystemAbilityTest, GetSystemAbility001, TestSize.Level2)
173 {
174 std::shared_ptr<SystemAbility> sysAby = std::make_shared<MockSaRealize>(SAID, false);
175 sptr<IRemoteObject> obj = sysAby->GetSystemAbility(-1);
176 EXPECT_TRUE(obj == nullptr);
177 }
178 }
179 }