File indexing completed on 2025-01-19 04:51:57
0001 #include <QTest> 0002 #include <QDebug> 0003 #include <QSignalSpy> 0004 #include <functional> 0005 #include <QStandardPaths> 0006 #include <QDir> 0007 #include <sink/test.h> 0008 #include <sink/store.h> 0009 0010 #include <accountsettings.h> 0011 0012 0013 class TestSettings : public AccountSettings 0014 { 0015 Q_OBJECT 0016 0017 public: 0018 TestSettings(QObject *parent = 0) 0019 : AccountSettings{parent} 0020 {} 0021 0022 Q_INVOKABLE virtual void load() Q_DECL_OVERRIDE 0023 { 0024 loadAccount(); 0025 loadImapResource(); 0026 loadMailtransportResource(); 0027 loadMaildirResource(); 0028 loadCardDavResource(); 0029 loadCalDavResource(); 0030 loadIdentity(); 0031 } 0032 0033 Q_INVOKABLE virtual void save() Q_DECL_OVERRIDE 0034 { 0035 saveAccount(); 0036 saveImapResource(); 0037 saveMailtransportResource(); 0038 saveMaildirResource(); 0039 saveCardDavResource(); 0040 saveCalDavResource(); 0041 saveIdentity(); 0042 } 0043 0044 Q_INVOKABLE virtual void remove() Q_DECL_OVERRIDE 0045 { 0046 removeResource(mMailtransportIdentifier); 0047 removeResource(mImapIdentifier); 0048 removeResource(mMaildirIdentifier); 0049 removeResource(mCardDavIdentifier); 0050 removeResource(mCalDavIdentifier); 0051 removeIdentity(); 0052 removeAccount(); 0053 } 0054 }; 0055 0056 class SettingsTest : public QObject 0057 { 0058 Q_OBJECT 0059 private slots: 0060 0061 void initTestCase() 0062 { 0063 Sink::Test::initTest(); 0064 } 0065 0066 void testLoad() 0067 { 0068 auto imapServer = QString("imapserver"); 0069 auto imapUsername = QString("imapName"); 0070 auto smtpServer = QString("smtpserver"); 0071 auto smtpUsername = QString("smtpName"); 0072 auto username = QString("username"); 0073 auto emailAddress = QString("emailAddress"); 0074 auto path = QString("path"); 0075 auto accountName = QString("accountName"); 0076 auto carddavServer = QString("carddavServer"); 0077 auto carddavUsername = QString("carddavUsername"); 0078 auto caldavServer = QString("caldavServer"); 0079 auto caldavUsername = QString("caldavUsername"); 0080 0081 TestSettings settings; 0082 settings.setProperty("accountType", "test"); 0083 settings.setProperty("accountName", accountName); 0084 settings.setProperty("imapServer", imapServer); 0085 settings.setProperty("imapUsername", imapUsername); 0086 settings.setProperty("smtpServer", smtpServer); 0087 settings.setProperty("smtpUsername", smtpUsername); 0088 settings.setProperty("carddavServer", carddavServer); 0089 settings.setProperty("carddavUsername", carddavUsername); 0090 settings.setProperty("caldavServer", caldavServer); 0091 settings.setProperty("caldavUsername", caldavUsername); 0092 settings.setProperty("path", path); 0093 settings.setProperty("userName", username); 0094 settings.setProperty("emailAddress", emailAddress); 0095 settings.save(); 0096 0097 auto accountId = settings.accountIdentifier(); 0098 0099 Sink::Store::fetchAll<Sink::ApplicationDomain::SinkResource>(Sink::Query()).then([](const QList<Sink::ApplicationDomain::SinkResource::Ptr> &resources) { 0100 QCOMPARE(resources.size(), 5); 0101 }) 0102 .exec().waitForFinished(); 0103 0104 //Ensure we can read back all the information using the accountid 0105 { 0106 TestSettings readSettings; 0107 QSignalSpy spy(&readSettings, &TestSettings::imapResourceChanged); 0108 QSignalSpy spy1(&readSettings, &TestSettings::smtpResourceChanged); 0109 QSignalSpy spy2(&readSettings, &TestSettings::cardDavResourceChanged); 0110 QSignalSpy spy3(&readSettings, &TestSettings::changed); 0111 QSignalSpy spy4(&readSettings, &TestSettings::pathChanged); 0112 QSignalSpy spy5(&readSettings, &TestSettings::calDavResourceChanged); 0113 readSettings.setAccountIdentifier(accountId); 0114 //Once for clear and once for the new setting 0115 QTRY_COMPARE(spy.count(), 2); 0116 QTRY_COMPARE(spy1.count(), 2); 0117 QTRY_COMPARE(spy2.count(), 2); 0118 QTRY_COMPARE(spy3.count(), 2); 0119 QTRY_COMPARE(spy4.count(), 2); 0120 QTRY_COMPARE(spy5.count(), 2); 0121 QVERIFY(!readSettings.accountIdentifier().isEmpty()); 0122 QCOMPARE(readSettings.property("accountName").toString(), accountName); 0123 QCOMPARE(readSettings.property("imapServer").toString(), imapServer); 0124 QCOMPARE(readSettings.property("imapUsername").toString(), imapUsername); 0125 QCOMPARE(readSettings.property("smtpServer").toString(), smtpServer); 0126 QCOMPARE(readSettings.property("smtpUsername").toString(), smtpUsername); 0127 QCOMPARE(readSettings.property("carddavServer").toString(), carddavServer); 0128 QCOMPARE(readSettings.property("carddavUsername").toString(), carddavUsername); 0129 QCOMPARE(readSettings.property("caldavServer").toString(), caldavServer); 0130 QCOMPARE(readSettings.property("caldavUsername").toString(), caldavUsername); 0131 QCOMPARE(readSettings.property("path").toString(), path); 0132 QCOMPARE(readSettings.property("userName").toString(), username); 0133 QCOMPARE(readSettings.property("emailAddress").toString(), emailAddress); 0134 } 0135 0136 //Modify all settings 0137 { 0138 settings.setProperty("accountName", accountName + "mod"); 0139 settings.setProperty("imapServer", imapServer + "mod"); 0140 settings.setProperty("imapUsername", imapUsername + "mod"); 0141 settings.setProperty("smtpServer", smtpServer + "mod"); 0142 settings.setProperty("smtpUsername", smtpUsername + "mod"); 0143 settings.setProperty("carddavServer", carddavServer + "mod"); 0144 settings.setProperty("carddavUsername", carddavUsername + "mod"); 0145 settings.setProperty("caldavServer", caldavServer + "mod"); 0146 settings.setProperty("caldavUsername", caldavUsername + "mod"); 0147 settings.setProperty("path", path + "mod"); 0148 settings.setProperty("userName", username + "mod"); 0149 settings.setProperty("emailAddress", emailAddress + "mod"); 0150 settings.save(); 0151 } 0152 0153 //Read back settings again 0154 { 0155 TestSettings readSettings; 0156 QSignalSpy spy(&readSettings, &TestSettings::imapResourceChanged); 0157 QSignalSpy spy1(&readSettings, &TestSettings::smtpResourceChanged); 0158 QSignalSpy spy2(&readSettings, &TestSettings::cardDavResourceChanged); 0159 QSignalSpy spy3(&readSettings, &TestSettings::changed); 0160 QSignalSpy spy4(&readSettings, &TestSettings::pathChanged); 0161 QSignalSpy spy5(&readSettings, &TestSettings::calDavResourceChanged); 0162 readSettings.setAccountIdentifier(accountId); 0163 //Once for clear and once for the new setting 0164 QTRY_COMPARE(spy.count(), 2); 0165 QTRY_COMPARE(spy1.count(), 2); 0166 QTRY_COMPARE(spy2.count(), 2); 0167 QTRY_COMPARE(spy3.count(), 2); 0168 QTRY_COMPARE(spy4.count(), 2); 0169 QTRY_COMPARE(spy5.count(), 2); 0170 QVERIFY(!readSettings.accountIdentifier().isEmpty()); 0171 QCOMPARE(readSettings.property("accountName").toString(), accountName + "mod"); 0172 QCOMPARE(readSettings.property("imapServer").toString(), imapServer + "mod"); 0173 QCOMPARE(readSettings.property("imapUsername").toString(), imapUsername + "mod"); 0174 QCOMPARE(readSettings.property("smtpServer").toString(), smtpServer + "mod"); 0175 QCOMPARE(readSettings.property("smtpUsername").toString(), smtpUsername + "mod"); 0176 QCOMPARE(readSettings.property("carddavServer").toString(), carddavServer + "mod"); 0177 QCOMPARE(readSettings.property("carddavUsername").toString(), carddavUsername + "mod"); 0178 QCOMPARE(readSettings.property("caldavServer").toString(), caldavServer + "mod"); 0179 QCOMPARE(readSettings.property("caldavUsername").toString(), caldavUsername + "mod"); 0180 QCOMPARE(readSettings.property("path").toString(), path + "mod"); 0181 QCOMPARE(readSettings.property("userName").toString(), username + "mod"); 0182 QCOMPARE(readSettings.property("emailAddress").toString(), emailAddress + "mod"); 0183 } 0184 0185 { 0186 TestSettings settings; 0187 QSignalSpy spy(&settings, &TestSettings::imapResourceChanged); 0188 settings.setAccountIdentifier(accountId); 0189 QTRY_COMPARE(spy.count(), 2); 0190 settings.remove(); 0191 } 0192 0193 Sink::Store::fetchAll<Sink::ApplicationDomain::SinkResource>(Sink::Query()).then([](const QList<Sink::ApplicationDomain::SinkResource::Ptr> &resources) { 0194 QCOMPARE(resources.size(), 0); 0195 }) 0196 .exec().waitForFinished(); 0197 } 0198 }; 0199 0200 QTEST_GUILESS_MAIN(SettingsTest) 0201 #include "settingstest.moc"