[Ktutorial-commits] SF.net SVN: ktutorial:[119] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
From: <dan...@us...> - 2010-03-07 18:50:44
|
Revision: 119 http://ktutorial.svn.sourceforge.net/ktutorial/?rev=119&view=rev Author: danxuliu Date: 2010-03-07 18:50:37 +0000 (Sun, 07 Mar 2010) Log Message: ----------- -Add StepTreeItem class to show a Step in a TreeModel. -Extract common methods from TutorialTreeItem and StepTreeItem to TreeItemUtil class. Modified Paths: -------------- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt Added Paths: ----------- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 18:50:37 UTC (rev 119) @@ -1,8 +1,10 @@ include_directories(${KDE4_INCLUDES}) set(ktutorial_editor_view_SRCS + StepTreeItem.cpp TextTreeItem.cpp TreeItem.cpp + TreeItemUtil.cpp TreeModel.cpp TutorialTreeItem.cpp ) Added: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -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 "StepTreeItem.h" + +#include <KLocalizedString> + +#include "TextTreeItem.h" +#include "TreeItemUtil.h" +#include "../Step.h" + +//public: + +StepTreeItem::StepTreeItem(Step* step, TreeItem* parent): + TreeItem(parent) { + Q_ASSERT(step); + + mTextItem = 0; + mSetupItem = 0; + mTearDownItem = 0; + + update(step); + connect(step, SIGNAL(dataChanged(Step*)), + this, SLOT(update(Step*))); +} + +QString StepTreeItem::text() const { + if (mStepId.isEmpty()) { + return i18nc("@item", "Step"); + } + + return i18nc("@item", "Step %1", mStepId); +} + +//private slots: + +void StepTreeItem::update(Step* step) { + int childIndex = 0; + + if (step->id().isEmpty()) { + if (!mStepId.isEmpty()) { + mStepId = QString(); + emit dataChanged(this); + } + } else { + mStepId = step->id(); + emit dataChanged(this); + } + + if (step->text().isEmpty()) { + TreeItemUtil::removeFlatItemIfNeeded(mTextItem); + } else { + TreeItemUtil::addFlatItemIfNeeded(this, mTextItem, childIndex); + mTextItem->setText(i18nc("@item", "Text: %1", step->text())); + + childIndex++; + } + + if (step->customSetupCode().isEmpty()) { + TreeItemUtil::removeNestedItemIfNeeded(mSetupItem); + } else { + TreeItemUtil::addNestedItemIfNeeded(this, mSetupItem, childIndex, + i18nc("@item", "Setup:")); + mSetupItem->setText(step->customSetupCode()); + + childIndex++; + } + + if (step->customTearDownCode().isEmpty()) { + TreeItemUtil::removeNestedItemIfNeeded(mTearDownItem); + } else { + TreeItemUtil::addNestedItemIfNeeded(this, mTearDownItem, childIndex, + i18nc("@item", "Tear down:")); + mTearDownItem->setText(step->customTearDownCode()); + + childIndex++; + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,115 @@ +/*************************************************************************** + * 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 STEPTREEITEM_H +#define STEPTREEITEM_H + +#include "TreeItem.h" + +class Step; +class TextTreeItem; + +/** + * A TreeItem that represents a Step. + * The tree representation of a step is: + * Step theId + * |-Text: the text + * |-Setup: + * | -The custom setup code + * --Tear down: + * -The custom tear down code + * + * The items only appear if they have some data to show. For example, if only + * the text of the Step is set, its representation is: + * Step + * -The text + * + * Note that composed elements like custom setup code don't appear at all, not + * even the parent item with just "Setup:". + * + * Whenever the step data changes, the StepTreeItem and its child items + * are updated as needed. + * + * Also note that the order of the child elements is always the same. Even if, + * for example, the setup code is set first and then the text, the text item + * will appear first and then the setup code item. + */ +class StepTreeItem: public TreeItem { +Q_OBJECT +public: + + /** + * Creates a new StepTreeItem for the given Step and with the given + * parent. + * + * @param step The step to represent. + * @param parent The parent TreeItem. + */ + explicit StepTreeItem(Step* step, TreeItem* parent = 0); + + /** + * Returns "Step " and the id of the step, or just "Step" if + * there is no id. + * + * @return The text for this TreeItem. + */ + virtual QString text() const; + +private: + + /** + * The id of the step. + */ + QString mStepId; + + /** + * The child item containing the text. + */ + TextTreeItem* mTextItem; + + /** + * The child item containing the custom setup code. + * This is a nested item. Its parent (and the direct child of this + * StepTreeItem) contains the "Setup:" text. + */ + TextTreeItem* mSetupItem; + + /** + * The child item containing the custom tear down code. + * This is a nested item. Its parent (and the direct child of this + * StepTreeItem) contains the "Tear down:" text. + */ + TextTreeItem* mTearDownItem; + +private Q_SLOTS: + + /** + * Updates this StepTreeItem when the data of its step 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 step The step. + */ + void update(Step* step); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/StepTreeItem.h ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,60 @@ +/*************************************************************************** + * 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 "TreeItemUtil.h" + +#include "TextTreeItem.h" + +//public: + +void TreeItemUtil::addFlatItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index) { + if (item == 0) { + item = new TextTreeItem(root); + root->insertChild(item, index); + } +} + +void TreeItemUtil::removeFlatItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + item->parent()->removeChild(item); + delete item; + item = 0; + } +} + +void TreeItemUtil::addNestedItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index, const QString& parentText) { + if (item == 0) { + TextTreeItem* parentItem = new TextTreeItem(root); + parentItem->setText(parentText); + + item = new TextTreeItem(parentItem); + parentItem->appendChild(item); + + root->insertChild(parentItem, index); + } +} + +void TreeItemUtil::removeNestedItemIfNeeded(TextTreeItem*& item) { + if (item != 0) { + item->parent()->parent()->removeChild(item->parent()); + delete item->parent(); + item = 0; + } +} Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h (rev 0) +++ trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,79 @@ +/*************************************************************************** + * 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 TREEITEMUTIL_H +#define TREEITEMUTIL_H + +#include "TreeItem.h" + +class TextTreeItem; + +/** + * Utility class for TreeItems. + * It provides some static methods to modify the structure of TreeItems, adding + * or removing children. + */ +class TreeItemUtil { +public: + + /** + * If there is no item, inserts a new TextTreeItem at the given index in the + * root item. + * The new TextTreeItem is stored in the given item. + * + * @param root The root item to add the item to. + * @param item The item to check and the variable to store the new item in. + * @param index The index in the root item to add the item. + */ + static void addFlatItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index); + + /** + * If there is an item, it is removed from its parent item. + * The given item is set to null after deleting it. + * + * @param item The item to remove, delete and clean. + */ + static void removeFlatItemIfNeeded(TextTreeItem*& item); + + /** + * If there is no item, inserts a new nested TextTreeItem at the given index + * in the root item. + * A TextTreeItem is inserted with the given parentText, and a new + * TextTreeItem child is appended to that parent. + * The child is stored in the given item. + * + * @param root The root item to add the parent item to. + * @param item The item to check and the place to store the new item. + * @param index The index in the root item to add the parent item. + * @param parentText The text to set in the added parent item. + */ + static void addNestedItemIfNeeded(TreeItem* root, TextTreeItem*& item, + int index, const QString& parentText); + + /** + * If there is an item, its parent is removed from its parent item. + * The given item is set to null after its parent is deleted. + * + * @param item The item to clean after removing and deleting its parent. + */ + static void removeNestedItemIfNeeded(TextTreeItem*& item); + +}; + +#endif Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TreeItemUtil.h ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -21,6 +21,7 @@ #include <KLocalizedString> #include "TextTreeItem.h" +#include "TreeItemUtil.h" #include "../Tutorial.h" //public: @@ -54,14 +55,14 @@ int childIndex = 0; if (tutorial->name().isEmpty()) { - removeFlatItemIfNeeded(mNameItem); + TreeItemUtil::removeFlatItemIfNeeded(mNameItem); if (!mTutorialId.isEmpty()) { mTutorialId = QString(); emit dataChanged(this); } } else { - addFlatItemIfNeeded(mNameItem, childIndex); + TreeItemUtil::addFlatItemIfNeeded(this, mNameItem, childIndex); mNameItem->setText(i18nc("@item", "Name: %1", tutorial->name())); mTutorialId = tutorial->id(); @@ -71,9 +72,9 @@ } if (tutorial->description().isEmpty()) { - removeFlatItemIfNeeded(mDescriptionItem); + TreeItemUtil::removeFlatItemIfNeeded(mDescriptionItem); } else { - addFlatItemIfNeeded(mDescriptionItem, childIndex); + TreeItemUtil::addFlatItemIfNeeded(this, mDescriptionItem, childIndex); mDescriptionItem->setText(i18nc("@item", "Description: %1", tutorial->description())); @@ -81,70 +82,32 @@ } if (tutorial->licenseText().isEmpty()) { - removeNestedItemIfNeeded(mLicenseItem); + TreeItemUtil::removeNestedItemIfNeeded(mLicenseItem); } else { - addNestedItemIfNeeded(mLicenseItem, childIndex, - i18nc("@item", "License:")); + TreeItemUtil::addNestedItemIfNeeded(this, mLicenseItem, childIndex, + i18nc("@item", "License:")); mLicenseItem->setText(tutorial->licenseText()); childIndex++; } if (tutorial->customSetupCode().isEmpty()) { - removeNestedItemIfNeeded(mSetupItem); + TreeItemUtil::removeNestedItemIfNeeded(mSetupItem); } else { - addNestedItemIfNeeded(mSetupItem, childIndex, - i18nc("@item", "Setup:")); + TreeItemUtil::addNestedItemIfNeeded(this, mSetupItem, childIndex, + i18nc("@item", "Setup:")); mSetupItem->setText(tutorial->customSetupCode()); childIndex++; } if (tutorial->customTearDownCode().isEmpty()) { - removeNestedItemIfNeeded(mTearDownItem); + TreeItemUtil::removeNestedItemIfNeeded(mTearDownItem); } else { - addNestedItemIfNeeded(mTearDownItem, childIndex, - i18nc("@item", "Tear down:")); + TreeItemUtil::addNestedItemIfNeeded(this, mTearDownItem, childIndex, + i18nc("@item", "Tear down:")); mTearDownItem->setText(tutorial->customTearDownCode()); childIndex++; } } - -void TutorialTreeItem::addFlatItemIfNeeded(TextTreeItem*& item, - int childIndex) { - if (item == 0) { - item = new TextTreeItem(this); - insertChild(item, childIndex); - } -} - -void TutorialTreeItem::removeFlatItemIfNeeded(TextTreeItem*& item) { - if (item != 0) { - removeChild(item); - delete item; - item = 0; - } -} - -void TutorialTreeItem::addNestedItemIfNeeded(TextTreeItem*& item, - int childIndex, - const QString& parentText) { - if (item == 0) { - TextTreeItem* parentItem = new TextTreeItem(this); - parentItem->setText(parentText); - - item = new TextTreeItem(parentItem); - parentItem->appendChild(item); - - insertChild(parentItem, childIndex); - } -} - -void TutorialTreeItem::removeNestedItemIfNeeded(TextTreeItem*& item) { - if (item != 0) { - removeChild(item->parent()); - delete item->parent(); - item = 0; - } -} Modified: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h =================================================================== --- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 18:50:37 UTC (rev 119) @@ -125,45 +125,6 @@ */ void update(Tutorial* tutorial); - /** - * If there is no item, inserts a new TextTreeItem at the given childIndex - * in this TutorialTreeItem. - * The new TextTreeItem is stored in the given item. - * - * @param item The item to check and the place to store the new item. - * @param childIndex The index in this TutorialTreeItem to add the item. - */ - void addFlatItemIfNeeded(TextTreeItem*& item, int childIndex); - - /** - * If there is an item, it is removed from this TutorialTreeItem. - * The given item is set to null after deleting it. - * - * @param item The item to remove, delete and clean. - */ - void removeFlatItemIfNeeded(TextTreeItem*& item); - - /** - * If there is no item, inserts a new nested TextTreeItem at the given - * childIndex in this TutorialTreeItem. - * A TextTreeItem is inserted with the given parentText, and a new - * TextTreeItem child is appended to that parent. - * The child is stored in the given item. - * - * @param item The item to check and the place to store the new item. - * @param childIndex The index in this TutorialTreeItem to add the item. - */ - void addNestedItemIfNeeded(TextTreeItem*& item, int childIndex, - const QString& parentText); - - /** - * If there is an item, its parent is removed from this TutorialTreeItem. - * The given item is set to null after its parent is deleted. - * - * @param item The item to clean after removing and deleting its parent. - */ - void removeNestedItemIfNeeded(TextTreeItem*& item); - }; #endif Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 07:38:57 UTC (rev 118) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 18:50:37 UTC (rev 119) @@ -16,7 +16,7 @@ ENDFOREACH(_className) ENDMACRO(UNIT_TESTS) -unit_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) +unit_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) MACRO(MEM_TESTS) FOREACH(_testname ${ARGN}) @@ -24,4 +24,4 @@ ENDFOREACH(_testname) ENDMACRO(MEM_TESTS) -mem_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem) +mem_tests(StepTreeItem TextTreeItem TreeItem TreeItemUtil TreeModel TutorialTreeItem) Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -0,0 +1,372 @@ +/*************************************************************************** + * 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 "StepTreeItem.h" + +#include <KLocalizedString> + +#include "../Step.h" + +class StepTreeItemTest: public QObject { +Q_OBJECT + +private slots: + + void initTestCase(); + + void testConstructor(); + void testConstructorFullStep(); + + void testStepSetId(); + void testStepSetIdChange(); + void testStepSetIdEmpty(); + + void testStepSetText(); + void testStepSetTextChange(); + void testStepSetTextEmpty(); + + void testStepSetCustomSetupCode(); + void testStepSetCustomSetupCodeChange(); + void testStepSetCustomSetupCodeEmpty(); + + void testStepSetCustomTearDownCode(); + void testStepSetCustomTearDownCodeChange(); + void testStepSetCustomTearDownCodeEmpty(); + + void testChildOrderWhenSettingDataInStep(); + void testChildOrderWhenUnsettingDataInStep(); + +private: + + int mTreeItemStarType; + + void assertText(TreeItem* textItem, const QString& licenseText) const; + void assertCustomSetupCode(TreeItem* setupItem, const QString& code) const; + void assertCustomTearDownCode(TreeItem* tearDownItem, + 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 StepTreeItemTest::initTestCase() { + //TreeItem* must be registered in order to be used with QSignalSpy + mTreeItemStarType = qRegisterMetaType<TreeItem*>("TreeItem*"); +} + +void StepTreeItemTest::testConstructor() { + Step step; + + StubTreeItem parent; + StepTreeItem item(&step, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testConstructorFullStep() { + Step step; + step.setId("The id"); + step.setText("The text"); + step.setCustomSetupCode("The setup code"); + step.setCustomTearDownCode("The tear down code"); + + StubTreeItem parent; + StepTreeItem item(&step, &parent); + + QCOMPARE(item.parent(), &parent); + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); +} + +//TreeItem* must be declared as a metatype to be used in qvariant_cast +Q_DECLARE_METATYPE(TreeItem*); + +void StepTreeItemTest::testStepSetId() { + Step step; + StepTreeItem item(&step); + + //Setting the id changes the data returned by text() in the + //StepTreeItem itself + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId("The id"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetIdChange() { + Step step; + StepTreeItem item(&step); + + step.setId("The id"); + + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId("The id changed"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id changed")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetIdEmpty() { + Step step; + StepTreeItem item(&step); + + step.setId("The id"); + + QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*))); + + step.setId(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, &item); +} + +void StepTreeItemTest::testStepSetText() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + + QCOMPARE(item.childCount(), 1); + assertText(item.child(0), "The text"); +} + +void StepTreeItemTest::testStepSetTextChange() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + + QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*))); + + step.setText("The text changed"); + + QCOMPARE(item.childCount(), 1); + assertText(item.child(0), "The text changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)); +} + +void StepTreeItemTest::testStepSetTextEmpty() { + Step step; + StepTreeItem item(&step); + + step.setText("The text"); + step.setText(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testStepSetCustomSetupCode() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); +} + +void StepTreeItemTest::testStepSetCustomSetupCodeChange() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + step.setCustomSetupCode("The setup code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void StepTreeItemTest::testStepSetCustomSetupCodeEmpty() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + step.setCustomSetupCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testStepSetCustomTearDownCode() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code"); +} + +void StepTreeItemTest::testStepSetCustomTearDownCodeChange() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + + QSignalSpy dataChangedSpy(item.child(0)->child(0), + SIGNAL(dataChanged(TreeItem*))); + + step.setCustomTearDownCode("The tear down code changed"); + + QCOMPARE(item.childCount(), 1); + assertCustomTearDownCode(item.child(0), "The tear down code changed"); + QCOMPARE(dataChangedSpy.count(), 1); + assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0)); +} + +void StepTreeItemTest::testStepSetCustomTearDownCodeEmpty() { + Step step; + StepTreeItem item(&step); + + step.setCustomTearDownCode("The tear down code"); + step.setCustomTearDownCode(""); + + QCOMPARE(item.childCount(), 0); +} + +void StepTreeItemTest::testChildOrderWhenSettingDataInStep() { + Step step; + StepTreeItem item(&step); + + step.setCustomSetupCode("The setup code"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setText("The text"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 2); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + + step.setCustomTearDownCode("The tear down code"); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); + + step.setId("The id"); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 3); + assertText(item.child(0), "The text"); + assertCustomSetupCode(item.child(1), "The setup code"); + assertCustomTearDownCode(item.child(2), "The tear down code"); +} + +void StepTreeItemTest::testChildOrderWhenUnsettingDataInStep() { + Step step; + step.setId("The id"); + step.setText("The text"); + step.setCustomSetupCode("The setup code"); + step.setCustomTearDownCode("The tear down code"); + + StepTreeItem item(&step); + + step.setText(""); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 2); + assertCustomSetupCode(item.child(0), "The setup code"); + assertCustomTearDownCode(item.child(1), "The tear down code"); + + step.setCustomTearDownCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Step %1", "The id")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setId(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 1); + assertCustomSetupCode(item.child(0), "The setup code"); + + step.setCustomSetupCode(""); + + QCOMPARE(item.text(), i18nc("@item", "Step")); + QCOMPARE(item.childCount(), 0); +} + +/////////////////////////////////// Helpers //////////////////////////////////// + +void StepTreeItemTest::assertText(TreeItem* textItem, + const QString& name) const { + QCOMPARE(textItem->text(), i18nc("@item", "Text: %1", name)); +} + +void StepTreeItemTest::assertCustomSetupCode(TreeItem* setupItem, + const QString& code) const { + QCOMPARE(setupItem->text(), i18nc("@item", "Setup:")); + QCOMPARE(setupItem->childCount(), 1); + QCOMPARE(setupItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void StepTreeItemTest::assertCustomTearDownCode(TreeItem* tearDownItem, + const QString& code) const { + QCOMPARE(tearDownItem->text(), i18nc("@item", "Tear down:")); + QCOMPARE(tearDownItem->childCount(), 1); + QCOMPARE(tearDownItem->child(0)->text(), i18nc("@item", code.toAscii())); +} + +void StepTreeItemTest::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(StepTreeItemTest) + +#include "StepTreeItemTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/StepTreeItemTest.cpp ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp =================================================================== --- trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp (rev 0) +++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp 2010-03-07 18:50:37 UTC (rev 119) @@ -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/>. * + ***************************************************************************/ + +#include <QtTest> + +#include "TreeItemUtil.h" + +#include "TextTreeItem.h" + +class TreeItemUtilTest: public QObject { +Q_OBJECT + +private slots: + + void testAddFlatItem(); + void testAddFlatItemAlreadyAdded(); + + void testRemoveFlatItem(); + void testRemoveFlatItemAlreadyRemoved(); + + void testAddNestedItem(); + void testAddNestedItemAlreadyAdded(); + + void testRemoveNestedItem(); + void testRemoveNestedItemAlreadyRemoved(); + +}; + +class StubTreeItem: public TreeItem { +public: + virtual QString text() const { + return ""; + } +}; + +void TreeItemUtilTest::testAddFlatItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + + QVERIFY(item); + QCOMPARE(item->parent(), &parent); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(parent.child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testAddFlatItemAlreadyAdded() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + TreeItemUtil::addFlatItemIfNeeded(&parent, item, 0); + + QVERIFY(item); + QCOMPARE(item->parent(), &parent); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(parent.child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testRemoveFlatItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + + TreeItemUtil::removeFlatItemIfNeeded(item); + + QVERIFY(!item); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testRemoveFlatItemAlreadyRemoved() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + + TreeItemUtil::removeFlatItemIfNeeded(item); + TreeItemUtil::removeFlatItemIfNeeded(item); + + QVERIFY(!item); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testAddNestedItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text"); + + QVERIFY(item); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(item->parent(), parent.child(0)); + QCOMPARE(parent.child(0)->parent(), &parent); + QCOMPARE(parent.child(0)->text(), QString("Parent text")); + QCOMPARE(parent.child(0)->childCount(), 1); + QCOMPARE(parent.child(0)->child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testAddNestedItemAlreadyAdded() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = 0; + + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text"); + TreeItemUtil::addNestedItemIfNeeded(&parent, item, 0, "Parent text2"); + + QVERIFY(item); + QCOMPARE(parent.childCount(), 2); + QCOMPARE(item->parent(), parent.child(0)); + QCOMPARE(parent.child(0)->parent(), &parent); + QCOMPARE(parent.child(0)->text(), QString("Parent text")); + QCOMPARE(parent.child(0)->childCount(), 1); + QCOMPARE(parent.child(0)->child(0), item); + QCOMPARE(parent.child(1), lastItem); +} + +void TreeItemUtilTest::testRemoveNestedItem() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + TextTreeItem* childItem = new TextTreeItem(item); + item->appendChild(childItem); + + TreeItemUtil::removeNestedItemIfNeeded(childItem); + + QVERIFY(!childItem); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +void TreeItemUtilTest::testRemoveNestedItemAlreadyRemoved() { + StubTreeItem parent; + TreeItem* lastItem = new TextTreeItem(&parent); + parent.appendChild(lastItem); + + TextTreeItem* item = new TextTreeItem(&parent); + parent.insertChild(item, 0); + TextTreeItem* childItem = new TextTreeItem(item); + item->appendChild(childItem); + + TreeItemUtil::removeNestedItemIfNeeded(childItem); + TreeItemUtil::removeNestedItemIfNeeded(childItem); + + QVERIFY(!childItem); + QCOMPARE(parent.childCount(), 1); + QCOMPARE(parent.child(0), lastItem); +} + +QTEST_MAIN(TreeItemUtilTest) + +#include "TreeItemUtilTest.moc" Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TreeItemUtilTest.cpp ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |