1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 
18 #include "iservice_registry.h"
19 #include "mock_bundle_manager.h"
20 #include "system_ability_definition.h"
21 #include "system_ability_manager_proxy.h"
22 
23 namespace OHOS {
24 static sptr<AppExecFwk::BundleMgrStub> g_appMgrService = nullptr;
25 
GetInstance()26 SystemAbilityManagerClient &SystemAbilityManagerClient::GetInstance()
27 {
28     static auto instance = new SystemAbilityManagerClient();
29     return *instance;
30 }
31 
GetSystemAbility(int32_t systemAbilityId)32 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId)
33 {
34     sptr<IRemoteObject> remote = nullptr;
35     switch (systemAbilityId) {
36         case BUNDLE_MGR_SERVICE_SYS_ABILITY_ID:
37             if (!g_appMgrService) {
38                 GTEST_LOG_(INFO) << "GetSystemAbility(" << systemAbilityId << "): return Mock";
39                 g_appMgrService = new AppExecFwk::BundleMgrStub();
40             }
41             remote = g_appMgrService;
42             break;
43         default:
44             GTEST_LOG_(INFO) << "This service is not dummy!!!!" << systemAbilityId;
45             break;
46     }
47     return remote;
48 }
49 
GetSystemAbilityManager()50 sptr<ISystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
51 {
52     std::lock_guard<std::mutex> lock(systemAbilityManagerLock_);
53     if (systemAbilityManager_ != nullptr) {
54         return systemAbilityManager_;
55     }
56 
57     systemAbilityManager_ = new SystemAbilityManagerProxy(nullptr);
58     return systemAbilityManager_;
59 }
60 
GetSystemAbility(int32_t systemAbilityId,const std::string & deviceId)61 sptr<IRemoteObject> SystemAbilityManagerProxy::GetSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
62 {
63     return GetSystemAbility(systemAbilityId);
64 }
65 } // namespace OHOS