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(ScheduleCacheProcess, void()); 65 Post()66 void Post() 67 { 68 lock_.Post(); 69 } 70 Wait()71 void Wait() 72 { 73 lock_.Wait(); 74 } 75 ShrinkMemory(const int level)76 void ShrinkMemory(const int level) 77 { 78 shrinkLevel_ = level; 79 lock_.Post(); 80 } 81 GetShrinkLevel()82 int GetShrinkLevel() const 83 { 84 return shrinkLevel_; 85 } 86 LaunchApplication(const AppLaunchData & launchData,const Configuration & config)87 void LaunchApplication(const AppLaunchData& launchData, const Configuration& config) 88 { 89 launchData_ = launchData; 90 lock_.Post(); 91 } 92 CompareAppLaunchData(const AppLaunchData & launchData)93 bool CompareAppLaunchData(const AppLaunchData& launchData) const 94 { 95 if (launchData_.GetApplicationInfo().name != launchData.GetApplicationInfo().name) { 96 return false; 97 } 98 if (launchData_.GetProfile().GetName() != launchData.GetProfile().GetName()) { 99 return false; 100 } 101 if (launchData_.GetProcessInfo().GetProcessName() != launchData.GetProcessInfo().GetProcessName()) { 102 return false; 103 } 104 return true; 105 } 106 LaunchAbility(const AbilityInfo & info,const sptr<IRemoteObject> &)107 void LaunchAbility(const AbilityInfo& info, const sptr<IRemoteObject>&) 108 { 109 abilityInfo_ = info; 110 lock_.Post(); 111 } 112 CompareAbilityInfo(const AbilityInfo & info)113 bool CompareAbilityInfo(const AbilityInfo& info) const 114 { 115 return (info.name == abilityInfo_.name); 116 } 117 ProfileChanged(const Profile & profile)118 void ProfileChanged(const Profile& profile) 119 { 120 profile_ = profile; 121 lock_.Post(); 122 } 123 CompareProfile(const Profile & profile)124 bool CompareProfile(const Profile& profile) const 125 { 126 return (profile.GetName() == profile_.GetName()); 127 } 128 ScheduleChangeAppGcState(int32_t state)129 int32_t ScheduleChangeAppGcState(int32_t state) override 130 { 131 return 0; 132 } 133 134 private: 135 Semaphore lock_; 136 volatile int shrinkLevel_ = 0; 137 AppLaunchData launchData_; 138 AbilityInfo abilityInfo_; 139 Profile profile_; 140 }; 141 } // namespace AppExecFwk 142 } // namespace OHOS 143 #endif // MOCK_OHOS_ABILITY_RUNTIME_MOCK_APPLICATION_H 144