[Ktutorial-commits] SF.net SVN: ktutorial:[153] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-14 18:10:30
|
Revision: 153 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=153&view=rev Author: danxuliu Date: 2010-03-14 17:15:38 +0000 (Sun, 14 Mar 2010) Log Message: ----------- Add ReactionTreeItem to represent a Reaction in a TreeModel. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/view/ReactionTreeItemTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-14 16:41:56 UTC (rev 152) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-14 17:15:38 UTC (rev 153) @@ -6,6 +6,7 @@ EditionWidget.cpp LicenseWidget.cpp NewWaitForWidget.cpp + ReactionTreeItem.cpp StepCustomCodeWidget.cpp StepDataWidget.cpp StepTreeItem.cpp Added: trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.cpp 2010-03-14 17:15:38 UTC (rev 153) @@ -0,0 +1,163 @@ +/*************************************************************************** + * 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 "ReactionTreeItem.h" + +#include <KLocalizedString> + +#include "TextTreeItem.h" +#include "WaitForTreeItem.h" +#include "../Reaction.h" + +//public: + +ReactionTreeItem::ReactionTreeItem(Reaction* reaction, TreeItem* parent): + TreeItem(parent), + mReaction(reaction) { + Q_ASSERT(reaction); + + mEmptyTriggerConditionItem = 0; + mTriggerConditionItem = 0; + mTriggerOptionItem = 0; + mResponseCustomCodeItem = 0; + mResponseNextStepItem = 0; + + //Add two dummy childs, as update method expects always to child items + appendChild(new TextTreeItem(this)); + appendChild(new TextTreeItem(this)); + update(reaction); + + connect(reaction, SIGNAL(dataChanged(Reaction*)), + this, SLOT(update(Reaction*))); +} + +QString ReactionTreeItem::text() const { + return i18nc("@item", "Reaction"); +} + +Reaction* ReactionTreeItem::reaction() const { + return mReaction; +} + +//private: + +void ReactionTreeItem::replaceItem(TreeItem* oldItem, TreeItem* newItem) { + Q_ASSERT(oldItem); + Q_ASSERT(newItem); + Q_ASSERT(oldItem->parent()); + Q_ASSERT(oldItem->parent() == newItem->parent()); + + int index = oldItem->childIndex(); + TreeItem* parent = oldItem->parent(); + + parent->removeChild(oldItem); + delete oldItem; + + parent->insertChild(newItem, index); +} + +void ReactionTreeItem::updateConditionItem(Reaction* reaction) { + if (!reaction->waitFor() && !mEmptyTriggerConditionItem) { + mEmptyTriggerConditionItem = new TextTreeItem(this); + mEmptyTriggerConditionItem->setText( + i18nc("@item", "(No condition set)")); + + replaceItem(child(0), mEmptyTriggerConditionItem); + } else if (reaction->waitFor()) { + mTriggerConditionItem = + WaitForTreeItem::treeItemForWaitFor(reaction->waitFor(), this); + + replaceItem(child(0), mTriggerConditionItem); + } +} + +void ReactionTreeItem::updateOptionItem(Reaction* reaction) { + if (!mTriggerOptionItem) { + mTriggerOptionItem = new TextTreeItem(this); + + replaceItem(child(0), mTriggerOptionItem); + } + + QString optionName = reaction->optionName(); + if (optionName.isEmpty()) { + optionName = i18nc("@item", "(option name not set)"); + } else { + optionName = '"' + reaction->optionName() + '"'; + } + + mTriggerOptionItem->setText( + i18nc("@item", "When the option %1 is selected", optionName)); +} + +void ReactionTreeItem::updateCustomCodeItem(Reaction* reaction) { + if (!mResponseCustomCodeItem) { + TextTreeItem* parent = new TextTreeItem(this); + parent->setText(i18nc("@item", "Execute the following code:")); + + mResponseCustomCodeItem = new TextTreeItem(parent); + parent->appendChild(mResponseCustomCodeItem); + + replaceItem(child(1), parent); + } + + if (reaction->customCode().isEmpty()) { + mResponseCustomCodeItem->setText("(No code set)"); + } else { + mResponseCustomCodeItem->setText(reaction->customCode()); + } +} + +void ReactionTreeItem::updateNextStepItem(Reaction* reaction) { + if (!mResponseNextStepItem) { + mResponseNextStepItem = new TextTreeItem(this); + + replaceItem(child(1), mResponseNextStepItem); + } + + QString nextStepId = reaction->nextStepId(); + if (nextStepId.isEmpty()) { + nextStepId = i18nc("@item", "(step id not set)"); + } else { + nextStepId = '"' + reaction->nextStepId() + '"'; + } + + mResponseNextStepItem->setText( + i18nc("@item", "Change to step %1", nextStepId)); +} + +//private slots: + +void ReactionTreeItem::update(Reaction* reaction) { + Q_ASSERT(reaction); + + if (reaction->triggerType() == Reaction::ConditionMet) { + updateConditionItem(reaction); + } + + if (reaction->triggerType() == Reaction::OptionSelected) { + updateOptionItem(reaction); + } + + if (reaction->responseType() == Reaction::CustomCode) { + updateCustomCodeItem(reaction); + } + + if (reaction->responseType() == Reaction::NextStep) { + updateNextStepItem(reaction); + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.h 2010-03-14 17:15:38 UTC (rev 153) @@ -0,0 +1,191 @@ +/*************************************************************************** + * 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 REACTIONTREEITEM_H +#define REACTIONTREEITEM_H + +#include <QPointer> +#include "TreeItem.h" + +class Reaction; +class TextTreeItem; +class WaitForTreeItem; + +/** + * A TreeItem that represents a Reaction. + * The tree representation of a reaction is: + * Reaction + * |-Trigger + * --Response + * + * Trigger can be either: + * -When the option "optionName" is selected + * or the representation of a WaitFor, depending on the type of trigger. If the + * trigger is an option and there is no option name set, or if the trigger is a + * condition but there is no WaitFor, a placeholder is used instead. Option + * placeholder is "(option name not set)", and condition placeholder is "(No + * condition set)" (without quotes, but with parenthesis). + * + * Response can be either: + * -Change to step "stepId" + * or: + * -Execute the following code: + * -Custom code + * depending on the type of response. If there is no stepId or code set, a + * placeholder is used instead. Step id placeholder is "(step id not set)", and + * code placeholder is "(No code set)" (without quotes, but with parenthesis). + * + * Whenever the reaction data changes, the ReactionTreeItem and its child items + * are updated as needed. + */ +class ReactionTreeItem: public TreeItem { +Q_OBJECT +public: + + /** + * Creates a new ReactionTreeItem for the given Reaction and with the given + * parent. + * + * @param reaction The reaction to represent. + * @param parent The parent TreeItem. + */ + explicit ReactionTreeItem(Reaction* reaction, TreeItem* parent = 0); + + /** + * Returns "Reaction". + * + * @return The text for this TreeItem. + */ + virtual QString text() const; + + /** + * Returns the Reaction. + * + * @return The Reaction. + */ + Reaction* reaction() const; + +private: + + /** + * The Reaction. + */ + Reaction* mReaction; + + /** + * The child item containing the warning text when there is no condition + * but a condition type is used. + * The pointer is automatically set to 0 if the object is deleted anywhere + * (that is, not by deleting this pointer, but another one pointing to the + * object). + */ + QPointer<TextTreeItem> mEmptyTriggerConditionItem; + + /** + * The child item containing the WaitForTreeItem for the condition, when a + * condition type is used. + * The pointer is automatically set to 0 if the object is deleted anywhere + * (that is, not by deleting this pointer, but another one pointing to the + * object). + */ + QPointer<WaitForTreeItem> mTriggerConditionItem; + + /** + * The child item containing the trigger text, when an option type is used. + * The pointer is automatically set to 0 if the object is deleted anywhere + * (that is, not by deleting this pointer, but another one pointing to the + * object). + */ + QPointer<TextTreeItem> mTriggerOptionItem; + + /** + * The child item containing the response text, when the custom code type is + * used. + * This is a nested item. Its parent (and the direct child of this + * ReactionTreeItem) contains the "Execute the following code:" text. + * The pointer is automatically set to 0 if the object is deleted anywhere + * (that is, not by deleting this pointer, but another one pointing to the + * object). + */ + QPointer<TextTreeItem> mResponseCustomCodeItem; + + /** + * The child item containing the response text, when the next step type is + * used. + * The pointer is automatically set to 0 if the object is deleted anywhere + * (that is, not by deleting this pointer, but another one pointing to the + * object). + */ + QPointer<TextTreeItem> mResponseNextStepItem; + + /** + * Replaces the old item with the new item. + * The old item is destroyed. + * + * @param oldItem The item to remove. + * @param newItem The item to insert. + */ + void replaceItem(TreeItem* oldItem, TreeItem* newItem); + + /** + * Sets a new WaitForTreeItem for the WaitFor of the reaction, or a + * TextTreeItem if there is no WaitFor. + * + * @param reaction The reaction to get the WaitFor from. + */ + void updateConditionItem(Reaction* reaction); + + /** + * Sets a new TextTreeItem or modifies the current one with the option name. + * + * @param reaction The reaction to get the option name from. + */ + void updateOptionItem(Reaction* reaction); + + /** + * Sets a new nested TextTreeItem or modifies the current one with the + * custom code. + * + * @param reaction The reaction to get the custom code from. + */ + void updateCustomCodeItem(Reaction* reaction); + + /** + * Sets a new TextTreeItem or modifies the current one with the step id. + * + * @param reaction The reaction to get the step id from. + */ + void updateNextStepItem(Reaction* reaction); + +private Q_SLOTS: + + /** + * Updates this ReactionTreeItem when the data of its reaction changed. + * If a child item is needed to show some data, it is inserted or updated + * (depending on whether it existed previously or not). + * If the child item is no longer needed, it is removed. + * + * Items may be flat or nested, depending on the data to show. + * + * @param reaction The reaction. + */ + void update(Reaction* reaction); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/ReactionTreeItem.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-14 16:41:56 UTC (rev 152) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-14 17:15:38 UTC (rev 153) @@ -21,6 +21,7 @@ EditionDialog LicenseWidget NewWaitForWidget + ReactionTreeItem StepCustomCodeWidget StepDataWidget StepTreeItem @@ -51,6 +52,7 @@ EditionDialog LicenseWidget NewWaitForWidget + ReactionTreeItem StepCustomCodeWidget StepDataWidget StepTreeItem Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/ReactionTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/ReactionTreeItemTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/ReactionTreeItemTest.cpp 2010-03-14 17:15:38 UTC (rev 153) @@ -0,0 +1,465 @@ +/*************************************************************************** + * 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 "ReactionTreeItem.h" + +#include <KLocalizedString> + +#include "WaitForTreeItem.h" +#include "../Reaction.h" +#include "../WaitForSignal.h" + +class ReactionTreeItemTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + + void testConstructorConditionCustomCode(); + void testConstructorOptionNextStep(); + void testConstructorConditionCustomCodeFull(); + void testConstructorOptionNextStepFull(); + + void testReactionSetOptionName(); + void testReactionSetOptionNameChange(); + void testReactionSetOptionNameEmpty(); + + void testReactionSetWaitFor(); + void testReactionSetWaitForChange(); + void testReactionSetWaitForNull(); + + void testReactionSetNextStepId(); + void testReactionSetNextStepIdChange(); + void testReactionSetNextStepIdEmpty(); + + void testReactionSetCustomCode(); + void testReactionSetCustomCodeChange(); + void testReactionSetCustomCodeEmpty(); + + void testReactionSetTriggerTypeToOptionSelected(); + void testReactionSetTriggerTypeToConditionMet(); + + void testReactionSetResponseTypeToNextStep(); + void testReactionSetResponseTypeToCustomCode(); + +private: + + int mTreeItemStarType; + + void assertText(TreeItem* textItem, const QString& licenseText) const; + + void assertWaitFor(TreeItem* item, WaitFor* waitFor) const; + + void assertCustomCode(TreeItem* textItem, const QString& code) const; + + void assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const; + +}; + +class StubTreeItem: public TreeItem { +public: + virtual QString text() const { + return ""; + } +}; + +void ReactionTreeItemTest::initTestCase() { + //TreeItem* must be registered in order to be used with QSignalSpy + mTreeItemStarType = qRegisterMetaType<TreeItem*>("TreeItem*"); +} + +void ReactionTreeItemTest::testConstructorConditionCustomCode() { + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + reaction.setResponseType(Reaction::CustomCode); + + StubTreeItem parent; + ReactionTreeItem item(&reaction, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Reaction")); + QCOMPARE(item.reaction(), &reaction); + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), 0); + assertCustomCode(item.child(1), i18nc("@item", "(No code set)")); +} + +void ReactionTreeItemTest::testConstructorOptionNextStep() { + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + reaction.setResponseType(Reaction::NextStep); + + StubTreeItem parent; + ReactionTreeItem item(&reaction, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Reaction")); + QCOMPARE(item.reaction(), &reaction); + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option (option name not " + "set) is selected")); + assertText(item.child(1), i18nc("@item", "Change to step (step id not " + "set)")); +} + +void ReactionTreeItemTest::testConstructorConditionCustomCodeFull() { + WaitFor* waitFor = new WaitForSignal(); + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + reaction.setWaitFor(waitFor); + reaction.setResponseType(Reaction::CustomCode); + reaction.setCustomCode("The custom code"); + + StubTreeItem parent; + ReactionTreeItem item(&reaction, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Reaction")); + QCOMPARE(item.reaction(), &reaction); + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), waitFor); + assertCustomCode(item.child(1), "The custom code"); +} + +void ReactionTreeItemTest::testConstructorOptionNextStepFull() { + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + reaction.setOptionName("The option name"); + reaction.setResponseType(Reaction::NextStep); + reaction.setNextStepId("The step id"); + + StubTreeItem parent; + ReactionTreeItem item(&reaction, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Reaction")); + QCOMPARE(item.reaction(), &reaction); + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option \"The option " + "name\" is selected")); + assertText(item.child(1), i18nc("@item", "Change to step \"The step id\"")); +} + +void ReactionTreeItemTest::testReactionSetOptionName() { + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + reaction.setOptionName("The option name"); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option \"The option " + "name\" is selected")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void ReactionTreeItemTest::testReactionSetOptionNameChange() { + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + + ReactionTreeItem item(&reaction); + + reaction.setOptionName("The option name"); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + reaction.setOptionName("The new option name"); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option \"The new option " + "name\" is selected")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void ReactionTreeItemTest::testReactionSetOptionNameEmpty() { + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + reaction.setOptionName("The option name"); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + reaction.setOptionName(""); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option (option name not " + "set) is selected")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void ReactionTreeItemTest::testReactionSetWaitFor() { + WaitForSignal* waitFor = new WaitForSignal(); + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + + ReactionTreeItem item(&reaction); + + reaction.setWaitFor(waitFor); + + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), waitFor); +} + +void ReactionTreeItemTest::testReactionSetWaitForChange() { + //It will be removed and not deleted by the Reaction, so it is created in + //stack + WaitForSignal waitFor; + + WaitForSignal* waitFor2 = new WaitForSignal(); + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + + ReactionTreeItem item(&reaction); + + reaction.setWaitFor(&waitFor); + reaction.setWaitFor(waitFor2); + + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), waitFor2); +} + +void ReactionTreeItemTest::testReactionSetWaitForNull() { + //It will be removed and not deleted by the Reaction, so it is created in + //stack + WaitForSignal waitFor; + + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + reaction.setWaitFor(&waitFor); + + ReactionTreeItem item(&reaction); + + reaction.setWaitFor(0); + + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), 0); +} + +void ReactionTreeItemTest::testReactionSetNextStepId() { + Reaction reaction; + reaction.setResponseType(Reaction::NextStep); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(1), SIGNAL(dataChanged(TreeItem*))); + + reaction.setNextStepId("The step id"); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(1), i18nc("@item", "Change to step \"The step id\"")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)); +} + +void ReactionTreeItemTest::testReactionSetNextStepIdChange() { + Reaction reaction; + reaction.setResponseType(Reaction::NextStep); + + ReactionTreeItem item(&reaction); + + reaction.setNextStepId("The step id"); + + QSignalSpy dataChangedSpy(item.child(1), SIGNAL(dataChanged(TreeItem*))); + + reaction.setNextStepId("The new step id"); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(1), i18nc("@item", "Change to step \"The new step " + "id\"")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)); +} + +void ReactionTreeItemTest::testReactionSetNextStepIdEmpty() { + Reaction reaction; + reaction.setResponseType(Reaction::NextStep); + reaction.setNextStepId("The step id"); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(1), SIGNAL(dataChanged(TreeItem*))); + + reaction.setNextStepId(""); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(1), i18nc("@item", "Change to step (step id not " + "set)")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)); +} + +void ReactionTreeItemTest::testReactionSetCustomCode() { + Reaction reaction; + reaction.setResponseType(Reaction::CustomCode); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(1)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + reaction.setCustomCode("The custom code"); + + QCOMPARE(item.childCount(), 2); + assertCustomCode(item.child(1), "The custom code"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)->child(0)); +} + +void ReactionTreeItemTest::testReactionSetCustomCodeChange() { + Reaction reaction; + reaction.setResponseType(Reaction::CustomCode); + + ReactionTreeItem item(&reaction); + + reaction.setCustomCode("The custom code"); + + QSignalSpy dataChangedSpy(item.child(1)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + reaction.setCustomCode("The new custom code"); + + QCOMPARE(item.childCount(), 2); + assertCustomCode(item.child(1), "The new custom code"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)->child(0)); +} + +void ReactionTreeItemTest::testReactionSetCustomCodeEmpty() { + Reaction reaction; + reaction.setResponseType(Reaction::CustomCode); + reaction.setCustomCode("The custom code"); + + ReactionTreeItem item(&reaction); + + QSignalSpy dataChangedSpy(item.child(1)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + reaction.setCustomCode(""); + + QCOMPARE(item.childCount(), 2); + assertCustomCode(item.child(1), i18nc("@item", "(No code set)")); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(1)->child(0)); +} + +void ReactionTreeItemTest::testReactionSetTriggerTypeToOptionSelected() { + Reaction reaction; + reaction.setTriggerType(Reaction::ConditionMet); + reaction.setOptionName("The option name"); + + ReactionTreeItem item(&reaction); + + reaction.setTriggerType(Reaction::OptionSelected); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), i18nc("@item", "When the option \"The option " + "name\" is selected")); +} + +void ReactionTreeItemTest::testReactionSetTriggerTypeToConditionMet() { + WaitForSignal* waitFor = new WaitForSignal(); + Reaction reaction; + reaction.setTriggerType(Reaction::OptionSelected); + reaction.setWaitFor(waitFor); + + ReactionTreeItem item(&reaction); + + reaction.setTriggerType(Reaction::ConditionMet); + + QCOMPARE(item.childCount(), 2); + assertWaitFor(item.child(0), waitFor); +} + +void ReactionTreeItemTest::testReactionSetResponseTypeToNextStep() { + Reaction reaction; + reaction.setResponseType(Reaction::CustomCode); + reaction.setNextStepId("The step id"); + + ReactionTreeItem item(&reaction); + + reaction.setResponseType(Reaction::NextStep); + + QCOMPARE(item.childCount(), 2); + assertText(item.child(1), i18nc("@item", "Change to step \"The step id\"")); +} + +void ReactionTreeItemTest::testReactionSetResponseTypeToCustomCode() { + Reaction reaction; + reaction.setResponseType(Reaction::NextStep); + reaction.setCustomCode("The custom code"); + + ReactionTreeItem item(&reaction); + + reaction.setResponseType(Reaction::CustomCode); + + QCOMPARE(item.childCount(), 2); + assertCustomCode(item.child(1), "The custom code"); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void ReactionTreeItemTest::assertText(TreeItem* textItem, + const QString& name) const { + QCOMPARE(textItem->text(), i18nc("@item", "%1", name)); +} + +void ReactionTreeItemTest::assertWaitFor(TreeItem* item, + WaitFor* waitFor) const { + if (!waitFor) { + QCOMPARE(item->text(), i18nc("@item", "(No condition set)")); + return; + } + + WaitForTreeItem* waitForItem = qobject_cast<WaitForTreeItem*>(item); + QVERIFY(waitForItem); + QCOMPARE(waitForItem->waitFor(), waitFor); +} + +void ReactionTreeItemTest::assertCustomCode(TreeItem* textItem, + const QString& code) const { + QCOMPARE(textItem->text(), i18nc("@item", "Execute the following code:")); + QCOMPARE(textItem->childCount(), 1); + QCOMPARE(textItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +//TreeItem* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(TreeItem*); + +void ReactionTreeItemTest::assertDataChanged(const QSignalSpy& spy, int index, + TreeItem* item) const { + QCOMPARE(spy.at(index).count(), 1); + + QVariant argument = spy.at(index).at(0); + QCOMPARE(argument.userType(), mTreeItemStarType); + QCOMPARE(qvariant_cast<TreeItem*>(argument), item); +} + +QTEST_MAIN(ReactionTreeItemTest) + +#include "ReactionTreeItemTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/ReactionTreeItemTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |