1 /*
2  * Copyright (c) 2022 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 "mock_context_impl.h"
17 #include "sys_mgr_client.h"
18 
19 namespace OHOS {
20 namespace AbilityRuntime {
21 
GetBundleName() const22 std::string MockContextImpl::GetBundleName() const
23 {
24     return "com.test.bundleName";
25 }
26 
GetBundleCodeDir()27 std::string MockContextImpl::GetBundleCodeDir()
28 {
29     return "/code";
30 }
31 
GetCacheDir()32 std::string MockContextImpl::GetCacheDir()
33 {
34     return "/cache";
35 }
36 
IsUpdatingConfigurations()37 bool MockContextImpl::IsUpdatingConfigurations()
38 {
39     return true;
40 }
41 
PrintDrawnCompleted()42 bool MockContextImpl::PrintDrawnCompleted()
43 {
44     return true;
45 }
46 
GetDatabaseDir()47 std::string MockContextImpl::GetDatabaseDir()
48 {
49     return "/data/app/database";
50 }
51 
GetPreferencesDir()52 std::string MockContextImpl::GetPreferencesDir()
53 {
54     return "/preferences";
55 }
56 
GetTempDir()57 std::string MockContextImpl::GetTempDir()
58 {
59     return "/temp";
60 }
61 
GetResourceDir()62 std::string MockContextImpl::GetResourceDir()
63 {
64     return "/resfile";
65 }
66 
GetFilesDir()67 std::string MockContextImpl::GetFilesDir()
68 {
69     return "/files";
70 }
71 
GetDistributedFilesDir()72 std::string MockContextImpl::GetDistributedFilesDir()
73 {
74     return "/mnt/hmdfs/device_view/local/data/bundleName";
75 }
76 
GetCloudFileDir()77 std::string MockContextImpl::GetCloudFileDir()
78 {
79     return "/cloud";
80 }
81 
GetGroupDir(std::string groupId)82 std::string MockContextImpl::GetGroupDir(std::string groupId)
83 {
84     return "/group";
85 }
86 
GetSystemDatabaseDir(const std::string & groupId,bool checkExist,std::string & databaseDir)87 int32_t MockContextImpl::GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir)
88 {
89     return 0;
90 }
91 
GetSystemPreferencesDir(const std::string & groupId,bool checkExist,std::string & preferencesDir)92 int32_t MockContextImpl::GetSystemPreferencesDir(const std::string &groupId, bool checkExist,
93     std::string &preferencesDir)
94 {
95     return 0;
96 }
97 
CreateModuleContext(const std::string & moduleName)98 std::shared_ptr<Context> MockContextImpl::CreateModuleContext(const std::string &moduleName)
99 {
100     std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
101     return appContext;
102 }
103 
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)104 std::shared_ptr<Context> MockContextImpl::CreateModuleContext(const std::string &bundleName,
105     const std::string &moduleName)
106 {
107     std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
108     return appContext;
109 }
110 
CreateSystemHspModuleResourceManager(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)111 int32_t MockContextImpl::CreateSystemHspModuleResourceManager(const std::string &bundleName,
112     const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
113 {
114     return 0;
115 }
116 
CreateModuleResourceManager(const std::string & bundleName,const std::string & moduleName)117 std::shared_ptr<Global::Resource::ResourceManager> MockContextImpl::CreateModuleResourceManager(
118     const std::string &bundleName, const std::string &moduleName)
119 {
120     return nullptr;
121 }
122 
GetArea()123 int MockContextImpl::GetArea()
124 {
125     return 1;
126 }
127 
GetBaseDir() const128 std::string MockContextImpl::GetBaseDir() const
129 {
130     return "/data/app/base";
131 }
132 
CreateBundleContext(const std::string & bundleName)133 std::shared_ptr<Context> MockContextImpl::CreateBundleContext(const std::string &bundleName)
134 {
135     std::shared_ptr<ContextImpl> appContext = std::make_shared<ContextImpl>();
136     return appContext;
137 }
138 
GetBundleManager() const139 sptr<AppExecFwk::IBundleMgr> MockContextImpl::GetBundleManager() const
140 {
141     auto instance = OHOS::DelayedSingleton<AppExecFwk::SysMrgClient>::GetInstance();
142     if (instance == nullptr) {
143         return nullptr;
144     }
145     auto bundleObj = instance->GetSystemAbility(401);
146     if (bundleObj == nullptr) {
147         return nullptr;
148     }
149     sptr<AppExecFwk::IBundleMgr> bms = iface_cast<AppExecFwk::IBundleMgr>(bundleObj);
150     return bms;
151 }
152 
GetBundleCodePath() const153 std::string MockContextImpl::GetBundleCodePath() const
154 {
155     return "codePath";
156 }
157 
InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> & abilityInfo)158 void MockContextImpl::InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo)
159 {}
160 
GetDeviceType() const161 Global::Resource::DeviceType MockContextImpl::GetDeviceType() const
162 {
163     return Global::Resource::DeviceType::DEVICE_NOT_SET;
164 }
165 
GetApplicationInfo() const166 std::shared_ptr<AppExecFwk::ApplicationInfo> MockContextImpl::GetApplicationInfo() const
167 {
168     std::shared_ptr<AppExecFwk::ApplicationInfo> info = std::make_shared<AppExecFwk::ApplicationInfo>();
169     info->name = "ContextTest";
170     return info;
171 }
172 
GetResourceManager() const173 std::shared_ptr<Global::Resource::ResourceManager> MockContextImpl::GetResourceManager() const
174 {
175     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
176     return resourceManager;
177 }
178 
GetConfiguration() const179 std::shared_ptr<AppExecFwk::Configuration> MockContextImpl::GetConfiguration() const
180 {
181     std::shared_ptr<AppExecFwk::Configuration> config = std::make_shared<AppExecFwk::Configuration>();
182     return config;
183 }
184 }  // namespace AbilityRuntime
185 }  // namespace OHOS
186