/*
 * Copyright (c) 2021-2022 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.
 */

#include "life_cycle_observer_ability.h"
#include "hilog_wrapper.h"
#include "base_ability.h"
#include "test_utils.h"

namespace OHOS {
namespace AppExecFwk {
using namespace OHOS::EventFwk;
namespace {
constexpr int loopCnt = 100;
}
void LifecycleObserverLifecycleObserver::DoTask()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::TWO:
            PostTask<LifecycleObserverLifecycleCallbacks>();
            break;
        case CaseIndex::THREE:
            DoWhile<LifecycleObserverLifecycleCallbacks>();
            break;
        case CaseIndex::SIX:
            break;
        default:
            break;
    }
}

void LifecycleObserverLifecycleObserver::OnActive()
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnActive");
}

void LifecycleObserverLifecycleObserver::OnBackground()
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnBackground");
}

void LifecycleObserverLifecycleObserver::OnForeground(const Want &want)
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnForeground");
}

void LifecycleObserverLifecycleObserver::OnInactive()
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnInactive");
}

void LifecycleObserverLifecycleObserver::OnStart(const Want &want)
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnStart");
}

void LifecycleObserverLifecycleObserver::OnStop()
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnStop");
}

void LifecycleObserverLifecycleObserver::OnStateChanged(LifeCycle::Event event, const Want &want)
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnStateChanged");
}

void LifecycleObserverLifecycleObserver::OnStateChanged(LifeCycle::Event event)
{
    DoTask();
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnStateChanged");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityStart(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityStart");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityInactive(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityInactive");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityBackground(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityBackground");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityForeground(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityForeground");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityActive(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityActive");
}

void LifecycleObserverLifecycleCallbacks::OnAbilityStop(const std::shared_ptr<Ability> &ability)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilityStop");
}

void LifecycleObserverLifecycleCallbacks::OnAbilitySaveState(const PacMap &outState)
{
    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 0, "OnAbilitySaveState");
}

LifeCycleObserverAbility::~LifeCycleObserverAbility()
{
    CommonEventManager::UnSubscribeCommonEvent(subscriber_);
}

void LifeCycleObserverAbility::Init(const std::shared_ptr<AbilityInfo> &abilityInfo,
    const std::shared_ptr<OHOSApplication> &application, std::shared_ptr<AbilityHandler> &handler,
    const sptr<IRemoteObject> &token)
{
    HILOG_INFO("LifeCycleObserverAbility::Init called.");
    BaseAbility::Init(abilityInfo, application, handler, token);
    SubscribeEvent();
    lifecycleCallbacks_ = std::make_shared<LifecycleObserverLifecycleCallbacks>();
    GetApplication()->RegisterAbilityLifecycleCallbacks(lifecycleCallbacks_);
    lifecycleObserver_ = std::make_shared<LifecycleObserverLifecycleObserver>();
    GetLifecycle()->AddObserver(lifecycleObserver_);

    TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME,
        BaseAbility::GetLifecycle()->GetLifecycleState(),
        "TestLifeCycleGetLifecycleState");
}

bool LifeCycleObserverAbility::SubscribeEvent()
{
    MatchingSkills matchingSkills;
    matchingSkills.AddEvent(APP_LIFE_CYCLE_OBSERVER_REQ_EVENT_NAME);
    CommonEventSubscribeInfo subscribeInfo(matchingSkills);
    subscribeInfo.SetPriority(1);
    subscriber_ = std::make_shared<LifeCycleObserverAbilityEventSubscriber>(subscribeInfo);
    subscriber_->mainAbility = *this;
    return CommonEventManager::SubscribeCommonEvent(subscriber_);
}

void LifeCycleObserverAbility::OnStart(const Want &want)
{
    sequenceNumber_ = std::stoi(GetNoFromWantInfo(want));
    want_ = want;

    BaseAbility::OnStart(want);
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::INACTIVE, "OnStart");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnStop()
{
    HILOG_INFO("LifeCycleObserverAbility::OnStop");
    BaseAbility::OnStop();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::INITIAL, "OnStop");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnActive()
{
    HILOG_INFO("LifeCycleObserverAbility::OnActive");
    BaseAbility::OnActive();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::ACTIVE, "OnActive");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnInactive()
{
    HILOG_INFO("LifeCycleObserverAbility::OnInactive");
    BaseAbility::OnInactive();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::INACTIVE, "OnInactive");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnBackground()
{
    HILOG_INFO("LifeCycleObserverAbility::OnBackground");
    BaseAbility::OnBackground();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::BACKGROUND, "OnBackground");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnForeground(const Want &want)
{
    HILOG_INFO("LifeCycleObserverAbility::OnForeground");
    BaseAbility::OnBackground();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::INACTIVE, "OnForeground");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::OnCommand(const Want &want, bool restart, int startId)
{
    HILOG_INFO("LifeCycleObserverAbility::OnCommand");

    BaseAbility::OnCommand(want, restart, startId);
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::ACTIVE, "OnCommand");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

sptr<IRemoteObject> LifeCycleObserverAbility::OnConnect(const Want &want)
{
    HILOG_INFO("LifeCycleObserverAbility::OnConnect");

    sptr<IRemoteObject> ret = BaseAbility::OnConnect(want);
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::ACTIVE, "OnConnect");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
    return ret;
}

void LifeCycleObserverAbility::OnDisconnect(const Want &want)
{
    HILOG_INFO("LifeCycleObserverAbility::OnDisconnect");

    BaseAbility::OnDisconnect(want);
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, AbilityLifecycleExecutor::LifecycleState::BACKGROUND, "OnDisconnect");

    auto lifecycle = GetLifecycle();
    TestUtils::PublishEvent(
        APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycleState");
}

void LifeCycleObserverAbility::TestConnectAbility()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want_, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestConnectAbility");
        } break;
        case CaseIndex::TWO: {
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKit", params);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestConnectAbility");
        } break;
        case CaseIndex::THREE: {
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            MAP_STR_STR params;
            Want want = TestUtils::MakeWant("", "AmsStServiceAbilityA1", "com.ohos.amsst.service.appA", params);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestConnectAbility");
        } break;
        case CaseIndex::FOUR: {
            for (int i = 0; i < (int)CaseIndex::THREE; i++) {
                stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
                connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
                bool ret = BaseAbility::GetContext()->ConnectAbility(want_, connCallback_);
                TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestConnectAbility");
            }
        } break;
        case CaseIndex::FIVE: {
            sptr<LifeCycleObserverConnectCallback> stub(new (std::nothrow) LifeCycleObserverConnectCallback());
            sptr<AbilityConnectionProxy> connCallback(new (std::nothrow) AbilityConnectionProxy(stub));
            bool ret = BaseAbility::GetContext()->ConnectAbility(want_, connCallback);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestConnectAbility");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestStopAbility()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            bool ret = BaseAbility::GetContext()->StopAbility(want_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestStopAbility");
        } break;
        case CaseIndex::TWO: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKit", params);
            bool ret = BaseAbility::GetContext()->StopAbility(want);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestStopAbility");
        } break;
        case CaseIndex::THREE: {
            MAP_STR_STR params;
            Want want = TestUtils::MakeWant("", "AmsStServiceAbilityA1", "com.ohos.amsst.service.appA", params);
            bool ret = BaseAbility::GetContext()->StopAbility(want);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestStopAbility");
        } break;
        case CaseIndex::FOUR: {
            for (int i = 0; i < (int)CaseIndex::THREE; i++) {
                bool ret = BaseAbility::GetContext()->StopAbility(want_);
                TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestStopAbility");
            }
        } break;
        case CaseIndex::FIVE: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKitx", params);
            bool ret = BaseAbility::GetContext()->StopAbility(want);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestStopAbility");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestDisconnectAbility()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want_, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestDisconnectAbility");
            BaseAbility::GetContext()->DisconnectAbility(connCallback_);
        } break;
        case CaseIndex::TWO: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKit", params);
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestDisconnectAbility");
            BaseAbility::GetContext()->DisconnectAbility(connCallback_);
        } break;
        case CaseIndex::THREE: {
            MAP_STR_STR params;
            Want want = TestUtils::MakeWant("", "AmsStServiceAbilityA1", "com.ohos.amsst.service.appA", params);
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestDisconnectAbility");
            BaseAbility::GetContext()->DisconnectAbility(connCallback_);
        } break;
        case CaseIndex::FOUR: {
            for (int i = 0; i < (int)CaseIndex::THREE; i++) {
                stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
                connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
                bool ret = BaseAbility::GetContext()->ConnectAbility(want_, connCallback_);
                TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestDisconnectAbility");
                BaseAbility::GetContext()->DisconnectAbility(connCallback_);
            }
        } break;
        case CaseIndex::FIVE: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKitx", params);
            stub_ = new (std::nothrow) LifeCycleObserverConnectCallback();
            connCallback_ = new (std::nothrow) AbilityConnectionProxy(stub_);
            bool ret = BaseAbility::GetContext()->ConnectAbility(want, connCallback_);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, ret, "TestDisconnectAbility");
            BaseAbility::GetContext()->DisconnectAbility(connCallback_);
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestStartAbility()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            BaseAbility::GetContext()->StartAbility(want_, 0);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestStartAbility");
        } break;
        case CaseIndex::TWO: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKit", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestStartAbility");
        } break;
        case CaseIndex::THREE: {
            MAP_STR_STR params;
            Want want = TestUtils::MakeWant("", "AmsStServiceAbilityA1", "com.ohos.amsst.service.appA", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestStartAbility");
        } break;
        case CaseIndex::FOUR: {
            for (int i = 0; i < (int)CaseIndex::THREE; i++) {
                BaseAbility::GetContext()->StartAbility(want_, 0);
                TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestStartAbility");
            }
        } break;
        case CaseIndex::FIVE: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKitx", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestStartAbility");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestTerminateAbility()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            BaseAbility::GetContext()->TerminateAbility();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestTerminateAbility");
        } break;
        case CaseIndex::TWO: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKit", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            BaseAbility::GetContext()->TerminateAbility();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestTerminateAbility");
        } break;
        case CaseIndex::THREE: {
            MAP_STR_STR params;
            Want want = TestUtils::MakeWant("", "AmsStServiceAbilityA1", "com.ohos.amsst.service.appA", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            BaseAbility::GetContext()->TerminateAbility();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestTerminateAbility");
        } break;
        case CaseIndex::FOUR: {
            for (int i = 0; i < (int)CaseIndex::THREE; i++) {
                BaseAbility::GetContext()->TerminateAbility();
                TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestTerminateAbility");
            }
        } break;
        case CaseIndex::FIVE: {
            MAP_STR_STR params;
            Want want =
                TestUtils::MakeWant("", "LifecycleCallbacksAbility", "com.ohos.amsst.service.AppKitx", params);
            BaseAbility::GetContext()->StartAbility(want, 0);
            BaseAbility::GetContext()->TerminateAbility();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestTerminateAbility");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleGetLifecycle()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            auto lifecycle = GetLifecycle();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycle");
        } break;
        case CaseIndex::TWO: {
            for (int i = 0; i < loopCnt; i++) {
                auto lifecycle = GetLifecycle();
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycle");
        } break;
        case CaseIndex::THREE: {
            auto lifecycle = GetLifecycle();
            TestUtils::PublishEvent(
                APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, lifecycle->GetLifecycleState(), "TestLifeCycleGetLifecycle");
        } break;
        case CaseIndex::FOUR: {
            auto lifecycle = GetLifecycle();
            lifecycle->AddObserver(nullptr);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycle");
        } break;
        case CaseIndex::FIVE: {
            auto lifecycle = GetLifecycle();
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycle");
        } break;
        case CaseIndex::SIX: {
            auto lifecycle = GetLifecycle();
            lifecycle->RemoveObserver(nullptr);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycle");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleGetLifecycleState()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            auto lifecycle = GetLifecycle();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME,
                lifecycle->GetLifecycleState(),
                "TestLifeCycleGetLifecycleState");
        } break;
        case CaseIndex::TWO: {
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                auto lifecycle = GetLifecycle();
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycleState");
        } break;
        case CaseIndex::THREE: {
            auto lifecycle = GetLifecycle();
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME,
                lifecycle->GetLifecycleState(),
                "TestLifeCycleGetLifecycleState");
        } break;
        case CaseIndex::FOUR: {
            auto lifecycle = GetLifecycle();
            lifecycle->AddObserver(nullptr);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycleState");
        } break;
        case CaseIndex::FIVE: {
            auto lifecycle = GetLifecycle();
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycleState");
        } break;
        case CaseIndex::SIX: {
            auto lifecycle = GetLifecycle();
            lifecycle->RemoveObserver(nullptr);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleGetLifecycleState");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleAddObserver()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleAddObserver");
        } break;
        case CaseIndex::TWO: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                lifecycle->AddObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleAddObserver");
        } break;
        case CaseIndex::THREE: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                    std::make_shared<LifecycleObserverLifecycleObserver>();
                lifecycle->AddObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleAddObserver");
        } break;
        case CaseIndex::FOUR: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                    std::make_shared<LifecycleObserverLifecycleObserver>();
                lifecycle->AddObserver(lifecycleObserver);
                lifecycle->RemoveObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleAddObserver");
        } break;
        case CaseIndex::FIVE: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                lifecycle->AddObserver(nullptr);
                lifecycle->RemoveObserver(nullptr);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleAddObserver");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleDispatchLifecycleOne()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::TWO: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_BACKGROUND);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::THREE: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_FOREGROUND);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::FOUR: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_INACTIVE);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleDispatchLifecycleTwo()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::FIVE: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_START);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::SIX: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_STOP);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::SEVEN: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->AddObserver(lifecycleObserver);
            lifecycle->DispatchLifecycle(LifeCycle::Event::UNDEFINED);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::EIGHT: {
            auto lifecycle = GetLifecycle();
            lifecycle->DispatchLifecycle(LifeCycle::Event::ON_ACTIVE);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        case CaseIndex::NINE: {
            auto lifecycle = GetLifecycle();
            lifecycle->DispatchLifecycle(LifeCycle::Event::UNDEFINED);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleDispatchLifecycle");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbility::TestLifeCycleDispatchLifecycle()
{
    TestLifeCycleDispatchLifecycleOne();
    TestLifeCycleDispatchLifecycleTwo();
}

void LifeCycleObserverAbility::TestLifeCycleRemoveObserver()
{
    switch ((CaseIndex)LifeCycleObserverAbility::sequenceNumber_) {
        case CaseIndex::ONE: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            lifecycle->RemoveObserver(lifecycleObserver);
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleRemoveObserver");
        } break;
        case CaseIndex::TWO: {
            auto lifecycle = GetLifecycle();
            std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                std::make_shared<LifecycleObserverLifecycleObserver>();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                lifecycle->RemoveObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleRemoveObserver");
        } break;
        case CaseIndex::THREE: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                    std::make_shared<LifecycleObserverLifecycleObserver>();
                lifecycle->RemoveObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleRemoveObserver");
        } break;
        case CaseIndex::FOUR: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                std::shared_ptr<LifecycleObserverLifecycleObserver> lifecycleObserver =
                    std::make_shared<LifecycleObserverLifecycleObserver>();
                lifecycle->AddObserver(lifecycleObserver);
                lifecycle->RemoveObserver(lifecycleObserver);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleRemoveObserver");
        } break;
        case CaseIndex::FIVE: {
            auto lifecycle = GetLifecycle();
            for (int i = 0; i < (int)CaseIndex::HANDRED; i++) {
                lifecycle->RemoveObserver(nullptr);
            }
            TestUtils::PublishEvent(APP_LIFE_CYCLE_OBSERVER_RESP_EVENT_NAME, 1, "TestLifeCycleRemoveObserver");
        } break;
        default:
            break;
    }
}

void LifeCycleObserverAbilityEventSubscriber::OnReceiveEvent(const CommonEventData &data)
{
    HILOG_INFO(
        "LifeCycleObserverAbilityEventSubscriber::OnReceiveEvent:event=%{public}s", data.GetWant().GetAction().c_str());
    HILOG_INFO("LifeCycleObserverAbilityEventSubscriber::OnReceiveEvent:data=%{public}s", data.GetData().c_str());
    HILOG_INFO("LifeCycleObserverAbilityEventSubscriber::OnReceiveEvent:code=%{public}d", data.GetCode());

    auto eventName = data.GetWant().GetAction();
    if (std::strcmp(eventName.c_str(), APP_LIFE_CYCLE_OBSERVER_REQ_EVENT_NAME.c_str()) == 0) {
        auto target = data.GetData();
        auto func = mapTestFunc_.find(target);
        if (func != mapTestFunc_.end()) {
            func->second();
        } else {
            HILOG_INFO("LifeCycleObserverAbilityEventSubscriber::OnReceiveEvent: CommonEventData error(%{public}s)",
                target.c_str());
        }
    }
}
REGISTER_AA(LifeCycleObserverAbility)
}  // namespace AppExecFwk
}  // namespace OHOS