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_METHOD2(ScheduleUpdateApplicationInfoInstalled, void(const ApplicationInfo&, const std::string&)); 43 MOCK_METHOD2(ScheduleAcceptWant, void(const AAFwk::Want& want, const std::string& moduleName)); 44 MOCK_METHOD1(SchedulePrepareTerminate, void(const std::string &moduleName)); 45 MOCK_METHOD2(ScheduleNewProcessRequest, void(const AAFwk::Want& want, const std::string& moduleName)); 46 MOCK_METHOD3(ScheduleNotifyLoadRepairPatch, int32_t(const std::string& bundleName, 47 const sptr<IQuickFixCallback>& callback, const int32_t recordId)); 48 MOCK_METHOD2(ScheduleNotifyHotReloadPage, int32_t(const sptr<IQuickFixCallback>& callback, const int32_t recordId)); 49 MOCK_METHOD3(ScheduleNotifyUnLoadRepairPatch, int32_t(const std::string& bundleName, 50 const sptr<IQuickFixCallback>& callback, const int32_t recordId)); 51 MOCK_METHOD1(ScheduleNotifyAppFault, int32_t(const FaultData &faultData)); 52 MOCK_METHOD1(AttachAppDebug, void(bool isDebugFromLocal)); 53 MOCK_METHOD0(DetachAppDebug, void()); 54 MOCK_METHOD2(SetAppWaitingDebug, int32_t(const std::string &bundleName, bool isPersist)); 55 MOCK_METHOD0(CancelAppWaitingDebug, int32_t()); 56 MOCK_METHOD1(GetWaitingDebugApp, int32_t(std::vector<std::string> &debugInfoList)); 57 MOCK_METHOD1(IsWaitingDebugApp, bool(const std::string &bundleName)); 58 MOCK_METHOD0(ClearNonPersistWaitingDebugFlag, void()); 59 MOCK_METHOD1(ScheduleDumpIpcStart, int32_t(std::string &result)); 60 MOCK_METHOD1(ScheduleDumpIpcStop, int32_t(std::string &result)); 61 MOCK_METHOD1(ScheduleDumpIpcStat, int32_t(std::string &result)); 62 MOCK_METHOD0(IsMemorySizeSufficent, bool()); 63 MOCK_METHOD1(ScheduleJsHeapMemory, void(OHOS::AppExecFwk::JsHeapDumpInfo &info)); 64 MOCK_METHOD1(ScheduleCjHeapMemory, void(OHOS::AppExecFwk::CjHeapDumpInfo &info)); 65 MOCK_METHOD1(ScheduleDumpFfrt, int32_t(std::string& result)); 66 MOCK_METHOD1(SetWatchdogBackgroundStatus, void(bool status)); 67 MOCK_METHOD0(ScheduleClearPageStack, void()); 68 MOCK_METHOD0(ScheduleCacheProcess, void()); 69 Post()70 void Post() 71 { 72 lock_.Post(); 73 } 74 Wait()75 void Wait() 76 { 77 lock_.Wait(); 78 } 79 ShrinkMemory(const int level)80 void ShrinkMemory(const int level) 81 { 82 shrinkLevel_ = level; 83 lock_.Post(); 84 } 85 GetShrinkLevel()86 int GetShrinkLevel() const 87 { 88 return shrinkLevel_; 89 } 90 LaunchApplication(const AppLaunchData & launchData,const Configuration & config)91 void LaunchApplication(const AppLaunchData& launchData, const Configuration& config) 92 { 93 launchData_ = launchData; 94 lock_.Post(); 95 } 96 CompareAppLaunchData(const AppLaunchData & launchData)97 bool CompareAppLaunchData(const AppLaunchData& launchData) const 98 { 99 if (launchData_.GetApplicationInfo().name != launchData.GetApplicationInfo().name) { 100 return false; 101 } 102 if (launchData_.GetProfile().GetName() != launchData.GetProfile().GetName()) { 103 return false; 104 } 105 if (launchData_.GetProcessInfo().GetProcessName() != launchData.GetProcessInfo().GetProcessName()) { 106 return false; 107 } 108 return true; 109 } 110 LaunchAbility(const AbilityInfo & info,const sptr<IRemoteObject> &)111 void LaunchAbility(const AbilityInfo& info, const sptr<IRemoteObject>&) 112 { 113 abilityInfo_ = info; 114 lock_.Post(); 115 } 116 CompareAbilityInfo(const AbilityInfo & info)117 bool CompareAbilityInfo(const AbilityInfo& info) const 118 { 119 return (info.name == abilityInfo_.name); 120 } 121 ProfileChanged(const Profile & profile)122 void ProfileChanged(const Profile& profile) 123 { 124 profile_ = profile; 125 lock_.Post(); 126 } 127 CompareProfile(const Profile & profile)128 bool CompareProfile(const Profile& profile) const 129 { 130 return (profile.GetName() == profile_.GetName()); 131 } 132 133 int32_t ScheduleChangeAppGcState(int32_t state, uint64_t tid = 0) override 134 { 135 return 0; 136 } 137 138 private: 139 Semaphore lock_; 140 volatile int shrinkLevel_ = 0; 141 AppLaunchData launchData_; 142 AbilityInfo abilityInfo_; 143 Profile profile_; 144 }; 145 } // namespace AppExecFwk 146 } // namespace OHOS 147 #endif // MOCK_OHOS_ABILITY_RUNTIME_MOCK_APPLICATION_H 148