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