-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge branch 'sibylle/feature/GameKit' into develop
- Loading branch information
Showing
8 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters