1 /*
2  * Copyright (c) 2021-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 #ifndef MOCK_OHOS_ABILITY_RUNTIME_MOCK_APPLICATION_H
16 #define MOCK_OHOS_ABILITY_RUNTIME_MOCK_APPLICATION_H
17 
18 #include "gmock/gmock.h"
19 #include "semaphore_ex.h"
20 #include "app_scheduler_host.h"
21 #include "app_malloc_info.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 class MockApplication : public AppSchedulerHost {
26 public:
27     MOCK_METHOD0(ScheduleForegroundApplication, bool());
28     MOCK_METHOD0(ScheduleBackgroundApplication, void());
29     MOCK_METHOD1(ScheduleTerminateApplication, void(bool));
30     MOCK_METHOD1(ScheduleShrinkMemory, void(const int));
31     MOCK_METHOD0(ScheduleLowMemory, void());
32     MOCK_METHOD1(ScheduleMemoryLevel, void(int32_t level));
33     MOCK_METHOD2(ScheduleHeapMemory, void(const int32_t pid, OHOS::AppExecFwk::MallocInfo &mallocInfo));
34     MOCK_METHOD2(ScheduleLaunchApplication, void(const AppLaunchData&, const Configuration& config));
35     MOCK_METHOD4(ScheduleLaunchAbility, void(const AbilityInfo&, const sptr<IRemoteObject>&,
36         const std::shared_ptr<AAFwk::Want>&, int32_t));
37     MOCK_METHOD2(ScheduleCleanAbility, void(const sptr<IRemoteObject>&, bool));
38     MOCK_METHOD1(ScheduleProfileChanged, void(const Profile&));
39     MOCK_METHOD1(ScheduleConfigurationUpdated, void(const Configuration&));
40     MOCK_METHOD0(ScheduleProcessSecurityExit, void());
41     MOCK_METHOD1(ScheduleAbilityStage, void(const HapModuleInfo&));
42     MOCK_METHOD1(ScheduleUpdateApplicationInfoInstalled, void(const ApplicationInfo&));
43     MOCK_METHOD2(ScheduleAcceptWant, void(const AAFwk::Want& want, const std::string& moduleName));
44     MOCK_METHOD2(ScheduleNewProcessRequest, void(const AAFwk::Want& want, const std::string& moduleName));
45     MOCK_METHOD3(ScheduleNotifyLoadRepairPatch, int32_t(const std::string& bundleName,
46         const sptr<IQuickFixCallback>& callback, const int32_t recordId));
47     MOCK_METHOD2(ScheduleNotifyHotReloadPage, int32_t(const sptr<IQuickFixCallback>& callback, const int32_t recordId));
48     MOCK_METHOD3(ScheduleNotifyUnLoadRepairPatch, int32_t(const std::string& bundleName,
49         const sptr<IQuickFixCallback>& callback, const int32_t recordId));
50     MOCK_METHOD1(ScheduleNotifyAppFault, int32_t(const FaultData &faultData));
51     MOCK_METHOD0(AttachAppDebug, void());
52     MOCK_METHOD0(DetachAppDebug, void());
53     MOCK_METHOD2(SetAppWaitingDebug, int32_t(const std::string &bundleName, bool isPersist));
54     MOCK_METHOD0(CancelAppWaitingDebug, int32_t());
55     MOCK_METHOD1(GetWaitingDebugApp, int32_t(std::vector<std::string> &debugInfoList));
56     MOCK_METHOD1(IsWaitingDebugApp, bool(const std::string &bundleName));
57     MOCK_METHOD0(ClearNonPersistWaitingDebugFlag, void());
58     MOCK_METHOD1(ScheduleDumpIpcStart, int32_t(std::string &result));
59     MOCK_METHOD1(ScheduleDumpIpcStop, int32_t(std::string &result));
60     MOCK_METHOD1(ScheduleDumpIpcStat, int32_t(std::string &result));
61     MOCK_METHOD0(IsMemorySizeSufficent, bool());
62     MOCK_METHOD1(ScheduleJsHeapMemory, void(OHOS::AppExecFwk::JsHeapDumpInfo &info));
63     MOCK_METHOD1(ScheduleDumpFfrt, int32_t(std::string& result));
64     MOCK_METHOD0(ScheduleClearPageStack, void());
65     MOCK_METHOD0(ScheduleCacheProcess, void());
66 
Post()67     void Post()
68     {
69         lock_.Post();
70     }
71 
Wait()72     void Wait()
73     {
74         lock_.Wait();
75     }
76 
ShrinkMemory(const int level)77     void ShrinkMemory(const int level)
78     {
79         shrinkLevel_ = level;
80         lock_.Post();
81     }
82 
GetShrinkLevel()83     int GetShrinkLevel() const
84     {
85         return shrinkLevel_;
86     }
87 
LaunchApplication(const AppLaunchData & launchData,const Configuration & config)88     void LaunchApplication(const AppLaunchData& launchData, const Configuration& config)
89     {
90         launchData_ = launchData;
91         lock_.Post();
92     }
93 
CompareAppLaunchData(const AppLaunchData & launchData)94     bool CompareAppLaunchData(const AppLaunchData& launchData) const
95     {
96         if (launchData_.GetApplicationInfo().name != launchData.GetApplicationInfo().name) {
97             return false;
98         }
99         if (launchData_.GetProfile().GetName() != launchData.GetProfile().GetName()) {
100             return false;
101         }
102         if (launchData_.GetProcessInfo().GetProcessName() != launchData.GetProcessInfo().GetProcessName()) {
103             return false;
104         }
105         return true;
106     }
107 
LaunchAbility(const AbilityInfo & info,const sptr<IRemoteObject> &)108     void LaunchAbility(const AbilityInfo& info, const sptr<IRemoteObject>&)
109     {
110         abilityInfo_ = info;
111         lock_.Post();
112     }
113 
CompareAbilityInfo(const AbilityInfo & info)114     bool CompareAbilityInfo(const AbilityInfo& info) const
115     {
116         return (info.name == abilityInfo_.name);
117     }
118 
ProfileChanged(const Profile & profile)119     void ProfileChanged(const Profile& profile)
120     {
121         profile_ = profile;
122         lock_.Post();
123     }
124 
CompareProfile(const Profile & profile)125     bool CompareProfile(const Profile& profile) const
126     {
127         return (profile.GetName() == profile_.GetName());
128     }
129 
ScheduleChangeAppGcState(int32_t state)130     int32_t ScheduleChangeAppGcState(int32_t state) override
131     {
132         return 0;
133     }
134 
135 private:
136     Semaphore lock_;
137     volatile int shrinkLevel_ = 0;
138     AppLaunchData launchData_;
139     AbilityInfo abilityInfo_;
140     Profile profile_;
141 };
142 }  // namespace AppExecFwk
143 }  // namespace OHOS
144 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_APPLICATION_H
145