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