tests: Extend SignalProxyTest to cover request/receive method pairs
[quassel.git] / tests / common / signalproxytest.cpp
index a6b91fa..4ac47eb 100644 (file)
@@ -100,6 +100,7 @@ TEST_F(SignalProxyTest, attachSignal)
         EXPECT_CALL(*_clientPeer, Dispatches(RpcCall(Eq(SIGNAL(sendData(int,QString))), ElementsAre(42, "Hello"))));
         EXPECT_CALL(*_clientPeer, Dispatches(RpcCall(Eq(SIGNAL(sendExtraData(int,QString))), ElementsAre(42, "Hello"))));
         EXPECT_CALL(*_serverPeer, Dispatches(RpcCall(Eq("2sendData(int,QString)"), ElementsAre(23, "World"))));
+        EXPECT_CALL(*_serverPeer, Dispatches(RpcCall(Eq("2sendToFunctor(int,QString)"), ElementsAre(17, "Hi Universe"))));
     }
 
     ProxyObject::Spy clientSpy, serverSpy;
@@ -107,14 +108,21 @@ TEST_F(SignalProxyTest, attachSignal)
     ProxyObject serverObject{&serverSpy, _serverPeer};
 
     // Deliberately not normalize some of the macro invocations
-    _clientProxy.attachSignal(&clientObject, SIGNAL(sendData(int, const QString&)));
-    _serverProxy.attachSlot(SIGNAL(sendData(int,QString)), &serverObject, SLOT(receiveData(int, const QString&)));
+    _clientProxy.attachSignal(&clientObject, &ProxyObject::sendData);
+    _serverProxy.attachSlot(SIGNAL(sendData(int,QString)), &serverObject, &ProxyObject::receiveData);
 
-    _clientProxy.attachSignal(&clientObject, SIGNAL(sendMoreData(int,QString)), SIGNAL(sendExtraData(int,QString)));
-    _serverProxy.attachSlot(SIGNAL(sendExtraData(int, const QString&)), &serverObject, SLOT(receiveExtraData(int,QString)));
+    _clientProxy.attachSignal(&clientObject, &ProxyObject::sendMoreData, SIGNAL(sendExtraData(int, const QString&)));
+    _serverProxy.attachSlot(SIGNAL(sendExtraData(int, const QString&)), &serverObject, &ProxyObject::receiveExtraData);
 
-    _serverProxy.attachSignal(&serverObject, SIGNAL(sendData(int,QString)));
-    _clientProxy.attachSlot(SIGNAL(sendData(int, const QString&)), &clientObject, SLOT(receiveData(int,QString)));
+    _serverProxy.attachSignal(&serverObject, &ProxyObject::sendData);
+    //_clientProxy.attachSlot(SIGNAL(sendData(int, const QString&)), &clientObject, SLOT(receiveData(int,QString)));
+    _clientProxy.attachSlot(SIGNAL(sendData(int, const QString&)), &clientObject, &ProxyObject::receiveData);
+
+    _serverProxy.attachSignal(&serverObject, &ProxyObject::sendToFunctor);
+    _clientProxy.attachSlot(SIGNAL(sendToFunctor(int, const QString&)), this, [this, &clientSpy](int i, const QString& s) {
+        EXPECT_EQ(_clientPeer, SignalProxy::current()->sourcePeer());
+        clientSpy.notify(std::make_pair(i, s));
+    });
 
     emit clientObject.sendData(42, "Hello");
     ASSERT_TRUE(serverSpy.wait());
@@ -127,6 +135,10 @@ TEST_F(SignalProxyTest, attachSignal)
     emit serverObject.sendData(23, "World");
     ASSERT_TRUE(clientSpy.wait());
     EXPECT_EQ(ProxyObject::Data(23, "World"), clientSpy.value());
+
+    emit serverObject.sendToFunctor(17, "Hi Universe");
+    ASSERT_TRUE(clientSpy.wait());
+    EXPECT_EQ(ProxyObject::Data(17, "Hi Universe"), clientSpy.value());
 }
 
 // -----------------------------------------------------------------------------------------------------------------------------------------
@@ -208,19 +220,45 @@ public slots:
         emit syncMethodCalled(intArg, stringArg);
     }
 
-    void requestMethod(const QString& stringArg, int intArg)
+    int requestInt(const QString& stringArg, int intArg)
+    {
+        REQUEST(ARG(stringArg), ARG(intArg));
+        REQUEST_OTHER(setIntProperty, ARG(intArg));
+        emit requestMethodCalled(stringArg, intArg);
+        return -intArg;
+    }
+
+    QString requestString(const QString& stringArg, int intArg)
     {
         REQUEST(ARG(stringArg), ARG(intArg));
         REQUEST_OTHER(setStringProperty, ARG(stringArg));
         emit requestMethodCalled(stringArg, intArg);
+        return stringArg.toUpper();
     }
 
+    // Receive methods can either have the full signature of the request method + return value, or...
+    void receiveInt(const QString&, int, int reply)
+    {
+        _intProperty = reply;
+        emit intReceived(reply);
+    }
+
+    // ... only the return value
+    void receiveString(const QString& reply)
+    {
+        _stringProperty = reply;
+        emit stringReceived(reply);
+    }
+
+
 signals:
     void intPropertyChanged(int);
     void stringPropertyChanged(const QString&);
     void fooDataChanged(const QByteArray&);
     void syncMethodCalled(int, const QString&);
     void requestMethodCalled(const QString&, int);
+    void intReceived(int);
+    void stringReceived(const QString&);
 
 private:
     int _intProperty{};
@@ -254,7 +292,12 @@ TEST_F(SignalProxyTest, syncableObject)
         EXPECT_CALL(*_serverPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("setStringProperty"), ElementsAre("World"))));
 
         // Request method
-        EXPECT_CALL(*_clientPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("requestMethod"), ElementsAre("Hello", 23))));
+        EXPECT_CALL(*_clientPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("requestInt"), ElementsAre("Hello", 23))));
+        EXPECT_CALL(*_serverPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("receiveInt"), ElementsAre("Hello", 23, -23))));
+        EXPECT_CALL(*_clientPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("setIntProperty"), ElementsAre(23))));
+
+        EXPECT_CALL(*_clientPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("requestString"), ElementsAre("Hello", 23))));
+        EXPECT_CALL(*_serverPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("receiveString"), ElementsAre("HELLO"))));
         EXPECT_CALL(*_clientPeer, Dispatches(SyncMessage(Eq("SyncObj"), Eq("Foo"), Eq("setStringProperty"), ElementsAre("Hello"))));
 
         // Update properties (twice)
@@ -323,10 +366,17 @@ TEST_F(SignalProxyTest, syncableObject)
 
     // -- Request method
 
-    spy.connect(&serverObject, &SyncObj::requestMethodCalled);
-    clientObject.requestMethod("Hello", 23);
+    spy.connect(&clientObject, &SyncObj::intReceived);
+    clientObject.requestInt("Hello", 23);
+    ASSERT_TRUE(spy.wait());
+    EXPECT_EQ(23, serverObject.intProperty());
+    EXPECT_EQ(-23, clientObject.intProperty());
+
+    spy.connect(&clientObject, &SyncObj::stringReceived);
+    clientObject.requestString("Hello", 23);
     ASSERT_TRUE(spy.wait());
     EXPECT_EQ("Hello", serverObject.stringProperty());
+    EXPECT_EQ("HELLO", clientObject.stringProperty());
 
     // -- Property update