[Ktutorial-commits] SF.net SVN: ktutorial:[117] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-03-07 07:21:24
|
Revision: 117
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=117&view=rev
Author: danxuliu
Date: 2010-03-07 07:21:18 +0000 (Sun, 07 Mar 2010)
Log Message:
-----------
Add TutorialTreeItem class to show a Tutorial 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/TutorialTreeItem.cpp
trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h
trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 06:21:19 UTC (rev 116)
+++ trunk/ktutorial/ktutorial-editor/src/view/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117)
@@ -4,8 +4,9 @@
TextTreeItem.cpp
TreeItem.cpp
TreeModel.cpp
+ TutorialTreeItem.cpp
)
kde4_add_library(ktutorial_editor_view ${ktutorial_editor_view_SRCS})
-target_link_libraries(ktutorial_editor_view ${KDE4_KDEUI_LIBS})
+target_link_libraries(ktutorial_editor_view ktutorial_editor ${KDE4_KDEUI_LIBS})
Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp 2010-03-07 07:21:18 UTC (rev 117)
@@ -0,0 +1,150 @@
+/***************************************************************************
+ * 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 "TutorialTreeItem.h"
+
+#include <KLocalizedString>
+
+#include "TextTreeItem.h"
+#include "../Tutorial.h"
+
+//public:
+
+TutorialTreeItem::TutorialTreeItem(Tutorial* tutorial, TreeItem* parent):
+ TreeItem(parent) {
+ Q_ASSERT(tutorial);
+
+ mNameItem = 0;
+ mDescriptionItem = 0;
+ mLicenseItem = 0;
+ mSetupItem = 0;
+ mTearDownItem = 0;
+
+ update(tutorial);
+ connect(tutorial, SIGNAL(dataChanged(Tutorial*)),
+ this, SLOT(update(Tutorial*)));
+}
+
+QString TutorialTreeItem::text() const {
+ if (mTutorialId.isEmpty()) {
+ return i18nc("@item", "Tutorial");
+ }
+
+ return i18nc("@item", "Tutorial %1", mTutorialId);
+}
+
+//private slots:
+
+void TutorialTreeItem::update(Tutorial* tutorial) {
+ int childIndex = 0;
+
+ if (tutorial->name().isEmpty()) {
+ removeFlatItemIfNeeded(mNameItem);
+
+ if (!mTutorialId.isEmpty()) {
+ mTutorialId = QString();
+ emit dataChanged(this);
+ }
+ } else {
+ addFlatItemIfNeeded(mNameItem, childIndex);
+ mNameItem->setText(i18nc("@item", "Name: %1", tutorial->name()));
+
+ mTutorialId = tutorial->id();
+ emit dataChanged(this);
+
+ childIndex++;
+ }
+
+ if (tutorial->description().isEmpty()) {
+ removeFlatItemIfNeeded(mDescriptionItem);
+ } else {
+ addFlatItemIfNeeded(mDescriptionItem, childIndex);
+ mDescriptionItem->setText(i18nc("@item", "Description: %1",
+ tutorial->description()));
+
+ childIndex++;
+ }
+
+ if (tutorial->licenseText().isEmpty()) {
+ removeNestedItemIfNeeded(mLicenseItem);
+ } else {
+ addNestedItemIfNeeded(mLicenseItem, childIndex,
+ i18nc("@item", "License:"));
+ mLicenseItem->setText(tutorial->licenseText());
+
+ childIndex++;
+ }
+
+ if (tutorial->customSetupCode().isEmpty()) {
+ removeNestedItemIfNeeded(mSetupItem);
+ } else {
+ addNestedItemIfNeeded(mSetupItem, childIndex,
+ i18nc("@item", "Setup:"));
+ mSetupItem->setText(tutorial->customSetupCode());
+
+ childIndex++;
+ }
+
+ if (tutorial->customTearDownCode().isEmpty()) {
+ removeNestedItemIfNeeded(mTearDownItem);
+ } else {
+ addNestedItemIfNeeded(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;
+ }
+}
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h (rev 0)
+++ trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.h 2010-03-07 07:21:18 UTC (rev 117)
@@ -0,0 +1,169 @@
+/***************************************************************************
+ * 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 TUTORIALTREEITEM_H
+#define TUTORIALTREEITEM_H
+
+#include "TreeItem.h"
+
+class TextTreeItem;
+class Tutorial;
+
+/**
+ * A TreeItem that represents a Tutorial.
+ * The tree representation of a tutorial is:
+ * Tutorial theId
+ * |-Name: the name
+ * |-Description: the description
+ * |-License:
+ * | -The license 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 name of the Tutorial is set, its representation is:
+ * Tutorial theId
+ * -Name: the name
+ *
+ * Note that composed elements like license don't appear at all, not even the
+ * parent item with just "License:".
+ *
+ * Whenever the tutorial data changes, the TutorialTreeItem 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 name, the name item
+ * will appear first and then the setup code item.
+ */
+class TutorialTreeItem: public TreeItem {
+Q_OBJECT
+public:
+
+ /**
+ * Creates a new TutorialTreeItem for the given Tutorial and with the given
+ * parent.
+ *
+ * @param tutorial The tutorial to represent.
+ * @param parent The parent TreeItem.
+ */
+ explicit TutorialTreeItem(Tutorial* tutorial, TreeItem* parent = 0);
+
+ /**
+ * Returns "Tutorial " and the id of the tutorial, or just "Tutorial" if
+ * there is no id.
+ *
+ * @return The text for this TreeItem.
+ */
+ virtual QString text() const;
+
+private:
+
+ /**
+ * The id of the tutorial.
+ */
+ QString mTutorialId;
+
+ /**
+ * The child item containing the name.
+ */
+ TextTreeItem* mNameItem;
+
+ /**
+ * The child item containing the description.
+ */
+ TextTreeItem* mDescriptionItem;
+
+ /**
+ * The child item containing the license.
+ * This is a nested item. Its parent (and the direct child of this
+ * TutorialTreeItem) contains the "License:" text.
+ */
+ TextTreeItem* mLicenseItem;
+
+ /**
+ * The child item containing the custom setup code.
+ * This is a nested item. Its parent (and the direct child of this
+ * TutorialTreeItem) 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
+ * TutorialTreeItem) contains the "Tear down:" text.
+ */
+ TextTreeItem* mTearDownItem;
+
+private Q_SLOTS:
+
+ /**
+ * Updates this TutorialTreeItem when the data of its tutorial 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 tutorial The tutorial.
+ */
+ 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
Property changes on: trunk/ktutorial/ktutorial-editor/src/view/TutorialTreeItem.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-07 06:21:19 UTC (rev 116)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/CMakeLists.txt 2010-03-07 07:21:18 UTC (rev 117)
@@ -12,11 +12,11 @@
FOREACH(_className ${ARGN})
set(_testName ${_className}Test)
kde4_add_unit_test(${_testName} TESTNAME ktutorial-editor-unit-${_testName} ${_testName}.cpp)
- target_link_libraries(${_testName} ktutorial_editor_view ${QT_QTTEST_LIBRARY})
+ target_link_libraries(${_testName} ktutorial_editor ktutorial_editor_view ${QT_QTTEST_LIBRARY})
ENDFOREACH(_className)
ENDMACRO(UNIT_TESTS)
-unit_tests(TextTreeItem TreeItem TreeModel)
+unit_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem)
MACRO(MEM_TESTS)
FOREACH(_testname ${ARGN})
@@ -24,4 +24,4 @@
ENDFOREACH(_testname)
ENDMACRO(MEM_TESTS)
-mem_tests(TextTreeItem TreeItem TreeModel)
+mem_tests(TextTreeItem TreeItem TreeModel TutorialTreeItem)
Added: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp 2010-03-07 07:21:18 UTC (rev 117)
@@ -0,0 +1,462 @@
+/***************************************************************************
+ * 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 "TutorialTreeItem.h"
+
+#include <KLocalizedString>
+
+#include "../Tutorial.h"
+
+class TutorialTreeItemTest: public QObject {
+Q_OBJECT
+
+private slots:
+
+ void initTestCase();
+
+ void testConstructor();
+ void testConstructorFullTutorial();
+
+ void testTutorialSetName();
+ void testTutorialSetNameChange();
+ void testTutorialSetNameEmpty();
+
+ void testTutorialSetDescription();
+ void testTutorialSetDescriptionChange();
+ void testTutorialSetDescriptionEmpty();
+
+ void testTutorialSetLicenseText();
+ void testTutorialSetLicenseTextChange();
+ void testTutorialSetLicenseTextEmpty();
+
+ void testTutorialSetCustomSetupCode();
+ void testTutorialSetCustomSetupCodeChange();
+ void testTutorialSetCustomSetupCodeEmpty();
+
+ void testTutorialSetCustomTearDownCode();
+ void testTutorialSetCustomTearDownCodeChange();
+ void testTutorialSetCustomTearDownCodeEmpty();
+
+ void testChildOrderWhenSettingDataInTutorial();
+ void testChildOrderWhenUnsettingDataInTutorial();
+
+private:
+
+ int mTreeItemStarType;
+
+ void assertName(TreeItem* nameItem, const QString& name) const;
+ void assertDescription(TreeItem* descriptionItem,
+ const QString& description) const;
+ void assertLicenseText(TreeItem* licenseItem,
+ 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 TutorialTreeItemTest::initTestCase() {
+ //TreeItem* must be registered in order to be used with QSignalSpy
+ mTreeItemStarType = qRegisterMetaType<TreeItem*>("TreeItem*");
+}
+
+void TutorialTreeItemTest::testConstructor() {
+ Tutorial tutorial;
+
+ StubTreeItem parent;
+ TutorialTreeItem item(&tutorial, &parent);
+
+ QCOMPARE(item.parent(), &parent);
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 0);
+}
+
+void TutorialTreeItemTest::testConstructorFullTutorial() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+ tutorial.setLicenseText("The license text");
+ tutorial.setCustomSetupCode("The setup code");
+ tutorial.setCustomTearDownCode("The tear down code");
+
+ StubTreeItem parent;
+ TutorialTreeItem item(&tutorial, &parent);
+
+ QCOMPARE(item.parent(), &parent);
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName"));
+ QCOMPARE(item.childCount(), 5);
+ assertName(item.child(0), "The name");
+ assertDescription(item.child(1), "The description");
+ assertLicenseText(item.child(2), "The license text");
+ assertCustomSetupCode(item.child(3), "The setup code");
+ assertCustomTearDownCode(item.child(4), "The tear down code");
+}
+
+//TreeItem* must be declared as a metatype to be used in qvariant_cast
+Q_DECLARE_METATYPE(TreeItem*);
+
+void TutorialTreeItemTest::testTutorialSetName() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ //Setting the name changes the data returned by text() in the
+ //TutorialTreeItem itself, as the id is based on the name
+ QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.setName("The name");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName"));
+ QCOMPARE(item.childCount(), 1);
+ assertName(item.child(0), "The name");
+ QCOMPARE(dataChangedSpy.count(), 1);
+ assertDataChanged(dataChangedSpy, 0, &item);
+}
+
+void TutorialTreeItemTest::testTutorialSetNameChange() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setName("The name");
+
+ QSignalSpy dataChangedRootSpy(&item, SIGNAL(dataChanged(TreeItem*)));
+ QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.setName("The name changed");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theNameChanged"));
+ QCOMPARE(item.childCount(), 1);
+ assertName(item.child(0), "The name changed");
+ QCOMPARE(dataChangedRootSpy.count(), 1);
+ assertDataChanged(dataChangedRootSpy, 0, &item);
+ QCOMPARE(dataChangedSpy.count(), 1);
+ assertDataChanged(dataChangedSpy, 0, item.child(0));
+}
+
+void TutorialTreeItemTest::testTutorialSetNameEmpty() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setName("The name");
+
+ QSignalSpy dataChangedSpy(&item, SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.setName("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 0);
+ QCOMPARE(dataChangedSpy.count(), 1);
+ assertDataChanged(dataChangedSpy, 0, &item);
+}
+
+void TutorialTreeItemTest::testTutorialSetDescription() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setDescription("The description");
+
+ QCOMPARE(item.childCount(), 1);
+ assertDescription(item.child(0), "The description");
+}
+
+void TutorialTreeItemTest::testTutorialSetDescriptionChange() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setDescription("The description");
+
+ QSignalSpy dataChangedSpy(item.child(0), SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.setDescription("The description changed");
+
+ QCOMPARE(item.childCount(), 1);
+ assertDescription(item.child(0), "The description changed");
+ QCOMPARE(dataChangedSpy.count(), 1);
+ assertDataChanged(dataChangedSpy, 0, item.child(0));
+}
+
+void TutorialTreeItemTest::testTutorialSetDescriptionEmpty() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setDescription("The description");
+ tutorial.setDescription("");
+
+ QCOMPARE(item.childCount(), 0);
+}
+
+void TutorialTreeItemTest::testTutorialSetLicenseText() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setLicenseText("The license text");
+
+ QCOMPARE(item.childCount(), 1);
+ assertLicenseText(item.child(0), "The license text");
+}
+
+void TutorialTreeItemTest::testTutorialSetLicenseTextChange() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setLicenseText("The license text");
+
+ QSignalSpy dataChangedSpy(item.child(0)->child(0),
+ SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.setLicenseText("The license text changed");
+
+ QCOMPARE(item.childCount(), 1);
+ assertLicenseText(item.child(0), "The license text changed");
+ QCOMPARE(dataChangedSpy.count(), 1);
+ assertDataChanged(dataChangedSpy, 0, item.child(0)->child(0));
+}
+
+void TutorialTreeItemTest::testTutorialSetLicenseTextEmpty() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setLicenseText("The license text");
+ tutorial.setLicenseText("");
+
+ QCOMPARE(item.childCount(), 0);
+}
+
+void TutorialTreeItemTest::testTutorialSetCustomSetupCode() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomSetupCode("The setup code");
+
+ QCOMPARE(item.childCount(), 1);
+ assertCustomSetupCode(item.child(0), "The setup code");
+}
+
+void TutorialTreeItemTest::testTutorialSetCustomSetupCodeChange() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomSetupCode("The setup code");
+
+ QSignalSpy dataChangedSpy(item.child(0)->child(0),
+ SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.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 TutorialTreeItemTest::testTutorialSetCustomSetupCodeEmpty() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomSetupCode("The setup code");
+ tutorial.setCustomSetupCode("");
+
+ QCOMPARE(item.childCount(), 0);
+}
+
+void TutorialTreeItemTest::testTutorialSetCustomTearDownCode() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomTearDownCode("The tear down code");
+
+ QCOMPARE(item.childCount(), 1);
+ assertCustomTearDownCode(item.child(0), "The tear down code");
+}
+
+void TutorialTreeItemTest::testTutorialSetCustomTearDownCodeChange() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomTearDownCode("The tear down code");
+
+ QSignalSpy dataChangedSpy(item.child(0)->child(0),
+ SIGNAL(dataChanged(TreeItem*)));
+
+ tutorial.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 TutorialTreeItemTest::testTutorialSetCustomTearDownCodeEmpty() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomTearDownCode("The tear down code");
+ tutorial.setCustomTearDownCode("");
+
+ QCOMPARE(item.childCount(), 0);
+}
+
+void TutorialTreeItemTest::testChildOrderWhenSettingDataInTutorial() {
+ Tutorial tutorial;
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setCustomSetupCode("The setup code");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 1);
+ assertCustomSetupCode(item.child(0), "The setup code");
+
+ tutorial.setDescription("The description");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 2);
+ assertDescription(item.child(0), "The description");
+ assertCustomSetupCode(item.child(1), "The setup code");
+
+ tutorial.setCustomTearDownCode("The tear down code");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 3);
+ assertDescription(item.child(0), "The description");
+ assertCustomSetupCode(item.child(1), "The setup code");
+ assertCustomTearDownCode(item.child(2), "The tear down code");
+
+ tutorial.setName("The name");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName"));
+ QCOMPARE(item.childCount(), 4);
+ assertName(item.child(0), "The name");
+ assertDescription(item.child(1), "The description");
+ assertCustomSetupCode(item.child(2), "The setup code");
+ assertCustomTearDownCode(item.child(3), "The tear down code");
+
+ tutorial.setLicenseText("The license text");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName"));
+ QCOMPARE(item.childCount(), 5);
+ assertName(item.child(0), "The name");
+ assertDescription(item.child(1), "The description");
+ assertLicenseText(item.child(2), "The license text");
+ assertCustomSetupCode(item.child(3), "The setup code");
+ assertCustomTearDownCode(item.child(4), "The tear down code");
+}
+
+void TutorialTreeItemTest::testChildOrderWhenUnsettingDataInTutorial() {
+ Tutorial tutorial;
+ tutorial.setName("The name");
+ tutorial.setDescription("The description");
+ tutorial.setLicenseText("The license text");
+ tutorial.setCustomSetupCode("The setup code");
+ tutorial.setCustomTearDownCode("The tear down code");
+
+ TutorialTreeItem item(&tutorial);
+
+ tutorial.setLicenseText("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial %1", "theName"));
+ QCOMPARE(item.childCount(), 4);
+ assertName(item.child(0), "The name");
+ assertDescription(item.child(1), "The description");
+ assertCustomSetupCode(item.child(2), "The setup code");
+ assertCustomTearDownCode(item.child(3), "The tear down code");
+
+ tutorial.setName("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 3);
+ assertDescription(item.child(0), "The description");
+ assertCustomSetupCode(item.child(1), "The setup code");
+ assertCustomTearDownCode(item.child(2), "The tear down code");
+
+ tutorial.setCustomTearDownCode("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 2);
+ assertDescription(item.child(0), "The description");
+ assertCustomSetupCode(item.child(1), "The setup code");
+
+ tutorial.setDescription("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 1);
+ assertCustomSetupCode(item.child(0), "The setup code");
+
+ tutorial.setCustomSetupCode("");
+
+ QCOMPARE(item.text(), i18nc("@item", "Tutorial"));
+ QCOMPARE(item.childCount(), 0);
+}
+
+/////////////////////////////////// Helpers ////////////////////////////////////
+
+void TutorialTreeItemTest::assertName(TreeItem* nameItem,
+ const QString& name) const {
+ QCOMPARE(nameItem->text(), i18nc("@item", "Name: %1", name));
+}
+
+void TutorialTreeItemTest::assertDescription(TreeItem* descriptionItem,
+ const QString& description) const {
+ QCOMPARE(descriptionItem->text(),
+ i18nc("@item", "Description: %1", description));
+}
+
+void TutorialTreeItemTest::assertLicenseText(TreeItem* licenseItem,
+ const QString& licenseText) const {
+ QCOMPARE(licenseItem->text(), i18nc("@item", "License:"));
+ QCOMPARE(licenseItem->childCount(), 1);
+ QCOMPARE(licenseItem->child(0)->text(),
+ i18nc("@item", licenseText.toAscii()));
+}
+
+void TutorialTreeItemTest::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 TutorialTreeItemTest::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 TutorialTreeItemTest::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(TutorialTreeItemTest)
+
+#include "TutorialTreeItemTest.moc"
Property changes on: trunk/ktutorial/ktutorial-editor/tests/unit/view/TutorialTreeItemTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|