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

Create Mock classes to get KBlocksItemGroup under test

To be able to instantiate a KBlocksItemGroup in a test suite, several
mock classes need to be created.
parent 2b17d389
enable_testing()
include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/..")
include_directories(AFTER "${CMAKE_CURRENT_SOURCE_DIR}/../src")
ecm_add_test(
basic.cpp
TEST_NAME basic
LINK_LIBRARIES Qt5::Test
NAME_PREFIX "basic-"
NAME_PREFIX "UnitTest-"
)
ecm_add_test(
test_KBlocksItemGroup.cpp
TEST_NAME KBlocksItemGroup
LINK_LIBRARIES Qt5::Test KBlocksCore
NAME_PREFIX "UnitTest-"
)
/******************************************************************************
* 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 <memory>
#include <QTest>
#include "Testing/MockGraphics.h"
#include "Testing/MockSingleGame.h"
#include "Testing/MockSound.h"
#include "KBlocksItemGroup.h"
class testKBlocksItemGroup : public QObject
{
Q_OBJECT
private slots:
void testStopGameShouldCallUpdateLayout();
};
void testKBlocksItemGroup::testStopGameShouldCallUpdateLayout()
{
/**
* When the game is stopped (on game over), updateLayout needs to be
* called. Otherwise, the play area does not show where the last item
* has moved, causing the game to be over.
*
* See bug 407244
*/
std::unique_ptr<SingleGameInterface> pSingleGame( new MockSingleGame() );
std::unique_ptr<GraphicsInterface> pGraphics( new MockGraphics() );
std::unique_ptr<SoundInterface> pSound( new MockSound() );
KBlocksItemGroup itemGroup(0, pSingleGame.get(), pGraphics.get(), pSound.get() );
QVERIFY( true );
}
QTEST_MAIN(testKBlocksItemGroup)
#include "test_KBlocksItemGroup.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 MOCKFIELD_H
#define MOCKFIELD_H
#include "FieldInterface.h"
class MockField : public FieldInterface
{
public:
MockField() : FieldInterface() {}
virtual ~MockField() = default;
public:
bool getCell(int, int) override { return false; }
int getWidth() override { return 0; }
int getHeight() override { return 0; }
};
#endif //MOCKFIELD_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 MOCKGRAPHICS_H
#define MOCKGRAPHICS_H
#include <memory>
#include <QSvgRenderer>
#include "GraphicsInterface.h"
class QSize;
class QString;
class KGameTheme;
class MockGraphics : public GraphicsInterface
{
public:
MockGraphics() : mRenderer(new QSvgRenderer()) {};
virtual ~MockGraphics() = default;
public:
bool loadTheme(const QString &) override { return false; }
void readThemeValues() override {}
void adjustForSize(const QSize &) override {}
KGameTheme *theme() const override { return nullptr; }
QSvgRenderer *renderer() const override { return mRenderer.get(); }
private:
std::unique_ptr<QSvgRenderer> mRenderer;
};
#endif // MOCKGRAPHICS_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 MOCKPIECE_H
#define MOCKPIECE_H
#include "PieceInterface.h"
class MockPiece : public PieceInterface
{
public:
MockPiece() : PieceInterface() {}
virtual ~MockPiece() = default;
public:
int toValue() override { return 0; }
int getType() override { return 0; }
int getPosX() override { return 0; }
int getPosY() override { return 0; }
int getRotation() override { return 0; }
int getCellCount() override { return 0; }
int getRotationCount() override { return 0; }
int getCellPosX(int) override { return 0; }
int getCellPosY(int) override { return 0; }
};
#endif //MOCKPIECE_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 MOCKSINGLEGAME_H
#define MOCKSINGLEGAME_H
#include <memory>
#include "MockField.h"
#include "MockPiece.h"
#include "SingleGameInterface.h"
class FieldInterface;
class PieceInterface;
class MockSingleGame : public SingleGameInterface
{
public:
MockSingleGame()
: SingleGameInterface(),
mockField(new MockField()),
mockPiece(new MockPiece())
{}
virtual ~MockSingleGame() = default;
public:
FieldInterface *getField() override { return mockField.get(); }
int getPieceCount() override { return 0; }
PieceInterface *getPiece(int) override { return mockPiece.get(); }
bool isActive() override { return false; }
bool isGameRunning() override { return false; }
int forceUpdateGame() override { return 0; }
int updateGame() override { return 0; }
int continueGame() override { return 0; }
bool setCurrentPiece(int, int, int) override { return false; }
bool pickGameResult(int *) override { return false; }
bool pickGameAction(int *, int *) override { return false; }
private:
std::unique_ptr<FieldInterface> mockField;
std::unique_ptr<PieceInterface> mockPiece;
};
#endif //SINGLE_GAME_INTERFACE
/*******************************************************************************
* 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. *
******************************************************************************/
#ifndef MOCKSOUND_H
#define MOCKSOUND_H
#include "SoundInterface.h"
class MockSound : public SoundInterface
{
public:
MockSound() = default;
virtual ~MockSound() = default;
public:
bool loadTheme(const QString &) override { return false; }
void setSoundsEnabled(bool) override {}
void playSound(Sound) override {}
};
#endif // MOCKSOUND_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