File indexing completed on 2024-05-19 04:01:09
0001 /* 0002 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include <provider.h> 0008 #include <platforminfosource.h> 0009 #include <screeninfosource.h> 0010 #include <startcountsource.h> 0011 0012 #include <QDebug> 0013 #include <QtTest/qtest.h> 0014 #include <QObject> 0015 #include <QSettings> 0016 #include <QSignalSpy> 0017 #include <QStandardPaths> 0018 0019 using namespace KUserFeedback; 0020 0021 class ProviderTest : public QObject 0022 { 0023 Q_OBJECT 0024 private Q_SLOTS: 0025 void initTestCase() 0026 { 0027 QCoreApplication::setOrganizationName(QStringLiteral("KDE")); 0028 QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org")); 0029 QCoreApplication::setApplicationName(QStringLiteral("providertest")); 0030 QStandardPaths::setTestModeEnabled(true); 0031 } 0032 0033 void init() 0034 { 0035 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback"));; 0036 s.beginGroup(QLatin1String("UserFeedback")); 0037 s.remove(QLatin1String("LastEncouragement")); 0038 s.remove(QLatin1String("Enabled")); 0039 } 0040 0041 void testProductId() 0042 { 0043 Provider p; 0044 QCOMPARE(p.productIdentifier(), QLatin1String("org.kde.providertest")); 0045 } 0046 0047 void testNoTelemetry() 0048 { 0049 Provider provider; 0050 provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct")); 0051 provider.addDataSource(new ScreenInfoSource); 0052 provider.addDataSource(new PlatformInfoSource); 0053 provider.setTelemetryMode(Provider::NoTelemetry); 0054 QByteArray b; 0055 QMetaObject::invokeMethod(&provider, "jsonData", Q_RETURN_ARG(QByteArray, b), Q_ARG(KUserFeedback::Provider::TelemetryMode, provider.telemetryMode())); 0056 b.replace('\n', ""); 0057 QCOMPARE(b.constData(), "{}"); 0058 } 0059 0060 void testLoadStore() 0061 { 0062 { 0063 Provider p; 0064 p.setTelemetryMode(Provider::NoTelemetry); 0065 p.setSurveyInterval(-1); 0066 } 0067 0068 { 0069 Provider p; 0070 QCOMPARE(p.telemetryMode(), Provider::NoTelemetry); 0071 QCOMPARE(p.surveyInterval(), -1); 0072 p.setTelemetryMode(Provider::DetailedSystemInformation); 0073 p.setSurveyInterval(90); 0074 } 0075 0076 { 0077 Provider p; 0078 QCOMPARE(p.telemetryMode(), Provider::DetailedSystemInformation); 0079 QCOMPARE(p.surveyInterval(), 90); 0080 } 0081 } 0082 0083 void testEncouragement() 0084 { 0085 { 0086 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0087 s.beginGroup(QLatin1String("UserFeedback")); 0088 s.remove(QLatin1String("LastEncouragement")); 0089 } 0090 0091 { 0092 Provider p; 0093 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0094 QVERIFY(spy.isValid()); 0095 p.setEncouragementDelay(0); 0096 QVERIFY(!spy.wait(10)); 0097 p.setApplicationStartsUntilEncouragement(0); 0098 p.setApplicationUsageTimeUntilEncouragement(0); 0099 QVERIFY(spy.wait(10)); 0100 QCOMPARE(spy.count(), 1); 0101 } 0102 0103 { 0104 Provider p; 0105 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0106 QVERIFY(spy.isValid()); 0107 p.setEncouragementDelay(0); 0108 p.setApplicationStartsUntilEncouragement(0); 0109 p.setApplicationUsageTimeUntilEncouragement(0); 0110 QVERIFY(!spy.wait(10)); 0111 } 0112 } 0113 0114 void testEncouragementDelay() 0115 { 0116 { 0117 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0118 s.beginGroup(QLatin1String("UserFeedback")); 0119 s.remove(QLatin1String("LastEncouragement")); 0120 } 0121 0122 { 0123 Provider p; 0124 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0125 QVERIFY(spy.isValid()); 0126 p.setEncouragementDelay(1); 0127 p.setApplicationStartsUntilEncouragement(0); 0128 p.setApplicationUsageTimeUntilEncouragement(0); 0129 QVERIFY(!spy.wait(10)); 0130 QVERIFY(spy.wait(1200)); 0131 QCOMPARE(spy.count(), 1); 0132 } 0133 } 0134 0135 void testNoEncouragementWithAllFeedbackEnabled() 0136 { 0137 { 0138 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0139 s.beginGroup(QLatin1String("UserFeedback")); 0140 s.remove(QLatin1String("LastEncouragement")); 0141 } 0142 0143 { 0144 Provider p; 0145 p.addDataSource(new PlatformInfoSource); 0146 p.setSurveyInterval(0); 0147 p.setTelemetryMode(Provider::BasicSystemInformation); 0148 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0149 QVERIFY(spy.isValid()); 0150 p.setEncouragementDelay(0); 0151 p.setApplicationStartsUntilEncouragement(0); 0152 p.setApplicationUsageTimeUntilEncouragement(0); 0153 QVERIFY(!spy.wait(10)); 0154 QCOMPARE(spy.count(), 0); 0155 } 0156 } 0157 0158 void testEncouragementRepetition() 0159 { 0160 { 0161 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0162 s.beginGroup(QLatin1String("UserFeedback")); 0163 s.setValue(QLatin1String("LastEncouragement"), QDateTime::currentDateTime().addSecs(-24 * 60 * 60 + 1)); 0164 } 0165 0166 { 0167 Provider p; 0168 p.setSurveyInterval(-1); 0169 p.setTelemetryMode(Provider::NoTelemetry); 0170 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0171 QVERIFY(spy.isValid()); 0172 p.setEncouragementDelay(0); 0173 p.setApplicationStartsUntilEncouragement(0); 0174 p.setEncouragementInterval(1); 0175 QVERIFY(spy.wait(1000)); 0176 QCOMPARE(spy.count(), 1); 0177 } 0178 0179 { 0180 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0181 s.beginGroup(QLatin1String("UserFeedback")); 0182 s.setValue(QLatin1String("LastEncouragement"), QDateTime::currentDateTime().addSecs(-24 * 60 * 60 - 1)); 0183 } 0184 0185 { 0186 Provider p; 0187 p.setSurveyInterval(90); 0188 p.setTelemetryMode(Provider::BasicSystemInformation); 0189 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0190 QVERIFY(spy.isValid()); 0191 p.setEncouragementDelay(0); 0192 p.setApplicationStartsUntilEncouragement(0); 0193 p.setEncouragementInterval(1); 0194 QVERIFY(!spy.wait(10)); 0195 } 0196 } 0197 0198 void testGlobalEncouragementCoordination() 0199 { 0200 { 0201 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0202 s.beginGroup(QLatin1String("UserFeedback")); 0203 s.remove(QLatin1String("LastEncouragement")); 0204 } 0205 0206 { 0207 Provider p; 0208 p.setSurveyInterval(-1); 0209 p.setTelemetryMode(Provider::NoTelemetry); 0210 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0211 QVERIFY(spy.isValid()); 0212 p.setEncouragementDelay(0); 0213 p.setApplicationStartsUntilEncouragement(0); 0214 p.setEncouragementInterval(1); 0215 QVERIFY(spy.wait(1000)); 0216 QCOMPARE(spy.count(), 1); 0217 } 0218 0219 // would qualify for encouragement, but global coordination should prevent it 0220 { 0221 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0222 s.beginGroup(QLatin1String("UserFeedback")); 0223 s.remove(QLatin1String("LastEncouragement")); 0224 } 0225 0226 { 0227 Provider p; 0228 p.setSurveyInterval(-1); 0229 p.setTelemetryMode(Provider::NoTelemetry); 0230 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0231 QVERIFY(spy.isValid()); 0232 p.setEncouragementDelay(0); 0233 p.setApplicationStartsUntilEncouragement(0); 0234 p.setEncouragementInterval(1); 0235 QVERIFY(!spy.wait(100)); 0236 } 0237 } 0238 0239 void testMultipleProviders() 0240 { 0241 { 0242 Provider p0; 0243 p0.setTelemetryMode(Provider::BasicUsageStatistics); // triggers store, so we want to avoid that below 0244 } 0245 0246 int c1 = -1; 0247 { 0248 Provider p1; 0249 auto s1 = new StartCountSource; 0250 s1->setTelemetryMode(Provider:: BasicUsageStatistics); 0251 p1.addDataSource(s1); 0252 c1 = s1->data().toMap().value(QLatin1String("value")).toInt(); 0253 int c2 = -1; 0254 0255 { 0256 Provider p2; 0257 auto s2 = new StartCountSource; 0258 s2->setTelemetryMode(Provider::BasicUsageStatistics); 0259 p2.addDataSource(s2); 0260 c2 = s2->data().toMap().value(QLatin1String("value")).toInt(); 0261 } 0262 0263 QVERIFY(c2 == c1 + 1); 0264 } 0265 0266 Provider p3; 0267 auto s3 = new StartCountSource; 0268 s3->setTelemetryMode(Provider::BasicUsageStatistics); 0269 p3.addDataSource(s3); 0270 int c3 = s3->data().toMap().value(QLatin1String("value")).toInt(); 0271 QVERIFY(c3 == c1 + 2); 0272 } 0273 0274 void testProductIdChange() 0275 { 0276 { 0277 Provider p0; 0278 } 0279 0280 int c1 = -1; 0281 { 0282 Provider p1; 0283 auto s1 = new StartCountSource; 0284 s1->setTelemetryMode(Provider::BasicUsageStatistics); 0285 p1.addDataSource(s1); 0286 p1.setSurveyInterval(90); 0287 const auto c0 = s1->data().toMap().value(QLatin1String("value")).toInt(); 0288 0289 p1.setProductIdentifier(QStringLiteral("org.kde.some.other.test")); 0290 p1.setSurveyInterval(-1); 0291 p1.setSurveyInterval(1); 0292 c1 = s1->data().toMap().value(QLatin1String("value")).toInt(); 0293 QVERIFY(c0 != c1); 0294 } 0295 0296 { 0297 Provider p2; 0298 auto s2 = new StartCountSource; 0299 s2->setTelemetryMode(Provider::BasicUsageStatistics); 0300 p2.addDataSource(s2); 0301 p2.setProductIdentifier(QStringLiteral("org.kde.some.other.test")); 0302 QCOMPARE(p2.surveyInterval(), 1); 0303 const auto c2 = s2->data().toMap().value(QLatin1String("value")).toInt(); 0304 QCOMPARE(c1 + 1, c2); 0305 } 0306 } 0307 0308 void testGlobalKillSwitch() 0309 { 0310 { 0311 Provider p1; 0312 QVERIFY(p1.isEnabled()); 0313 0314 p1.setEnabled(false); 0315 QVERIFY(!p1.isEnabled()); 0316 } 0317 0318 Provider p2; 0319 QVERIFY(!p2.isEnabled()); 0320 0321 // check encouragements are disabled 0322 { 0323 QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.providertest"));; 0324 s.beginGroup(QLatin1String("UserFeedback")); 0325 s.remove(QLatin1String("LastEncouragement")); 0326 } 0327 0328 { 0329 Provider p; 0330 p.setSurveyInterval(-1); 0331 p.setTelemetryMode(Provider::NoTelemetry); 0332 QSignalSpy spy(&p, SIGNAL(showEncouragementMessage())); 0333 QVERIFY(spy.isValid()); 0334 p.setEncouragementDelay(0); 0335 p.setApplicationStartsUntilEncouragement(0); 0336 p.setEncouragementInterval(1); 0337 QVERIFY(!spy.wait(100)); 0338 } 0339 } 0340 0341 void testDataSourceLookup() 0342 { 0343 Provider p; 0344 0345 auto screenInfoDataSource = new ScreenInfoSource; 0346 auto screenInfoDataSourceId = screenInfoDataSource->id(); 0347 0348 auto platformInfoDataSource = new PlatformInfoSource; 0349 auto platformInfoDataSourceId = platformInfoDataSource->id(); 0350 0351 p.addDataSource(screenInfoDataSource); 0352 p.addDataSource(platformInfoDataSource); 0353 0354 QCOMPARE(screenInfoDataSource, p.dataSource(screenInfoDataSourceId)); 0355 QCOMPARE(platformInfoDataSource, p.dataSource(platformInfoDataSourceId)); 0356 0357 QVERIFY(!p.dataSource(QStringLiteral("SomeInvalidId"))); 0358 } 0359 }; 0360 0361 QTEST_GUILESS_MAIN(ProviderTest) 0362 0363 #include "providertest.moc"