Skip to content

Commit

Permalink
🔀 Merge branch 'sibylle/feature/GameKit' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Aug 19, 2022
2 parents fe01ffc + b696a2e commit 9ff28ed
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_subdirectory(${LIBS_DIR}/ContainerKit)
add_subdirectory(${LIBS_DIR}/EventLoopKit)
add_subdirectory(${LIBS_DIR}/FileManagerKit)
add_subdirectory(${LIBS_DIR}/FirmwareKit)
add_subdirectory(${LIBS_DIR}/GameKit)
add_subdirectory(${LIBS_DIR}/IOKit)
add_subdirectory(${LIBS_DIR}/LedKit)
add_subdirectory(${LIBS_DIR}/RobotKit)
Expand Down
26 changes: 26 additions & 0 deletions libs/GameKit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Leka - LekaOS
# Copyright 2022 APF France handicap
# SPDX-License-Identifier: Apache-2.0

add_library(GameKit STATIC)

target_include_directories(GameKit
PUBLIC
include
include/games
)

target_sources(GameKit
PRIVATE
source/GameKit.cpp
)

target_link_libraries(GameKit
EventLoopKit
)

if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
leka_unit_tests_sources(
tests/GameKit_test.cpp
)
endif()
21 changes: 21 additions & 0 deletions libs/GameKit/include/Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

namespace leka::interface {

class Game
{
public:
virtual ~Game() = default;

virtual void start() = 0;
virtual void run() = 0;
virtual void stop() = 0;

[[nodiscard]] virtual auto isRunning() const -> bool = 0;
};

} // namespace leka::interface
29 changes: 29 additions & 0 deletions libs/GameKit/include/GameKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <interface/libs/EventLoop.h>

#include "Game.h"

namespace leka {

class GameKit
{
public:
explicit GameKit(interface::EventLoop &event_loop) : _event_loop(event_loop) {}

void init();
void start(interface::Game *game);
void stop();

private:
void run();

interface::EventLoop &_event_loop;
interface::Game *_game = nullptr;
};

} // namespace leka
45 changes: 45 additions & 0 deletions libs/GameKit/source/GameKit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "GameKit.h"

#include "rtos/ThisThread.h"

using namespace leka;
using namespace std::chrono;

void GameKit::init()
{
_event_loop.registerCallback([this] { run(); });
}

void GameKit::start(interface::Game *game)
{
stop();

_game = game;

if (_game == nullptr) {
return;
}
_game->start();
_event_loop.start();
}

void GameKit::run()
{
while (_game->isRunning()) {
_game->run();
rtos::ThisThread::sleep_for(50ms);
}
}

void GameKit::stop()
{
_event_loop.stop();

if (_game != nullptr) {
_game->stop();
}
}
108 changes: 108 additions & 0 deletions libs/GameKit/tests/GameKit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "GameKit.h"

#include "gtest/gtest.h"
#include "mocks/Game.h"
#include "stubs/leka/EventLoopKit.h"

using namespace leka;
using namespace std::chrono;

using ::testing::InSequence;

class GameKitTest : public ::testing::Test
{
protected:
GameKitTest() = default;

// void SetUp() override {}
// void TearDown() override {}

stub::EventLoopKit stub_event_loop {};

GameKit gamekit {stub_event_loop};

mock::Game mock_game {};
};

TEST_F(GameKitTest, initialization)
{
EXPECT_NE(&mock_game, nullptr);
}

TEST_F(GameKitTest, startGame)
{
EXPECT_CALL(mock_game, startCalled).Times(1);
EXPECT_FALSE(mock_game.isRunning());

gamekit.start(&mock_game);
EXPECT_TRUE(mock_game.isRunning());
}

TEST_F(GameKitTest, startNullPtr)
{
EXPECT_CALL(mock_game, startCalled).Times(0);
EXPECT_FALSE(mock_game.isRunning());

gamekit.start(nullptr);
EXPECT_FALSE(mock_game.isRunning());
}

TEST_F(GameKitTest, runGame)
{
auto kMaxStageNumber = 10;
EXPECT_CALL(mock_game, startCalled).Times(1);
EXPECT_CALL(mock_game, stageCalled).Times(kMaxStageNumber);

gamekit.init();
gamekit.start(&mock_game);
}

TEST_F(GameKitTest, stopWithoutGame)
{
EXPECT_CALL(mock_game, stopCalled).Times(0);

gamekit.stop();
}

TEST_F(GameKitTest, stopStartedGame)
{
EXPECT_CALL(mock_game, startCalled).Times(1);
EXPECT_CALL(mock_game, stopCalled).Times(1);
EXPECT_FALSE(mock_game.isRunning());

gamekit.start(&mock_game);
EXPECT_TRUE(mock_game.isRunning());

gamekit.stop();
EXPECT_FALSE(mock_game.isRunning());
}

TEST_F(GameKitTest, startNewGameSequence)
{
mock::Game mock_new_game;

{
InSequence seq;

EXPECT_CALL(mock_game, startCalled).Times(1);
EXPECT_CALL(mock_game, stopCalled).Times(1);
EXPECT_CALL(mock_new_game, startCalled).Times(1);
}

EXPECT_FALSE(mock_game.isRunning());
EXPECT_FALSE(mock_new_game.isRunning());

gamekit.start(&mock_game);

EXPECT_TRUE(mock_game.isRunning());
EXPECT_FALSE(mock_new_game.isRunning());

gamekit.start(&mock_new_game);

EXPECT_FALSE(mock_game.isRunning());
EXPECT_TRUE(mock_new_game.isRunning());
}
51 changes: 51 additions & 0 deletions libs/GameKit/tests/mocks/Game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "Game.h"
#include "gmock/gmock.h"

namespace leka::mock {

class Game : public interface::Game
{
public:
explicit Game() = default;

void start() final
{
startCalled();
_running = true;
_stage = 0;
}
void run() final
{
auto kMaxStageNumber = 10;
if (_stage < kMaxStageNumber) {
++_stage;
stageCalled();
} else {
_running = false;
}
}
void stop() final
{
stopCalled();
_running = false;
}
auto isRunning() const -> bool final { return _running; }

MOCK_METHOD(void, startCalled, (), ());
MOCK_METHOD(void, stopCalled, (), ());
MOCK_METHOD(void, stageCalled, (), ());

private:
bool _running = false;
uint8_t _stage = 0;
};

} // namespace leka::mock
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ leka_register_unit_tests_for_library(ContainerKit)
leka_register_unit_tests_for_library(EventLoopKit)
leka_register_unit_tests_for_library(FileManagerKit)
leka_register_unit_tests_for_library(FirmwareKit)
leka_register_unit_tests_for_library(GameKit)
leka_register_unit_tests_for_library(WebKit)
leka_register_unit_tests_for_library(IOKit)
leka_register_unit_tests_for_library(LedKit)
Expand Down

0 comments on commit 9ff28ed

Please sign in to comment.