swift
Loading...
Searching...
No Matches
testsharedstate.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2020 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
10
11#include "testsharedstate.h"
12
13#include <QDBusConnection>
14#include <QProcess>
15#include <QTest>
16
17#include "test.h"
18
19#include "misc/dbusserver.h"
23
24using namespace QTest;
25using namespace swift::misc;
26using namespace swift::misc::shared_state;
27
28namespace MiscTest
29{
32 {
34
35 private slots:
37 void initTestCase();
38
40 void testPromise();
41
43 void localScalar();
44
46 void localList();
47
49 void dbusScalar();
50
52 void dbusList();
53 };
54
55 void CTestSharedState::initTestCase()
56 {
59 }
60
61 void CTestSharedState::testPromise()
62 {
63 int result = 0;
64 CPromise<int> promise;
65 doAfter(promise.future(), nullptr, [&](QFuture<int> future) { result = future.result(); });
66 promise.setResult(42);
67 bool ok = qWaitFor([&] { return result == 42; });
68 QVERIFY2(ok, "future contains expected result");
69 }
70
71 void CTestSharedState::localScalar()
72 {
73 CDataLinkLocal dataLink;
74 CTestScalarMutator mutator(this);
75 CTestScalarJournal journal(this);
76 CTestScalarObserver observer(this);
77 mutator.initialize(&dataLink);
78 journal.initialize(&dataLink);
79 observer.initialize(&dataLink);
80
81 mutator.setValue(42);
82 bool ok = qWaitFor([&] { return observer.value() == 42; });
83 QVERIFY2(ok, "expected value received");
84 qWait(0);
85 QVERIFY2(observer.value() == 42, "still has expected value");
86
87 CTestScalarObserver observer2(this);
88 observer2.initialize(&dataLink);
89
90 ok = qWaitFor([&] { return observer2.value() == 42; });
91 QVERIFY2(ok, "new observer got existing value");
92 qWait(0);
93 QVERIFY2(observer2.value() == 42, "still has correct value");
94 }
95
96 void CTestSharedState::localList()
97 {
98 CDataLinkLocal dataLink;
99 CTestListMutator mutator(this);
100 CTestListJournal journal(this);
101 CTestListObserver observer(this);
102 mutator.initialize(&dataLink);
103 journal.initialize(&dataLink);
104 observer.initialize(&dataLink);
105
106 observer.setFilter({});
107 mutator.addElement(1);
108 bool ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1 }; });
109 QVERIFY2(ok, "expected value received");
110 qWait(0);
111 QVERIFY2(observer.allValues() == QList<int> { 1 }, "still has expected value");
112
113 for (int e = 2; e <= 6; ++e)
114 {
115 mutator.addElement(e);
116 }
117 ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1, 2, 3, 4, 5, 6 }; });
118 QVERIFY2(ok, "expected value received");
119
120 observer.allValues() = {};
121 observer.setFilter({ 1 });
122 ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1, 3, 5 }; });
123 QVERIFY2(ok, "expected value received");
124 }
125
127 class Server
128 {
129 public:
132 {
133 QObject::connect(&m_process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
134 [](int code, QProcess::ExitStatus status) {
135 qDebug() << "Server process exited" << (status ? "abnormally" : "normally")
136 << "with exit code" << code;
137 });
138 m_process.start(QCoreApplication::applicationDirPath() + "/tests_sharedstatetestserver", QStringList());
139 if (!m_process.waitForStarted())
140 {
141 qDebug() << "Server failed to start";
142 }
143 }
144
146 {
147 m_process.disconnect();
148 m_process.kill();
149 m_process.waitForFinished();
150 }
151
152 private:
153 QProcess m_process;
154 };
155
156 void CTestSharedState::dbusScalar()
157 {
159 if (!connection.isConnected())
160 {
161 QSKIP("No session bus");
162 }
163 Server s;
164
165 CDataLinkDBus dataLink;
166 dataLink.initializeRemote(connection, SWIFT_SERVICENAME);
167 bool ok = qWaitFor([&] { return dataLink.watcher()->isConnected(); });
168 QVERIFY2(ok, "Connection failed");
169
170 CTestScalarObserver observer(this);
171 CTestScalarMutator mutator(this);
172 observer.initialize(&dataLink);
173 mutator.initialize(&dataLink);
174
175 ok = qWaitFor([&] { return observer.value() == 42; });
176 QVERIFY2(ok, "expected value received");
177 qWait(1000);
178 QVERIFY2(observer.value() == 42, "still has expected value");
179
180 mutator.setValue(69);
181 ok = qWaitFor([&] { return observer.value() == 69; });
182 QVERIFY2(ok, "expected value received");
183 qWait(1000);
184 QVERIFY2(observer.value() == 69, "still has expected value");
185 }
186
187 void CTestSharedState::dbusList()
188 {
189 QDBusConnection connection = QDBusConnection::sessionBus();
190 if (!connection.isConnected())
191 {
192 QSKIP("No session bus");
193 }
194 Server s;
195
196 CDataLinkDBus dataLink;
197 dataLink.initializeRemote(connection, SWIFT_SERVICENAME);
198 bool ok = qWaitFor([&] { return dataLink.watcher()->isConnected(); });
199 QVERIFY2(ok, "Connection failed");
200
201 CTestListObserver observer(this);
202 CTestListMutator mutator(this);
203 observer.initialize(&dataLink);
204 mutator.initialize(&dataLink);
205
206 observer.setFilter({});
207 ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1, 2, 3, 4, 5, 6 }; });
208 QVERIFY2(ok, "expected value received");
209 qWait(1000);
210 QVERIFY2(observer.allValues() == QList<int>({ 1, 2, 3, 4, 5, 6 }), "still has expected value");
211
212 observer.allValues() = {};
213 observer.setFilter({ 1 });
214 ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1, 3, 5 }; });
215 QVERIFY2(ok, "expected value received");
216
217 mutator.addElement(7);
218 ok = qWaitFor([&] { return observer.allValues() == QList<int> { 1, 3, 5, 7 }; });
219 QVERIFY2(ok, "expected value received");
220 qWait(1000);
221 QVERIFY2(observer.allValues() == QList<int>({ 1, 3, 5, 7 }), "still has expected value");
222 }
223} // namespace MiscTest
224
227
228#include "testsharedstate.moc"
229
DBus implementation classes tests.
A promise-based interface to QFuture, similar to std::promise for std::future.
Definition promise.h:87
QFuture< T > future()
Return a future that can be used to access the result.
Definition promise.h:90
void setResult(const T &value)
Set the result value that will be made available through the future.
Definition promise.h:100
@ Server
What server are you on?
Definition enums.h:78
#define SWIFT_SERVICENAME
Service name of DBus service.
Definition dbusserver.h:23
Utilities for sharing state between multiple objects.
Definition application.h:48
Free functions in swift::misc.
void registerMetadata()
Register all relevant metadata in Misc.
void doAfter(QFuture< T > future, QObject *context, F &&func)
Connect a slot or function to be invoked in the given context when a QFuture is finished.
Definition promise.h:68
QString applicationDirPath()
bool isConnected() const const
QDBusConnection sessionBus()
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void finished(int exitCode, QProcess::ExitStatus exitStatus)
QSKIP(description)
QVERIFY2(condition, message)
void qWait(int msecs)
bool qWaitFor(Functor predicate, QDeadlineTimer deadline)
#define SWIFTTEST_MAIN(TestObject)
Implements a main() function that executes all tests in TestObject including instantiating a QCoreApp...
Definition test.h:57