1 /*
2  * Copyright (c) 2021 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 "system_ability_helper.h"
17 
18 #include <map>
19 
20 #include "hilog_tag_wrapper.h"
21 #include "mock_ability_mgr_host.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 static std::map<int32_t, sptr<IRemoteObject>> g_abilities;
27 
GetSystemAbility(const int32_t systemAbilityId)28 sptr<IRemoteObject> SystemAbilityHelper::GetSystemAbility(const int32_t systemAbilityId)
29 {
30     TAG_LOGD(AAFwkTag::TEST, "mock system ability helper get %{public}d system ability", systemAbilityId);
31     auto iter = g_abilities.find(systemAbilityId);
32     if (iter != g_abilities.end()) {
33         return iter->second;
34     }
35     if (systemAbilityId == ABILITY_MGR_SERVICE_ID) {
36         return new MockAbilityMgrStub();
37     }
38     return nullptr;
39 }
40 
AddSystemAbility(const int32_t systemAbilityId,const sptr<IRemoteObject> & systemAbility)41 bool SystemAbilityHelper::AddSystemAbility(const int32_t systemAbilityId, const sptr<IRemoteObject>& systemAbility)
42 {
43     if (g_abilities.erase(systemAbilityId) > 0) {
44         TAG_LOGD(AAFwkTag::TEST, "mock system ability helper add system ability erase exist key");
45     }
46     TAG_LOGD(AAFwkTag::TEST, "mock system ability helper emplace %{public}d system ability", systemAbilityId);
47     g_abilities.emplace(systemAbilityId, systemAbility);
48     // mock helper always return true.
49     return true;
50 }
51 
RemoveSystemAbility(const int32_t systemAbilityId)52 bool SystemAbilityHelper::RemoveSystemAbility(const int32_t systemAbilityId)
53 {
54     TAG_LOGD(AAFwkTag::TEST, "mock system ability helper remove system ability");
55     if (g_abilities.erase(systemAbilityId) > 0) {
56         TAG_LOGD(AAFwkTag::TEST, "mock system ability helper remove %{public}d system ability erase exist key",
57                  systemAbilityId);
58     }
59     // mock helper always return true.
60     return true;
61 }
62 }  // namespace AppExecFwk
63 }  // namespace OHOS
64