• 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 #ifndef FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H
17 #define FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H
18 
19 #include <gtest/gtest.h>
20 #include "gmock/gmock.h"
21 #include "semaphore_ex.h"
22 #include "app_scheduler_interface.h"
23 #include "app_mgr_stub.h"
24 #include "app_log_wrapper.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 class MockAppMgrService : public AppMgrStub {
29 public:
30     MOCK_METHOD4(LoadAbility,
31         void(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken,
32             const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo));
33     MOCK_METHOD1(TerminateAbility, void(const sptr<IRemoteObject> &token));
34     MOCK_METHOD2(UpdateAbilityState, void(const sptr<IRemoteObject> &token, const AbilityState state));
35     MOCK_METHOD1(SetAppFreezingTime, void(int time));
36     MOCK_METHOD1(GetAppFreezingTime, void(int &time));
37 
AttachApplication(const sptr<IRemoteObject> & app)38     virtual void AttachApplication(const sptr<IRemoteObject> &app) override
39     {
40         GTEST_LOG_(INFO) << "MockAppMgrService::AttachApplication called";
41         Attached_ = true;
42         EXPECT_TRUE(Attached_);
43         Appthread_ = iface_cast<IAppScheduler>(app);
44     }
45 
ApplicationForegrounded(const int32_t recordId)46     virtual void ApplicationForegrounded(const int32_t recordId)
47     {
48         GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationForegrounded called";
49         Foregrounded_ = true;
50         EXPECT_TRUE(Foregrounded_);
51     }
52 
ApplicationBackgrounded(const int32_t recordId)53     virtual void ApplicationBackgrounded(const int32_t recordId)
54     {
55         GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationBackgrounded called";
56         Backgrounded_ = true;
57         EXPECT_TRUE(Backgrounded_);
58     }
59 
ApplicationTerminated(const int32_t recordId)60     virtual void ApplicationTerminated(const int32_t recordId)
61     {
62         GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationTerminated called";
63         Terminated_ = true;
64         EXPECT_TRUE(Terminated_);
65     }
66     MOCK_METHOD2(CheckPermission, int32_t(const int32_t recordId, const std::string &permission));
67 
AbilityCleaned(const sptr<IRemoteObject> & token)68     virtual void AbilityCleaned(const sptr<IRemoteObject> &token)
69     {
70         GTEST_LOG_(INFO) << "MockAppMgrService::AbilityCleaned called";
71         Cleaned_ = true;
72         EXPECT_TRUE(Cleaned_);
73     }
74 
75     MOCK_METHOD1(KillApplication, int(const std::string &appName));
76 
GetAmsMgr()77     virtual sptr<IAmsMgr> GetAmsMgr() override
78     {
79         return nullptr;
80     };
ClearUpApplicationData(const std::string & appName)81     virtual int32_t ClearUpApplicationData(const std::string &appName) override
82     {
83         return 0;
84     }
85 
IsBackgroundRunningRestricted(const std::string & appName)86     virtual int IsBackgroundRunningRestricted(const std::string &appName) override
87     {
88         return 0;
89     };
GetAllRunningProcesses(std::vector<RunningProcessInfo> & info)90     virtual int GetAllRunningProcesses(std::vector<RunningProcessInfo> &info) override
91     {
92         return 0;
93     };
94 
RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)95     virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback)
96     {
97         callback_ = callback;
98     }
99 
CheckPermissionImpl(const int32_t recordId,const std::string & data)100     int32_t CheckPermissionImpl([[maybe_unused]] const int32_t recordId, const std::string &data)
101     {
102         data_ = data;
103         return 0;
104     }
105 
KillApplicationImpl(const std::string & data)106     void KillApplicationImpl(const std::string &data)
107     {
108         data_ = data;
109     }
110 
GetData()111     const std::string &GetData() const
112     {
113         return data_;
114     }
115 
Wait()116     void Wait()
117     {
118         sem_.Wait();
119     }
120 
Post()121     void Post()
122     {
123         sem_.Post();
124     }
125 
UpdateState()126     void UpdateState() const
127     {
128         if (!callback_) {
129             return;
130         }
131         AppProcessData processData;
132         processData.appName = "";
133         processData.pid = 1;
134         processData.appState = ApplicationState::APP_STATE_BEGIN;
135         callback_->OnAppStateChanged(processData);
136     }
137 
Terminate(const sptr<IRemoteObject> & token)138     void Terminate(const sptr<IRemoteObject> &token) const
139     {
140         if (!callback_) {
141             return;
142         }
143         AbilityState st = AbilityState::ABILITY_STATE_BEGIN;
144         callback_->OnAbilityRequestDone(token, st);
145     }
146 
ScheduleTerminateApplication()147     void ScheduleTerminateApplication()
148     {
149         if (Appthread_ != nullptr) {
150             Appthread_->ScheduleTerminateApplication();
151         }
152     }
153 
ScheduleLaunchApplication(const AppLaunchData & lanchdata)154     void ScheduleLaunchApplication(const AppLaunchData &lanchdata)
155     {
156         if (Appthread_ != nullptr) {
157             Appthread_->ScheduleLaunchApplication(lanchdata);
158         }
159     }
160 
ScheduleForegroundApplication()161     void ScheduleForegroundApplication()
162     {
163         if (Appthread_ != nullptr) {
164             Appthread_->ScheduleForegroundApplication();
165         }
166     }
167 
ScheduleBackgroundApplication()168     void ScheduleBackgroundApplication()
169     {
170         if (Appthread_ != nullptr) {
171             Appthread_->ScheduleBackgroundApplication();
172         }
173     }
174 
ScheduleShrinkMemory(const int32_t level)175     void ScheduleShrinkMemory(const int32_t level)
176     {
177         if (Appthread_ != nullptr) {
178             Appthread_->ScheduleShrinkMemory(level);
179         }
180     }
181 
ScheduleLowMemory()182     void ScheduleLowMemory()
183     {
184         if (Appthread_ != nullptr) {
185             Appthread_->ScheduleLowMemory();
186         }
187     }
188 
ScheduleLaunchAbility(const AbilityInfo & abilityinf,const sptr<IRemoteObject> & token)189     void ScheduleLaunchAbility(const AbilityInfo &abilityinf, const sptr<IRemoteObject> &token)
190     {
191         if (Appthread_ != nullptr) {
192             Appthread_->ScheduleLaunchAbility(abilityinf, token);
193         }
194     }
195 
ScheduleCleanAbility(const sptr<IRemoteObject> & token)196     void ScheduleCleanAbility(const sptr<IRemoteObject> &token)
197     {
198         if (Appthread_ != nullptr) {
199             Appthread_->ScheduleCleanAbility(token);
200         }
201     }
202 
ScheduleProfileChanged(const Profile & profile)203     void ScheduleProfileChanged(const Profile &profile)
204     {
205         if (Appthread_ != nullptr) {
206             Appthread_->ScheduleProfileChanged(profile);
207         }
208     }
209 
ScheduleConfigurationUpdated(const Configuration & config)210     void ScheduleConfigurationUpdated(const Configuration &config)
211     {
212         if (Appthread_ != nullptr) {
213             Appthread_->ScheduleConfigurationUpdated(config);
214         }
215     }
216 
GetAppthread()217     sptr<IAppScheduler> GetAppthread()
218     {
219         return Appthread_;
220     }
221 
IsAttached()222     bool IsAttached()
223     {
224         APP_LOGI("MockAppMgrService::IsAttached Attached_ = %{public}d", Attached_);
225         return Attached_;
226     }
227 
IsForegrounded()228     bool IsForegrounded()
229     {
230         APP_LOGI("MockAppMgrService::IsForegrounded Foregrounded_ = %{public}d", Foregrounded_);
231         return Foregrounded_;
232     }
233 
IsBackgrounded()234     bool IsBackgrounded()
235     {
236         APP_LOGI("MockAppMgrService::IsBackgrounded Backgrounded_ = %{public}d", Backgrounded_);
237         return Backgrounded_;
238     }
239 
IsTerminated()240     bool IsTerminated()
241     {
242         APP_LOGI("MockAppMgrService::IsTerminated Terminated_ = %{public}d", Terminated_);
243         return Terminated_;
244     }
245 
init()246     void init()
247     {
248         APP_LOGI("MockAppMgrService::init called");
249         Attached_ = false;
250     }
251 
AddDeathRecipient(const sptr<DeathRecipient> & recipient)252     bool AddDeathRecipient(const sptr<DeathRecipient> &recipient)
253     {
254         return true;
255     }
256 
257 private:
258     bool Attached_ = false;
259     bool Foregrounded_ = false;
260     bool Backgrounded_ = false;
261     bool Terminated_ = false;
262     bool Cleaned_ = false;
263     sptr<IAppScheduler> Appthread_ = nullptr;
264     Semaphore sem_;
265     std::string data_;
266     sptr<IAppStateCallback> callback_;
267 };
268 
269 }  // namespace AppExecFwk
270 }  // namespace OHOS
271 #endif  // FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H