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 <core/product.h>
0008 #include <core/sample.h>
0009 #include <core/schemaentrytemplates.h>
0010 
0011 #include <QDebug>
0012 #include <QtTest/qtest.h>
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 
0016 using namespace KUserFeedback::Console;
0017 
0018 class SampleTest : public QObject
0019 {
0020     Q_OBJECT
0021 private Q_SLOTS:
0022     void initTestCase()
0023     {
0024         Q_INIT_RESOURCE(schematemplates);
0025         QStandardPaths::setTestModeEnabled(true);
0026     }
0027 
0028     void testFromJson()
0029     {
0030         Product p;
0031         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
0032         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0033             p.addTemplate(tpl);
0034         QVERIFY(p.isValid());
0035 
0036         auto samples = Sample::fromJson("[]", p);
0037         QVERIFY(samples.isEmpty());
0038 
0039         samples = Sample::fromJson(R"([{
0040             "id": 42,
0041             "timestamp": "2016-11-27 16:09:06",
0042             "platform": { "os": "linux", "version": "suse" },
0043             "applicationVersion": { "value": "1.9.84" },
0044             "screens": [ { "width": 1920, "height": 1200 }, { "width": 640, "height": 480 } ],
0045             "newPropertyRatio": { "value1": { "property": 0.55 }, "value2": { "property": 0.45 } }
0046         }])", p);
0047 
0048         QCOMPARE(samples.size(), 1);
0049         auto s = samples.at(0);
0050         QCOMPARE(s.timestamp(), QDateTime(QDate(2016, 11, 27), QTime(16, 9, 6)));
0051         QCOMPARE(s.value(QLatin1String("platform.os")).toString(), QLatin1String("linux"));
0052         QCOMPARE(s.value(QLatin1String("platform.version")).toString(), QLatin1String("suse"));
0053         QCOMPARE(s.value(QLatin1String("applicationVersion.value")).toString(), QLatin1String("1.9.84"));
0054         auto screens = s.value(QLatin1String("screens")).toList();
0055         QCOMPARE(screens.size(), 2);
0056         QCOMPARE(screens.at(0).toMap().value(QLatin1String("width")).toInt(), 1920);
0057         QCOMPARE(screens.at(1).toMap().value(QLatin1String("height")).toInt(), 480);
0058         auto ratio = s.value(QLatin1String("newPropertyRatio")).toMap();
0059         QCOMPARE(ratio.size(), 2);
0060         QCOMPARE(ratio.value(QLatin1String("value1")).toMap().value(QLatin1String("property")).toDouble(), 0.55);
0061         QCOMPARE(ratio.value(QLatin1String("value2")).toMap().value(QLatin1String("property")).toDouble(), 0.45);
0062     }
0063 };
0064 
0065 QTEST_MAIN(SampleTest)
0066 
0067 #include "sampletest.moc"