Commit a3e9b5fe authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Add test that stopGame is not called after single games are deleted

To fix the bug I am targeting, stopGame() triggers an update of the
layout, which includes processing the remaining game actions. To do
this, the corresponding single game objects need to still exist.

In this commit, I have just created the test. It is currently failing.
parent e426bdca
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2009-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#include <QTest>
#include "KBlocksGameLogic.h"
#include "KBlocksWin.h"
#include "Testing/MockGameLogic.h"
#include "Testing/MockScene.h"
#include "Testing/TestingKBlocksWin.h"
class testKBlocksWin : public QObject
{
Q_OBJECT
private slots:
void callStopGameBeforeSingleGamesAreDeleted();
};
void testKBlocksWin::callStopGameBeforeSingleGamesAreDeleted()
{
/**
* The KBlocksItemGroups which are part of KBlocksScene contain a
* pointer to the single game(s). Thus, KBlocksScene should not
* be used after the single games have been deleted. This is in
* particular true for the stopGame() method which processes the
* final game action items.
*/
MockGameLogic logic;
TestingKBlocksWin win(&logic, nullptr, 0, 0);
MockScene *scene = new MockScene(&logic);
win.replaceScene(scene);
win.callStopGame();
QVERIFY(!scene->singleGameDeletedWhenStopGameCalled);
}
QTEST_MAIN(testKBlocksWin)
#include "test_KBlocksWin.moc"
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#ifndef MOCKGAMELOGIC_H
#define MOCKGAMELOGIC_H
#include "GameLogicInterface.h"
class MockGameLogic : public GameLogicInterface
{
public:
MockGameLogic()
: singleGamesDeleted(false)
{
}
virtual ~MockGameLogic() = default;
public:
SingleGameInterface *getSingleGame(int) override { return nullptr; }
int levelUpGame(int) override { return 0; }
int updateGame(int *) override { return 0; }
void setGameSeed(int) override {}
void setGameStandbyMode(bool) override {}
bool startGame(int) override { return true; }
bool stopGame() override { return true; }
void pauseGame(bool) override {}
void continueGame() override {}
bool deleteSingleGames() override { singleGamesDeleted = true; return true; }
public:
bool singleGamesDeleted;
};
#endif //MOCKGAMELOGIC_H
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#ifndef MOCKSCENE_H
#define MOCKSCENE_H
#include "SceneInterface.h"
#include "Testing/MockGameLogic.h"
class MockScene : public SceneInterface
{
public:
MockScene(MockGameLogic *gameLogic)
: singleGameDeletedWhenStopGameCalled(false),
mpGameLogic(gameLogic)
{
}
virtual ~MockScene() = default;
public:
void createGameItemGroups(int, bool) override {}
void deleteGameItemGroups() override {}
void setGamesPerLine(int) override {}
void setGameAnimEnabled(bool) override {}
void setWaitForAllUpdate(bool) override {}
void setUpdateInterval(int) override {}
void setSoundsEnabled(bool) override {}
void readSettings(const QSize&) override {}
void startGame() override {}
void stopGame() override {
if (mpGameLogic->singleGamesDeleted) {
singleGameDeletedWhenStopGameCalled = true;
}
}
void pauseGame(bool, bool) override {}
void addScore(int, int) override {}
public slots:
void playMoveSound() override {}
void playDropSound() override {}
public:
bool singleGameDeletedWhenStopGameCalled;
private:
MockGameLogic *mpGameLogic;
};
#endif // MOCKSCENE_H
/******************************************************************************
* KBlocks, a falling blocks game by KDE *
* Copyright (C) 2010-2021 Julian Helfferich <julian.helfferich@mailbox.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
******************************************************************************/
#ifndef TESTINGKBLOCKSWIN_H
#define TESTINGKBLOCKSWIN_H
#include "KBlocksWin.h"
#include "SceneInterface.h"
class GameLogicInterface;
class KBlocksPlayManager;
class TestingKBlocksWin : private KBlocksWin
{
public:
TestingKBlocksWin(GameLogicInterface *p, KBlocksPlayManager *pM, int capacity, int gamecount)
: KBlocksWin(p, pM, capacity, gamecount)
{
}
virtual ~TestingKBlocksWin() = default;
void callStopGame() { KBlocksWin::stopGame(); }
void replaceScene(SceneInterface *newScene) {
disconnect(
mpKBPlayer, &KBlocksKeyboardPlayer::blockMoved,
mpGameScene, &SceneInterface::playMoveSound
);
disconnect(
mpKBPlayer, &KBlocksKeyboardPlayer::blockDropped,
mpGameScene, &SceneInterface::playDropSound
);
delete mpGameScene;
mpGameScene = newScene;
connect(
mpKBPlayer, &KBlocksKeyboardPlayer::blockMoved,
mpGameScene, &SceneInterface::playMoveSound
);
connect(
mpKBPlayer, &KBlocksKeyboardPlayer::blockDropped,
mpGameScene, &SceneInterface::playDropSound
);
mpGameView->setScene(mpGameScene);
}
};
#endif // TESTINGKBLOCKSWIN_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment