1 #ifndef _TelepathyQt_tests_lib_test_h_HEADER_GUARD_
2 #define _TelepathyQt_tests_lib_test_h_HEADER_GUARD_
3 
4 #include <QtDBus>
5 #include <QtTest>
6 
7 #include <TelepathyQt/PendingOperation>
8 #include <TelepathyQt/PendingVariant>
9 #include <TelepathyQt/Constants>
10 
11 namespace Tp
12 {
13 class DBusProxy;
14 }
15 
16 class Test : public QObject
17 {
18     Q_OBJECT
19 
20 public:
21 
22     Test(QObject *parent = 0);
23 
24     virtual ~Test();
25 
26     QEventLoop *mLoop;
27     void processDBusQueue(Tp::DBusProxy *proxy);
28 
29     // The last error received in expectFailure()
30     QString mLastError;
31     QString mLastErrorMessage;
32 
33 protected:
34     template<typename T> bool waitForProperty(Tp::PendingVariant *pv, T *value);
35 
36 protected Q_SLOTS:
37     void expectSuccessfulCall(QDBusPendingCallWatcher*);
38     void expectSuccessfulCall(Tp::PendingOperation*);
39     void expectFailure(Tp::PendingOperation*);
40     void expectSuccessfulProperty(Tp::PendingOperation *op);
41     void onWatchdog();
42 
43     virtual void initTestCaseImpl();
44     virtual void initImpl();
45 
46     virtual void cleanupImpl();
47     virtual void cleanupTestCaseImpl();
48 
49 private:
50     // The property retrieved by expectSuccessfulProperty()
51     QVariant mPropertyValue;
52 };
53 
54 template<typename T>
waitForProperty(Tp::PendingVariant * pv,T * value)55 bool Test::waitForProperty(Tp::PendingVariant *pv, T *value)
56 {
57     connect(pv,
58             SIGNAL(finished(Tp::PendingOperation*)),
59             SLOT(expectSuccessfulProperty(Tp::PendingOperation*)));
60     if (mLoop->exec() == 1000) {
61         *value = qdbus_cast<T>(mPropertyValue);
62         return true;
63     }
64     else {
65         *value = T();
66         return false;
67     }
68 }
69 
70 #define TEST_VERIFY_OP(op) \
71     if (!op->isFinished()) { \
72         qWarning() << "unfinished"; \
73         mLoop->exit(1); \
74         return; \
75     } \
76     if (op->isError()) { \
77         qWarning().nospace() << op->errorName() << ": " << op->errorMessage(); \
78         mLoop->exit(2); \
79         return; \
80     } \
81     if (!op->isValid()) { \
82         qWarning() << "inconsistent results"; \
83         mLoop->exit(3); \
84         return; \
85     } \
86     qDebug() << "finished";
87 
88 #endif // _TelepathyQt_tests_lib_test_h_HEADER_GUARD_
89