Commit 8d717bad authored by Julian Helfferich's avatar Julian Helfferich 💬
Browse files

Set mMessageBox pointer to nullptr

This is done to prevent double delete. Now, the deleteGameItemGroups()
can safely be called multiple times.

I have added a simple test to verify that there is no crash.
parent 546a8ca3
Pipeline #51382 passed with stage
in 4 minutes
......@@ -23,6 +23,13 @@ ecm_add_test(
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_KBlocksScene.cpp
TEST_NAME KBlocksScene
LINK_LIBRARIES Qt5::Test KBlocksCore
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_KBlocksWin.cpp
TEST_NAME KBlocksWin
......
/******************************************************************************
* 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 "KBlocksScene.h"
#include "Testing/MockGameLogic.h"
#include "Testing/MockGraphics.h"
#include "Testing/MockSound.h"
class testKBlocksScene : public QObject
{
Q_OBJECT
private slots:
void redundantCallsToDeleteGameItemGroupsShouldBeAllowed();
};
void testKBlocksScene::redundantCallsToDeleteGameItemGroupsShouldBeAllowed()
{
/**
* The method deleteGameItemGroups() should remove all existing
* KBlocksItemGroup objects as well as the message box. It should
* be valid to call deleteGameItemGroups() multiple times under all
* circumstances.
*
* Currently, this test is only a should-not-crash test. It does not
* verify that the game item groups are indeed deleted.
*/
MockGameLogic gameLogic;
MockGraphics graphics;
MockSound sound;
KBlocksScene scene(&gameLogic, &graphics, &sound);
scene.deleteGameItemGroups();
scene.deleteGameItemGroups();
scene.createGameItemGroups(1);
scene.deleteGameItemGroups();
scene.deleteGameItemGroups();
}
QTEST_MAIN(testKBlocksScene)
#include "test_KBlocksScene.moc"
......@@ -114,6 +114,7 @@ void KBlocksScene::deleteGameItemGroups()
if (mMessageBox) {
removeItem(mMessageBox);
delete mMessageBox;
mMessageBox = nullptr;
}
for (int i = 0; i < mGroupCount; i++) {
......
......@@ -11,18 +11,20 @@
#define MOCKGAMELOGIC_H
#include "GameLogicInterface.h"
#include "Testing/MockSingleGame.h"
class MockGameLogic : public GameLogicInterface
{
public:
MockGameLogic()
: singleGamesDeleted(false)
: singleGamesDeleted(false),
singleGame{}
{
}
virtual ~MockGameLogic() = default;
public:
SingleGameInterface *getSingleGame(int) override { return nullptr; }
SingleGameInterface *getSingleGame(int) override { return &singleGame; }
int levelUpGame(int) override { return 0; }
int updateGame(int *) override { return 0; }
......@@ -42,6 +44,9 @@ public:
public:
bool singleGamesDeleted;
private:
MockSingleGame singleGame;
};
#endif //MOCKGAMELOGIC_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