File indexing completed on 2024-03-03 04:04:22

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include <QAbstractItemModel>
0008 #include <QJsonObject>
0009 #include <QObject>
0010 #include <QQmlApplicationEngine>
0011 #include <QQmlContext>
0012 #include <QSignalSpy>
0013 #include <QStandardItemModel>
0014 #include <QTest>
0015 
0016 #include <memory>
0017 
0018 #ifdef IMPORT_ITEMMODELSPLUGIN
0019 #include <QPluginLoader>
0020 Q_IMPORT_PLUGIN(Plugin)
0021 #endif
0022 
0023 class tst_KRoleNamesQml : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void testAttachedNonModel();
0028     void testAttachedInvalidRole();
0029     void testAttachedInvalidRoleName();
0030     void testAttachedRoleToRoleName();
0031     void testAttachedRoleNameToRole();
0032 
0033 private:
0034     QAbstractItemModel *createMonthTestModel(QObject *parent = nullptr);
0035 };
0036 
0037 QAbstractItemModel *tst_KRoleNamesQml::createMonthTestModel(QObject *parent)
0038 {
0039     auto testModel = new QStandardItemModel(parent);
0040     for (int i = 1; i <= 12; i++) {
0041         auto entry = new QStandardItem();
0042         entry->setData(QLocale::c().monthName(i), Qt::DisplayRole);
0043         entry->setData(i, Qt::UserRole);
0044         testModel->appendRow(entry);
0045     }
0046     testModel->setItemRoleNames({{Qt::UserRole, "user"}, {Qt::DisplayRole, "display"}});
0047     return testModel;
0048 }
0049 
0050 void tst_KRoleNamesQml::testAttachedNonModel()
0051 {
0052     QTest::ignoreMessage(QtWarningMsg, QRegularExpression(".* KRoleNames must be attached to a QAbstractItemModel"));
0053     QQmlApplicationEngine app;
0054     app.loadData(R"(
0055         import QtQml
0056         import org.kde.kitemmodels as KItemModels
0057 
0058         QtObject {
0059             property int role
0060 
0061             Component.onCompleted: {
0062                 role = KItemModels.KRoleNames.role("display");
0063             }
0064         }
0065     )");
0066     QCOMPARE(app.rootObjects().count(), 1);
0067 }
0068 
0069 void tst_KRoleNamesQml::testAttachedInvalidRole()
0070 {
0071     QQmlApplicationEngine app;
0072     std::unique_ptr<QStandardItemModel> model = std::make_unique<QStandardItemModel>();
0073     model->setItemRoleNames({});
0074     app.setInitialProperties({{"model", QVariant::fromValue(&*model)}});
0075     app.loadData(R"(
0076         import QtQml
0077         import org.kde.kitemmodels as KItemModels
0078 
0079         QtObject {
0080             required property var model
0081             property int role
0082 
0083             Component.onCompleted: {
0084                 role = model.KItemModels.KRoleNames.role(Qt.UserRole);
0085             }
0086         }
0087     )");
0088     QCOMPARE(app.rootObjects().count(), 1);
0089     const auto object = app.rootObjects().first();
0090     const auto role = object->property("role").toInt();
0091     QCOMPARE(role, -1);
0092 }
0093 
0094 void tst_KRoleNamesQml::testAttachedInvalidRoleName()
0095 {
0096     QQmlApplicationEngine app;
0097     std::unique_ptr<QStandardItemModel> model = std::make_unique<QStandardItemModel>();
0098     model->setItemRoleNames({});
0099     app.setInitialProperties({{"model", QVariant::fromValue(&*model)}});
0100     app.loadData(R"(
0101         import QtQml
0102         import org.kde.kitemmodels as KItemModels
0103 
0104         QtObject {
0105             required property var model
0106             property string roleName
0107 
0108             Component.onCompleted: {
0109                 roleName = model.KItemModels.KRoleNames.roleName(Qt.UserRole);
0110             }
0111         }
0112     )");
0113     QCOMPARE(app.rootObjects().count(), 1);
0114     const auto object = app.rootObjects().first();
0115     const auto roleName = object->property("roleName").toString();
0116     QCOMPARE(roleName, "");
0117 }
0118 
0119 void tst_KRoleNamesQml::testAttachedRoleNameToRole()
0120 {
0121     QQmlApplicationEngine app;
0122     std::unique_ptr<QAbstractItemModel> model{createMonthTestModel(nullptr)};
0123     app.setInitialProperties({{"model", QVariant::fromValue(&*model)}});
0124     app.loadData(R"(
0125         import QtQml
0126         import org.kde.kitemmodels as KItemModels
0127 
0128         QtObject {
0129             required property var model
0130             property int role
0131 
0132             Component.onCompleted: {
0133                 role = model.KItemModels.KRoleNames.role("user");
0134             }
0135         }
0136     )");
0137     QCOMPARE(app.rootObjects().count(), 1);
0138     const auto object = app.rootObjects().first();
0139     const auto role = object->property("role").toInt();
0140     QCOMPARE(role, Qt::UserRole);
0141 }
0142 
0143 void tst_KRoleNamesQml::testAttachedRoleToRoleName()
0144 {
0145     QQmlApplicationEngine app;
0146     std::unique_ptr<QAbstractItemModel> model{createMonthTestModel(nullptr)};
0147     app.setInitialProperties({{"model", QVariant::fromValue(&*model)}});
0148     app.loadData(R"(
0149         import QtQml
0150         import org.kde.kitemmodels as KItemModels
0151 
0152         QtObject {
0153             required property var model
0154             property string roleName
0155 
0156             Component.onCompleted: {
0157                 roleName = model.KItemModels.KRoleNames.roleName(Qt.UserRole);
0158             }
0159         }
0160     )");
0161     QCOMPARE(app.rootObjects().count(), 1);
0162     const auto object = app.rootObjects().first();
0163     const auto roleName = object->property("roleName").toString();
0164     QCOMPARE(roleName, "user");
0165 }
0166 
0167 QTEST_GUILESS_MAIN(tst_KRoleNamesQml)
0168 
0169 #include "krolenames_qml.moc"