[Ktutorial-commits] SF.net SVN: ktutorial:[310] trunk/ktutorial/ktutorial-library
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2011-05-24 14:42:01
|
Revision: 310
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=310&view=rev
Author: danxuliu
Date: 2011-05-24 14:41:54 +0000 (Tue, 24 May 2011)
Log Message:
-----------
Add WaitForStepActivation class to wait for its step to be activated.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-library/src/CMakeLists.txt
trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp
trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt
trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingModuleTest.cpp
trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingTest.cpp
Added Paths:
-----------
trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.cpp
trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.h
trunk/ktutorial/ktutorial-library/tests/WaitForStepActivationTest.cpp
Modified: trunk/ktutorial/ktutorial-library/src/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2011-05-24 14:33:07 UTC (rev 309)
+++ trunk/ktutorial/ktutorial-library/src/CMakeLists.txt 2011-05-24 14:41:54 UTC (rev 310)
@@ -30,6 +30,7 @@
WaitForOr.cpp
WaitForProperty.cpp
WaitForSignal.cpp
+ WaitForStepActivation.cpp
WaitForWindow.cpp
)
@@ -61,6 +62,7 @@
WaitForOr.h
WaitForProperty.h
WaitForSignal.h
+ WaitForStepActivation.h
WaitForWindow.h
)
Added: trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.cpp 2011-05-24 14:41:54 UTC (rev 310)
@@ -0,0 +1,70 @@
+/***************************************************************************
+ * Copyright (C) 2011 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 "WaitForStepActivation.h"
+
+#include <QEvent>
+
+#include <KDebug>
+
+#include "Tutorial.h"
+
+//public:
+
+WaitForStepActivation::WaitForStepActivation(): WaitFor(),
+ mDuringStepActivation(false) {
+}
+
+WaitForStepActivation::WaitForStepActivation(const Tutorial* tutorial,
+ const Step* step): WaitFor(),
+ mDuringStepActivation(false) {
+ setStep(tutorial, step);
+}
+
+void WaitForStepActivation::setStep(const Tutorial* tutorial,
+ const Step* step) {
+ if (!tutorial) {
+ kWarning() << "The tutorial that contains the step to wait for its"
+ << "activation is null!";
+ return;
+ }
+
+ if (!step) {
+ kWarning() << "The step to wait for its activation is null!";
+ return;
+ }
+
+ connect(tutorial, SIGNAL(stepActivated(Step*)),
+ this, SLOT(checkStepActivatedToEndTheWait(Step*)));
+}
+
+bool WaitForStepActivation::conditionMet() const {
+ return mDuringStepActivation;
+}
+
+//private slots:
+
+void WaitForStepActivation::checkStepActivatedToEndTheWait(Step* step) {
+ if (!isActive()) {
+ return;
+ }
+
+ mDuringStepActivation = true;
+ emit waitEnded(this);
+ mDuringStepActivation = false;
+}
Property changes on: trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Added: trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.h
===================================================================
--- trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.h (rev 0)
+++ trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.h 2011-05-24 14:41:54 UTC (rev 310)
@@ -0,0 +1,119 @@
+/***************************************************************************
+ * Copyright (C) 2011 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 WAITFORSTEPACTIVATION_H
+#define WAITFORSTEPACTIVATION_H
+
+#include "ktutorial_export.h"
+
+#include "WaitFor.h"
+
+class Step;
+class Tutorial;
+
+/**
+ * Waits for the activation of a step.
+ * When the step is activated, the wait ends.
+ *
+ * The purpose of WaitForStepActivations is to be combined with one or more
+ * WaitForProperties using a WaitForAnd. If the properties have the expected
+ * value when the step is activated, the associated slot is executed or the next
+ * step is requested (depending on the Step::addWaitFor() method used). It can
+ * be used to set "preconditions" for a step, and change to another step if
+ * some things that should have been done before that step were not done yet.
+ *
+ * Note that the condition is met only while the step is being activated. Once
+ * the step is active, the condition is no longer met (until the next time that
+ * the step is being activated again). Due to this, it only makes sense to use
+ * WaitForStepActivation with the same step that the WaitForStepActivation is
+ * going to be added to (if used to wait for the activation of another step, the
+ * WaitForStepActivation will always be inactive when that step is being
+ * activated).
+ *
+ * Despite waiting for the step activation, WaitForStepActivation can be safely
+ * created and added to a step in its setup, and several WaitForStepActivation
+ * can be added to the same step without problems.
+ *
+ * @see WaitForAnd
+ * @see WaitForProperty
+ */
+class KTUTORIAL_EXPORT WaitForStepActivation: public WaitFor {
+Q_OBJECT
+public:
+
+ /**
+ * Creates a new WaitForStepActivation.
+ * This constructor is needed to dynamically create WaitForStepActivation
+ * objects in scripts using ScriptingModule::newWaitFor(const QString&).
+ * Method setStep(const Tutorial*, const Step*) must be called to finish
+ * setting up the object. For C++ tutorials, use
+ * WaitForStepActivation(const Tutorial*, const Step*) constructor instead
+ * of this one.
+ */
+ Q_INVOKABLE WaitForStepActivation();
+
+ /**
+ * Creates a new WaitForStepActivation.
+ *
+ * @param tutorial The tutorial that contains the step.
+ * @param step The step to wait for its activation.
+ */
+ WaitForStepActivation(const Tutorial* tutorial, const Step* step);
+
+ /**
+ * Sets the step to wait for its activation.
+ * This method can be invoked from a script.
+ *
+ * In fact, you should only invoke this method from a script, and only once,
+ * to set up the object. For C++ tutorials, use
+ * WaitForStepActivation(const Tutorial*, const Step*) constructor when
+ * creating this WaitForStepActivation.
+ *
+ * @param tutorial The tutorial that contains the step.
+ * @param step The step to wait for its activation.
+ */
+ Q_INVOKABLE void setStep(const Tutorial* tutorial, const Step* step);
+
+ /**
+ * Returns true if the step is being activated.
+ * Note that true is only returned during the step activation, but not once
+ * it is already active.
+ *
+ * @return True if the step is being activated, false otherwise.
+ */
+ virtual bool conditionMet() const;
+
+private:
+
+ /**
+ * Whether the step is being activated or not.
+ */
+ bool mDuringStepActivation;
+
+private slots:
+
+ /**
+ * When the step is activated, this method notifies that the wait for the
+ * activation ended.
+ * The wait is only ended if this WaitForStepActivation is active.
+ */
+ void checkStepActivatedToEndTheWait(Step* step);
+
+};
+
+#endif
Property changes on: trunk/ktutorial/ktutorial-library/src/WaitForStepActivation.h
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2011-05-24 14:33:07 UTC (rev 309)
+++ trunk/ktutorial/ktutorial-library/src/scripting/ScriptingModule.cpp 2011-05-24 14:41:54 UTC (rev 310)
@@ -29,6 +29,7 @@
#include "../WaitForOr.h"
#include "../WaitForProperty.h"
#include "../WaitForSignal.h"
+#include "../WaitForStepActivation.h"
#include "../WaitForWindow.h"
namespace scripting {
@@ -42,6 +43,8 @@
sSelf->registerWaitForMetaObject(WaitForOr::staticMetaObject);
sSelf->registerWaitForMetaObject(WaitForProperty::staticMetaObject);
sSelf->registerWaitForMetaObject(WaitForSignal::staticMetaObject);
+ sSelf->registerWaitForMetaObject(
+ WaitForStepActivation::staticMetaObject);
sSelf->registerWaitForMetaObject(WaitForWindow::staticMetaObject);
}
Modified: trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2011-05-24 14:33:07 UTC (rev 309)
+++ trunk/ktutorial/ktutorial-library/tests/CMakeLists.txt 2011-05-24 14:41:54 UTC (rev 310)
@@ -40,6 +40,7 @@
WaitForOr
WaitForProperty
WaitForSignal
+ WaitForStepActivation
WaitForWindow
)
@@ -64,5 +65,6 @@
WaitForOr
WaitForProperty
WaitForSignal
+ WaitForStepActivation
WaitForWindow
)
Added: trunk/ktutorial/ktutorial-library/tests/WaitForStepActivationTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/WaitForStepActivationTest.cpp (rev 0)
+++ trunk/ktutorial/ktutorial-library/tests/WaitForStepActivationTest.cpp 2011-05-24 14:41:54 UTC (rev 310)
@@ -0,0 +1,179 @@
+/***************************************************************************
+ * Copyright (C) 2011 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 "WaitForStepActivation.h"
+
+#include "Step.h"
+#include "Tutorial.h"
+
+class WaitForStepActivationTest: public QObject {
+Q_OBJECT
+
+public slots:
+
+ void assertConditionMet(WaitFor* waitForToAssertItsCondition) {
+ QVERIFY(waitForToAssertItsCondition);
+ QVERIFY(waitForToAssertItsCondition->conditionMet());
+ }
+
+private slots:
+
+ void testConstructor();
+ void testConstructorWithNullTutorial();
+ void testConstructorWithNullStep();
+ void testConstructorDefault();
+ void testConstructorDefaultWithNullTutorial();
+ void testConstructorDefaultWithNullStep();
+
+ void testStepActivation();
+ void testStepActivationWhenWaitForIsInactive();
+
+};
+
+class InspectedTutorial: public Tutorial {
+public:
+
+ int mstepActivatedConnectionCount;
+
+ InspectedTutorial(TutorialInformation* tutorialInformation):
+ Tutorial(tutorialInformation),
+ mstepActivatedConnectionCount(0) {
+ }
+
+protected:
+
+ void connectNotify(const char* signal) {
+ if (QLatin1String(signal) == SIGNAL(stepActivated(Step*))) {
+ mstepActivatedConnectionCount++;
+ }
+ }
+
+};
+
+void WaitForStepActivationTest::testConstructor() {
+ InspectedTutorial tutorial(0);
+ Step step("stepName");
+ WaitForStepActivation waitForStepActivation(&tutorial, &step);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+ QCOMPARE(tutorial.mstepActivatedConnectionCount, 1);
+}
+
+void WaitForStepActivationTest::testConstructorWithNullTutorial() {
+ Step step("stepName");
+ WaitForStepActivation waitForStepActivation(0, &step);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+}
+
+void WaitForStepActivationTest::testConstructorWithNullStep() {
+ InspectedTutorial tutorial(0);
+ WaitForStepActivation waitForStepActivation(&tutorial, 0);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+ QCOMPARE(tutorial.mstepActivatedConnectionCount, 0);
+}
+
+void WaitForStepActivationTest::testConstructorDefault() {
+ InspectedTutorial tutorial(0);
+ Step step("stepName");
+ WaitForStepActivation waitForStepActivation;
+ waitForStepActivation.setStep(&tutorial, &step);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+ QCOMPARE(tutorial.mstepActivatedConnectionCount, 1);
+}
+
+void WaitForStepActivationTest::testConstructorDefaultWithNullTutorial() {
+ Step step("stepName");
+ WaitForStepActivation waitForStepActivation;
+ waitForStepActivation.setStep(0, &step);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+}
+
+void WaitForStepActivationTest::testConstructorDefaultWithNullStep() {
+ InspectedTutorial tutorial(0);
+ WaitForStepActivation waitForStepActivation;
+ waitForStepActivation.setStep(&tutorial, 0);
+
+ QVERIFY(!waitForStepActivation.isActive());
+ QVERIFY(!waitForStepActivation.conditionMet());
+ QCOMPARE(tutorial.mstepActivatedConnectionCount, 0);
+}
+
+//WaitFor* must be declared as a metatype to be used in qvariant_cast
+Q_DECLARE_METATYPE(WaitFor*);
+
+void WaitForStepActivationTest::testStepActivation() {
+ InspectedTutorial tutorial(0);
+ Step* startStep = new Step("start");
+ tutorial.addStep(startStep);
+
+ WaitForStepActivation* waitForStepActivation =
+ new WaitForStepActivation(&tutorial, startStep);
+ waitForStepActivation->setActive(true);
+
+ QVERIFY(!waitForStepActivation->conditionMet());
+
+ //WaitFor* must be registered in order to be used with QSignalSpy
+ int waitForStarType = qRegisterMetaType<WaitFor*>("WaitFor*");
+ QSignalSpy waitEndedSpy(waitForStepActivation, SIGNAL(waitEnded(WaitFor*)));
+
+ connect(waitForStepActivation, SIGNAL(waitEnded(WaitFor*)),
+ this, SLOT(assertConditionMet(WaitFor*)));
+
+ tutorial.start();
+
+ QVERIFY(!waitForStepActivation->conditionMet());
+ QCOMPARE(waitEndedSpy.count(), 1);
+ QVariant argument = waitEndedSpy.at(0).at(0);
+ QCOMPARE(argument.userType(), waitForStarType);
+ QCOMPARE(qvariant_cast<WaitFor*>(argument), waitForStepActivation);
+}
+
+//Should not happen, as WaitForStepActivation is meant to be used only with the
+//step that contains the WaitFor... but just in case ;)
+void WaitForStepActivationTest::testStepActivationWhenWaitForIsInactive() {
+ InspectedTutorial tutorial(0);
+ Step* startStep = new Step("start");
+ tutorial.addStep(startStep);
+
+ WaitForStepActivation* waitForStepActivation =
+ new WaitForStepActivation(&tutorial, startStep);
+
+ //WaitFor* must be registered in order to be used with QSignalSpy
+ qRegisterMetaType<WaitFor*>("WaitFor*");
+ QSignalSpy waitEndedSpy(waitForStepActivation, SIGNAL(waitEnded(WaitFor*)));
+
+ tutorial.start();
+
+ QVERIFY(!waitForStepActivation->conditionMet());
+ QCOMPARE(waitEndedSpy.count(), 0);
+}
+
+QTEST_MAIN(WaitForStepActivationTest)
+
+#include "WaitForStepActivationTest.moc"
Property changes on: trunk/ktutorial/ktutorial-library/tests/WaitForStepActivationTest.cpp
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingModuleTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingModuleTest.cpp 2011-05-24 14:33:07 UTC (rev 309)
+++ trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingModuleTest.cpp 2011-05-24 14:41:54 UTC (rev 310)
@@ -35,6 +35,7 @@
#include "../WaitForOr.h"
#include "../WaitForProperty.h"
#include "../WaitForSignal.h"
+#include "../WaitForStepActivation.h"
#include "../WaitForWindow.h"
class MockWaitForDefaultNamespace: public WaitFor {
@@ -178,6 +179,11 @@
type = metaObject(scriptingModule, "WaitForSignal");
QCOMPARE(type.className(), WaitForSignal::staticMetaObject.className());
+ QVERIFY(containsMetaObject(scriptingModule, "WaitForStepActivation"));
+ type = metaObject(scriptingModule, "WaitForStepActivation");
+ QCOMPARE(type.className(),
+ WaitForStepActivation::staticMetaObject.className());
+
QVERIFY(containsMetaObject(scriptingModule, "WaitForWindow"));
type = metaObject(scriptingModule, "WaitForWindow");
QCOMPARE(type.className(), WaitForWindow::staticMetaObject.className());
Modified: trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingTest.cpp 2011-05-24 14:33:07 UTC (rev 309)
+++ trunk/ktutorial/ktutorial-library/tests/scripting/ScriptingTest.cpp 2011-05-24 14:41:54 UTC (rev 310)
@@ -328,6 +328,8 @@
out << "tutorial.addStep(thirdStep);\n";
out << "fourthStep = ktutorial.newStep(\"fourth\");\n";
out << "tutorial.addStep(fourthStep);\n";
+ out << "fifthStep = ktutorial.newStep(\"fifth\");\n";
+ out << "tutorial.addStep(fifthStep);\n";
out << "endStep = ktutorial.newStep(\"end\");\n";
out << "endStep.setText(\"The tutorial has ended.\");\n";
out << "tutorial.addStep(endStep);\n";
@@ -371,8 +373,27 @@
out << "waitForIntProperty.setProperty(testObject, \"intProperty\", \
\"42\");\n";
- out << "fourthStep.addWaitFor(waitForIntProperty, \"end\");\n";
+ out << "fourthStep.addWaitFor(waitForIntProperty, \"fifth\");\n";
+ out << "function fifthStepSetup(step) {\n";
+ out << " waitForStepActivation = ktutorial.newWaitFor(\
+\"WaitForStepActivation\");\n";
+ out << " waitForStepActivation.setStep(tutorial, step);\n";
+
+ out << " waitForIntProperty = ktutorial.newWaitFor(\
+\"WaitForProperty\");\n";
+ out << " waitForIntProperty.setProperty(testObject, \"intProperty\", \
+\"42\");\n";
+
+ out << " waitForAnd = ktutorial.newWaitFor(\"WaitForAnd\");\n";
+ out << " waitForAnd.add(waitForStepActivation);\n";
+ out << " waitForAnd.add(waitForIntProperty);\n";
+ out << " step.addWaitFor(waitForAnd, \"end\");\n";
+ out << "}\n";
+
+ out << "connect(fifthStep, \"setup(QObject*)\",\n"
+ << " this, \"fifthStepSetup(QObject*)\");\n";
+
out.flush();
ScriptedTutorial scriptedTutorial(mTemporaryFile->fileName());
@@ -422,6 +443,8 @@
mIntProperty = 42;
emit intPropertyChanged();
+ //Fifth step changes to end step when activated
+
QCOMPARE(scriptedTutorial.mCurrentStep->id(), QString("end"));
QCOMPARE(scriptedTutorial.mCurrentStep->text(),
QString("The tutorial has ended."));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|