From 57ee01b2392298e961b8418e31a3600583eaf221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Thu, 21 Dec 2023 14:46:09 +0100 Subject: [PATCH] chore(StatusQ): ListModelWrapper excluded to a separate file --- ui/StatusQ/tests/CMakeLists.txt | 6 +- ui/StatusQ/tests/main.cpp | 4 +- .../src/TestHelpers/listmodelwrapper.cpp | 118 ++++++++ .../tests/src/TestHelpers/listmodelwrapper.h | 37 +++ ui/StatusQ/tests/tst_ConcatModel.cpp | 281 ++++++------------ ui/StatusQ/tests/tst_SubmodelProxyModel.cpp | 80 +---- 6 files changed, 253 insertions(+), 273 deletions(-) create mode 100644 ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.cpp create mode 100644 ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.h diff --git a/ui/StatusQ/tests/CMakeLists.txt b/ui/StatusQ/tests/CMakeLists.txt index a5f0821542..67be198da3 100644 --- a/ui/StatusQ/tests/CMakeLists.txt +++ b/ui/StatusQ/tests/CMakeLists.txt @@ -13,6 +13,8 @@ find_package(Qt5 COMPONENTS QuickTest Qml Quick WebEngine REQUIRED) add_library(StatusQTestLib src/TestHelpers/MonitorQtOutput.cpp src/TestHelpers/MonitorQtOutput.h + src/TestHelpers/listmodelwrapper.cpp + src/TestHelpers/listmodelwrapper.h src/TestHelpers/modelaccessobserverproxy.cpp src/TestHelpers/modelaccessobserverproxy.h ) @@ -55,7 +57,7 @@ target_link_libraries(LeftJoinModelTest PRIVATE Qt5::Test StatusQ) add_test(NAME LeftJoinModelTest COMMAND LeftJoinModelTest) add_executable(SubmodelProxyModelTest tst_SubmodelProxyModel.cpp) -target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ) +target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib) add_test(NAME SubmodelProxyModelTest COMMAND SubmodelProxyModelTest) add_executable(AggregatorTest tst_Aggregator.cpp) @@ -71,5 +73,5 @@ target_link_libraries(SumAggregatorTest PRIVATE Qt5::Test StatusQ) add_test(NAME SumAggregatorTest COMMAND SumAggregatorTest) add_executable(ConcatModelTest tst_ConcatModel.cpp) -target_link_libraries(ConcatModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ) +target_link_libraries(ConcatModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib) add_test(NAME ConcatModelTest COMMAND ConcatModelTest) diff --git a/ui/StatusQ/tests/main.cpp b/ui/StatusQ/tests/main.cpp index c271286a6e..bc5a6d5583 100644 --- a/ui/StatusQ/tests/main.cpp +++ b/ui/StatusQ/tests/main.cpp @@ -3,8 +3,8 @@ #include -#include "TestHelpers/MonitorQtOutput.h" -#include "TestHelpers/modelaccessobserverproxy.h" +#include +#include class RunBeforeQApplicationIsInitialized { public: diff --git a/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.cpp b/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.cpp new file mode 100644 index 0000000000..4ffbe31c12 --- /dev/null +++ b/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.cpp @@ -0,0 +1,118 @@ +#include "listmodelwrapper.h" + +#include +#include +#include +#include +#include +#include +#include + +ListModelWrapper::ListModelWrapper(QQmlEngine& engine, const QString& content) +{ + QQmlComponent component(&engine); + auto componentBody = QStringLiteral(R"( + import QtQml 2.15 + import QtQml.Models 2.15 + + ListModel { + Component.onCompleted: { + const content = %1 + + if (content.length) + append(content) + } + } + )").arg(content); + + component.setData(componentBody.toUtf8(), {}); + + m_model.reset(qobject_cast( + component.create(engine.rootContext()))); +} + +ListModelWrapper::ListModelWrapper(QQmlEngine& engine, const QJsonArray& content) + : ListModelWrapper(engine, QJsonDocument(content).toJson()) +{ +} + +QAbstractItemModel* ListModelWrapper::model() const +{ + return m_model.get(); +} + +ListModelWrapper::operator QAbstractItemModel*() const +{ + return model(); +} + +int ListModelWrapper::count() const +{ + return m_model->rowCount(); +} + +int ListModelWrapper::role(const QString& roleName) +{ + QHash roleNames = m_model->roleNames(); + QList roles = roleNames.keys(roleName.toUtf8()); + + return roles.length() != 1 ? -1 : roles.first(); +} + +void ListModelWrapper::set(int index, const QJsonObject& dict) +{ + QString jsonDict = QJsonDocument(dict).toJson(); + runExpression(QString("set(%1, %2)").arg(index).arg(jsonDict)); +} + +void ListModelWrapper::setProperty(int index, const QString& property, + const QVariant& value) +{ + QString valueStr = value.type() == QVariant::String + ? QString("'%1'").arg(value.toString()) + : value.toString(); + + runExpression(QString("setProperty(%1, '%2', %3)").arg(index) + .arg(property, valueStr)); +} + +QVariant ListModelWrapper::get(int index, const QString& roleName) +{ + auto role = this->role(roleName); + + if (role == -1) + return {}; + + return m_model->data(m_model->index(index, 0), role); +} + +void ListModelWrapper::insert(int index, const QJsonObject& dict) { + QString jsonDict = QJsonDocument(dict).toJson(); + runExpression(QString("insert(%1, %2)").arg(index).arg(jsonDict)); +} + +void ListModelWrapper::append(const QJsonArray& data) { + QString jsonData = QJsonDocument(data).toJson(); + runExpression(QString("append(%1)").arg(jsonData)); +} + +void ListModelWrapper::clear() { + runExpression(QString("clear()")); +} + +void ListModelWrapper::remove(int index, int count) { + runExpression(QString("remove(%1, %2)").arg(QString::number(index), + QString::number(count))); +} + +void ListModelWrapper::move(int from, int to, int n) { + runExpression(QString("move(%1, %2, %3)").arg(QString::number(from), + QString::number(to), + QString::number(n))); +} + +void ListModelWrapper::runExpression(const QString& expression) +{ + QQmlExpression(QQmlEngine::contextForObject(m_model.get()), + m_model.get(), expression).evaluate(); +} diff --git a/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.h b/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.h new file mode 100644 index 0000000000..4a72ad8fa0 --- /dev/null +++ b/ui/StatusQ/tests/src/TestHelpers/listmodelwrapper.h @@ -0,0 +1,37 @@ +#include + +#include + +class QJsonArray; +class QJsonObject; +class QQmlEngine; +class QAbstractItemModel; + +class ListModelWrapper { + +public: + explicit ListModelWrapper(QQmlEngine& engine, const QString& content = "[]"); + explicit ListModelWrapper(QQmlEngine& engine, const QJsonArray& content); + + QAbstractItemModel* model() const; + operator QAbstractItemModel*() const; + + int count() const; + int role(const QString& roleName); + + void set(int index, const QJsonObject& dict); + void setProperty(int index, const QString& property, const QVariant& value); + + QVariant get(int index, const QString& roleName); + + void insert(int index, const QJsonObject& dict); + void append(const QJsonArray& data); + void clear(); + void remove(int index, int count = 1); + void move(int from, int to, int n = 1); + +private: + void runExpression(const QString& expression); + + std::unique_ptr m_model; +}; diff --git a/ui/StatusQ/tests/tst_ConcatModel.cpp b/ui/StatusQ/tests/tst_ConcatModel.cpp index d21680cbca..2e33520cda 100644 --- a/ui/StatusQ/tests/tst_ConcatModel.cpp +++ b/ui/StatusQ/tests/tst_ConcatModel.cpp @@ -1,20 +1,15 @@ #include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include - #include #include -#include #include +#include namespace { // Workaround for https://bugreports.qt.io/browse/QTBUG-57971 (ListModel doesn't @@ -29,116 +24,6 @@ public: } }; -class ListModelWrapper { - -public: - explicit ListModelWrapper(QQmlEngine& engine, const QString& content = "[]") - { - QQmlComponent component(&engine); - auto componentBody = QStringLiteral(R"( - import QtQml 2.15 - import QtQml.Models 2.15 - - ListModel { - Component.onCompleted: { - const content = %1 - - if (content.length) - append(content) - } - } - )").arg(content); - - component.setData(componentBody.toUtf8(), {}); - - m_model.reset(qobject_cast( - component.create(engine.rootContext()))); - } - - explicit ListModelWrapper(QQmlEngine& engine, const QJsonArray& content) - : ListModelWrapper(engine, QJsonDocument(content).toJson()) - { - } - - QAbstractItemModel* model() const - { - return m_model.get(); - } - - int count() const - { - return m_model->rowCount(); - } - - int role(const QString& roleName) - { - QHash roleNames = m_model->roleNames(); - QList roles = roleNames.keys(roleName.toUtf8()); - - return roles.length() != 1 ? -1 : roles.first(); - } - - void set(int index, const QJsonObject& dict) - { - QString jsonDict = QJsonDocument(dict).toJson(); - runExpression(QString("set(%1, %2)").arg(index).arg(jsonDict)); - } - - void setProperty(int index, const QString& property, const QVariant& value) - { - QString valueStr = value.type() == QVariant::String - ? QString("'%1'").arg(value.toString()) - : value.toString(); - - runExpression(QString("setProperty(%1, '%2', %3)").arg(index) - .arg(property, valueStr)); - } - - QVariant get(int index, const QString& roleName) - { - auto role = this->role(roleName); - - if (role == -1) - return {}; - - return m_model->data(m_model->index(index, 0), role); - } - - void insert(int index, const QJsonObject& dict) { - QString jsonDict = QJsonDocument(dict).toJson(); - runExpression(QString("insert(%1, %2)").arg(index).arg(jsonDict)); - } - - void append(const QJsonArray& data) { - QString jsonData = QJsonDocument(data).toJson(); - runExpression(QString("append(%1)").arg(jsonData)); - } - - void clear() { - runExpression(QString("clear()")); - } - - void remove(int index, int count = 1) { - runExpression(QString("remove(%1, %2)").arg(QString::number(index), - QString::number(count))); - } - - void move(int from, int to, int n = 1) { - runExpression(QString("move(%1, %2, %3)").arg(QString::number(from), - QString::number(to), - QString::number(n))); - } - -private: - void runExpression(const QString& expression) - { - QQmlExpression(QQmlEngine::contextForObject(m_model.get()), - m_model.get(), expression).evaluate(); - } - - std::unique_ptr m_model; -}; - } // unnamed namespace class TestConcatModel: public QObject @@ -181,16 +66,16 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); SourceModel source2; - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); SourceModel source3; - source3.setModel(sourceModel3.model()); + source3.setModel(sourceModel3); SourceModel source4; - source4.setModel(sourceModel4.model()); + source4.setModel(sourceModel4); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -258,13 +143,13 @@ private slots: QCOMPARE(model.data(model.index(0, 0), roleOutOfRange), {}); // getting source model and source model row - QCOMPARE(model.sourceModel(0), sourceModel1.model()); - QCOMPARE(model.sourceModel(1), sourceModel1.model()); - QCOMPARE(model.sourceModel(2), sourceModel1.model()); - QCOMPARE(model.sourceModel(3), sourceModel3.model()); - QCOMPARE(model.sourceModel(4), sourceModel3.model()); - QCOMPARE(model.sourceModel(5), sourceModel3.model()); - QCOMPARE(model.sourceModel(6), sourceModel3.model()); + QCOMPARE(model.sourceModel(0), sourceModel1); + QCOMPARE(model.sourceModel(1), sourceModel1); + QCOMPARE(model.sourceModel(2), sourceModel1); + QCOMPARE(model.sourceModel(3), sourceModel3); + QCOMPARE(model.sourceModel(4), sourceModel3); + QCOMPARE(model.sourceModel(5), sourceModel3); + QCOMPARE(model.sourceModel(6), sourceModel3); QCOMPARE(model.sourceModel(7), nullptr); QCOMPARE(model.sourceModel(-1), nullptr); @@ -281,17 +166,17 @@ private slots: // getting row by source model source model row QCOMPARE(model.fromSourceRow(nullptr, 0), -1); - QCOMPARE(model.fromSourceRow(sourceModel1.model(), 0), 0); - QCOMPARE(model.fromSourceRow(sourceModel1.model(), 1), 1); - QCOMPARE(model.fromSourceRow(sourceModel1.model(), 2), 2); - QCOMPARE(model.fromSourceRow(sourceModel1.model(), 3), -1); - QCOMPARE(model.fromSourceRow(sourceModel1.model(), -1), -1); - QCOMPARE(model.fromSourceRow(sourceModel2.model(), 0), -1); - QCOMPARE(model.fromSourceRow(sourceModel3.model(), 0), 3); - QCOMPARE(model.fromSourceRow(sourceModel3.model(), 1), 4); - QCOMPARE(model.fromSourceRow(sourceModel3.model(), 2), 5); - QCOMPARE(model.fromSourceRow(sourceModel3.model(), 3), 6); - QCOMPARE(model.fromSourceRow(sourceModel3.model(), 4), -1); + QCOMPARE(model.fromSourceRow(sourceModel1, 0), 0); + QCOMPARE(model.fromSourceRow(sourceModel1, 1), 1); + QCOMPARE(model.fromSourceRow(sourceModel1, 2), 2); + QCOMPARE(model.fromSourceRow(sourceModel1, 3), -1); + QCOMPARE(model.fromSourceRow(sourceModel1, -1), -1); + QCOMPARE(model.fromSourceRow(sourceModel2, 0), -1); + QCOMPARE(model.fromSourceRow(sourceModel3, 0), 3); + QCOMPARE(model.fromSourceRow(sourceModel3, 1), 4); + QCOMPARE(model.fromSourceRow(sourceModel3, 2), 5); + QCOMPARE(model.fromSourceRow(sourceModel3, 3), 6); + QCOMPARE(model.fromSourceRow(sourceModel3, 4), -1); } void dataChangeTest() @@ -319,16 +204,16 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); SourceModel source2; - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); SourceModel source3; - source3.setModel(sourceModel3.model()); + source3.setModel(sourceModel3); SourceModel source4; - source4.setModel(sourceModel4.model()); + source4.setModel(sourceModel4); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -470,8 +355,8 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1, source2; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -521,16 +406,16 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); SourceModel source2; - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); SourceModel source3; - source3.setModel(sourceModel3.model()); + source3.setModel(sourceModel3); SourceModel source4; - source4.setModel(sourceModel4.model()); + source4.setModel(sourceModel4); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -690,10 +575,10 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); SourceModel source2; - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -814,7 +699,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 0); }); - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); } QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); @@ -846,7 +731,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 2); }); - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); } QCOMPARE(rowsInsertedSpy.count(), 1); @@ -888,8 +773,8 @@ private slots: model.componentComplete(); - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); QCOMPARE(model.rowCount(), 0); QCOMPARE(model.roleNames(), {}); @@ -986,7 +871,7 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1012,7 +897,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 2); }); - source2.setModel(sourceModel2.model()); + source2.setModel(sourceModel2); } QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); @@ -1048,7 +933,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 4); }); - source3.setModel(sourceModel3.model()); + source3.setModel(sourceModel3); } QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); @@ -1096,9 +981,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1235,9 +1120,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1271,7 +1156,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 4); }); - source2.setModel(sourceModel4.model()); + source2.setModel(sourceModel4); } QCOMPARE(model.rowCount(), 7); @@ -1358,9 +1243,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2->model()); - source3.setModel(sourceModel3.model()); + source1.setModel(sourceModel1); + source2.setModel(*sourceModel2); + source3.setModel(sourceModel3); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1425,9 +1310,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1502,9 +1387,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1573,8 +1458,8 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -1626,9 +1511,9 @@ private slots: IdentityModel proxy1, proxy2, proxy3; - proxy1.setSourceModel(sourceModel1.model()); - proxy2.setSourceModel(sourceModel2.model()); - proxy3.setSourceModel(sourceModel3.model()); + proxy1.setSourceModel(sourceModel1); + proxy2.setSourceModel(sourceModel2); + proxy3.setSourceModel(sourceModel3); source1.setModel(&proxy1); source2.setModel(&proxy2); @@ -1654,7 +1539,7 @@ private slots: QSignalSpy rowsAboutToBeInsertedSpy(&model, &ConcatModel::rowsAboutToBeInserted); QSignalSpy rowsInsertedSpy(&model, &ConcatModel::rowsInserted); - proxy2.setSourceModel(sourceModel4.model()); + proxy2.setSourceModel(sourceModel4); QCOMPARE(modelAboutToBeResetSpy.count(), 0); QCOMPARE(modelResetSpy.count(), 0); @@ -1678,7 +1563,7 @@ private slots: connect(&model, &ConcatModel::rowsAboutToBeInserted, &context, [&model] { QCOMPARE(model.rowCount(), 0); }); - proxy2.setSourceModel(sourceModel5.model()); + proxy2.setSourceModel(sourceModel5); } QCOMPARE(modelAboutToBeResetSpy.count(), 0); @@ -1735,9 +1620,9 @@ private slots: IdentityModel proxy1, proxy2, proxy3; - proxy1.setSourceModel(sourceModel1.model()); - proxy2.setSourceModel(sourceModel2.model()); - proxy3.setSourceModel(sourceModel3.model()); + proxy1.setSourceModel(sourceModel1); + proxy2.setSourceModel(sourceModel2); + proxy3.setSourceModel(sourceModel3); source1.setModel(&proxy1); source2.setModel(&proxy2); @@ -1780,7 +1665,7 @@ private slots: QCOMPARE(model.data(model.index(3, 0), roleForName(roles, "color")), {}); }); - proxy2.setSourceModel(sourceModel4.model()); + proxy2.setSourceModel(sourceModel4); } QCOMPARE(modelAboutToBeResetSpy.count(), 0); @@ -1856,7 +1741,7 @@ private slots: QCOMPARE(model.rowCount(), 0); }); - proxy1.setSourceModel(sourceModel5.model()); + proxy1.setSourceModel(sourceModel5); } QCOMPARE(modelAboutToBeResetSpy.count(), 0); @@ -1901,9 +1786,9 @@ private slots: SourceModel source1, source2, source3; - source1.setModel(sourceModel.model()); - source2.setModel(sourceModel.model()); - source3.setModel(sourceModel.model()); + source1.setModel(sourceModel); + source2.setModel(sourceModel); + source3.setModel(sourceModel); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -2004,7 +1889,7 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1, source2, source3; - source1.setModel(sourceModel1.model()); + source1.setModel(sourceModel1); sources.append(&sources, &source1); sources.append(&sources, &source2); @@ -2027,8 +1912,8 @@ private slots: std::set expectedRoleNamesSet({"key", "color", "value", "whichModel"}); QCOMPARE(roleNamesSet, expectedRoleNamesSet); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); QCOMPARE(model.rowCount(), 7); } @@ -2058,10 +1943,10 @@ private slots: QQmlListProperty sources = model.sources(); SourceModel source1, source2, source3, source4; - source1.setModel(sourceModel1.model()); - source2.setModel(sourceModel2.model()); - source3.setModel(sourceModel3.model()); - source4.setModel(sourceModel4.model()); + source1.setModel(sourceModel1); + source2.setModel(sourceModel2); + source3.setModel(sourceModel3); + source4.setModel(sourceModel4); source1.setMarkerRoleValue("model 1"); source2.setMarkerRoleValue("model 2"); diff --git a/ui/StatusQ/tests/tst_SubmodelProxyModel.cpp b/ui/StatusQ/tests/tst_SubmodelProxyModel.cpp index 6642f68246..3e00d64887 100644 --- a/ui/StatusQ/tests/tst_SubmodelProxyModel.cpp +++ b/ui/StatusQ/tests/tst_SubmodelProxyModel.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -12,68 +11,7 @@ #include #include - -namespace { - -class ListViewWrapper { - -public: - explicit ListViewWrapper(QQmlEngine& engine, const QString& content = "[]") - { - QQmlComponent component(&engine); - auto componentBody = QStringLiteral(R"( - import QtQml 2.15 - import QtQml.Models 2.15 - - ListModel { - Component.onCompleted: append(%1) - } - )").arg(content); - - component.setData(componentBody.toUtf8(), QUrl()); - - m_model.reset(qobject_cast( - component.create(engine.rootContext()))); - } - - explicit ListViewWrapper(QQmlEngine& engine, const QJsonArray& content) - : ListViewWrapper(engine, QJsonDocument(content).toJson()) - { - } - - QAbstractItemModel* model() const - { - return m_model.get(); - } - - int count() const - { - return m_model->rowCount(); - } - - int role(const QString& roleName) - { - QHash roleNames = m_model->roleNames(); - QList roles = roleNames.keys(roleName.toUtf8()); - - return roles.length() != 1 ? -1 : roles.first(); - } - - QVariant get(int index, const QString& roleName) - { - auto role = this->role(roleName); - - if (role == -1) - return {}; - - return m_model->data(m_model->index(index, 0), role); - } - -private: - std::unique_ptr m_model; -}; - -} // unnamed namespace +#include class TestSubmodelProxyModel: public QObject { @@ -94,7 +32,7 @@ private slots: delegate.setData(delegateData, QUrl()); SubmodelProxyModel model; - ListViewWrapper sourceModel(engine, QJsonArray { + ListModelWrapper sourceModel(engine, QJsonArray { QJsonObject {{ "balances", 11 }, { "name", "name 1" }}, QJsonObject {{ "balances", 12 }, { "name", "name 2" }}, QJsonObject {{ "balances", 123}, { "name", "name 3" }}, @@ -107,7 +45,7 @@ private slots: QSignalSpy submodelRoleNameChangedSpy( &model, &SubmodelProxyModel::submodelRoleNameChanged); - model.setSourceModel(sourceModel.model()); + model.setSourceModel(sourceModel); model.setDelegateModel(&delegate); model.setSubmodelRoleName(QStringLiteral("balances")); @@ -115,7 +53,7 @@ private slots: QCOMPARE(delegateChangedSpy.count(), 1); QCOMPARE(submodelRoleNameChangedSpy.count(), 1); - QCOMPARE(model.sourceModel(), sourceModel.model()); + QCOMPARE(model.sourceModel(), sourceModel); QCOMPARE(model.delegateModel(), &delegate); QCOMPARE(model.submodelRoleName(), QStringLiteral("balances")); @@ -152,13 +90,13 @@ private slots: )"), QUrl()); SubmodelProxyModel model; - ListViewWrapper sourceModel(engine, QJsonArray { + ListModelWrapper sourceModel(engine, QJsonArray { QJsonObject {{ "balances", 11 }, { "name", "name 1" }}, QJsonObject {{ "balances", 12 }, { "name", "name 2" }}, QJsonObject {{ "balances", 123}, { "name", "name 3" }} }); - model.setSourceModel(sourceModel.model()); + model.setSourceModel(sourceModel); model.setDelegateModel(delegate.get()); model.setSubmodelRoleName(QStringLiteral("balances")); @@ -191,7 +129,7 @@ private slots: SubmodelProxyModel model; - auto sourceModel = std::make_unique(engine, + auto sourceModel = std::make_unique(engine, QJsonArray { QJsonObject {{ "balances", 11 }, { "name", "name 1" }}, QJsonObject {{ "balances", 12 }, { "name", "name 2" }}, @@ -224,13 +162,13 @@ private slots: )"), QUrl()); SubmodelProxyModel model; - ListViewWrapper sourceModel(engine, QJsonArray { + ListModelWrapper sourceModel(engine, QJsonArray { QJsonObject {{ "balances", 11 }, { "name", "name 1" }}, QJsonObject {{ "balances", 12 }, { "name", "name 2" }}, QJsonObject {{ "balances", 123}, { "name", "name 3" }} }); - model.setSourceModel(sourceModel.model()); + model.setSourceModel(sourceModel); model.setDelegateModel(delegate.get()); QTest::ignoreMessage(QtWarningMsg, "Submodel role not found!");