summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Craig <ext-chris.craig@nokia.com>2012-05-29 15:47:06 -0400
committerChris Craig <ext-chris.craig@nokia.com>2012-05-30 23:38:51 +0200
commit2beb33bc03b3b83aeaf83e1a5b0919c5f1ab3bef (patch)
tree9ae23bc5a860fad5c682bec008bf2d07e8e7a918
parentb19fec0ecd76e36f21283c75c2ddbf265f6a11b5 (diff)
handle endpoint name change
Change-Id: I53800873d9929d4b6f8b967327974ed916b8fc48 Reviewed-by: Alexei Rousskikh <ext-alexei.rousskikh@nokia.com> Reviewed-by: Chris Craig <ext-chris.craig@nokia.com>
-rw-r--r--src/jsonendpoint.cpp3
-rw-r--r--src/jsonendpoint.h7
-rw-r--r--src/jsonendpointmanager.cpp6
-rw-r--r--src/jsonendpointmanager_p.h3
-rw-r--r--tests/auto/jsonconnection/tst_jsonconnection.cpp37
5 files changed, 54 insertions, 2 deletions
diff --git a/src/jsonendpoint.cpp b/src/jsonendpoint.cpp
index 4825708..5ef840b 100644
--- a/src/jsonendpoint.cpp
+++ b/src/jsonendpoint.cpp
@@ -115,6 +115,7 @@ void JsonEndpoint::setName( const QString & name )
{
Q_D(JsonEndpoint);
d->mName = name;
+ emit nameChanged();
}
/*!
@@ -135,6 +136,8 @@ void JsonEndpoint::setConnection(JsonConnection *connection)
d->mConnection = connection;
if (d->mConnection)
d->mConnection->addEndpoint(this);
+
+ emit connectionChanged();
}
/*!
diff --git a/src/jsonendpoint.h b/src/jsonendpoint.h
index 26b40a0..00880cf 100644
--- a/src/jsonendpoint.h
+++ b/src/jsonendpoint.h
@@ -54,8 +54,8 @@ class JsonEndpointPrivate;
class Q_ADDON_JSONSTREAM_EXPORT JsonEndpoint : public QObject
{
Q_OBJECT
- Q_PROPERTY(QString name READ name WRITE setName)
- Q_PROPERTY(JsonConnection* connection READ connection WRITE setConnection)
+ Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
+ Q_PROPERTY(JsonConnection* connection READ connection WRITE setConnection NOTIFY connectionChanged)
public:
JsonEndpoint(const QString & = QString::null, JsonConnection * = 0);
virtual ~JsonEndpoint();
@@ -75,6 +75,9 @@ public:
Q_INVOKABLE QVariantMap readMessageMap();
signals:
+ void nameChanged();
+ void connectionChanged();
+
void readyReadMessage();
protected slots:
diff --git a/src/jsonendpointmanager.cpp b/src/jsonendpointmanager.cpp
index 93038a8..5d266c2 100644
--- a/src/jsonendpointmanager.cpp
+++ b/src/jsonendpointmanager.cpp
@@ -98,6 +98,7 @@ void JsonEndpointManager::addEndpoint(JsonEndpoint *endpoint)
if (mEndpoints.key(endpoint).isEmpty()) {
mInit = false; // rehashing required
mEndpoints.insert(QString::number((ulong)endpoint), endpoint);
+ connect(endpoint, SIGNAL(nameChanged()), SLOT(handleNameChange()));
}
}
@@ -135,6 +136,11 @@ void JsonEndpointManager::clear()
mEndpoints.clear();
}
+void JsonEndpointManager::handleNameChange()
+{
+ mInit = false; // next call to endpoints() will rehash everything
+}
+
#include "moc_jsonendpointmanager_p.cpp"
QT_END_NAMESPACE_JSONSTREAM
diff --git a/src/jsonendpointmanager_p.h b/src/jsonendpointmanager_p.h
index 4502417..7ad3ac8 100644
--- a/src/jsonendpointmanager_p.h
+++ b/src/jsonendpointmanager_p.h
@@ -75,6 +75,9 @@ public:
virtual JsonEndpoint *endpoint(const QJsonObject &);
+protected slots:
+ void handleNameChange();
+
protected:
bool mInit;
QString mEndpointPropertyName;
diff --git a/tests/auto/jsonconnection/tst_jsonconnection.cpp b/tests/auto/jsonconnection/tst_jsonconnection.cpp
index b32c7d2..458022f 100644
--- a/tests/auto/jsonconnection/tst_jsonconnection.cpp
+++ b/tests/auto/jsonconnection/tst_jsonconnection.cpp
@@ -307,6 +307,7 @@ private slots:
void multipleEndpointsTest();
void multipleThreadTest();
void autoreconnectTest();
+ void nameChangeTest();
private:
void registerQmlTypes();
@@ -857,6 +858,42 @@ void tst_JsonConnection::autoreconnectTest()
child.waitForFinished();
}
+void tst_JsonConnection::nameChangeTest()
+{
+ QString socketname = "/tmp/tst_socket";
+
+ Child child("testClient/testClient",
+ QStringList() << "-socket" << socketname);
+
+ QSignalSpy spy0(&child, SIGNAL(serverReady()));
+ waitForSpy(spy0, 1);
+
+ ConnectionContainer c(socketname,true);
+ c.connection()->setAutoReconnectEnabled(true);
+ QSignalSpy spyDef(&c, SIGNAL(messageReceived(const QJsonObject&, QObject *)));;
+
+ JsonEndpoint *endpoint = c.addEndpoint("wrong");
+ QVERIFY(endpoint->name() == "wrong");
+
+ QVERIFY(c.connection()->state() == JsonConnection::Unconnected);
+ c.doConnect();
+ QVERIFY(c.connection()->state() == JsonConnection::Connected);
+
+ c.sendMessage("test");
+
+ waitForSpy(spyDef, 1);
+ QObject *source = (spyDef.at(0).at(1)).value<QObject *>();
+ QVERIFY(source == c.connection()->defaultEndpoint());
+ spyDef.clear();
+
+ endpoint->setName("test");
+ c.sendMessage("test");
+
+ waitForSpy(spyDef,1);
+ source = (spyDef.at(0).at(1)).value<QObject *>();
+ QVERIFY(source == endpoint);
+}
+
QTEST_MAIN
(tst_JsonConnection)