From 598a389c3e92768feb71f44b11b5cdee9479cc84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blak?= Date: Tue, 9 Apr 2024 14:12:15 +0200 Subject: [PATCH] feat(StatusQ/TestHelpers): Add method to test model removing items and emiting layoutChanged --- .../tests/src/TestHelpers/testmodel.cpp | 29 +++++++++++++++++++ ui/StatusQ/tests/src/TestHelpers/testmodel.h | 7 +++++ 2 files changed, 36 insertions(+) diff --git a/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp b/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp index 1793743e12..c7ed02ce77 100644 --- a/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp +++ b/ui/StatusQ/tests/src/TestHelpers/testmodel.cpp @@ -85,6 +85,9 @@ void TestModel::remove(int index) void TestModel::invert() { + if (m_data.size() < 2) + return; + emit layoutAboutToBeChanged(); for (auto& entry : m_data) @@ -99,6 +102,32 @@ void TestModel::invert() emit layoutChanged(); } +void TestModel::removeEverySecond() +{ + if (m_data.empty()) + return; + + emit layoutAboutToBeChanged(); + + for (auto& entry : m_data) { + QVariantList& data = entry.second; + + for (auto i = 0; i < data.size(); i++) + data.removeAt(i); + } + + const auto persistentIndexes = persistentIndexList(); + + for (const QModelIndex& index : persistentIndexes) { + if (index.row() % 2 == 0) + changePersistentIndex(index, {}); + else + changePersistentIndex(index, createIndex(index.row() / 2, 0)); + } + + emit layoutChanged(); +} + void TestModel::initRoles() { m_roles.reserve(m_data.size()); diff --git a/ui/StatusQ/tests/src/TestHelpers/testmodel.h b/ui/StatusQ/tests/src/TestHelpers/testmodel.h index 25a64a2376..c49dcc43d6 100644 --- a/ui/StatusQ/tests/src/TestHelpers/testmodel.h +++ b/ui/StatusQ/tests/src/TestHelpers/testmodel.h @@ -19,6 +19,13 @@ public: // inverts order of items, emits layoutAboutToBeChanged / layoutChanged void invert(); + // removes every second item from the model but doesn't emit + // rowsAboutToBeRemoved/rowsRemoved. The update is notified via + // layoutAboutToBeChanged/layoutChanged. It's useful for testing proxy + // models against that scenario, which may occur in some circumstances, e.g. + // during SFPM initialization where initial filtering is notified this way. + void removeEverySecond(); + private: void initRoles();