11#include < QString>
22#include < QtTest>
33#include < QCoreApplication>
4+ #include < QtService/ServiceControl>
45#include < testlib.h>
56#include < testdata.h>
67using namespace QtDataSync ;
@@ -20,11 +21,13 @@ private Q_SLOTS:
2021 void testRemoveAndResetAccount ();
2122 void testAddAccountTrusted ();
2223
24+ void testAddAccountOnCreate ();
25+
2326Q_SIGNALS:
2427 void unlock ();
2528
2629private:
27- QProcess *server;
30+ QtService::ServiceControl *server;
2831
2932 AccountManager *acc1;
3033 SyncManager *sync1;
@@ -45,21 +48,24 @@ void IntegrationTest::initTestCase()
4548 QVERIFY (QFile::exists (QString::fromUtf8 (confPath)));
4649 qputenv (" QDSAPP_CONFIG_FILE" , confPath);
4750
51+ const QString launchPath { QStringLiteral (BUILD_BIN_DIR " qdsapp" ) };
4852#ifdef Q_OS_UNIX
49- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappd" ) };
53+ const QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappd" ) };
5054#elif Q_OS_WIN
51- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappsvc" ) };
55+ const QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappsvc" ) };
5256#else
53- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsapp " ) } ;
57+ const auto binPath = launchPath ;
5458#endif
5559 QVERIFY (QFile::exists (binPath));
60+ if (!QFile::exists (launchPath))
61+ QVERIFY (QFile::link (binPath, launchPath));
62+ QVERIFY (QFile::exists (launchPath));
5663
57- server = new QProcess (this );
58- server->setProgram (binPath);
59- server->setProcessChannelMode (QProcess::ForwardedErrorChannel);
60- server->start ();
61- QVERIFY (server->waitForStarted (5000 ));
62- QVERIFY (!server->waitForFinished (5000 ));
64+ server = QtService::ServiceControl::create (QStringLiteral (" debug" ), launchPath, this );
65+ QVERIFY (server);
66+ QVERIFY (server->serviceExists ());
67+ server->setBlocking (true );
68+ QVERIFY (server->start ());
6369
6470 try {
6571 TestLib::init ();
@@ -117,16 +123,9 @@ void IntegrationTest::cleanupTestCase()
117123 acc2 = nullptr ;
118124 Setup::removeSetup (QStringLiteral (" setup2" ), true );
119125
120- // send a signal to stop
121- #ifdef Q_OS_UNIX
122- server->terminate (); // same as kill(SIGTERM)
123- #elif Q_OS_WIN
124- GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, server->processId ());
125- #endif
126- QVERIFY (server->waitForFinished (5000 ));
127- QCOMPARE (server->exitStatus (), QProcess::NormalExit);
128- QCOMPARE (server->exitCode (), 0 );
129- server->close ();
126+ QVERIFY (server->stop ());
127+ QTRY_COMPARE (server->status (), QtService::ServiceControl::ServiceStopped);
128+ server->deleteLater ();
130129}
131130
132131void IntegrationTest::testPrepareData ()
@@ -241,12 +240,12 @@ void IntegrationTest::testAddAccount()
241240 emit unlock ();
242241 QCOMPARE (s, SyncManager::Synchronized);
243242 }, true );
244- unlockSpy.wait ();
243+ QVERIFY ( unlockSpy.wait () );
245244 sync2->runOnSynchronized ([this ](SyncManager::SyncState s) {
246245 emit unlock ();
247246 QCOMPARE (s, SyncManager::Synchronized);
248247 });
249- unlockSpy.wait ();
248+ QVERIFY ( unlockSpy.wait () );
250249 } while (oldCnt1 != store1->count () || oldCnt2 != store2->count ());
251250
252251 QCOMPARE (store1->count (), 20 );
@@ -598,12 +597,12 @@ void IntegrationTest::testAddAccountTrusted()
598597 emit unlock ();
599598 QCOMPARE (s, SyncManager::Synchronized);
600599 }, true );
601- unlockSpy.wait ();
600+ QVERIFY ( unlockSpy.wait () );
602601 sync2->runOnSynchronized ([this ](SyncManager::SyncState s) {
603602 emit unlock ();
604603 QCOMPARE (s, SyncManager::Synchronized);
605604 });
606- unlockSpy.wait ();
605+ QVERIFY ( unlockSpy.wait () );
607606
608607 QCOMPARE (store1->count (), 0 );
609608 QCOMPARE (store2->count (), 0 );
@@ -615,6 +614,89 @@ void IntegrationTest::testAddAccountTrusted()
615614 }
616615}
617616
617+ void IntegrationTest::testAddAccountOnCreate ()
618+ {
619+ try {
620+ QSignalSpy error1Spy (acc1, &AccountManager::lastErrorChanged);
621+ QSignalSpy grantSpy (acc1, &AccountManager::accountAccessGranted);
622+ QSignalSpy unlockSpy (this , &IntegrationTest::unlock);
623+
624+ // export from acc1
625+ auto password = QStringLiteral (" password" );
626+ QJsonObject exportData;
627+ acc1->exportAccountTrusted (false , password, [&](QJsonObject exp) {
628+ QVERIFY (AccountManager::isTrustedImport (exp));
629+ exportData = exp;
630+ emit unlock ();
631+ }, [](QString e) {
632+ QFAIL (qUtf8Printable (e));
633+ });
634+ QVERIFY (unlockSpy.wait ());
635+ QVERIFY (!exportData.isEmpty ());
636+
637+ // create acc3 with account from setup
638+ AccountManager *acc3;
639+ SyncManager *sync3;
640+ DataTypeStore<TestData> *store3;
641+ QString setupName3 = QStringLiteral (" setup3" );
642+ {
643+ Setup setup3;
644+ TestLib::setup (setup3);
645+ setup3.setLocalDir (setup3.localDir () + QLatin1Char (' /' ) + setupName3)
646+ .setRemoteConfiguration (QUrl (QStringLiteral (" ws://localhost:14242" )))
647+ .setAccountTrusted (exportData, password, false , false );
648+ setup3.create (setupName3);
649+
650+ acc3 = new AccountManager (setupName3, this );
651+ QVERIFY (acc3->replica ()->waitForSource (5000 ));
652+ sync3 = new SyncManager (setupName3, this );
653+ QVERIFY (sync3->replica ()->waitForSource (5000 ));
654+ store3 = new DataTypeStore<TestData>(setupName3, this );
655+ }
656+
657+ QSignalSpy error3Spy (acc3, &AccountManager::lastErrorChanged);
658+ QSignalSpy acceptSpy (acc3, &AccountManager::importAccepted);
659+
660+ // wait for accept
661+ if (acceptSpy.isEmpty ())
662+ QVERIFY (acceptSpy.wait ());
663+ QCOMPARE (acceptSpy.size (), 1 );
664+
665+ // wait for grant
666+ if (grantSpy.isEmpty ())
667+ QVERIFY (grantSpy.wait ());
668+ QCOMPARE (grantSpy.size (), 1 );
669+
670+ // wait for sync
671+ sync1->runOnSynchronized ([this ](SyncManager::SyncState s) {
672+ emit unlock ();
673+ QCOMPARE (s, SyncManager::Synchronized);
674+ }, true );
675+ QVERIFY (unlockSpy.wait ());
676+ sync3->runOnSynchronized ([this ](SyncManager::SyncState s) {
677+ emit unlock ();
678+ QCOMPARE (s, SyncManager::Synchronized);
679+ });
680+ QVERIFY (unlockSpy.wait ());
681+
682+ QCOMPARE (store1->count (), 0 );
683+ QCOMPARE (store3->count (), 0 );
684+
685+ QVERIFY (error1Spy.isEmpty ());
686+ QVERIFY (error3Spy.isEmpty ());
687+
688+ delete store3;
689+ store3 = nullptr ;
690+ delete sync3;
691+ sync3 = nullptr ;
692+ delete acc3;
693+ acc3 = nullptr ;
694+ Setup::removeSetup (setupName3, true );
695+ } catch (std::exception &e) {
696+ QFAIL (e.what ());
697+ }
698+ }
699+
618700QTEST_MAIN (IntegrationTest)
619701
620702#include " tst_integration.moc"
0 commit comments