diff --git a/ui/StatusQ/tests/CMakeLists.txt b/ui/StatusQ/tests/CMakeLists.txt index 67be198da3..06875dfc51 100644 --- a/ui/StatusQ/tests/CMakeLists.txt +++ b/ui/StatusQ/tests/CMakeLists.txt @@ -17,6 +17,8 @@ add_library(StatusQTestLib src/TestHelpers/listmodelwrapper.h src/TestHelpers/modelaccessobserverproxy.cpp src/TestHelpers/modelaccessobserverproxy.h + src/TestHelpers/testmodel.cpp + src/TestHelpers/testmodel.h ) target_link_libraries(StatusQTestLib PUBLIC Qt5::Core Qt5::Quick) @@ -49,29 +51,29 @@ add_test(NAME QmlTests WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ########### add_executable(RolesRenamingModelTest tst_RolesRenamingModel.cpp) -target_link_libraries(RolesRenamingModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ) +target_link_libraries(RolesRenamingModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME RolesRenamingModelTest COMMAND RolesRenamingModelTest) add_executable(LeftJoinModelTest tst_LeftJoinModel.cpp) -target_link_libraries(LeftJoinModelTest PRIVATE Qt5::Test StatusQ) +target_link_libraries(LeftJoinModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME LeftJoinModelTest COMMAND LeftJoinModelTest) add_executable(SubmodelProxyModelTest tst_SubmodelProxyModel.cpp) -target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib) +target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME SubmodelProxyModelTest COMMAND SubmodelProxyModelTest) add_executable(AggregatorTest tst_Aggregator.cpp) -target_link_libraries(AggregatorTest PRIVATE Qt5::Test StatusQ) +target_link_libraries(AggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME AggregatorTest COMMAND AggregatorTest) add_executable(SingleRoleAggregatorTest tst_SingleRoleAggregator.cpp) -target_link_libraries(SingleRoleAggregatorTest PRIVATE Qt5::Test StatusQ) +target_link_libraries(SingleRoleAggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME SingleRoleAggregatorTest COMMAND SingleRoleAggregatorTest) add_executable(SumAggregatorTest tst_SumAggregator.cpp) -target_link_libraries(SumAggregatorTest PRIVATE Qt5::Test StatusQ) +target_link_libraries(SumAggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME SumAggregatorTest COMMAND SumAggregatorTest) add_executable(ConcatModelTest tst_ConcatModel.cpp) -target_link_libraries(ConcatModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib) +target_link_libraries(ConcatModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib) add_test(NAME ConcatModelTest COMMAND ConcatModelTest) diff --git a/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp b/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp new file mode 100644 index 0000000000..44337814a3 --- /dev/null +++ b/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp @@ -0,0 +1,91 @@ +#include "testmodel.h" + + +TestModel::TestModel(QList> data) + : m_data(std::move(data)) +{ + initRoles(); +} + +TestModel::TestModel(QList roles) +{ + QList> data; + data.reserve(roles.size()); + + for (auto& role : roles) + data.append({std::move(role), {}}); + + m_data = std::move(data); + initRoles(); +} + +int TestModel::rowCount(const QModelIndex& parent) const +{ + if(parent.isValid()) + return 0; + + Q_ASSERT(m_data.size()); + return m_data.first().second.size(); +} + +QHash TestModel::roleNames() const +{ + return m_roles; +} + +QVariant TestModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid() || role < 0 || role >= m_data.size()) + return {}; + + const auto row = index.row(); + + if (role >= m_data.length() || row >= m_data.at(0).second.length()) + return {}; + + return m_data.at(role).second.at(row); +} + +void TestModel::insert(int index, QVariantList row) +{ + beginInsertRows(QModelIndex{}, index, index); + + Q_ASSERT(row.size() == m_data.size()); + + for (int i = 0; i < m_data.size(); i++) { + auto& roleVariantList = m_data[i].second; + Q_ASSERT(index <= roleVariantList.size()); + roleVariantList.insert(index, std::move(row[i])); + } + + endInsertRows(); +} + +void TestModel::update(int index, int role, QVariant value) +{ + Q_ASSERT(role < m_data.size() && index < m_data[role].second.size()); + m_data[role].second[index].setValue(std::move(value)); + + emit dataChanged(this->index(index, 0), this->index(index, 0), { role }); +} + +void TestModel::remove(int index) +{ + beginRemoveRows(QModelIndex{}, index, index); + + for (int i = 0; i < m_data.size(); i++) { + auto& roleVariantList = m_data[i].second; + Q_ASSERT(index < roleVariantList.size()); + roleVariantList.removeAt(index); + } + + endRemoveRows(); +} + +void TestModel::initRoles() +{ + m_roles.reserve(m_data.size()); + + for (auto i = 0; i < m_data.size(); i++) + m_roles.insert(i, m_data.at(i).first.toUtf8()); +} diff --git a/ui/StatusQ/tests/src/TestHelpers/testmodel.h b/ui/StatusQ/tests/src/TestHelpers/testmodel.h new file mode 100644 index 0000000000..c1e4d74e06 --- /dev/null +++ b/ui/StatusQ/tests/src/TestHelpers/testmodel.h @@ -0,0 +1,22 @@ +#include + +class TestModel : public QAbstractListModel { + +public: + explicit TestModel(QList> data); + explicit TestModel(QList roles); + + int rowCount(const QModelIndex& parent) const override; + QHash roleNames() const override; + QVariant data(const QModelIndex& index, int role) const override; + + void insert(int index, QVariantList row); + void update(int index, int role, QVariant value); + void remove(int index); + +private: + void initRoles(); + + QList> m_data; + QHash m_roles; +}; diff --git a/ui/StatusQ/tests/tst_Aggregator.cpp b/ui/StatusQ/tests/tst_Aggregator.cpp index 2d802b7392..de4e5af796 100644 --- a/ui/StatusQ/tests/tst_Aggregator.cpp +++ b/ui/StatusQ/tests/tst_Aggregator.cpp @@ -1,75 +1,10 @@ #include -#include -#include -#include "StatusQ/aggregator.h" +#include +#include namespace { -// TODO: To be removed once issue #12843 is resolved and we have a testing utils -class TestSourceModel : public QAbstractListModel { - -public: - explicit TestSourceModel(QList> data) - : m_data(std::move(data)) { - m_roles.reserve(m_data.size()); - - for (auto i = 0; i < m_data.size(); i++) - m_roles.insert(i, m_data.at(i).first.toUtf8()); - } - - int rowCount(const QModelIndex& parent) const override { - Q_ASSERT(m_data.size()); - return m_data.first().second.size(); - } - - QVariant data(const QModelIndex& index, int role) const override { - if (!index.isValid() || role < 0 || role >= m_data.size()) - return {}; - - const auto row = index.row(); - - if (role >= m_data.length() || row >= m_data.at(0).second.length()) - return {}; - - return m_data.at(role).second.at(row); - } - - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override { - beginInsertRows(parent, row, row + count - 1); - m_data.insert(row, QPair()); - endInsertRows(); - return true; - } - - void update(int index, int role, QVariant value) { - Q_ASSERT(role < m_data.size() && index < m_data[role].second.size()); - m_data[role].second[index].setValue(std::move(value)); - - emit dataChanged(this->index(index, 0), this->index(index, 0), { role }); - } - - void remove(int index) { - beginRemoveRows(QModelIndex{}, index, index); - - for (int i = 0; i < m_data.size(); i++) { - auto& roleVariantList = m_data[i].second; - Q_ASSERT(index < roleVariantList.size()); - roleVariantList.removeAt(index); - } - - endRemoveRows(); - } - - QHash roleNames() const override { - return m_roles; - } - -private: - QList> m_data; - QHash m_roles; -}; - class ChildAggregator : public Aggregator { Q_OBJECT @@ -98,10 +33,10 @@ private: private slots: void testModel() { ChildAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} + }); QSignalSpy modelChangedSpy(&aggregator, &Aggregator::modelChanged); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); @@ -120,10 +55,10 @@ private slots: void testCalculateAggregationTrigger() { ChildAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { 0.123, 1.0, 1.45, 25.45 }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { 0.123, 1.0, 1.45, 25.45 }} + }); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); int valueChangedSpyCount = 0; diff --git a/ui/StatusQ/tests/tst_LeftJoinModel.cpp b/ui/StatusQ/tests/tst_LeftJoinModel.cpp index b63483270b..ae9e1dbe49 100644 --- a/ui/StatusQ/tests/tst_LeftJoinModel.cpp +++ b/ui/StatusQ/tests/tst_LeftJoinModel.cpp @@ -5,89 +5,7 @@ #include #include - -namespace { - -class TestSourceModel : public QAbstractListModel { - -public: - explicit TestSourceModel(QList> data) - : m_data(std::move(data)) - { - m_roles.reserve(m_data.size()); - - for (auto i = 0; i < m_data.size(); i++) - m_roles.insert(i, m_data.at(i).first.toUtf8()); - } - - int rowCount(const QModelIndex& parent) const override - { - if(parent.isValid()) return 0; //no children - - assert(m_data.size()); - return m_data.first().second.size(); - } - - QVariant data(const QModelIndex& index, int role) const override - { - if (!index.isValid() || role < 0 || role >= m_data.size()) - return {}; - - const auto row = index.row(); - - if (role >= m_data.length() || row >= m_data.at(0).second.length()) - return {}; - - return m_data.at(role).second.at(row); - } - - void insert(int index, QVariantList row) - { - beginInsertRows(QModelIndex{}, index, index); - - assert(row.size() == m_data.size()); - - for (int i = 0; i < m_data.size(); i++) { - auto& roleVariantList = m_data[i].second; - assert(index <= roleVariantList.size()); - roleVariantList.insert(index, row.at(i)); - } - - endInsertRows(); - } - - void update(int index, int role, QVariant value) - { - assert(role < m_data.size() && index < m_data[role].second.size()); - m_data[role].second[index].setValue(std::move(value)); - - emit dataChanged(this->index(index, 0), this->index(index, 0), { role }); - } - - void remove(int index) - { - beginRemoveRows(QModelIndex{}, index, index); - - for (int i = 0; i < m_data.size(); i++) { - auto& roleVariantList = m_data[i].second; - assert(index < roleVariantList.size()); - roleVariantList.removeAt(index); - } - - endRemoveRows(); - } - - QHash roleNames() const override - { - return m_roles; - } - -private: - QList> m_data; - QHash m_roles; -}; - -} // anonymous namespace +#include class TestLeftJoinModel: public QObject { @@ -105,12 +23,12 @@ private slots: void initializationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -138,12 +56,12 @@ private slots: void collidingRolesTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "name", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -174,13 +92,13 @@ private slots: void duplicatedRolesTest() { { - TestSourceModel leftModel({ + TestModel leftModel({ { "name", { "Token 1", "Token 2" }}, { "name", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "title", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -208,13 +126,13 @@ private slots: QCOMPARE(model.roleNames(), {}); } { - TestSourceModel leftModel({ + TestModel leftModel({ { "name", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "title", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -242,12 +160,12 @@ private slots: QCOMPARE(model.roleNames(), {}); } { - TestSourceModel leftModel({ + TestModel leftModel({ { "name", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "title", { "Community 1", "Community 2" }}, { "title", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} @@ -280,12 +198,12 @@ private slots: void noJoinRoleTest() { { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -313,12 +231,12 @@ private slots: QCOMPARE(model.roleNames(), {}); } { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -346,12 +264,12 @@ private slots: QCOMPARE(model.roleNames(), {}); } { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -382,12 +300,12 @@ private slots: void basicAccesTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "color", { "red", "blue" }}, { "communityId", { "community_1", "community_2" }} @@ -413,12 +331,12 @@ private slots: void changesPropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }}, { "color", { "red", "green" }} @@ -472,12 +390,12 @@ private slots: // TODO: cover also move and layoutChanged void insertRemovePropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }}, { "color", { "red", "green" }} @@ -529,12 +447,12 @@ private slots: void rightModelJoinRoleChangesPropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -566,12 +484,12 @@ private slots: void rightModelRemovalPropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2" }}, { "communityId", { "community_1", "community_2" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -603,12 +521,12 @@ private slots: void rightModelAdditionPropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_3" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -643,12 +561,12 @@ private slots: void leftModelJoinRoleChangesPropagationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -686,13 +604,13 @@ private slots: void modelsDeletedBeforeInitializationTest() { - auto leftModel = std::make_unique( + auto leftModel = std::make_unique( QList>{ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - auto rightModel = std::make_unique( + auto rightModel = std::make_unique( QList>{ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} @@ -716,12 +634,12 @@ private slots: QCOMPARE(model.data(model.index(0, 0), 0), {}); QCOMPARE(model.data(model.index(0, 0), 2), {}); - TestSourceModel newLeftModel({ + TestModel newLeftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel newRightModel({ + TestModel newRightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -745,13 +663,13 @@ private slots: void modelsDeletedAfterInitializationTest() { - auto leftModel = std::make_unique( + auto leftModel = std::make_unique( QList>{ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - auto rightModel = std::make_unique( + auto rightModel = std::make_unique( QList>{ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} @@ -778,12 +696,12 @@ private slots: QCOMPARE(model.data(model.index(0, 0), 0), {}); QCOMPARE(model.data(model.index(0, 0), 2), {}); - TestSourceModel newLeftModel({ + TestModel newLeftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel newRightModel({ + TestModel newRightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -811,13 +729,13 @@ private slots: void rightModelDeletedAfterInitializationTest() { - auto leftModel = std::make_unique( + auto leftModel = std::make_unique( QList>{ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - auto rightModel = std::make_unique( + auto rightModel = std::make_unique( QList>{ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} @@ -840,12 +758,12 @@ private slots: QCOMPARE(model.data(model.index(0, 0), 1), "community_1"); QCOMPARE(model.data(model.index(0, 0), 2), {}); - TestSourceModel newLeftModel({ + TestModel newLeftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel newRightModel({ + TestModel newRightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -866,12 +784,12 @@ private slots: void rightModelChangedWithSameRolesAfterInitializationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -884,7 +802,7 @@ private slots: model.setJoinRole("communityId"); - TestSourceModel newRightModel({ + TestModel newRightModel({ { "name", { "Community A", "Community B" }}, { "communityId", { "community_1", "community_2" }} }); @@ -919,12 +837,12 @@ private slots: void rightModelChangedWithDifferentRolesAfterInitializationTest() { - TestSourceModel leftModel({ + TestModel leftModel({ { "title", { "Token 1", "Token 2", "Token 3"}}, { "communityId", { "community_1", "community_2", "community_1" }} }); - TestSourceModel rightModel({ + TestModel rightModel({ { "name", { "Community 1", "Community 2" }}, { "communityId", { "community_1", "community_2" }} }); @@ -937,7 +855,7 @@ private slots: model.setJoinRole("communityId"); - TestSourceModel newRightModel({ + TestModel newRightModel({ { "communityId", { "community_1", "community_2" }}, { "name", { "Community A", "Community B" }} }); diff --git a/ui/StatusQ/tests/tst_RolesRenamingModel.cpp b/ui/StatusQ/tests/tst_RolesRenamingModel.cpp index 31bc4b4de9..1d7ac21884 100644 --- a/ui/StatusQ/tests/tst_RolesRenamingModel.cpp +++ b/ui/StatusQ/tests/tst_RolesRenamingModel.cpp @@ -4,47 +4,7 @@ #include #include - -namespace { - -class TestSourceModel : public QAbstractListModel { - -public: - explicit TestSourceModel(QList roles) - : m_roles(std::move(roles)) - { - } - - QVariant data(const QModelIndex& index, int role) const override - { - if(!index.isValid() || index.row() >= capacity) - return {}; - - return 42; - } - - int rowCount(const QModelIndex& parent) const override - { - return capacity; - } - - QHash roleNames() const override - { - QHash roles; - roles.remove(m_roles.size()); - - for (auto i = 0; i < m_roles.size(); i++) - roles.insert(i, m_roles.at(i).toUtf8()); - - return roles; - } - -private: - static constexpr auto capacity = 5; - QList m_roles; -}; - -} +#include class TestRolesRenamingModel: public QObject { @@ -53,7 +13,7 @@ class TestRolesRenamingModel: public QObject private slots: void initializationWithBrokenMappingTest() { - TestSourceModel sourceModel({"id", "name", "color"}); + TestModel sourceModel({"id", "name", "color"}); RolesRenamingModel model; QQmlListProperty mapping = model.mapping(); @@ -78,7 +38,7 @@ private slots: void remappingTest() { - TestSourceModel sourceModel({"id", "name", "color"}); + TestModel sourceModel({"id", "name", "color"}); RolesRenamingModel model; QQmlListProperty mapping = model.mapping(); @@ -103,7 +63,7 @@ private slots: void addMappingAfterFetchingRoleNamesTest() { - TestSourceModel sourceModel({"id", "name", "color"}); + TestModel sourceModel({"id", "name", "color"}); RolesRenamingModel model; QQmlListProperty mapping = model.mapping(); @@ -134,7 +94,7 @@ private slots: void duplicatedNamesTest() { - TestSourceModel sourceModel({"id", "name", "color"}); + TestModel sourceModel({"id", "name", "color"}); RolesRenamingModel model; QQmlListProperty mapping = model.mapping(); @@ -182,8 +142,13 @@ private slots: void sourceModelDeletedTest() { - auto sourceModel = std::make_unique( - QList{"id", "name", "color"}); + auto sourceModel = std::make_unique( + QList> { + { "id", { 1, 2, 3 }}, + { "name", { "a", "b", "c" }}, + { "color", { "red", "green", "blue" }} + }); + RolesRenamingModel model; QQmlListProperty mapping = model.mapping(); @@ -204,12 +169,12 @@ private slots: {0, "tokenId"}, {1, "tokenName"}, {2, "color"} }; QCOMPARE(model.roleNames(), expectedRoles); - QCOMPARE(model.rowCount(), 5); + QCOMPARE(model.rowCount(), 3); - QCOMPARE(model.data(model.index(0, 0), 0), 42); - QCOMPARE(model.data(model.index(0, 0), 1), 42); - QCOMPARE(model.data(model.index(5, 0), 0), {}); - QCOMPARE(model.data(model.index(5, 0), 1), {}); + QCOMPARE(model.data(model.index(0, 0), 0), 1); + QCOMPARE(model.data(model.index(0, 0), 1), "a"); + QCOMPARE(model.data(model.index(2, 0), 0), 3); + QCOMPARE(model.data(model.index(2, 0), 1), "c"); QSignalSpy destroyedSpy(sourceModel.get(), &QObject::destroyed); sourceModel.reset(); @@ -222,8 +187,8 @@ private slots: QCOMPARE(model.roleNames(), {}); QCOMPARE(model.data(model.index(0, 0), 0), {}); QCOMPARE(model.data(model.index(0, 0), 1), {}); - QCOMPARE(model.data(model.index(5, 0), 0), {}); - QCOMPARE(model.data(model.index(5, 0), 1), {}); + QCOMPARE(model.data(model.index(2, 0), 0), {}); + QCOMPARE(model.data(model.index(2, 0), 1), {}); } }; diff --git a/ui/StatusQ/tests/tst_SingleRoleAggregator.cpp b/ui/StatusQ/tests/tst_SingleRoleAggregator.cpp index 9de85a87db..b935c2dd27 100644 --- a/ui/StatusQ/tests/tst_SingleRoleAggregator.cpp +++ b/ui/StatusQ/tests/tst_SingleRoleAggregator.cpp @@ -1,75 +1,10 @@ #include -#include -#include -#include "StatusQ/singleroleaggregator.h" +#include +#include namespace { -// TODO: To be removed once issue #12843 is resolved and we have a testing utils -class TestSourceModel : public QAbstractListModel { - -public: - explicit TestSourceModel(QList> data) - : m_data(std::move(data)) { - m_roles.reserve(m_data.size()); - - for (auto i = 0; i < m_data.size(); i++) - m_roles.insert(i, m_data.at(i).first.toUtf8()); - } - - int rowCount(const QModelIndex& parent) const override { - Q_ASSERT(m_data.size()); - return m_data.first().second.size(); - } - - QVariant data(const QModelIndex& index, int role) const override { - if (!index.isValid() || role < 0 || role >= m_data.size()) - return {}; - - const auto row = index.row(); - - if (role >= m_data.length() || row >= m_data.at(0).second.length()) - return {}; - - return m_data.at(role).second.at(row); - } - - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override { - beginInsertRows(parent, row, row + count - 1); - m_data.insert(row, QPair()); - endInsertRows(); - return true; - } - - void update(int index, int role, QVariant value) { - Q_ASSERT(role < m_data.size() && index < m_data[role].second.size()); - m_data[role].second[index].setValue(std::move(value)); - - emit dataChanged(this->index(index, 0), this->index(index, 0), { role }); - } - - void remove(int index) { - beginRemoveRows(QModelIndex{}, index, index); - - for (int i = 0; i < m_data.size(); i++) { - auto& roleVariantList = m_data[i].second; - Q_ASSERT(index < roleVariantList.size()); - roleVariantList.removeAt(index); - } - - endRemoveRows(); - } - - QHash roleNames() const override { - return m_roles; - } - -private: - QList> m_data; - QHash m_roles; -}; - class ChildSingleRoleAggregator : public SingleRoleAggregator { Q_OBJECT @@ -93,10 +28,10 @@ private slots: void testRoleName() { ChildSingleRoleAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} + }); QSignalSpy roleNameSpy(&aggregator, &SingleRoleAggregator::roleNameChanged); // Test 1 - Assign role name but model is nullptr diff --git a/ui/StatusQ/tests/tst_SumAggregator.cpp b/ui/StatusQ/tests/tst_SumAggregator.cpp index 73b025aeb4..8146c0bbf4 100644 --- a/ui/StatusQ/tests/tst_SumAggregator.cpp +++ b/ui/StatusQ/tests/tst_SumAggregator.cpp @@ -1,76 +1,7 @@ #include -#include -#include -#include "StatusQ/sumaggregator.h" - -namespace { - -// TODO: To be removed once issue #12843 is resolved and we have a testing utils -class TestSourceModel : public QAbstractListModel { - -public: - explicit TestSourceModel(QList> data) - : m_data(std::move(data)) { - m_roles.reserve(m_data.size()); - - for (auto i = 0; i < m_data.size(); i++) - m_roles.insert(i, m_data.at(i).first.toUtf8()); - } - - int rowCount(const QModelIndex& parent) const override { - Q_ASSERT(m_data.size()); - return m_data.first().second.size(); - } - - QVariant data(const QModelIndex& index, int role) const override { - if (!index.isValid() || role < 0 || role >= m_data.size()) - return {}; - - const auto row = index.row(); - - if (role >= m_data.length() || row >= m_data.at(0).second.length()) - return {}; - - return m_data.at(role).second.at(row); - } - - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override { - beginInsertRows(parent, row, row + count - 1); - m_data.insert(row, QPair()); - endInsertRows(); - return true; - } - - void update(int index, int role, QVariant value) { - Q_ASSERT(role < m_data.size() && index < m_data[role].second.size()); - m_data[role].second[index].setValue(std::move(value)); - - emit dataChanged(this->index(index, 0), this->index(index, 0), { role }); - } - - void remove(int index) { - beginRemoveRows(QModelIndex{}, index, index); - - for (int i = 0; i < m_data.size(); i++) { - auto& roleVariantList = m_data[i].second; - Q_ASSERT(index < roleVariantList.size()); - roleVariantList.removeAt(index); - } - - endRemoveRows(); - } - - QHash roleNames() const override { - return m_roles; - } - -private: - QList> m_data; - QHash m_roles; -}; - -} // anonymous namespace +#include +#include class TestSumAggregator : public QObject { @@ -88,10 +19,10 @@ private slots: void testModel() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} + }); QSignalSpy modelChangedSpy(&aggregator, &Aggregator::modelChanged); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); @@ -110,10 +41,10 @@ private slots: void testRoleName() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }} + }); QSignalSpy roleNameSpy(&aggregator, &SingleRoleAggregator::roleNameChanged); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); @@ -143,10 +74,10 @@ private slots: void testStringTypeValue() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { "0.123", "1", "1.45", "25.45" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { "0.123", "1", "1.45", "25.45" }} + }); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); int valueChangedSpyCount = 0; @@ -178,10 +109,10 @@ private slots: void testFloatTypeValue() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13", "1", "321" }}, - { "balance", { 0.123, 1.0, 1.45, 25.45 }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13", "1", "321" }}, + { "balance", { 0.123, 1.0, 1.45, 25.45 }} + }); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); int valueChangedSpyCount = 0; @@ -213,10 +144,10 @@ private slots: void testStringUnsupportedTypeValue() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13" }}, - { "balance", { "aa", "bb" }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13" }}, + { "balance", { "aa", "bb" }} + }); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); aggregator.setModel(&sourceModel); @@ -234,10 +165,10 @@ private slots: void testUnsupportedTypeValue() { SumAggregator aggregator; - TestSourceModel sourceModel({ - { "chainId", { "12", "13" }}, - { "balance", { QByteArray(), QByteArray() }} - }); + TestModel sourceModel({ + { "chainId", { "12", "13" }}, + { "balance", { QByteArray(), QByteArray() }} + }); QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged); aggregator.setModel(&sourceModel);