1 /*
2 * Copyright (c) 2023-2024 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 "timed_task.h"
17 #include "ability_manager_helper.h"
18 #include "app_mgr_helper.h"
19 #include "bundle_manager_helper.h"
20 #include "common_event_observer.h"
21 #ifdef ENABLE_BACKGROUND_TASK_MGR
22 #include "background_task_helper.h"
23 #endif
24 #include "ibundle_manager_helper.h"
25
26 namespace {
27 static constexpr char TEST_DEFAULT_BUNDLE[] = "bundleName";
28 static constexpr int32_t EXEMPT_ALL_RESOURCES = 100;
29 bool g_mockGetAllRunningProcesses = true;
30 bool g_mockGetRunningSystemProcess = true;
31 bool g_mockGetBackgroundTask = true;
32 bool g_mockSubscribeObserver = true;
33 }
34
35 namespace OHOS {
36 namespace DevStandbyMgr {
37 namespace {
38 std::shared_ptr<IBundleManagerHelper> bundleManagerHelperMock;
39 }
40
SetBundleManagerHelper(std::shared_ptr<IBundleManagerHelper> mock)41 void SetBundleManagerHelper(std::shared_ptr<IBundleManagerHelper> mock)
42 {
43 bundleManagerHelperMock = mock;
44 }
45
CleanBundleManagerHelper()46 void CleanBundleManagerHelper()
47 {
48 bundleManagerHelperMock.reset();
49 }
50
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)51 bool BundleManagerHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag,
52 const int userId, AppExecFwk::ApplicationInfo &appInfo)
53 {
54 bool ret {true};
55 if (bundleManagerHelperMock) {
56 ret = bundleManagerHelperMock->GetApplicationInfo(appName, flag, userId, appInfo);
57 } else {
58 appInfo.resourcesApply = { EXEMPT_ALL_RESOURCES };
59 }
60 return ret;
61 }
62
CreateTimer(bool repeat,uint64_t interval,bool isExact,bool isIdle,const std::function<void ()> & callBack)63 uint64_t TimedTask::CreateTimer(bool repeat, uint64_t interval, bool isExact, bool isIdle,
64 const std::function<void()>& callBack)
65 {
66 return 1;
67 }
68
StartDayNightSwitchTimer(uint64_t & timeId)69 bool TimedTask::StartDayNightSwitchTimer(uint64_t& timeId)
70 {
71 return true;
72 }
73
GetRunningSystemProcess(std::list<SystemProcessInfo> & systemProcessInfos)74 bool AbilityManagerHelper::GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos)
75 {
76 systemProcessInfos.emplace_back(SystemProcessInfo{});
77 return g_mockGetRunningSystemProcess;
78 }
79
GetAllRunningProcesses(std::vector<AppExecFwk::RunningProcessInfo> & allAppProcessInfos)80 bool AppMgrHelper::GetAllRunningProcesses(std::vector<AppExecFwk::RunningProcessInfo>& allAppProcessInfos)
81 {
82 allAppProcessInfos.emplace_back(AppExecFwk::RunningProcessInfo{});
83 return g_mockGetAllRunningProcesses;
84 }
85
GetForegroundApplications(std::vector<AppExecFwk::AppStateData> & fgApps)86 bool AppMgrHelper::GetForegroundApplications(std::vector<AppExecFwk::AppStateData>& fgApps)
87 {
88 return g_mockGetAllRunningProcesses;
89 }
90
91
Connect()92 bool AppMgrHelper::Connect()
93 {
94 return true;
95 }
96
GetAppRunningStateByBundleName(const std::string & bundleName,bool & isRunning)97 bool AppMgrHelper::GetAppRunningStateByBundleName(const std::string &bundleName, bool& isRunning)
98 {
99 isRunning = true;
100 return true;
101 }
102
GetClientBundleName(int32_t uid)103 std::string BundleManagerHelper::GetClientBundleName(int32_t uid)
104 {
105 return TEST_DEFAULT_BUNDLE;
106 }
107
Connect()108 bool BundleManagerHelper::Connect()
109 {
110 return true;
111 }
112
Subscribe()113 bool CommonEventObserver::Subscribe()
114 {
115 return true;
116 }
117
Unsubscribe()118 bool CommonEventObserver::Unsubscribe()
119 {
120 return true;
121 }
122
123 #ifdef ENABLE_BACKGROUND_TASK_MGR
GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> & list)124 bool BackgroundTaskHelper::GetContinuousTaskApps(std::vector<std::shared_ptr<ContinuousTaskCallbackInfo>> &list)
125 {
126 return g_mockGetBackgroundTask;
127 }
128
GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> & list)129 bool BackgroundTaskHelper::GetTransientTaskApps(std::vector<std::shared_ptr<TransientTaskAppInfo>> &list)
130 {
131 return g_mockGetBackgroundTask;
132 }
133 #endif
134
SubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> & observer)135 bool AppMgrHelper::SubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> &observer)
136 {
137 return g_mockSubscribeObserver;
138 }
139
UnsubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> & observer)140 bool AppMgrHelper::UnsubscribeObserver(const sptr<AppExecFwk::IApplicationStateObserver> &observer)
141 {
142 return g_mockSubscribeObserver;
143 }
144
MockGetAllRunningProcesses(bool mockRet)145 void IBundleManagerHelper::MockGetAllRunningProcesses(bool mockRet)
146 {
147 g_mockGetAllRunningProcesses = mockRet;
148 }
149
MockGetRunningSystemProcess(bool mockRet)150 void IBundleManagerHelper::MockGetRunningSystemProcess(bool mockRet)
151 {
152 g_mockGetRunningSystemProcess = mockRet;
153 }
154
MockGetBackgroundTask(bool mockRet)155 void IBundleManagerHelper::MockGetBackgroundTask(bool mockRet)
156 {
157 g_mockGetBackgroundTask = mockRet;
158 }
159
MockSubscribeObserver(bool mockRet)160 void IBundleManagerHelper::MockSubscribeObserver(bool mockRet)
161 {
162 g_mockSubscribeObserver = mockRet;
163 }
164 } // namespace DevStandbyMgr
165 } // namespace OHOS