/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H #define FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H #include #include "gmock/gmock.h" #include "semaphore_ex.h" #include "app_scheduler_interface.h" #include "app_mgr_stub.h" #include "app_log_wrapper.h" namespace OHOS { namespace AppExecFwk { class MockAppMgrService : public AppMgrStub { public: MOCK_METHOD4(LoadAbility, void(const sptr &token, const sptr &preToken, const std::shared_ptr &abilityInfo, const std::shared_ptr &appInfo)); MOCK_METHOD1(TerminateAbility, void(const sptr &token)); MOCK_METHOD2(UpdateAbilityState, void(const sptr &token, const AbilityState state)); MOCK_METHOD1(SetAppFreezingTime, void(int time)); MOCK_METHOD1(GetAppFreezingTime, void(int &time)); virtual void AttachApplication(const sptr &app) override { GTEST_LOG_(INFO) << "MockAppMgrService::AttachApplication called"; Attached_ = true; EXPECT_TRUE(Attached_); Appthread_ = iface_cast(app); } virtual void ApplicationForegrounded(const int32_t recordId) { GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationForegrounded called"; Foregrounded_ = true; EXPECT_TRUE(Foregrounded_); } virtual void ApplicationBackgrounded(const int32_t recordId) { GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationBackgrounded called"; Backgrounded_ = true; EXPECT_TRUE(Backgrounded_); } virtual void ApplicationTerminated(const int32_t recordId) { GTEST_LOG_(INFO) << "MockAppMgrService::ApplicationTerminated called"; Terminated_ = true; EXPECT_TRUE(Terminated_); } MOCK_METHOD2(CheckPermission, int32_t(const int32_t recordId, const std::string &permission)); virtual void AbilityCleaned(const sptr &token) { GTEST_LOG_(INFO) << "MockAppMgrService::AbilityCleaned called"; Cleaned_ = true; EXPECT_TRUE(Cleaned_); } MOCK_METHOD1(KillApplication, int(const std::string &appName)); virtual sptr GetAmsMgr() override { return nullptr; }; virtual int32_t ClearUpApplicationData(const std::string &appName) override { return 0; } virtual int IsBackgroundRunningRestricted(const std::string &appName) override { return 0; }; virtual int GetAllRunningProcesses(std::vector &info) override { return 0; }; virtual void RegisterAppStateCallback(const sptr &callback) { callback_ = callback; } int32_t CheckPermissionImpl([[maybe_unused]] const int32_t recordId, const std::string &data) { data_ = data; return 0; } void KillApplicationImpl(const std::string &data) { data_ = data; } const std::string &GetData() const { return data_; } void Wait() { sem_.Wait(); } void Post() { sem_.Post(); } void UpdateState() const { if (!callback_) { return; } AppProcessData processData; processData.appName = ""; processData.pid = 1; processData.appState = ApplicationState::APP_STATE_BEGIN; callback_->OnAppStateChanged(processData); } void Terminate(const sptr &token) const { if (!callback_) { return; } AbilityState st = AbilityState::ABILITY_STATE_BEGIN; callback_->OnAbilityRequestDone(token, st); } void ScheduleTerminateApplication() { if (Appthread_ != nullptr) { Appthread_->ScheduleTerminateApplication(); } } void ScheduleLaunchApplication(const AppLaunchData &lanchdata) { if (Appthread_ != nullptr) { Appthread_->ScheduleLaunchApplication(lanchdata); } } void ScheduleForegroundApplication() { if (Appthread_ != nullptr) { Appthread_->ScheduleForegroundApplication(); } } void ScheduleBackgroundApplication() { if (Appthread_ != nullptr) { Appthread_->ScheduleBackgroundApplication(); } } void ScheduleShrinkMemory(const int32_t level) { if (Appthread_ != nullptr) { Appthread_->ScheduleShrinkMemory(level); } } void ScheduleLowMemory() { if (Appthread_ != nullptr) { Appthread_->ScheduleLowMemory(); } } void ScheduleLaunchAbility(const AbilityInfo &abilityinf, const sptr &token) { if (Appthread_ != nullptr) { Appthread_->ScheduleLaunchAbility(abilityinf, token); } } void ScheduleCleanAbility(const sptr &token) { if (Appthread_ != nullptr) { Appthread_->ScheduleCleanAbility(token); } } void ScheduleProfileChanged(const Profile &profile) { if (Appthread_ != nullptr) { Appthread_->ScheduleProfileChanged(profile); } } void ScheduleConfigurationUpdated(const Configuration &config) { if (Appthread_ != nullptr) { Appthread_->ScheduleConfigurationUpdated(config); } } sptr GetAppthread() { return Appthread_; } bool IsAttached() { APP_LOGI("MockAppMgrService::IsAttached Attached_ = %{public}d", Attached_); return Attached_; } bool IsForegrounded() { APP_LOGI("MockAppMgrService::IsForegrounded Foregrounded_ = %{public}d", Foregrounded_); return Foregrounded_; } bool IsBackgrounded() { APP_LOGI("MockAppMgrService::IsBackgrounded Backgrounded_ = %{public}d", Backgrounded_); return Backgrounded_; } bool IsTerminated() { APP_LOGI("MockAppMgrService::IsTerminated Terminated_ = %{public}d", Terminated_); return Terminated_; } void init() { APP_LOGI("MockAppMgrService::init called"); Attached_ = false; } bool AddDeathRecipient(const sptr &recipient) { return true; } private: bool Attached_ = false; bool Foregrounded_ = false; bool Backgrounded_ = false; bool Terminated_ = false; bool Cleaned_ = false; sptr Appthread_ = nullptr; Semaphore sem_; std::string data_; sptr callback_; }; } // namespace AppExecFwk } // namespace OHOS #endif // FOUNDATION_APPEXECFWK_KITS_APPKIT_TEST_MOCK_INCLUDE_MOCK_APP_MGR_SERVICE_H