[Ktutorial-commits] SF.net SVN: ktutorial:[151] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-13 18:54:41
|
Revision: 151 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=151&view=rev Author: danxuliu Date: 2010-03-13 18:54:34 +0000 (Sat, 13 Mar 2010) Log Message: ----------- Add Reaction class to store the data of reactions (changing to a step or executing some code when an option is selected or a condition is met). Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/Reaction.cpp trunk/ktutorial/ktutorial-editor/src/Reaction.h trunk/ktutorial/ktutorial-editor/tests/unit/ReactionTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-13 18:38:49 UTC (rev 150) +++ trunk/ktutorial/ktutorial-editor/src/CMakeLists.txt 2010-03-13 18:54:34 UTC (rev 151) @@ -8,6 +8,7 @@ set(ktutorial_editor_SRCS KTutorialEditor.cpp + Reaction.cpp Step.cpp Tutorial.cpp WaitFor.cpp Added: trunk/ktutorial/ktutorial-editor/src/Reaction.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Reaction.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Reaction.cpp 2010-03-13 18:54:34 UTC (rev 151) @@ -0,0 +1,93 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include "Reaction.h" +#include "WaitFor.h" + +//public: + +Reaction::Reaction(QObject* parent): + QObject(parent), + mWaitFor(0), + mTriggerType(OptionSelected), + mResponseType(NextStep) { +} + +Reaction::~Reaction() { + delete mWaitFor; +} + +Reaction::TriggerType Reaction::triggerType() const { + return mTriggerType; +} + +void Reaction::setTriggerType(TriggerType triggerType) { + mTriggerType = triggerType; + + emit dataChanged(this); +} + +QString Reaction::optionName() const { + return mOptionName; +} + +void Reaction::setOptionName(const QString& optionName) { + mOptionName = optionName; + + emit dataChanged(this); +} + +WaitFor* Reaction::waitFor() const { + return mWaitFor; +} + +void Reaction::setWaitFor(WaitFor* waitFor) { + mWaitFor = waitFor; + + emit dataChanged(this); +} + +Reaction::ResponseType Reaction::responseType() const { + return mResponseType; +} + +void Reaction::setResponseType(ResponseType responseType) { + mResponseType = responseType; + + emit dataChanged(this); +} + +QString Reaction::nextStepId() const { + return mNextStepId; +} + +void Reaction::setNextStepId(const QString& nextStepId) { + mNextStepId = nextStepId; + + emit dataChanged(this); +} + +QString Reaction::customCode() const { + return mCustomCode; +} + +void Reaction::setCustomCode(const QString& customCode) { + mCustomCode = customCode; + + emit dataChanged(this); +} Property changes on: trunk/ktutorial/ktutorial-editor/src/Reaction.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/Reaction.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/Reaction.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/Reaction.h 2010-03-13 18:54:34 UTC (rev 151) @@ -0,0 +1,102 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#ifndef REACTION_H +#define REACTION_H + +#include <QObject> + +class WaitFor; + +/** + * Container for reaction data. + * It stores the data necessary to call addWaitFor and addOption methods in a + * KTutorial::Step. That is, it stores the data that defines how a step reacts. + * + * A reaction is composed by a trigger and a response. The trigger can be the + * user selecting an option of the step, or a condition to wait for that is met. + * The response can be changing to another step, or custom code for complexer + * behavior. + * + * When any attribute is modified, dataChanged(Step*) signal is emitted. + */ +class Reaction: public QObject { +Q_OBJECT +public: + + enum TriggerType { + OptionSelected, + ConditionMet + }; + + enum ResponseType { + NextStep, + CustomCode + }; + + Reaction(QObject* parent = 0); + virtual ~Reaction(); + + TriggerType triggerType() const; + void setTriggerType(TriggerType triggerType); + + QString optionName() const; + void setOptionName(const QString& optionName); + + WaitFor* waitFor() const; + + /** + * Sets the condition to wait for. + * The WaitFor will be destroyed when this Reaction is destroyed. However, + * if a WaitFor is set over a previous one, the previous one must be deleted + * explicitly. + * + * @param waitFor The condition to wait for. + */ + void setWaitFor(WaitFor* waitFor); + + ResponseType responseType() const; + void setResponseType(ResponseType responseType); + + QString nextStepId() const; + void setNextStepId(const QString& nextStepId); + + QString customCode() const; + void setCustomCode(const QString& customCode); + +Q_SIGNALS: + + /** + * Emitted when any data in the reaction changed. + * + * @param reaction This reaction. + */ + void dataChanged(Reaction* reaction); + +private: + + TriggerType mTriggerType; + QString mOptionName; + WaitFor* mWaitFor; + ResponseType mResponseType; + QString mNextStepId; + QString mCustomCode; + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/Reaction.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-13 18:38:49 UTC (rev 150) +++ trunk/ktutorial/ktutorial-editor/tests/unit/CMakeLists.txt 2010-03-13 18:54:34 UTC (rev 151) @@ -15,6 +15,7 @@ ENDMACRO(UNIT_TESTS) unit_tests( + Reaction Step Tutorial WaitForComposed @@ -29,6 +30,7 @@ ENDMACRO(MEM_TESTS) mem_tests( + Reaction Step Tutorial WaitForComposed Added: trunk/ktutorial/ktutorial-editor/tests/unit/ReactionTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/ReactionTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/ReactionTest.cpp 2010-03-13 18:54:34 UTC (rev 151) @@ -0,0 +1,168 @@ +/*************************************************************************** + * Copyright (C) 2010 by Daniel Calviño Sánchez * + * dan...@gm... * + * * + * 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 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; If not, see <http://www.gnu.org/licenses/>. * + ***************************************************************************/ + +#include <QtTest> + +#include "Reaction.h" +#include "WaitFor.h" + +class ReactionTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + + void testConstructor(); + + void testSetTriggerType(); + + void testSetOptionName(); + + void testSetWaitFor(); + + void testSetResponseType(); + + void testSetNextStepId(); + + void testSetCustomCode(); + +private: + + int mReactionStarType; + + void assertDataChanged(const QSignalSpy& spy, int index, + Reaction* reaction) const; + +}; + +class MockWaitFor: public WaitFor { +Q_OBJECT +public: + + void emitDataChanged() { + emit dataChanged(this); + } + +}; + +void ReactionTest::initTestCase() { + //Reaction* must be registered in order to be used with QSignalSpy + mReactionStarType = qRegisterMetaType<Reaction*>("Reaction*"); +} + +void ReactionTest::testConstructor() { + QObject parent; + Reaction* reaction = new Reaction(&parent); + + QCOMPARE(reaction->parent(), &parent); + QCOMPARE(reaction->triggerType(), Reaction::OptionSelected); + QCOMPARE(reaction->responseType(), Reaction::NextStep); + QCOMPARE(reaction->waitFor(), (WaitFor*)0); +} + +void ReactionTest::testSetTriggerType() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + reaction.setTriggerType(Reaction::ConditionMet); + + QCOMPARE(reaction.triggerType(), Reaction::ConditionMet); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +void ReactionTest::testSetOptionName() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + reaction.setOptionName("The option name"); + + QCOMPARE(reaction.optionName(), QString("The option name")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +void ReactionTest::testSetWaitFor() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + MockWaitFor* waitFor = new MockWaitFor(); + reaction.setWaitFor(waitFor); + + QCOMPARE(reaction.waitFor(), waitFor); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +void ReactionTest::testSetResponseType() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + reaction.setResponseType(Reaction::CustomCode); + + QCOMPARE(reaction.responseType(), Reaction::CustomCode); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +void ReactionTest::testSetNextStepId() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + reaction.setNextStepId("The id"); + + QCOMPARE(reaction.nextStepId(), QString("The id")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +void ReactionTest::testSetCustomCode() { + Reaction reaction; + + QSignalSpy dataChangedSpy(&reaction, SIGNAL(dataChanged(Reaction*))); + + reaction.setCustomCode("The custom code"); + + QCOMPARE(reaction.customCode(), QString("The custom code")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &reaction); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +//Reaction* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(Reaction*); + +void ReactionTest::assertDataChanged(const QSignalSpy& spy, int index, + Reaction* reaction) const { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mReactionStarType); + QCOMPARE(qvariant_cast<Reaction*>(argument), reaction); +} + +QTEST_MAIN(ReactionTest) + +#include "ReactionTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/ReactionTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |