|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-only |
| 3 | + * MuseScore-CLA-applies |
| 4 | + * |
| 5 | + * MuseScore |
| 6 | + * Music Composition & Notation |
| 7 | + * |
| 8 | + * Copyright (C) 2026 MuseScore Limited and others |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 3 as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | + |
| 23 | +#include <memory> |
| 24 | + |
| 25 | +#include <gtest/gtest.h> |
| 26 | + |
| 27 | +#include <QString> |
| 28 | +#include <QStringList> |
| 29 | +#include <QStringListModel> |
| 30 | + |
| 31 | +#include "uicomponents/qml/Muse/UiComponents/filtervalue.h" |
| 32 | +#include "uicomponents/qml/Muse/UiComponents/sortervalue.h" |
| 33 | +#include "uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.h" |
| 34 | + |
| 35 | +using namespace Qt::StringLiterals; |
| 36 | + |
| 37 | +namespace muse::uicomponents { |
| 38 | +class UiComponents_SortFilterProxyModelTests : public ::testing::Test |
| 39 | +{ |
| 40 | +public: |
| 41 | + UiComponents_SortFilterProxyModelTests() |
| 42 | + { |
| 43 | + QStringList modelData; |
| 44 | + modelData << u"hay"_s; |
| 45 | + modelData << u"needle"_s; |
| 46 | + modelData << u"hay needle hay"_s; |
| 47 | + modelData << u"needle"_s; |
| 48 | + modelData << u"hay hay"_s; |
| 49 | + m_model->setStringList(modelData); |
| 50 | + } |
| 51 | + |
| 52 | + QAbstractListModel* listModel() const |
| 53 | + { |
| 54 | + return m_model.get(); |
| 55 | + } |
| 56 | + |
| 57 | +private: |
| 58 | + std::unique_ptr<QStringListModel> m_model = std::make_unique<QStringListModel>(); |
| 59 | +}; |
| 60 | + |
| 61 | +TEST_F(UiComponents_SortFilterProxyModelTests, testFilterValue) |
| 62 | +{ |
| 63 | + QAbstractListModel* model = this->listModel(); |
| 64 | + auto proxyModel = std::make_unique<SortFilterProxyModel>(); |
| 65 | + proxyModel->setSourceModel(model); |
| 66 | + |
| 67 | + FilterValue* filter = new FilterValue(proxyModel.get()); |
| 68 | + filter->setCompareType(CompareType::Equal); |
| 69 | + filter->setRoleName(u"display"_s); |
| 70 | + filter->setRoleValue(u"needle"_s); |
| 71 | + |
| 72 | + QQmlListProperty<Filter> filters = proxyModel->filters(); |
| 73 | + ASSERT_TRUE(filters.append); |
| 74 | + filters.append(&filters, filter); |
| 75 | + |
| 76 | + EXPECT_EQ(proxyModel->rowCount(), 2); |
| 77 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(0, 0)), model->index(1)); |
| 78 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(1, 0)), model->index(3)); |
| 79 | + |
| 80 | + filter->setCompareType(CompareType::NotEqual); |
| 81 | + EXPECT_EQ(proxyModel->rowCount(), 3); |
| 82 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(0, 0)), model->index(0)); |
| 83 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(1, 0)), model->index(2)); |
| 84 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(2, 0)), model->index(4)); |
| 85 | + |
| 86 | + filter->setCompareType(CompareType::Contains); |
| 87 | + EXPECT_EQ(proxyModel->rowCount(), 3); |
| 88 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(0, 0)), model->index(1)); |
| 89 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(1, 0)), model->index(2)); |
| 90 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(2, 0)), model->index(3)); |
| 91 | + |
| 92 | + filter->setEnabled(false); |
| 93 | + EXPECT_EQ(proxyModel->rowCount(), model->rowCount()); |
| 94 | +} |
| 95 | + |
| 96 | +TEST_F(UiComponents_SortFilterProxyModelTests, testFilterValueMultiple) |
| 97 | +{ |
| 98 | + QAbstractListModel* model = this->listModel(); |
| 99 | + auto proxyModel = std::make_unique<SortFilterProxyModel>(); |
| 100 | + proxyModel->setSourceModel(model); |
| 101 | + |
| 102 | + FilterValue* hayFilter = new FilterValue(proxyModel.get()); |
| 103 | + hayFilter->setCompareType(CompareType::Contains); |
| 104 | + hayFilter->setRoleName(u"display"_s); |
| 105 | + hayFilter->setRoleValue(u"hay"_s); |
| 106 | + |
| 107 | + FilterValue* needleFilter = new FilterValue(proxyModel.get()); |
| 108 | + needleFilter->setCompareType(CompareType::Contains); |
| 109 | + needleFilter->setRoleName(u"display"_s); |
| 110 | + needleFilter->setRoleValue(u"needle"_s); |
| 111 | + |
| 112 | + QQmlListProperty<Filter> filters = proxyModel->filters(); |
| 113 | + ASSERT_TRUE(filters.append); |
| 114 | + filters.append(&filters, hayFilter); |
| 115 | + filters.append(&filters, needleFilter); |
| 116 | + |
| 117 | + EXPECT_EQ(proxyModel->rowCount(), 1); |
| 118 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(0, 0)), model->index(2)); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(UiComponents_SortFilterProxyModelTests, testSorterValue) |
| 122 | +{ |
| 123 | + QAbstractListModel* model = this->listModel(); |
| 124 | + auto proxyModel = std::make_unique<SortFilterProxyModel>(); |
| 125 | + proxyModel->setSourceModel(model); |
| 126 | + |
| 127 | + SorterValue* sorter = new SorterValue(proxyModel.get()); |
| 128 | + sorter->setRoleName(u"display"_s); |
| 129 | + |
| 130 | + QQmlListProperty<Sorter> sorters = proxyModel->sorters(); |
| 131 | + ASSERT_TRUE(sorters.append); |
| 132 | + sorters.append(&sorters, sorter); |
| 133 | + |
| 134 | + EXPECT_EQ(proxyModel->rowCount(), model->rowCount()); |
| 135 | + for (int row = 0; row < proxyModel->rowCount(); ++row) { |
| 136 | + SCOPED_TRACE(row); |
| 137 | + EXPECT_EQ(proxyModel->mapToSource(proxyModel->index(row, 0)), model->index(row)); |
| 138 | + } |
| 139 | + |
| 140 | + sorter->setEnabled(true); |
| 141 | + for (int row = 1; row < proxyModel->rowCount(); ++row) { |
| 142 | + SCOPED_TRACE(row); |
| 143 | + |
| 144 | + const QPartialOrdering ordering = QVariant::compare(proxyModel->data(proxyModel->index(row - 1, 0)), |
| 145 | + proxyModel->data(proxyModel->index(row, 0))); |
| 146 | + EXPECT_TRUE(ordering == QPartialOrdering::Less || ordering == QPartialOrdering::Equivalent); |
| 147 | + } |
| 148 | + |
| 149 | + sorter->setSortOrder(Qt::DescendingOrder); |
| 150 | + for (int row = 1; row < proxyModel->rowCount(); ++row) { |
| 151 | + SCOPED_TRACE(row); |
| 152 | + |
| 153 | + const QPartialOrdering ordering = QVariant::compare(proxyModel->data(proxyModel->index(row - 1, 0)), |
| 154 | + proxyModel->data(proxyModel->index(row, 0))); |
| 155 | + EXPECT_TRUE(ordering == QPartialOrdering::Greater || ordering == QPartialOrdering::Equivalent); |
| 156 | + } |
| 157 | +} |
| 158 | +} |
0 commit comments