File indexing completed on 2024-05-19 04:01:08

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include <applicationversionsource.h>
0008 #include <compilerinfosource.h>
0009 #include <cpuinfosource.h>
0010 #include <localeinfosource.h>
0011 #include <platforminfosource.h>
0012 #include <propertyratiosource.h>
0013 #include <qpainfosource.h>
0014 #include <qtversionsource.h>
0015 #include <screeninfosource.h>
0016 #include <startcountsource.h>
0017 #include <usagetimesource.h>
0018 
0019 #include <QDebug>
0020 #include <QtTest/qtest.h>
0021 #include <QObject>
0022 #include <QSettings>
0023 #include <QStandardPaths>
0024 
0025 using namespace KUserFeedback;
0026 
0027 class DataSourceTest : public QObject
0028 {
0029     Q_OBJECT
0030     Q_PROPERTY(int prop READ prop WRITE setProp NOTIFY propChanged)
0031 public:
0032     int prop() const
0033     {
0034         return m_propValue;
0035     }
0036 
0037     void setProp(int v)
0038     {
0039         m_propValue = v;
0040         Q_EMIT propChanged();
0041     }
0042 
0043 Q_SIGNALS:
0044     void propChanged();
0045 
0046 private:
0047     int m_propValue = 42;
0048 
0049 private Q_SLOTS:
0050     void initTestCase()
0051     {
0052         QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0053         QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
0054         QStandardPaths::setTestModeEnabled(true);
0055     }
0056 
0057     void testPlatformInfoSource()
0058     {
0059         PlatformInfoSource src;
0060         auto obj = src.data().toMap();
0061         QVERIFY(obj.contains(QLatin1String("os")));
0062         auto v = obj.value(QLatin1String("os"));
0063         QCOMPARE(v.type(), QVariant::String);
0064         auto s = v.toString();
0065         QVERIFY(!s.isEmpty());
0066 
0067         QVERIFY(obj.contains(QLatin1String("version")));
0068         v = obj.value(QLatin1String("version"));
0069         QCOMPARE(v.type(), QVariant::String);
0070         s = v.toString();
0071         QVERIFY(!s.isEmpty());
0072     }
0073 
0074     void testScreenInfoSource()
0075     {
0076         ScreenInfoSource src;
0077         auto v = src.data();
0078         QVERIFY(v.canConvert<QVariantList>());
0079         auto a = v.value<QVariantList>();
0080         QVERIFY(a.size() > 0);
0081 
0082         for (int i = 0; i < a.size(); ++i) {
0083             v = a.at(i);
0084             QVERIFY(v.canConvert<QVariantMap>());
0085             const auto scr = v.toMap();
0086             QVERIFY(scr.contains(QLatin1String("height")));
0087             QVERIFY(scr.contains(QLatin1String("width")));
0088             QVERIFY(scr.contains(QLatin1String("dpi")));
0089         }
0090     }
0091 
0092     void testPropertyRatioSource()
0093     {
0094         PropertyRatioSource src(this, "prop", QStringLiteral("ratioSample"));
0095         src.addValueMapping(42, QStringLiteral("value1"));
0096         src.addValueMapping(23, QStringLiteral("value2"));
0097         QTest::qWait(1);
0098 
0099         auto v = src.data();
0100         QVERIFY(v.canConvert<QVariantMap>());
0101         auto o = v.toMap();
0102         QCOMPARE(o.size(), 0); // nothing recorded
0103 
0104         QTest::qWait(1200);
0105         v = src.data();
0106         o = v.toMap();
0107         QCOMPARE(o.size(), 1);
0108         QVERIFY(o.contains(QLatin1String("value1")));
0109         v = o.value(QLatin1String("value1")).toMap().value(QLatin1String("property"));;
0110         QCOMPARE(v.type(), QVariant::Double);
0111 
0112         setProp(23);
0113         QTest::qWait(1200);
0114         v = src.data();
0115         o = v.toMap();
0116         QCOMPARE(o.size(), 2);
0117         QVERIFY(o.contains(QLatin1String("value2")));
0118         v = o.value(QLatin1String("value2")).toMap().value(QLatin1String("property"));
0119         QCOMPARE(v.type(), QVariant::Double);
0120     }
0121 
0122     void testMultiPropertyRatioSource()
0123     {
0124         QSettings s;
0125         s.remove(QStringLiteral("MultiPropSource"));
0126         s.beginGroup(QStringLiteral("MultiPropSource"));
0127 
0128         {
0129             setProp(5198);
0130 
0131             PropertyRatioSource src1(this, "prop", QStringLiteral("ratioSample"));
0132             src1.addValueMapping(5198, QStringLiteral("value1"));
0133             src1.load(&s);
0134 
0135             PropertyRatioSource src2(this, "prop", QStringLiteral("ratioSample"));
0136             src2.addValueMapping(5198, QStringLiteral("value2"));
0137             src2.load(&s);
0138 
0139             QTest::qWait(1200);
0140 
0141             src1.store(&s);
0142             src2.store(&s);
0143         }
0144 
0145         {
0146             PropertyRatioSource src3(this, "prop", QStringLiteral("ratioSample"));
0147             src3.load(&s);
0148             const auto map = src3.data().toMap();
0149             QCOMPARE(map.size(), 2);
0150             QVERIFY(map.contains(QStringLiteral("value1")));
0151             QVERIFY(map.contains(QStringLiteral("value2")));
0152             src3.reset(&s);
0153             QVERIFY(src3.data().toMap().isEmpty());
0154         }
0155 
0156         {
0157             PropertyRatioSource src4(this, "prop", QStringLiteral("ratioSample"));
0158             src4.load(&s);
0159             const auto map = src4.data().toMap();
0160             QCOMPARE(map.size(), 0);
0161         }
0162     }
0163 
0164     void testApplicationVersionSource()
0165     {
0166         ApplicationVersionSource src;
0167         auto v = src.data();
0168         QVERIFY(v.isNull());
0169 
0170         QCoreApplication::setApplicationVersion(QStringLiteral("1.9.84"));
0171         auto m = src.data().toMap();
0172         QVERIFY(m.contains(QLatin1String("value")));
0173         QCOMPARE(m.value(QLatin1String("value")).toString(), QLatin1String("1.9.84"));
0174     }
0175 
0176     void testQtVersionSource()
0177     {
0178         QtVersionSource src;
0179         const auto m = src.data().toMap();
0180         QVERIFY(m.contains(QLatin1String("value")));
0181         QCOMPARE(m.value(QLatin1String("value")).toString(), QLatin1String(QT_VERSION_STR));
0182     }
0183 
0184     void testStartCountSource()
0185     {
0186         Provider p;
0187         auto src = new StartCountSource;
0188         QVERIFY(!src->description().isEmpty());
0189         p.addDataSource(src);
0190         const auto m = src->data().toMap();
0191         QVERIFY(m.contains(QLatin1String("value")));
0192         QVERIFY(m.value(QLatin1String("value")).toInt() >= 1);
0193     }
0194 
0195     void testUsageTimeSource()
0196     {
0197         Provider p;
0198         auto src = new UsageTimeSource;
0199         QVERIFY(!src->description().isEmpty());
0200         src->setTelemetryMode(Provider::DetailedUsageStatistics);
0201         p.addDataSource(src);
0202         QTest::qWait(1200);
0203         const auto m = src->data().toMap();
0204         QVERIFY(m.contains(QLatin1String("value")));
0205         QVERIFY(m.value(QLatin1String("value")).toInt() >= 1);
0206     }
0207 
0208     void testCpuInfoSource()
0209     {
0210         CpuInfoSource src;
0211         const auto m = src.data().toMap();
0212         QVERIFY(m.contains(QLatin1String("architecture")));
0213         QVERIFY(!m.value(QLatin1String("architecture")).toString().isEmpty());
0214         QVERIFY(m.contains(QLatin1String("count")));
0215         QVERIFY(m.value(QLatin1String("count")).toInt() >= 1);
0216     }
0217 
0218     void testLocaleInfoSource()
0219     {
0220         LocaleInfoSource src;
0221         const auto m = src.data().toMap();
0222         QVERIFY(m.contains(QLatin1String("language")));
0223         QVERIFY(!m.value(QLatin1String("language")).toString().isEmpty());
0224         QVERIFY(m.contains(QLatin1String("region")));
0225         QVERIFY(!m.value(QLatin1String("region")).toString().isEmpty());
0226     }
0227 
0228     void testCompilerInfoSource()
0229     {
0230         CompilerInfoSource src;
0231         const auto m = src.data().toMap();
0232         QVERIFY(m.contains(QLatin1String("type")));
0233         QVERIFY(!m.value(QLatin1String("type")).toString().isEmpty());
0234         QVERIFY(m.contains(QLatin1String("version")));
0235         QVERIFY(!m.value(QLatin1String("version")).toString().isEmpty());
0236     }
0237 
0238     void testQPAInfoSource()
0239     {
0240         QPAInfoSource src;
0241         QVERIFY(!src.description().isEmpty());
0242         const auto m = src.data().toMap();
0243         QVERIFY(m.contains(QLatin1String("name")));
0244         QVERIFY(!m.value(QLatin1String("name")).toString().isEmpty());
0245     }
0246 
0247     void testActiveState()
0248     {
0249         CpuInfoSource src;
0250 
0251         QVERIFY(src.isActive());
0252 
0253         src.setActive(false);
0254 
0255         QVERIFY(!src.isActive());
0256     }
0257 
0258     void testCommonActiveStateSetting()
0259     {
0260         CpuInfoSource src;
0261         src.setActive(false);
0262 
0263         QSettings s;
0264 
0265         src.store(&s);
0266 
0267         src.setActive(true);
0268 
0269         src.load(&s);
0270 
0271         QVERIFY(!src.isActive());
0272 
0273         // NOTE: reset shouldn't change global settings that might be changed by a user via UI
0274         src.reset(&s);
0275 
0276         QVERIFY(!src.isActive());
0277     }
0278 };
0279 
0280 QTEST_MAIN(DataSourceTest)
0281 
0282 #include "datasourcetest.moc"