1
1
#include < QString>
2
2
#include < QtTest>
3
3
#include < QCoreApplication>
4
+ #include < QtService/ServiceControl>
4
5
#include < testlib.h>
5
6
#include < testdata.h>
6
7
using namespace QtDataSync ;
@@ -20,11 +21,13 @@ private Q_SLOTS:
20
21
void testRemoveAndResetAccount ();
21
22
void testAddAccountTrusted ();
22
23
24
+ void testAddAccountOnCreate ();
25
+
23
26
Q_SIGNALS:
24
27
void unlock ();
25
28
26
29
private:
27
- QProcess *server;
30
+ QtService::ServiceControl *server;
28
31
29
32
AccountManager *acc1;
30
33
SyncManager *sync1;
@@ -45,21 +48,24 @@ void IntegrationTest::initTestCase()
45
48
QVERIFY (QFile::exists (QString::fromUtf8 (confPath)));
46
49
qputenv (" QDSAPP_CONFIG_FILE" , confPath);
47
50
51
+ const QString launchPath { QStringLiteral (BUILD_BIN_DIR " qdsapp" ) };
48
52
#ifdef Q_OS_UNIX
49
- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappd" ) };
53
+ const QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappd" ) };
50
54
#elif Q_OS_WIN
51
- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappsvc" ) };
55
+ const QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsappsvc" ) };
52
56
#else
53
- QString binPath { QStringLiteral (BUILD_BIN_DIR " qdsapp " ) } ;
57
+ const auto binPath = launchPath ;
54
58
#endif
55
59
QVERIFY (QFile::exists (binPath));
60
+ if (!QFile::exists (launchPath))
61
+ QVERIFY (QFile::link (binPath, launchPath));
62
+ QVERIFY (QFile::exists (launchPath));
56
63
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 ());
63
69
64
70
try {
65
71
TestLib::init ();
@@ -117,16 +123,9 @@ void IntegrationTest::cleanupTestCase()
117
123
acc2 = nullptr ;
118
124
Setup::removeSetup (QStringLiteral (" setup2" ), true );
119
125
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 ();
130
129
}
131
130
132
131
void IntegrationTest::testPrepareData ()
@@ -241,12 +240,12 @@ void IntegrationTest::testAddAccount()
241
240
emit unlock ();
242
241
QCOMPARE (s, SyncManager::Synchronized);
243
242
}, true );
244
- unlockSpy.wait ();
243
+ QVERIFY ( unlockSpy.wait () );
245
244
sync2->runOnSynchronized ([this ](SyncManager::SyncState s) {
246
245
emit unlock ();
247
246
QCOMPARE (s, SyncManager::Synchronized);
248
247
});
249
- unlockSpy.wait ();
248
+ QVERIFY ( unlockSpy.wait () );
250
249
} while (oldCnt1 != store1->count () || oldCnt2 != store2->count ());
251
250
252
251
QCOMPARE (store1->count (), 20 );
@@ -598,12 +597,12 @@ void IntegrationTest::testAddAccountTrusted()
598
597
emit unlock ();
599
598
QCOMPARE (s, SyncManager::Synchronized);
600
599
}, true );
601
- unlockSpy.wait ();
600
+ QVERIFY ( unlockSpy.wait () );
602
601
sync2->runOnSynchronized ([this ](SyncManager::SyncState s) {
603
602
emit unlock ();
604
603
QCOMPARE (s, SyncManager::Synchronized);
605
604
});
606
- unlockSpy.wait ();
605
+ QVERIFY ( unlockSpy.wait () );
607
606
608
607
QCOMPARE (store1->count (), 0 );
609
608
QCOMPARE (store2->count (), 0 );
@@ -615,6 +614,89 @@ void IntegrationTest::testAddAccountTrusted()
615
614
}
616
615
}
617
616
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
+
618
700
QTEST_MAIN (IntegrationTest)
619
701
620
702
#include " tst_integration.moc"
0 commit comments