tests/auto/qdbusthreading/tst_qdbusthreading.cpp
changeset 37 758a864f9613
parent 18 2f34d5167611
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    68     QSemaphore threadJoin;
    68     QSemaphore threadJoin;
    69 public:
    69 public:
    70     QSemaphore sem1, sem2;
    70     QSemaphore sem1, sem2;
    71     volatile bool success;
    71     volatile bool success;
    72     QEventLoop *loop;
    72     QEventLoop *loop;
    73     const char *functionSpy;
    73     enum FunctionSpy {
       
    74         NoMethod = 0,
       
    75         Adaptor_method,
       
    76         Object_method
       
    77     } functionSpy;
       
    78 
    74     QThread *threadSpy;
    79     QThread *threadSpy;
    75     int signalSpy;
    80     int signalSpy;
    76 
    81 
    77     tst_QDBusThreading();
    82     tst_QDBusThreading();
    78     static inline tst_QDBusThreading *self() { return _self; }
    83     static inline tst_QDBusThreading *self() { return _self; }
   125     }
   130     }
   126 
   131 
   127 public Q_SLOTS:
   132 public Q_SLOTS:
   128     void method()
   133     void method()
   129     {
   134     {
   130         tst_QDBusThreading::self()->functionSpy = Q_FUNC_INFO;
   135         tst_QDBusThreading::self()->functionSpy = tst_QDBusThreading::Adaptor_method;
   131         tst_QDBusThreading::self()->threadSpy = QThread::currentThread();
   136         tst_QDBusThreading::self()->threadSpy = QThread::currentThread();
   132         emit signal();
   137         emit signal();
   133     }
   138     }
   134 
   139 
   135 Q_SIGNALS:
   140 Q_SIGNALS:
   153     }
   158     }
   154 
   159 
   155 public Q_SLOTS:
   160 public Q_SLOTS:
   156     void method()
   161     void method()
   157     {
   162     {
   158         tst_QDBusThreading::self()->functionSpy = Q_FUNC_INFO;
   163         tst_QDBusThreading::self()->functionSpy = tst_QDBusThreading::Object_method;
   159         tst_QDBusThreading::self()->threadSpy = QThread::currentThread();
   164         tst_QDBusThreading::self()->threadSpy = QThread::currentThread();
   160         emit signal();
   165         emit signal();
   161         deleteLater();
   166         deleteLater();
   162     }
   167     }
   163 
   168 
   196 }
   201 }
   197 
   202 
   198 static const char myConnectionName[] = "connection";
   203 static const char myConnectionName[] = "connection";
   199 
   204 
   200 tst_QDBusThreading::tst_QDBusThreading()
   205 tst_QDBusThreading::tst_QDBusThreading()
   201     : loop(0), functionSpy(0), threadSpy(0)
   206     : loop(0), functionSpy(NoMethod), threadSpy(0)
   202 {
   207 {
   203     _self = this;
   208     _self = this;
   204     QCoreApplication::instance()->thread()->setObjectName("Main thread");
   209     QCoreApplication::instance()->thread()->setObjectName("Main thread");
   205 }
   210 }
   206 
   211 
   418     connect(&iface, SIGNAL(signal()), SLOT(signalSpySlot()));
   423     connect(&iface, SIGNAL(signal()), SLOT(signalSpySlot()));
   419 
   424 
   420     QTest::qWait(100);
   425     QTest::qWait(100);
   421     QCOMPARE(signalSpy, 0);
   426     QCOMPARE(signalSpy, 0);
   422 
   427 
   423     functionSpy = 0;
   428     functionSpy = NoMethod;
   424     threadSpy = 0;
   429     threadSpy = 0;
   425     QDBusReply<void> reply = iface.call("method");
   430     QDBusReply<void> reply = iface.call("method");
   426     QVERIFY(reply.isValid());
   431     QVERIFY(reply.isValid());
   427     QCOMPARE(functionSpy, "void Object::method()");
   432     QCOMPARE(functionSpy, Object_method);
   428     QCOMPARE(threadSpy, th);
   433     QCOMPARE(threadSpy, th);
   429 
   434 
   430     QTest::qWait(100);
   435     QTest::qWait(100);
   431     QCOMPARE(signalSpy, 1);
   436     QCOMPARE(signalSpy, 1);
   432 
   437 
   433     sem2.acquire();             // the object is gone
   438     sem2.acquire();             // the object is gone
   434     functionSpy = 0;
   439     functionSpy = NoMethod;
   435     threadSpy = 0;
   440     threadSpy = 0;
   436     reply = iface.call("method");
   441     reply = iface.call("method");
   437     QVERIFY(!reply.isValid());
   442     QVERIFY(!reply.isValid());
   438     QCOMPARE(functionSpy, (const char*)0);
   443     QCOMPARE(functionSpy, NoMethod);
   439     QCOMPARE(threadSpy, (QThread*)0);
   444     QCOMPARE(threadSpy, (QThread*)0);
   440 }
   445 }
   441 
   446 
   442 void tst_QDBusThreading::registerAdaptorInOtherThread_thread()
   447 void tst_QDBusThreading::registerAdaptorInOtherThread_thread()
   443 {
   448 {
   466 
   471 
   467     signalSpy = 0;
   472     signalSpy = 0;
   468     connect(&adaptor, SIGNAL(signal()), SLOT(signalSpySlot()));
   473     connect(&adaptor, SIGNAL(signal()), SLOT(signalSpySlot()));
   469     QCOMPARE(signalSpy, 0);
   474     QCOMPARE(signalSpy, 0);
   470 
   475 
   471     functionSpy = 0;
   476     functionSpy = NoMethod;
   472     threadSpy = 0;
   477     threadSpy = 0;
   473     QDBusReply<void> reply = adaptor.call("method");
   478     QDBusReply<void> reply = adaptor.call("method");
   474     QVERIFY(reply.isValid());
   479     QVERIFY(reply.isValid());
   475     QCOMPARE(functionSpy, "void Adaptor::method()");
   480     QCOMPARE(functionSpy, Adaptor_method);
   476     QCOMPARE(threadSpy, th);
   481     QCOMPARE(threadSpy, th);
   477 
   482 
   478     QTest::qWait(100);
   483     QTest::qWait(100);
   479     QCOMPARE(signalSpy, 1);
   484     QCOMPARE(signalSpy, 1);
   480 
   485 
   481     functionSpy = 0;
   486     functionSpy = NoMethod;
   482     threadSpy = 0;
   487     threadSpy = 0;
   483     reply = object.call("method");
   488     reply = object.call("method");
   484     QVERIFY(reply.isValid());
   489     QVERIFY(reply.isValid());
   485     QCOMPARE(functionSpy, "void Object::method()");
   490     QCOMPARE(functionSpy, Object_method);
   486     QCOMPARE(threadSpy, th);
   491     QCOMPARE(threadSpy, th);
   487 
   492 
   488     QTest::qWait(100);
   493     QTest::qWait(100);
   489     QCOMPARE(signalSpy, 1);
   494     QCOMPARE(signalSpy, 1);
   490 
   495 
   491     sem2.acquire();             // the object is gone
   496     sem2.acquire();             // the object is gone
   492     functionSpy = 0;
   497     functionSpy = NoMethod;
   493     threadSpy = 0;
   498     threadSpy = 0;
   494     reply = adaptor.call("method");
   499     reply = adaptor.call("method");
   495     QVERIFY(!reply.isValid());
   500     QVERIFY(!reply.isValid());
   496     QCOMPARE(functionSpy, (const char*)0);
   501     QCOMPARE(functionSpy, NoMethod);
   497     QCOMPARE(threadSpy, (QThread*)0);
   502     QCOMPARE(threadSpy, (QThread*)0);
   498     reply = object.call("method");
   503     reply = object.call("method");
   499     QVERIFY(!reply.isValid());
   504     QVERIFY(!reply.isValid());
   500     QCOMPARE(functionSpy, (const char*)0);
   505     QCOMPARE(functionSpy, NoMethod);
   501     QCOMPARE(threadSpy, (QThread*)0);
   506     QCOMPARE(threadSpy, (QThread*)0);
   502 }
   507 }
   503 
   508 
   504 void tst_QDBusThreading::callbackInMainThread_thread()
   509 void tst_QDBusThreading::callbackInMainThread_thread()
   505 {
   510 {