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