|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | + |
| 14 | +#include "src/lifecycle_client_lib/test/ut/mocks/lifecyclemanagermock.h" |
| 15 | +#include "src/lifecycle_client_lib/include/lifecyclemanager.h" |
| 16 | + |
| 17 | +#include <functional> |
| 18 | + |
| 19 | +namespace |
| 20 | +{ |
| 21 | + |
| 22 | +auto& GetConstructorCallback() noexcept |
| 23 | +{ |
| 24 | + static std::function<void()> constructor_callback{}; |
| 25 | + return constructor_callback; |
| 26 | +} |
| 27 | + |
| 28 | +auto& GetDestructorCallback() noexcept |
| 29 | +{ |
| 30 | + static std::function<void()> destructor_callback{}; |
| 31 | + return destructor_callback; |
| 32 | +} |
| 33 | + |
| 34 | +auto& GetRunCallback() noexcept |
| 35 | +{ |
| 36 | + static std::function<std::int32_t(score::mw::lifecycle::Application & app, |
| 37 | + const score::mw::lifecycle::ApplicationContext& context)> |
| 38 | + run_callback; |
| 39 | + return run_callback; |
| 40 | +} |
| 41 | + |
| 42 | +} // namespace |
| 43 | + |
| 44 | +score::mw::lifecycle::LifeCycleManagerMock::LifeCycleManagerMock() |
| 45 | +{ |
| 46 | + GetConstructorCallback() = [this] { |
| 47 | + ctor(); |
| 48 | + }; |
| 49 | + GetDestructorCallback() = [this] { |
| 50 | + dtor(); |
| 51 | + }; |
| 52 | + ResetCallbackForRunMethod(); |
| 53 | +} |
| 54 | + |
| 55 | +score::mw::lifecycle::LifeCycleManagerMock::~LifeCycleManagerMock() |
| 56 | +{ |
| 57 | + GetConstructorCallback() = nullptr; |
| 58 | + GetDestructorCallback() = nullptr; |
| 59 | + GetRunCallback() = nullptr; |
| 60 | +} |
| 61 | + |
| 62 | +void score::mw::lifecycle::LifeCycleManagerMock::SetCallbackForRunMethod( |
| 63 | + std::function<std::int32_t(score::mw::lifecycle::Application& app, const score::mw::lifecycle::ApplicationContext& context)> callback) |
| 64 | +{ |
| 65 | + GetRunCallback() = std::move(callback); |
| 66 | +} |
| 67 | + |
| 68 | +void score::mw::lifecycle::LifeCycleManagerMock::ResetCallbackForRunMethod() |
| 69 | +{ |
| 70 | + GetRunCallback() = [this](score::mw::lifecycle::Application& app, |
| 71 | + const score::mw::lifecycle::ApplicationContext& context) { |
| 72 | + return run(app, context); |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +score::mw::lifecycle::LifeCycleManager::LifeCycleManager(std::unique_ptr<score::os::Signal>) noexcept |
| 77 | +{ |
| 78 | + auto& constructor_callback = GetConstructorCallback(); |
| 79 | + constructor_callback(); |
| 80 | +} |
| 81 | + |
| 82 | +score::mw::lifecycle::LifeCycleManager::~LifeCycleManager() noexcept |
| 83 | +{ |
| 84 | + auto& destructor_callback = GetDestructorCallback(); |
| 85 | + destructor_callback(); |
| 86 | +} |
| 87 | + |
| 88 | +std::int32_t score::mw::lifecycle::LifeCycleManager::run(score::mw::lifecycle::Application& app, |
| 89 | + const score::mw::lifecycle::ApplicationContext& context) |
| 90 | +{ |
| 91 | + report_running(); |
| 92 | + auto& run_callback = GetRunCallback(); |
| 93 | + const auto result = run_callback(app, context); |
| 94 | + report_shutdown(); |
| 95 | + return result; |
| 96 | +} |
0 commit comments