1 /* 2 * Copyright (C) 2022 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_APP_MGR_SERVICE_H 16 #define MOCK_APP_MGR_SERVICE_H 17 #include <vector> 18 #include "app_mgr_stub.h" 19 #include <gmock/gmock.h> 20 #include "iremote_proxy.h" 21 #include "iremote_stub.h" 22 #include "semaphore_ex.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 class MockAppMgrService : public AppMgrStub { 27 public: 28 MOCK_METHOD5(LoadAbility, 29 void(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 30 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<ApplicationInfo> &appInfo, 31 const std::shared_ptr<AAFwk::Want> &want)); 32 MOCK_METHOD2(TerminateAbility, void(const sptr<IRemoteObject> &token, bool clearMissionFlag)); 33 MOCK_METHOD2(UpdateAbilityState, void(const sptr<IRemoteObject> &token, const AbilityState state)); 34 MOCK_METHOD1(AttachApplication, void(const sptr<IRemoteObject> &app)); 35 MOCK_METHOD1(NotifyMemoryLevel, int(int32_t level)); 36 MOCK_METHOD1(ApplicationForegrounded, void(const int32_t recordId)); 37 MOCK_METHOD1(ApplicationBackgrounded, void(const int32_t recordId)); 38 MOCK_METHOD1(ApplicationTerminated, void(const int32_t recordId)); 39 MOCK_METHOD2(CheckPermission, int32_t(const int32_t recordId, const std::string &permission)); 40 MOCK_METHOD1(AbilityCleaned, void(const sptr<IRemoteObject> &token)); 41 MOCK_METHOD1(KillApplication, int32_t(const std::string &appName)); 42 MOCK_METHOD2(KillApplicationByUid, int(const std::string &, const int uid)); 43 MOCK_METHOD1(IsBackgroundRunningRestricted, int(const std::string &bundleName)); 44 MOCK_METHOD1(GetAllRunningProcesses, int(std::vector<RunningProcessInfo> &info)); 45 MOCK_METHOD2(GetProcessRunningInfosByUserId, int(std::vector<RunningProcessInfo> &info, int32_t userId)); 46 MOCK_METHOD0(GetAmsMgr, sptr<IAmsMgr>()); 47 MOCK_METHOD1(GetAppFreezingTime, void(int &time)); 48 MOCK_METHOD1(SetAppFreezingTime, void(int time)); 49 MOCK_METHOD1(ClearUpApplicationData, int32_t(const std::string &bundleName)); 50 MOCK_METHOD1(StartupResidentProcess, void(const std::vector<AppExecFwk::BundleInfo> &bundleInfos)); 51 MOCK_METHOD1(AddAbilityStageDone, void(const int32_t recordId)); 52 MOCK_METHOD0(PreStartNWebSpawnProcess, int()); 53 MOCK_METHOD4(StartRenderProcess, int(const std::string&, int32_t, int32_t, pid_t&)); 54 MOCK_METHOD1(AttachRenderProcess, void(const sptr<IRemoteObject> &renderScheduler)); 55 MOCK_METHOD2(GetRenderProcessTerminationStatus, int(pid_t renderPid, int &status)); 56 MOCK_METHOD3(ScheduleAcceptWantDone, 57 void(const int32_t recordId, const AAFwk::Want &want, const std::string &flag)); 58 MOCK_METHOD2(GetAbilityRecordsByProcessID, int(const int pid, std::vector<sptr<IRemoteObject>> &tokens)); 59 MOCK_METHOD1(GetConfiguration, int32_t(Configuration& config)); 60 MOCK_METHOD1(UpdateConfiguration, int32_t(const Configuration &config)); 61 MOCK_METHOD1(RegisterConfigurationObserver, int32_t(const sptr<IConfigurationObserver> &observer)); 62 MOCK_METHOD1(UnregisterConfigurationObserver, int32_t(const sptr<IConfigurationObserver> &observer)); 63 #ifdef ABILITY_COMMAND_FOR_TEST 64 MOCK_METHOD0(BlockAppService, int()); 65 #endif 66 MOCK_METHOD1(GetAppRunningStateByBundleName, bool(const std::string &bundleName)); 67 MOCK_METHOD2(NotifyLoadRepairPatch, int32_t(const std::string &bundleName, 68 const sptr<IQuickFixCallback> &callback)); 69 MOCK_METHOD2(NotifyHotReloadPage, int32_t(const std::string &bundleName, const sptr<IQuickFixCallback> &callback)); 70 MOCK_METHOD2(NotifyUnLoadRepairPatch, int32_t(const std::string &bundleName, 71 const sptr<IQuickFixCallback> &callback)); 72 int32_t GetForegroundApplications(std::vector<AppStateData> &list) override; 73 int32_t RegisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer, 74 const std::vector<std::string> &bundleNameList); 75 int32_t UnregisterApplicationStateObserver(const sptr<IApplicationStateObserver> &observer); 76 77 void SwitchForeOrBackGround(uint32_t tokenId, int32_t flag); 78 StartUserTestProcess(const AAFwk::Want & want,const sptr<IRemoteObject> & observer,const BundleInfo & bundleInfo,int32_t userId)79 virtual int StartUserTestProcess( 80 const AAFwk::Want &want, const sptr<IRemoteObject> &observer, const BundleInfo &bundleInfo, int32_t userId) 81 { 82 return 0; 83 } 84 FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)85 virtual int FinishUserTest(const std::string &msg, const int64_t &resultCode, const std::string &bundleName) 86 { 87 return 0; 88 } 89 RegisterAppStateCallback(const sptr<IAppStateCallback> & callback)90 virtual void RegisterAppStateCallback(const sptr<IAppStateCallback> &callback) 91 { 92 callback_ = callback; 93 } 94 CheckPermissionImpl(const int32_t recordId,const std::string & data)95 int32_t CheckPermissionImpl([[maybe_unused]] const int32_t recordId, const std::string &data) 96 { 97 data_ = data; 98 return 0; 99 } 100 KillApplicationImpl(const std::string & data)101 void KillApplicationImpl(const std::string &data) 102 { 103 data_ = data; 104 } 105 GetData()106 const std::string &GetData() const 107 { 108 return data_; 109 } 110 Wait()111 void Wait() 112 { 113 sem_.Wait(); 114 } 115 Post()116 void Post() 117 { 118 sem_.Post(); 119 } 120 UpdateState()121 void UpdateState() const 122 { 123 if (!callback_) { 124 return; 125 } 126 AppProcessData processData; 127 processData.pid = 1; 128 processData.appState = ApplicationState::APP_STATE_BEGIN; 129 callback_->OnAppStateChanged(processData); 130 } 131 Terminate(const sptr<IRemoteObject> & token)132 void Terminate(const sptr<IRemoteObject> &token) const 133 { 134 if (!callback_) { 135 return; 136 } 137 AbilityState st = AbilityState::ABILITY_STATE_BEGIN; 138 callback_->OnAbilityRequestDone(token, st); 139 } 140 141 MOCK_METHOD4(SendRequest, int(uint32_t, MessageParcel &, MessageParcel &, MessageOption &)); 142 InvokeSendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)143 int InvokeSendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 144 { 145 code_ = code; 146 147 return 0; 148 } 149 150 int code_; 151 152 private: 153 Semaphore sem_; 154 std::string data_; 155 sptr<IAppStateCallback> callback_; 156 sptr<IApplicationStateObserver> observer_; 157 }; 158 } 159 } 160 #endif // MOCK_APP_MGR_SERVICE_H