1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
// Copyright (C) 2017 Ford Motor Company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "rep_engine_merged.h"
#include "rep_subclass_merged.h"
#include "../shared/model_utilities.h"
#include <QtTest/QtTest>
#include <QRemoteObjectReplica>
#include <QRemoteObjectNode>
#include "../../shared/testutils.h"
const QUrl localHostUrl = QUrl(QLatin1String(LOCAL_SOCKET ":testHost"));
const QUrl tcpHostUrl = QUrl(QLatin1String("tcp://127.0.0.1:9989"));
const QUrl proxyNodeUrl = QUrl(QLatin1String("tcp://127.0.0.1:12123"));
const QUrl remoteNodeUrl = QUrl(QLatin1String("tcp://127.0.0.1:23234"));
const QUrl registryUrl = QUrl(QLatin1String(LOCAL_SOCKET ":testRegistry"));
const QUrl proxyHostUrl = QUrl(QLatin1String(LOCAL_SOCKET ":fromProxy"));
#define SET_NODE_NAME(obj) (obj).setName(QLatin1String(#obj))
class ProxyTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void cleanup()
{
// wait for delivery of RemoveObject events to the source
QTest::qWait(200);
}
void testProxy_data();
void testProxy();
void testForwardProxy();
void testReverseProxy();
// The following should fail to compile, verifying the SourceAPI templates work
// for subclasses
/*
void testSubclass()
{
QRemoteObjectHost host(localHostUrl);
struct invalidchild {
MyPOD myPOD() { return MyPOD(12, 13.f, QStringLiteral("Yay!")); }
};
struct badparent {
invalidchild *subClass() { return new invalidchild; }
} parent;
host.enableRemoting<ParentClassSourceAPI>(&parent);
}
*/
void testTopLevelModel();
};
void ProxyTest::testProxy_data()
{
QTest::addColumn<bool>("sourceApi");
QTest::addColumn<bool>("useProxy");
QTest::addColumn<bool>("dynamic");
QTest::newRow("dynamicApi, no proxy") << false << false << false;
QTest::newRow("sourceApi, no proxy") << true << false << false;
QTest::newRow("dynamicApi, with proxy") << false << true << false;
QTest::newRow("sourceApi, with proxy") << true << true << false;
QTest::newRow("dynamicApi, no proxy, dynamicRep") << false << false << true;
QTest::newRow("sourceApi, no proxy, dynamicRep") << true << false << true;
QTest::newRow("dynamicApi, with proxy, dynamicRep") << false << true << true;
QTest::newRow("sourceApi, with proxy, dynamicRep") << true << true << true;
}
void ProxyTest::testProxy()
{
QFETCH(bool, sourceApi);
QFETCH(bool, useProxy);
QFETCH(bool, dynamic);
//Setup Local Registry
QRemoteObjectRegistryHost registry(registryUrl);
SET_NODE_NAME(registry);
//Setup Local Host
QRemoteObjectHost host(localHostUrl);
SET_NODE_NAME(host);
host.setRegistryUrl(registryUrl);
EngineSimpleSource engine;
engine.setRpm(1234);
engine.setType(EngineSimpleSource::Gas);
if (sourceApi)
host.enableRemoting<EngineSourceAPI>(&engine);
else
host.enableRemoting(&engine);
QRemoteObjectHost proxyNode;
SET_NODE_NAME(proxyNode);
if (useProxy) {
proxyNode.setHostUrl(tcpHostUrl);
proxyNode.proxy(registryUrl);
}
//Setup Local Replica
QRemoteObjectNode client;
SET_NODE_NAME(client);
if (useProxy)
client.connectToNode(tcpHostUrl);
else
client.setRegistryUrl(registryUrl);
QScopedPointer<QRemoteObjectReplica> replica;
if (!dynamic) {
//QLoggingCategory::setFilterRules("qt.remoteobjects*=true");
replica.reset(client.acquire<EngineReplica>());
QVERIFY(replica->waitForSource(1000));
EngineReplica *rep = qobject_cast<EngineReplica *>(replica.data());
//Compare Replica to Source
QCOMPARE(rep->rpm(), engine.rpm());
QCOMPARE(EngineReplica::EngineType(rep->type()), EngineReplica::Gas);
//Change Replica and make sure change propagates to source
QSignalSpy sourceSpy(&engine, &EngineSimpleSource::rpmChanged);
QSignalSpy replicaSpy(rep, &EngineReplica::rpmChanged);
rep->pushRpm(42);
sourceSpy.wait();
QCOMPARE(sourceSpy.size(), 1);
QCOMPARE(engine.rpm(), 42);
// ... and the change makes it back to the replica
replicaSpy.wait();
QCOMPARE(replicaSpy.size(), 1);
QCOMPARE(rep->rpm(), 42);
} else {
replica.reset(client.acquireDynamic(QStringLiteral("Engine")));
QVERIFY(replica->waitForSource(1000));
//Compare Replica to Source
const QMetaObject *metaObject = replica->metaObject();
const int rpmIndex = metaObject->indexOfProperty("rpm");
Q_ASSERT(rpmIndex != -1);
const QMetaProperty rpmMeta = metaObject->property(rpmIndex);
QCOMPARE(rpmMeta.read(replica.data()).value<int>(), engine.rpm());
const int typeIndex = metaObject->indexOfProperty("type");
Q_ASSERT(typeIndex != -1);
const QMetaProperty typeMeta = metaObject->property(typeIndex);
QCOMPARE(typeMeta.read(replica.data()).value<EngineReplica::EngineType>(), EngineReplica::Gas);
//Change Replica and make sure change propagates to source
QSignalSpy sourceSpy(&engine, &EngineSimpleSource::rpmChanged);
QSignalSpy replicaSpy(replica.data(), QByteArray(QByteArrayLiteral("2")+rpmMeta.notifySignal().methodSignature().constData()));
const int rpmPushIndex = metaObject->indexOfMethod("pushRpm(int)");
Q_ASSERT(rpmPushIndex != -1);
QMetaMethod pushMethod = metaObject->method(rpmPushIndex);
Q_ASSERT(pushMethod.isValid());
QVERIFY(pushMethod.invoke(replica.data(), Q_ARG(int, 42)));
sourceSpy.wait();
QCOMPARE(sourceSpy.size(), 1);
QCOMPARE(engine.rpm(), 42);
// ... and the change makes it back to the replica
replicaSpy.wait();
QCOMPARE(replicaSpy.size(), 1);
QCOMPARE(rpmMeta.read(replica.data()).value<int>(), engine.rpm());
}
// Make sure disabling the Source cascades the state change
bool res = host.disableRemoting(&engine);
Q_ASSERT(res);
QSignalSpy stateSpy(replica.data(), &QRemoteObjectReplica::stateChanged);
stateSpy.wait();
QCOMPARE(stateSpy.size(), 1);
QCOMPARE(replica->state(), QRemoteObjectReplica::Suspect);
// Now test subclass Source
ParentClassSimpleSource parent;
SubClassSimpleSource subclass;
const MyPOD initialValue(42, 3.14f, QStringLiteral("SubClass"));
subclass.setMyPOD(initialValue);
QStringListModel model;
model.setStringList(QStringList() << "Track1" << "Track2" << "Track3");
parent.setSubClass(&subclass);
parent.setTracks(&model);
QCOMPARE(subclass.myPOD(), initialValue);
if (sourceApi)
host.enableRemoting<ParentClassSourceAPI>(&parent);
else
host.enableRemoting(&parent);
if (!dynamic) {
replica.reset(client.acquire<ParentClassReplica>());
ParentClassReplica *rep = qobject_cast<ParentClassReplica *>(replica.data());
QSignalSpy tracksSpy(rep->tracks(), &QAbstractItemModelReplica::initialized);
QVERIFY(replica->waitForSource(1000));
//QLoggingCategory::setFilterRules("qt.remoteobjects*=false");
//Compare Replica to Source
QVERIFY(rep->subClass() != nullptr);
QCOMPARE(rep->subClass()->myPOD(), parent.subClass()->myPOD());
QVERIFY(rep->tracks() != nullptr);
QVERIFY(tracksSpy.size() || tracksSpy.wait());
// Rep file only uses display role, but proxy doesn't forward that yet
if (!useProxy)
QCOMPARE(rep->tracks()->availableRoles(), QList<int> { Qt::DisplayRole });
else {
const auto &availableRolesVec = rep->tracks()->availableRoles();
QSet<int> availableRoles;
for (int r : availableRolesVec)
availableRoles.insert(r);
const auto &rolesHash = model.roleNames();
QSet<int> roles;
for (auto it = rolesHash.cbegin(), end = rolesHash.cend(); it != end; ++it)
roles.insert(it.key());
QCOMPARE(availableRoles, roles);
}
QList<QModelIndex> pending;
QTRY_COMPARE(rep->tracks()->rowCount(), model.rowCount());
for (int i = 0; i < rep->tracks()->rowCount(); i++)
{
// We haven't received any data yet
const auto index = rep->tracks()->index(i, 0);
QCOMPARE(rep->tracks()->data(index), QVariant());
pending.append(index);
}
if (useProxy) { // A first batch of updates will be the empty proxy values
WaitForDataChanged w(rep->tracks(), pending);
QVERIFY(w.wait());
}
WaitForDataChanged w(rep->tracks(), pending);
QVERIFY(w.wait());
for (int i = 0; i < rep->tracks()->rowCount(); i++)
{
QTRY_COMPARE(rep->tracks()->data(rep->tracks()->index(i, 0)), model.data(model.index(i), Qt::DisplayRole));
}
//Change SubClass and make sure change propagates
SubClassSimpleSource updatedSubclass;
const MyPOD updatedValue(-1, 123.456f, QStringLiteral("Updated"));
updatedSubclass.setMyPOD(updatedValue);
QSignalSpy replicaSpy(rep, &ParentClassReplica::subClassChanged);
parent.setSubClass(&updatedSubclass);
replicaSpy.wait();
QCOMPARE(replicaSpy.size(), 1);
QCOMPARE(rep->subClass()->myPOD(), parent.subClass()->myPOD());
QCOMPARE(rep->subClass()->myPOD(), updatedValue);
} else {
replica.reset(client.acquireDynamic(QStringLiteral("ParentClass")));
QVERIFY(replica->waitForSource(1000));
const QMetaObject *metaObject = replica->metaObject();
// Verify subClass pointer
const int subclassIndex = metaObject->indexOfProperty("subClass");
QVERIFY(subclassIndex != -1);
const QMetaProperty subclassMeta = metaObject->property(subclassIndex);
QObject *subclassQObjectPtr = subclassMeta.read(replica.data()).value<QObject *>();
QVERIFY(subclassQObjectPtr != nullptr);
QRemoteObjectDynamicReplica *subclassReplica = qobject_cast<QRemoteObjectDynamicReplica *>(subclassQObjectPtr);
QVERIFY(subclassReplica != nullptr);
// Verify tracks pointer
const int tracksIndex = metaObject->indexOfProperty("tracks");
QVERIFY(tracksIndex != -1);
const QMetaProperty tracksMeta = metaObject->property(tracksIndex);
QObject *tracksQObjectPtr = tracksMeta.read(replica.data()).value<QObject *>();
QVERIFY(tracksQObjectPtr != nullptr);
QAbstractItemModelReplica *tracksReplica = qobject_cast<QAbstractItemModelReplica *>(tracksQObjectPtr);
QVERIFY(tracksReplica != nullptr);
// Verify subClass data
const int podIndex = subclassReplica->metaObject()->indexOfProperty("myPOD");
QVERIFY(podIndex != -1);
const QMetaProperty podMeta = subclassReplica->metaObject()->property(podIndex);
MyPOD pod = podMeta.read(subclassReplica).value<MyPOD>();
QCOMPARE(pod, parent.subClass()->myPOD());
// Verify tracks data
// Rep file only uses display role, but proxy doesn't forward that yet
if (!useProxy)
QCOMPARE(tracksReplica->availableRoles(), QList<int> { Qt::DisplayRole });
else {
const auto &availableRolesVec = tracksReplica->availableRoles();
QSet<int> availableRoles;
for (int r : availableRolesVec)
availableRoles.insert(r);
const auto &rolesHash = model.roleNames();
QSet<int> roles;
for (auto it = rolesHash.cbegin(), end = rolesHash.cend(); it != end; ++it)
roles.insert(it.key());
QCOMPARE(availableRoles, roles);
}
QTRY_COMPARE(tracksReplica->isInitialized(), true);
QSignalSpy dataSpy(tracksReplica, &QAbstractItemModelReplica::dataChanged);
QList<QModelIndex> pending;
QTRY_COMPARE(tracksReplica->rowCount(), model.rowCount());
for (int i = 0; i < tracksReplica->rowCount(); i++)
{
// We haven't received any data yet
const auto index = tracksReplica->index(i, 0);
QCOMPARE(tracksReplica->data(index), QVariant());
pending.append(index);
}
if (useProxy) { // A first batch of updates will be the empty proxy values
WaitForDataChanged w(tracksReplica, pending);
QVERIFY(w.wait());
}
WaitForDataChanged w(tracksReplica, pending);
QVERIFY(w.wait());
for (int i = 0; i < tracksReplica->rowCount(); i++)
{
QCOMPARE(tracksReplica->data(tracksReplica->index(i, 0)), model.data(model.index(i), Qt::DisplayRole));
}
//Change SubClass and make sure change propagates
SubClassSimpleSource updatedSubclass;
const MyPOD updatedValue(-1, 123.456f, QStringLiteral("Updated"));
updatedSubclass.setMyPOD(updatedValue);
QSignalSpy replicaSpy(replica.data(), QByteArray(QByteArrayLiteral("2")+subclassMeta.notifySignal().methodSignature().constData()));
parent.setSubClass(&updatedSubclass);
replicaSpy.wait();
QCOMPARE(replicaSpy.size(), 1);
subclassQObjectPtr = subclassMeta.read(replica.data()).value<QObject *>();
QVERIFY(subclassQObjectPtr != nullptr);
subclassReplica = qobject_cast<QRemoteObjectDynamicReplica *>(subclassQObjectPtr);
QVERIFY(subclassReplica != nullptr);
pod = podMeta.read(subclassReplica).value<MyPOD>();
QCOMPARE(pod, parent.subClass()->myPOD());
}
replica.reset();
}
void ProxyTest::testForwardProxy()
{
// Setup Local Registry
QRemoteObjectRegistryHost registry(registryUrl);
SET_NODE_NAME(registry);
// Setup Local Host
QRemoteObjectHost host(localHostUrl, registryUrl);
SET_NODE_NAME(host);
// Setup Proxy
QRemoteObjectRegistryHost proxyNode(proxyNodeUrl);
SET_NODE_NAME(proxyNode);
proxyNode.proxy(registryUrl, proxyHostUrl);
// Include the reverseProxy to make sure we don't try to send back
// proxied objects.
proxyNode.reverseProxy();
// Setup Source
EngineSimpleSource engine;
engine.setRpm(1234);
// Setup Remote Node
QRemoteObjectHost remoteNode(remoteNodeUrl);
SET_NODE_NAME(remoteNode);
remoteNode.connectToNode(proxyNodeUrl);
// Add source
host.enableRemoting(&engine);
// Setup Replica
const QScopedPointer<EngineReplica> replica(remoteNode.acquire<EngineReplica>());
QTRY_VERIFY(replica->waitForSource(300));
// Compare Replica to Source
QCOMPARE(replica->rpm(), engine.rpm());
}
void ProxyTest::testReverseProxy()
{
// Setup Local Registry
QRemoteObjectRegistryHost registry(registryUrl);
SET_NODE_NAME(registry);
// Setup Local Host
QRemoteObjectHost host(localHostUrl, registryUrl);
SET_NODE_NAME(host);
// Setup Proxy
QRemoteObjectRegistryHost proxyNode(proxyNodeUrl);
SET_NODE_NAME(proxyNode);
proxyNode.proxy(registryUrl, proxyHostUrl);
proxyNode.reverseProxy();
// Setup Source
EngineSimpleSource engine;
engine.setRpm(1234);
// Setup Remote Node
QRemoteObjectHost remoteNode(remoteNodeUrl, proxyNodeUrl);
SET_NODE_NAME(remoteNode);
// Add source
remoteNode.enableRemoting(&engine);
// Setup Replica
const QScopedPointer<EngineReplica> replica(host.acquire<EngineReplica>());
QTRY_VERIFY(replica->waitForSource(300));
//Compare Replica to Source
QCOMPARE(replica->rpm(), engine.rpm());
}
void ProxyTest::testTopLevelModel()
{
QRemoteObjectRegistryHost registry(registryUrl);
//Setup Local Host
QRemoteObjectHost host(localHostUrl);
SET_NODE_NAME(host);
host.setRegistryUrl(registryUrl);
QStringListModel model;
model.setStringList(QStringList() << "Track1" << "Track2" << "Track3");
host.enableRemoting(&model, "trackList", QList<int> { Qt::DisplayRole });
QRemoteObjectHost proxyNode;
SET_NODE_NAME(proxyNode);
proxyNode.setHostUrl(tcpHostUrl);
proxyNode.proxy(registryUrl);
//Setup Local Replica
QRemoteObjectNode client;
SET_NODE_NAME(client);
client.connectToNode(tcpHostUrl);
QAbstractItemModelReplica *replica = client.acquireModel("trackList");
QSignalSpy tracksSpy(replica, &QAbstractItemModelReplica::initialized);
QVERIFY(tracksSpy.wait());
QTRY_COMPARE(replica->rowCount(), model.rowCount());
}
QTEST_MAIN(ProxyTest)
#include "tst_proxy.moc"
|