[Ktutorial-commits] SF.net SVN: ktutorial:[101] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2010-02-24 02:24:10
|
Revision: 101
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=101&view=rev
Author: danxuliu
Date: 2010-02-24 02:24:04 +0000 (Wed, 24 Feb 2010)
Log Message:
-----------
WaitFors and Options added in the setup() method of a Step derived class are now removed and deleted automatically when the step is deactivated.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-library/src/Step.cpp
trunk/ktutorial/ktutorial-library/src/Step.h
trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp
trunk/ktutorial/ktutorial-library/test/StepTest.cpp
Modified: trunk/ktutorial/ktutorial-library/src/Step.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/src/Step.cpp 2010-02-23 18:33:39 UTC (rev 100)
+++ trunk/ktutorial/ktutorial-library/src/Step.cpp 2010-02-24 02:24:04 UTC (rev 101)
@@ -1,5 +1,5 @@
/***************************************************************************
- * Copyright (C) 2008-2009 by Daniel Calviño Sánchez *
+ * Copyright (C) 2008-2010 by Daniel Calviño Sánchez *
* dan...@gm... *
* *
* This program is free software; you can redistribute it and/or modify *
@@ -27,9 +27,9 @@
void Step::setActive(bool active) {
if (active) {
- setup();
+ setupWrapper();
} else {
- tearDown();
+ tearDownWrapper();
}
mActive = active;
@@ -58,10 +58,21 @@
mOptions.append(option);
+
+ bool deleteAddedObjectsInTearDownValue = mDeleteAddedObjectsInTearDown;
+ mDeleteAddedObjectsInTearDown = false;
+
WaitForSignal* waitFor = new WaitForSignal(option, SIGNAL(selected()));
addWaitFor(waitFor, receiver, slot);
mOptionsWaitsFor.append(waitFor);
+
+ mDeleteAddedObjectsInTearDown = deleteAddedObjectsInTearDownValue;
+
+
+ if (mDeleteAddedObjectsInTearDown) {
+ mOptionsToBeDeletedInTearDown.append(option);
+ }
}
void Step::addWaitFor(WaitFor* waitFor, QObject* receiver, const QString& slot) {
@@ -76,6 +87,10 @@
mWaitsFor.append(waitFor);
connectWaitFor(waitFor, receiver, slot);
+
+ if (mDeleteAddedObjectsInTearDown) {
+ mWaitsForToBeDeletedInTearDown.append(waitFor);
+ }
}
void Step::removeOption(Option* option) {
@@ -126,3 +141,27 @@
void Step::disconnectWaitFor(WaitFor* waitFor) {
disconnect(waitFor, SIGNAL(waitEnded(WaitFor*)), 0, 0);
}
+
+//private:
+
+void Step::setupWrapper() {
+ mDeleteAddedObjectsInTearDown = true;
+ setup();
+ mDeleteAddedObjectsInTearDown = false;
+}
+
+void Step::tearDownWrapper() {
+ tearDown();
+
+ foreach (Option* option, mOptionsToBeDeletedInTearDown) {
+ removeOption(option);
+ delete option;
+ }
+ mOptionsToBeDeletedInTearDown.clear();
+
+ foreach (WaitFor* waitFor, mWaitsForToBeDeletedInTearDown) {
+ removeWaitFor(waitFor);
+ delete waitFor;
+ }
+ mWaitsForToBeDeletedInTearDown.clear();
+}
Modified: trunk/ktutorial/ktutorial-library/src/Step.h
===================================================================
--- trunk/ktutorial/ktutorial-library/src/Step.h 2010-02-23 18:33:39 UTC (rev 100)
+++ trunk/ktutorial/ktutorial-library/src/Step.h 2010-02-24 02:24:04 UTC (rev 101)
@@ -1,5 +1,5 @@
/***************************************************************************
- * Copyright (C) 2008-2009 by Daniel Calviño Sánchez *
+ * Copyright (C) 2008-2010 by Daniel Calviño Sánchez *
* dan...@gm... *
* *
* This program is free software; you can redistribute it and/or modify *
@@ -105,8 +105,9 @@
* This may be needed, for example, when a Step needs to use an object that
* didn't exist when the Tutorial was created (like something in a dialog
* deleted every time it is closed). In this case, a subclass of Step should be
- * made with setup() and tearDown() methods adding and removing the WaitFor for
- * the "volatile" object.
+ * made with a setup() method adding the WaitFor for the "volatile" object.
+ * Note, however, that you don't have to remove nor delete the WaitFor added in
+ * the setup() method. It is done automatically when the step is deactivated.
*
* \code
* void StepSubclass::setup() {
@@ -115,11 +116,6 @@
* waitForAttribute = new WaitForSignal(textArea, SIGNAL(textChanged()));
* addWaitFor(waitForAttribute, this, SLOT(startDone()));
* }
- *
- * void StepSubclass::tearDown() {
- * removeWaitFor(waitForAttribute);
- * delete waitForAttribute;
- * }
* \endcode
*/
class KTUTORIAL_EXPORT Step: public QObject {
@@ -134,7 +130,8 @@
*/
explicit Step(const QString& id): QObject(),
mId(id),
- mActive(false) {
+ mActive(false),
+ mDeleteAddedObjectsInTearDown(false) {
}
/**
@@ -202,7 +199,10 @@
* Note that the slot name can be set with or without using the SLOT macro.
*
* The Option is reparented to this Step, and thus deleted when this Step
- * is deleted.
+ * is deleted. However, if the Option is added in the setup() method of a
+ * Step derived class, the Option is automatically removed and deleted when
+ * the Step is deactivated (that is, you don't have to do it explicitly in
+ * the tearDown() method).
*
* If you try to add the same Option (or different ones with the same name)
* twice, nothing will happen. Only the Option added in first place and its
@@ -224,7 +224,10 @@
* Note that the slot name can be set with or without using the SLOT macro.
*
* The WaitFor is reparented to this Step, and thus deleted when this Step
- * is deleted.
+ * is deleted. However, if the WaitFor is added in the setup() method of a
+ * Step derived class, the WaitFor is automatically removed and deleted when
+ * the Step is deactivated (that is, you don't have to do it explicitly in
+ * the tearDown() method).
*
* If you try to add the same WaitFor twice, nothing will happen. Only the
* WaitFor added in first place and its associated slot will be taken in
@@ -328,11 +331,22 @@
bool mActive;
/**
+ * When this flag is on, the conditions and conditions to wait for added are
+ * removed and deleted the next time this Step is deactivated.
+ */
+ bool mDeleteAddedObjectsInTearDown;
+
+ /**
* The Options for this Step.
*/
QList<Option*> mOptions;
/**
+ * The Options added in the setup to be deleted in the tearDown.
+ */
+ QList<Option*> mOptionsToBeDeletedInTearDown;
+
+ /**
* The conditions to wait for in each Option.
* The order of both lists is the same, so the index in the Options list
* is the index of it associated WaitFor.
@@ -344,6 +358,26 @@
*/
QList<WaitFor*> mWaitsFor;
+ /**
+ * The conditions to wait for added in the setup to be deleted in the
+ * tearDown.
+ */
+ QList<WaitFor*> mWaitsForToBeDeletedInTearDown;
+
+ /**
+ * Wraps setup method to ensure that some code is executed before and after
+ * inherited setup method.
+ * It follows a Template Design Pattern.
+ */
+ void setupWrapper();
+
+ /**
+ * Wraps tearDown method to ensure that some code is executed before and
+ * after inherited tearDown method.
+ * It follows a Template Design Pattern.
+ */
+ void tearDownWrapper();
+
};
#endif
Modified: trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2010-02-23 18:33:39 UTC (rev 100)
+++ trunk/ktutorial/ktutorial-library/src/tutorials/UsingKTutorial.cpp 2010-02-24 02:24:04 UTC (rev 101)
@@ -47,21 +47,13 @@
"Look at me! I am the text area!"));
textEdit->show();
- mWaitForTextChanged = new WaitForSignal(textEdit,
- SIGNAL(textChanged()));
- addWaitFor(mWaitForTextChanged,
+ addWaitFor(new WaitForSignal(textEdit, SIGNAL(textChanged())),
mUsingKTutorial, SLOT(clearTextTextModified()));
}
- virtual void tearDown() {
- removeWaitFor(mWaitForTextChanged);
- delete mWaitForTextChanged;
- }
-
private:
UsingKTutorial* mUsingKTutorial;
- WaitFor* mWaitForTextChanged;
};
@@ -81,17 +73,10 @@
//is closed.
textEdit->installEventFilter(this);
- mWaitForWidgetClosed = new WaitForSignal(this,
- SIGNAL(textEditClosed()));
- addWaitFor(mWaitForWidgetClosed,
+ addWaitFor(new WaitForSignal(this, SIGNAL(textEditClosed())),
mUsingKTutorial, SLOT(closeTextEditDone()));
}
- virtual void tearDown() {
- removeWaitFor(mWaitForWidgetClosed);
- delete mWaitForWidgetClosed;
- }
-
bool eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object);
if (event->type() == QEvent::Close) {
@@ -108,7 +93,6 @@
private:
UsingKTutorial* mUsingKTutorial;
- WaitFor* mWaitForWidgetClosed;
};
@@ -128,17 +112,10 @@
//tutorial is closed.
mStepWidget->installEventFilter(this);
- mWaitForMousePressed = new WaitForSignal(this,
- SIGNAL(mousePressedOnWidget()));
- addWaitFor(mWaitForMousePressed,
+ addWaitFor(new WaitForSignal(this, SIGNAL(mousePressedOnWidget())),
mUsingKTutorial, SLOT(moveWidgetPressDone()));
}
- virtual void tearDown() {
- removeWaitFor(mWaitForMousePressed);
- delete mWaitForMousePressed;
- }
-
bool eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object);
if (event->type() == QEvent::MouseButtonPress) {
@@ -158,7 +135,6 @@
private:
UsingKTutorial* mUsingKTutorial;
- WaitFor* mWaitForMousePressed;
};
@@ -178,17 +154,10 @@
//tutorial is closed.
mStepWidget->installEventFilter(this);
- mWaitForMouseReleased = new WaitForSignal(this,
- SIGNAL(mouseReleasedOnWidget()));
- addWaitFor(mWaitForMouseReleased,
+ addWaitFor(new WaitForSignal(this, SIGNAL(mouseReleasedOnWidget())),
mUsingKTutorial, SLOT(moveWidgetReleaseDone()));
}
- virtual void tearDown() {
- removeWaitFor(mWaitForMouseReleased);
- delete mWaitForMouseReleased;
- }
-
bool eventFilter(QObject* object, QEvent* event) {
Q_UNUSED(object);
if (event->type() == QEvent::MouseButtonRelease) {
@@ -205,7 +174,6 @@
private:
UsingKTutorial* mUsingKTutorial;
- WaitFor* mWaitForMouseReleased;
};
Modified: trunk/ktutorial/ktutorial-library/test/StepTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/test/StepTest.cpp 2010-02-23 18:33:39 UTC (rev 100)
+++ trunk/ktutorial/ktutorial-library/test/StepTest.cpp 2010-02-24 02:24:04 UTC (rev 101)
@@ -1,5 +1,5 @@
/***************************************************************************
- * Copyright (C) 2008-2009 by Daniel Calviño Sánchez *
+ * Copyright (C) 2008-2010 by Daniel Calviño Sánchez *
* dan...@gm... *
* *
* This program is free software; you can redistribute it and/or modify *
@@ -70,12 +70,14 @@
void testAddOption();
void testAddOptionWithoutSlotMacro();
void testAddOptionSeveralOptions();
+ void testAddOptionDuringSetup();
void testAddOptionTwice();
void testAddOptionDifferentOptionsWithSameName();
void testAddWaitFor();
void testAddWaitForWithoutSlotMacro();
void testAddWaitForSeveralWaitFors();
+ void testAddWaitForDuringSetup();
void testAddWaitForTwice();
void testRemoveOption();
@@ -109,6 +111,48 @@
};
+class StepWithWaitForAddedInSetup: public Step {
+public:
+
+ WaitFor* mWaitFor;
+ StepTest* mStepTest;
+
+ StepWithWaitForAddedInSetup(const QString& id, StepTest* stepTest):
+ Step(id),
+ mStepTest(stepTest),
+ mWaitFor(0) {
+ }
+
+protected:
+
+ virtual void setup() {
+ mWaitFor = new WaitForSignal(mStepTest, SIGNAL(dummySignal()));
+ addWaitFor(mWaitFor, mStepTest, SLOT(dummySlot()));
+ }
+
+};
+
+class StepWithOptionAddedInSetup: public Step {
+public:
+
+ Option* mOption;
+ StepTest* mStepTest;
+
+ StepWithOptionAddedInSetup(const QString& id, StepTest* stepTest):
+ Step(id),
+ mStepTest(stepTest),
+ mOption(0) {
+ }
+
+protected:
+
+ virtual void setup() {
+ mOption = new Option("Bathe your iguana");
+ addOption(mOption, mStepTest, SLOT(dummySlot()));
+ }
+
+};
+
void StepTest::testConstructor() {
InspectedStep step("doSomethingConstructive");
@@ -226,6 +270,31 @@
QCOMPARE(mDummySlotCallCount, 3);
}
+void StepTest::testAddOptionDuringSetup() {
+ StepWithOptionAddedInSetup step("doSomethingConstructive", this);
+
+ step.setActive(true);
+
+ connect(this, SIGNAL(dummySignal()), step.mOption, SIGNAL(selected()));
+
+ QCOMPARE(step.mOption->parent(), &step);
+ QCOMPARE(step.options().count(), 1);
+ QVERIFY(step.options().contains(step.mOption));
+ QCOMPARE(mDummySlotCallCount, 0);
+
+ emit dummySignal();
+ QCOMPARE(mDummySlotCallCount, 1);
+
+ QSignalSpy destroyedSpy(step.mOption, SIGNAL(destroyed(QObject*)));
+
+ step.setActive(false);
+
+ QCOMPARE(step.options().count(), 0);
+ QCOMPARE(step.mWaitsForToBeDeletedInTearDown.count(), 0);
+ QCOMPARE(step.mOptionsToBeDeletedInTearDown.count(), 0);
+ QCOMPARE(destroyedSpy.count(), 1);
+}
+
void StepTest::testAddOptionTwice() {
Step step("doSomethingConstructive");
@@ -361,6 +430,29 @@
QCOMPARE(mDummySlotCallCount, 3);
}
+void StepTest::testAddWaitForDuringSetup() {
+ StepWithWaitForAddedInSetup step("doSomethingConstructive", this);
+
+ step.setActive(true);
+
+ QCOMPARE(step.mWaitFor->parent(), &step);
+ QCOMPARE(step.mWaitsFor.count(), 1);
+ QVERIFY(step.mWaitsFor.contains(step.mWaitFor));
+ QCOMPARE(mDummySlotCallCount, 0);
+
+ emit dummySignal();
+ QCOMPARE(mDummySlotCallCount, 1);
+
+ QSignalSpy destroyedSpy(step.mWaitFor, SIGNAL(destroyed(QObject*)));
+
+ step.setActive(false);
+
+ QCOMPARE(step.mWaitsFor.count(), 0);
+ QCOMPARE(step.mWaitsForToBeDeletedInTearDown.count(), 0);
+ QCOMPARE(step.mOptionsToBeDeletedInTearDown.count(), 0);
+ QCOMPARE(destroyedSpy.count(), 1);
+}
+
void StepTest::testAddWaitForTwice() {
Step step("doSomethingConstructive");
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|