首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试QStateMachine?

如何测试QStateMachine?
EN

Stack Overflow用户
提问于 2014-06-10 18:47:20
回答 2查看 2.5K关注 0票数 2

我对如何测试QStateMachine感到有点困惑。我有一个项目组织得很好,一边是源代码,另一边是测试代码。

标题

代码语言:javascript
复制
class Foo
{
    signals:
        void sigGoToStateOne();
        void sigGoToStateTwo();
        void sigGoToStateThree();

    private:
        QStateMachine *stateMachine;
        QState *state1;
        QState *state2;

        void initStateMachine();
}

在源文件中

代码语言:javascript
复制
Foo::initStateMachine()
{
    // constructors
    state1->addTransition(this,SIGNAL(sigGoToStateTwo()),this->state2);
    state2->addTransition(this,SIGNAL(sigGoToStateOne()),this->state1);
}

我想知道是否有一种很好的方法来测试我的stateMachine是否正确。换句话说,如果我在状态机上发出sigGoToStateThree(),那么状态机是如何反应的,等等。

我看到的解决方案:1-获取stateMachine的地址(并最终获得所有其他状态)并测试它(但我不知道如何)2-从测试文件中模拟信号(sigGoToStateX()) (同样,不知道是否可以在另一个类中发出我的类Foo的信号)

我唯一的要求是我不想修改源文件的核心。

谢谢已经提前了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-10 21:33:43

在Qt 5中,信号总是公共方法。要使代码与Qt 4兼容,可以将信号显式地公开如下:

代码语言:javascript
复制
class Foo {
public:
  Q_SIGNAL void sigGoToStateOne();
  ...
}

或者,您可以保持任意信号的可见性,并声明一个朋友测试类:

代码语言:javascript
复制
class Foo {
  friend class FooTest;
  ...
}

最后,您可以创建一个测试项目,其中使用Qt的测试框架测试Foo类的行为。下面的代码在Qt4和Qt5中都有效。

代码语言:javascript
复制
// main.cpp
#include <QCoreApplication>
#include <QStateMachine>
#include <QEventLoop>
#include <QtTest>
#include <QTimer>

class Waiter {
   QTimer m_timer;
public:
   Waiter() {}
   Waiter(QObject * obj, const char * signal) {
      m_timer.connect(obj, signal, SIGNAL(timeout()));
   }
   void stop() {
      m_timer.stop();
      QMetaObject::invokeMethod(&m_timer, "timeout");
   }
   void wait(int timeout = 5000) {
      QEventLoop loop;
      m_timer.start(timeout);
      loop.connect(&m_timer, SIGNAL(timeout()), SLOT(quit()));
      loop.exec();
   }
};

class SignalWaiter : public QObject, public Waiter {
   Q_OBJECT
   int m_count;
   Q_SLOT void triggered() {
      ++ m_count;
      stop();
   }
public:
   SignalWaiter(QObject * obj, const char * signal) : m_count(0) {
      connect(obj, signal, SLOT(triggered()), Qt::QueuedConnection);
   }
   int count() const { return m_count; }
};

#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
typedef QSignalSpy SignalSpy;
#else
class SignalSpy : public QSignalSpy, public Waiter {
public:
   SignalSpy(QObject * obj, const char * signal) :
      QSignalSpy(obj, signal), Waiter(obj, signal) {}
};
#endif

class Foo : public QObject {
   Q_OBJECT
   friend class FooTest;
   QStateMachine m_stateMachine;
   QState m_state1;
   QState m_state2;
   Q_SIGNAL void sigGoToStateOne();
   Q_SIGNAL void sigGoToStateTwo();
public:
   explicit Foo(QObject * parent = 0) :
      QObject(parent),
      m_state1(&m_stateMachine),
      m_state2(&m_stateMachine)
   {
      m_stateMachine.setInitialState(&m_state1);
      m_state1.addTransition(this, SIGNAL(sigGoToStateTwo()), &m_state2);
      m_state2.addTransition(this, SIGNAL(sigGoToStateOne()), &m_state1);
   }
   Q_SLOT void start() {
      m_stateMachine.start();
   }
};

class FooTest : public QObject {
   Q_OBJECT
   void call(QObject * obj, const char * method) {
      QMetaObject::invokeMethod(obj, method, Qt::QueuedConnection);
   }
   Q_SLOT void test1() {
      // Uses QSignalSpy
      Foo foo;
      SignalSpy state1(&foo.m_state1, SIGNAL(entered()));
      SignalSpy state2(&foo.m_state2, SIGNAL(entered()));
      call(&foo, "start");
      state1.wait();
      QCOMPARE(state1.count(), 1);
      call(&foo, "sigGoToStateTwo");
      state2.wait();
      QCOMPARE(state2.count(), 1);
      call(&foo, "sigGoToStateOne");
      state1.wait();
      QCOMPARE(state1.count(), 2);
   }

   Q_SLOT void test2() {
      // Uses SignalWaiter
      Foo foo;
      SignalWaiter state1(&foo.m_state1, SIGNAL(entered()));
      SignalWaiter state2(&foo.m_state2, SIGNAL(entered()));
      foo.start();
      state1.wait();
      QCOMPARE(state1.count(), 1);
      emit foo.sigGoToStateTwo();
      state2.wait();
      QCOMPARE(state2.count(), 1);
      emit foo.sigGoToStateOne();
      state1.wait();
      QCOMPARE(state1.count(), 2);
   }
};

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   FooTest test;
   QTest::qExec(&test, a.arguments());
   QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
   return a.exec();
}

#include "main.moc"

我强制从事件循环执行所有的信号调用,这样事件转换只能在事件循环运行时发生。这使得测试代码在每次转换后一致等待。否则,第二个wait将超时:

代码语言:javascript
复制
Q_SLOT void test1() {
   SignalSpy state1(&m_foo.m_state1, SIGNAL(entered()));
   SignalSpy state2(&m_foo.m_state2, SIGNAL(entered()));
   m_foo.start();
   state1.wait();
   QCOMPARE(state1.count(), 1);
   emit m_foo.sigGoToStateTwo(); // The state2.entered() signal is emitted here.
   state2.wait(); // But we wait for it here, and this wait will time out.
   QCOMPARE(state2.count(), 1); // But of course the count will match.
   emit m_foo.sigGoToStateOne();
   state1.wait(); // This would timeout as well.
   QCOMPARE(state1.count(), 2);
}

这可以通过使用内部使用排队连接的信号间谍类来实现,而无需使用显式排队调用。

票数 4
EN

Stack Overflow用户

发布于 2014-06-11 01:01:55

Kuba很好地分析了如何使用测试框架& SignalSpy对您的状态机进行深入测试。

如果您所要做的只是从一个测试文件生成一个sigGoToStateX(),那么不要忘记,您可以将信号链在一起。

例如,给定一个类"Tester":

代码语言:javascript
复制
class Tester : public QObject {
Q_OBJECT
public:
    Tester(Foo *fooClass) {
        //Connecting signals gives you the kind of behaviour you were asking about
        connect(this, SIGNAL(testTransitionToState1()), fooClass, SIGNAL(sigGoToState1()));
        connect(this, SIGNAL(testTransitionToState2()), fooClass, SIGNAL(sigGoToState2()));
        connect(this, SIGNAL(testTransitionToState3()), fooClass, SIGNAL(sigGoToState3()));
    }

    void SwitchState(int newState) {
        //Now any time we emit the test signals, the foo class's signals will be emitted too!
        if (newState == 1) emit testTransitionToState1();
        else if (newState == 2) emit testTransitionToState1();
        else if (newState == 3) emit testTransitionToState1();
    }

signals:
    void testTransitionToState1();
    void testTransitionToState2();
    void testTransitionToState3();
}

例如,调用SwitchState( 1 )将调用正确的信号以切换到状态1,如果这个简单的情况是测试所需的全部,那么这就是您真正需要的。

如果您需要更复杂的内容,请使用完整的SignalSpy示例。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24148670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档