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 <console/core/sample.h>
0008 #include <console/core/schemaentrytemplates.h>
0009 #include <console/model/datamodel.h>
0010 
0011 #include <QDebug>
0012 #include <QtTest/qtest.h>
0013 #include <QObject>
0014 #include <QStandardPaths>
0015 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0016 #include <QAbstractItemModelTester>
0017 #endif
0018 
0019 using namespace KUserFeedback::Console;
0020 
0021 class DataModelTest : public QObject
0022 {
0023     Q_OBJECT
0024 private:
0025     int findColumn(QAbstractItemModel *model, const QString &name)
0026     {
0027         for (int i = 0; i < model->columnCount(); ++i) {
0028             if (model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString() == name)
0029                 return i;
0030         }
0031         return -1;
0032     }
0033 
0034 private Q_SLOTS:
0035     void initTestCase()
0036     {
0037         Q_INIT_RESOURCE(schematemplates);
0038         QStandardPaths::setTestModeEnabled(true);
0039     }
0040 
0041     void testEmptyDataModel()
0042     {
0043         DataModel model;
0044 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0045         QAbstractItemModelTester modelTest(&model);
0046 #endif
0047 
0048         model.setProduct({});
0049         QCOMPARE(model.rowCount(), 0);
0050         QCOMPARE(model.columnCount(), 1);
0051 
0052         Product p;
0053         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0054             p.addTemplate(tpl);
0055         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0056         model.setProduct(p);
0057         QVERIFY(model.columnCount() > 8);
0058     }
0059 
0060     void testDataModelContent()
0061     {
0062         DataModel model;
0063 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0064         QAbstractItemModelTester modelTest(&model);
0065 #endif
0066         Product p;
0067         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0068             p.addTemplate(tpl);
0069         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0070         model.setProduct(p);
0071 
0072         auto samples = Sample::fromJson(R"([{
0073             "id": 42,
0074             "timestamp": "2016-11-27 16:09:06",
0075             "platform": { "os": "linux", "version": "suse" },
0076             "applicationVersion": { "value": "1.9.84" },
0077             "screens": [ { "width": 1920, "height": 1200 }, { "width": 640, "height": 480 } ],
0078             "newPropertyRatio": { "value1": { "property": 0.55 }, "value2": { "property": 0.45 } }
0079         }, {
0080             "id": 43,
0081             "timestamp": "2016-12-09 12:00:00",
0082             "platform": { "os": "linux", "version": "suse" },
0083             "applicationVersion": { "value": "1.9.84" },
0084             "screens": [ { "width": 1920, "height": 1200 } ],
0085             "newPropertyRatio": { "value1": { "property": 0.1 }, "value2": { "property": 0.9 } }
0086         }])", p);
0087         QCOMPARE(samples.size(), 2);
0088         model.setSamples(samples);
0089         QCOMPARE(model.rowCount(), 2);
0090 
0091         auto colIdx = findColumn(&model, QLatin1String("platform.os"));
0092         QCOMPARE(model.index(0, colIdx).data(Qt::DisplayRole).toString(), QLatin1String("linux"));
0093         colIdx = findColumn(&model, QLatin1String("platform.version"));
0094         QCOMPARE(model.index(0, colIdx).data(Qt::DisplayRole).toString(), QLatin1String("suse"));
0095         colIdx = findColumn(&model, QLatin1String("applicationVersion.value"));
0096         QCOMPARE(model.index(0, colIdx).data(Qt::DisplayRole).toString(), QLatin1String("1.9.84"));
0097 
0098         colIdx = findColumn(&model, QLatin1String("screens"));
0099         QCOMPARE(model.index(0, colIdx).data(Qt::DisplayRole).toString(), QLatin1String("[{height: 1200, width: 1920}, {height: 480, width: 640}]"));
0100 
0101         colIdx = findColumn(&model, QLatin1String("newPropertyRatio"));
0102         QCOMPARE(model.index(0, colIdx).data(Qt::DisplayRole).toString(), QLatin1String("value1: {property: 0.55}, value2: {property: 0.45}"));
0103 
0104         const auto sample = model.index(1, 1).data(DataModel::SampleRole).value<Sample>();
0105         QCOMPARE(sample.timestamp().date(), QDate(2016, 12, 9));
0106     }
0107 };
0108 
0109 QTEST_MAIN(DataModelTest)
0110 
0111 #include "datamodeltest.moc"