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/model/categoryaggregationmodel.h>
0008 #include <console/model/datamodel.h>
0009 #include <console/model/timeaggregationmodel.h>
0010 #include <console/core/sample.h>
0011 #include <console/core/schemaentrytemplates.h>
0012 
0013 #include <QDebug>
0014 #include <QtTest/qtest.h>
0015 #include <QObject>
0016 #include <QStandardPaths>
0017 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0018 #include <QAbstractItemModelTester>
0019 #endif
0020 
0021 using namespace KUserFeedback::Console;
0022 
0023 class CategoryAggregationModelTest : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void initTestCase()
0028     {
0029         Q_INIT_RESOURCE(schematemplates);
0030         QStandardPaths::setTestModeEnabled(true);
0031     }
0032 
0033     void testEmptyModel()
0034     {
0035         CategoryAggregationModel model;
0036 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0037         QAbstractItemModelTester modelTest(&model);
0038 #endif
0039         model.setAggregation(Aggregation());
0040         AggregationElement aggrElem;
0041         {
0042             SchemaEntry entry;
0043             entry.setName(QLatin1String("applicationVersion"));
0044             aggrElem.setSchemaEntry(entry);
0045             SchemaEntryElement elem;
0046             elem.setName(QLatin1String("value"));
0047             aggrElem.setSchemaEntryElement(elem);
0048             aggrElem.setType(AggregationElement::Value);
0049         }
0050         Aggregation aggr;
0051         aggr.setType(Aggregation::Category);
0052         aggr.setElements({aggrElem});
0053         model.setAggregation(aggr);
0054 
0055         TimeAggregationModel timeModel;
0056         model.setSourceModel(&timeModel);
0057 
0058         DataModel srcModel;
0059         timeModel.setSourceModel(&srcModel);
0060         srcModel.setProduct({});
0061         QCOMPARE(model.rowCount(), 0);
0062         QCOMPARE(model.columnCount(), 1);
0063 
0064         Product p;
0065         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0066             p.addTemplate(tpl);
0067         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0068         srcModel.setProduct(p);
0069         QCOMPARE(model.columnCount(), 1);
0070         QCOMPARE(model.rowCount(), 0);
0071     }
0072 
0073     void testModelContentDepth1()
0074     {
0075         CategoryAggregationModel model;
0076 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0077         QAbstractItemModelTester modelTest(&model);
0078 #endif
0079         AggregationElement aggrElem;
0080         {
0081             SchemaEntry entry;
0082             entry.setName(QLatin1String("applicationVersion"));
0083             aggrElem.setSchemaEntry(entry);
0084             SchemaEntryElement elem;
0085             elem.setName(QLatin1String("value"));
0086             aggrElem.setSchemaEntryElement(elem);
0087             aggrElem.setType(AggregationElement::Value);
0088         }
0089         Aggregation aggr;
0090         aggr.setType(Aggregation::Category);
0091         aggr.setElements({aggrElem});
0092         model.setAggregation(aggr);
0093 
0094         TimeAggregationModel timeModel;
0095         model.setSourceModel(&timeModel);
0096 
0097         DataModel srcModel;
0098         timeModel.setSourceModel(&srcModel);
0099         timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
0100         Product p;
0101         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0102             p.addTemplate(tpl);
0103         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0104         srcModel.setProduct(p);
0105 
0106         auto samples = Sample::fromJson(R"([
0107             { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.0" } },
0108             { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.9.84" } },
0109             { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.9.84" } },
0110             { "timestamp": "2016-11-28 12:00:00", "applicationVersion": { "value": "1.9.84" } },
0111             { "timestamp": "2016-11-28 12:00:00", "applicationVersion": { "value": "1.9.84" } },
0112             { "timestamp": "2016-11-28 12:00:00" }
0113         ])", p);
0114         QCOMPARE(samples.size(), 6);
0115         srcModel.setSamples(samples);
0116 
0117         QCOMPARE(model.columnCount(), 4);
0118         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
0119         QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("1.0"));
0120         QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("1.9.84"));
0121 
0122         QCOMPARE(model.rowCount(), 2);
0123         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0124         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0);
0125         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 1);
0126         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2);
0127         QCOMPARE(model.index(0, 3).data(TimeAggregationModel::AccumulatedDisplayRole).toInt(), 3);
0128 
0129         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
0130         QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toInt(), 1);
0131         QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toInt(), 0);
0132         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2);
0133         QCOMPARE(model.index(0, 3).data(TimeAggregationModel::AccumulatedDisplayRole).toInt(), 3);
0134 
0135         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(), 3);
0136     }
0137 
0138     void testModelContentDepth2()
0139     {
0140         const auto p = Product::fromJson(R"({
0141             "name": "depth2test",
0142             "schema": [{
0143                 "name": "platform",
0144                 "type": "scalar",
0145                 "elements": [
0146                     { "name": "os", "type": "string" },
0147                     { "name": "version", "type": "string" }
0148                 ]
0149             }],
0150             "aggregation": [{
0151                 "type": "category",
0152                 "name": "OS Details",
0153                 "elements": [
0154                     { "type": "value", "schemaEntry": "platform", "schemaEntryElement": "os" },
0155                     { "type": "value", "schemaEntry": "platform", "schemaEntryElement": "version" }
0156                 ]
0157             }]
0158         })").at(0);
0159         QVERIFY(p.isValid());
0160         QCOMPARE(p.aggregations().size(), 1);
0161 
0162         CategoryAggregationModel model;
0163 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0164         QAbstractItemModelTester modelTest(&model);
0165 #endif
0166         model.setAggregation(p.aggregations().at(0));
0167 
0168         TimeAggregationModel timeModel;
0169         model.setSourceModel(&timeModel);
0170 
0171         DataModel srcModel;
0172         timeModel.setSourceModel(&srcModel);
0173         timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
0174         srcModel.setProduct(p);
0175 
0176         auto samples = Sample::fromJson(R"([
0177             { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "windows", "version": "10" } },
0178             { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "linux", "version": "10" } },
0179             { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "linux", "version": "10" } },
0180             { "timestamp": "2016-11-28 12:00:00", "platform": { "os": "windows", "version": "10" } },
0181             { "timestamp": "2016-11-28 12:00:00", "platform": { "os": "linux", "version": "42" } },
0182             { "timestamp": "2016-11-28 12:00:00" }
0183         ])", p);
0184         QCOMPARE(samples.size(), 6);
0185         srcModel.setSamples(samples);
0186 
0187         QCOMPARE(model.columnCount(), 5);
0188         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
0189         QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("10")); // linux
0190         QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("42"));
0191         QCOMPARE(model.headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("10")); // windows
0192 
0193         QCOMPARE(model.rowCount(), 2);
0194         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0195         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0);
0196         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 2);
0197         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 0);
0198         QCOMPARE(model.index(0, 4).data(Qt::DisplayRole).toInt(), 1);
0199 
0200         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
0201         QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toInt(), 1);
0202         QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toInt(), 0);
0203         QCOMPARE(model.index(1, 3).data(Qt::DisplayRole).toInt(), 1);
0204         QCOMPARE(model.index(1, 4).data(Qt::DisplayRole).toInt(), 1);
0205 
0206         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(), 3);
0207 
0208         model.setDepth(1);
0209         QCOMPARE(model.columnCount(), 4);
0210         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
0211         QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("linux"));
0212         QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("windows"));
0213 
0214         QCOMPARE(model.rowCount(), 2);
0215         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0);
0216         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 2);
0217         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 1);
0218     }
0219 
0220     void testModelContentDepth3()
0221     {
0222         const auto p = Product::fromJson(R"({
0223             "name": "depth2test",
0224             "schema": [{
0225                 "name": "opengl",
0226                 "type": "scalar",
0227                 "elements": [
0228                     { "name": "os", "type": "string" },
0229                     { "name": "type", "type": "string" },
0230                     { "name": "version", "type": "string" }
0231                 ]
0232             }],
0233             "aggregation": [{
0234                 "type": "category",
0235                 "name": "OpenGL Details",
0236                 "elements": [
0237                     { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "os" },
0238                     { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "type" },
0239                     { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "version" }
0240                 ]
0241             }]
0242         })").at(0);
0243         QVERIFY(p.isValid());
0244         QCOMPARE(p.aggregations().size(), 1);
0245 
0246         CategoryAggregationModel model;
0247 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0248         QAbstractItemModelTester modelTest(&model);
0249 #endif
0250         model.setAggregation(p.aggregations().at(0));
0251 
0252         TimeAggregationModel timeModel;
0253         model.setSourceModel(&timeModel);
0254 
0255         DataModel srcModel;
0256         timeModel.setSourceModel(&srcModel);
0257         timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
0258         srcModel.setProduct(p);
0259 
0260         auto samples = Sample::fromJson(R"([
0261             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows", "type": "GL", "version": "2.0" } },
0262             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows", "type": "GLES", "version": "2.0" } },
0263             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows" } },
0264             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "4.4" } },
0265             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "3.1" } },
0266             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "4.4" } },
0267             { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GLES", "version": "2.0" } },
0268             { "timestamp": "2016-11-28 12:00:00" }
0269         ])", p);
0270         QCOMPARE(samples.size(), 8);
0271         srcModel.setSamples(samples);
0272 
0273         QCOMPARE(model.columnCount(), 8);
0274         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
0275         QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("3.1")); // linux GL
0276         QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("4.4")); // linux GL
0277         QCOMPARE(model.headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // linux GLES
0278         QCOMPARE(model.headerData(5, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]")); // windows empty
0279         QCOMPARE(model.headerData(6, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // windows GL
0280         QCOMPARE(model.headerData(7, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // windows GLES
0281 
0282         QCOMPARE(model.rowCount(), 2);
0283         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0284         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0); // empty
0285         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 1); // linux GL
0286         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2); // linux GL
0287         QCOMPARE(model.index(0, 4).data(Qt::DisplayRole).toInt(), 1); // linux GLES
0288         QCOMPARE(model.index(0, 5).data(Qt::DisplayRole).toInt(), 1); // windows empty
0289         QCOMPARE(model.index(0, 6).data(Qt::DisplayRole).toInt(), 1); // windows GL
0290         QCOMPARE(model.index(0, 7).data(Qt::DisplayRole).toInt(), 1); // windows GLES
0291 
0292         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
0293         QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toInt(), 1);
0294         QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toInt(), 0);
0295         QCOMPARE(model.index(1, 3).data(Qt::DisplayRole).toInt(), 0);
0296         QCOMPARE(model.index(1, 4).data(Qt::DisplayRole).toInt(), 0);
0297         QCOMPARE(model.index(1, 5).data(Qt::DisplayRole).toInt(), 0);
0298         QCOMPARE(model.index(1, 6).data(Qt::DisplayRole).toInt(), 0);
0299         QCOMPARE(model.index(1, 7).data(Qt::DisplayRole).toInt(), 0);
0300     }
0301 };
0302 
0303 QTEST_MAIN(CategoryAggregationModelTest)
0304 
0305 #include "categoryaggregationmodeltest.moc"