File indexing completed on 2024-12-01 04:35:24
0001 /* 0002 SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "inputcompletermodeltest.h" 0008 #include "model/inputcompletermodel.h" 0009 #include "test_model_helpers.h" 0010 #include <QJsonDocument> 0011 #include <QJsonObject> 0012 #include <QSignalSpy> 0013 #include <QTest> 0014 0015 QTEST_GUILESS_MAIN(InputCompleterModelTest) 0016 0017 InputCompleterModelTest::InputCompleterModelTest(QObject *parent) 0018 : QObject(parent) 0019 { 0020 } 0021 0022 void InputCompleterModelTest::shouldHaveDefaultValue() 0023 { 0024 InputCompleterModel w(nullptr); 0025 QCOMPARE(w.rowCount(), 0); 0026 } 0027 0028 void InputCompleterModelTest::shouldAssignValues() 0029 { 0030 InputCompleterModel w(nullptr); 0031 QSignalSpy rowInsertedSpy(&w, &InputCompleterModel::rowsInserted); 0032 QSignalSpy rowABTInserted(&w, &InputCompleterModel::rowsAboutToBeInserted); 0033 QSignalSpy modelAboutToResetSpy(&w, &InputCompleterModel::modelAboutToBeReset); 0034 0035 QVector<ChannelUserCompleter> channelList; 0036 for (int i = 0; i < 10; ++i) { 0037 ChannelUserCompleter c; 0038 c.setName(QStringLiteral("roomname%1").arg(i)); 0039 channelList.append(std::move(c)); 0040 } 0041 w.setChannels(channelList); 0042 QCOMPARE(w.rowCount(), 10); 0043 QCOMPARE(rowInsertedSpy.count(), 1); 0044 QCOMPARE(rowABTInserted.count(), 1); 0045 QCOMPARE(modelAboutToResetSpy.count(), 0); 0046 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,9")); 0047 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,9")); 0048 0049 // add Empty list 0050 channelList.clear(); 0051 rowInsertedSpy.clear(); 0052 rowABTInserted.clear(); 0053 modelAboutToResetSpy.clear(); 0054 0055 w.setChannels(channelList); 0056 0057 QCOMPARE(w.rowCount(), 0); 0058 QCOMPARE(rowInsertedSpy.count(), 0); 0059 QCOMPARE(rowABTInserted.count(), 0); 0060 QCOMPARE(modelAboutToResetSpy.count(), 1); 0061 0062 // Add same element 0063 rowInsertedSpy.clear(); 0064 rowABTInserted.clear(); 0065 modelAboutToResetSpy.clear(); 0066 0067 w.setChannels(channelList); 0068 0069 QCOMPARE(w.rowCount(), 0); 0070 QCOMPARE(rowInsertedSpy.count(), 0); 0071 QCOMPARE(rowABTInserted.count(), 0); 0072 QCOMPARE(modelAboutToResetSpy.count(), 0); 0073 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QString()); 0074 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QString()); 0075 0076 // Test add same number of element. 0077 channelList.clear(); 0078 rowInsertedSpy.clear(); 0079 rowABTInserted.clear(); 0080 modelAboutToResetSpy.clear(); 0081 0082 for (int i = 0; i < 5; ++i) { 0083 ChannelUserCompleter c; 0084 c.setName(QStringLiteral("roomname%1").arg(i)); 0085 channelList.append(std::move(c)); 0086 } 0087 w.setChannels(channelList); 0088 QCOMPARE(w.rowCount(), 5); 0089 QCOMPARE(rowInsertedSpy.count(), 1); 0090 QCOMPARE(rowABTInserted.count(), 1); 0091 QCOMPARE(modelAboutToResetSpy.count(), 0); 0092 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,4")); 0093 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,4")); 0094 0095 rowInsertedSpy.clear(); 0096 rowABTInserted.clear(); 0097 modelAboutToResetSpy.clear(); 0098 w.setChannels(channelList); 0099 0100 QCOMPARE(rowInsertedSpy.count(), 1); 0101 QCOMPARE(rowABTInserted.count(), 1); 0102 QCOMPARE(modelAboutToResetSpy.count(), 1); 0103 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,4")); 0104 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,4")); 0105 } 0106 0107 QJsonObject loadFile(const QString &file) 0108 { 0109 const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + file; 0110 QFile f(originalJsonFile); 0111 if (!f.open(QIODevice::ReadOnly)) { 0112 qWarning() << " Unable to load file " << file; 0113 return {}; 0114 } 0115 const QByteArray content = f.readAll(); 0116 f.close(); 0117 const QJsonDocument doc = QJsonDocument::fromJson(content); 0118 const QJsonObject root = doc.object(); 0119 const QJsonObject obj = root.value(QLatin1String("result")).toObject(); 0120 return obj; 0121 } 0122 0123 void InputCompleterModelTest::shouldLoadValueFromJson() 0124 { 0125 InputCompleterModel w(nullptr); 0126 QSignalSpy rowInsertedSpy(&w, &InputCompleterModel::rowsInserted); 0127 QSignalSpy rowABTInserted(&w, &InputCompleterModel::rowsAboutToBeInserted); 0128 QSignalSpy modelAboutToResetSpy(&w, &InputCompleterModel::modelAboutToBeReset); 0129 0130 QJsonObject obj = loadFile(QStringLiteral("channelparent.json")); 0131 InputCompleterModel::SearchInfo info; 0132 info.searchType = InputCompleterModel::SearchInfo::ChannelsAndUsers; 0133 w.setSearchInfo(std::move(info)); 0134 w.parseChannels(obj); 0135 QCOMPARE(w.rowCount(), 8); 0136 QCOMPARE(rowInsertedSpy.count(), 1); 0137 QCOMPARE(rowABTInserted.count(), 1); 0138 QCOMPARE(modelAboutToResetSpy.count(), 0); 0139 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,7")); 0140 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,7")); 0141 0142 rowInsertedSpy.clear(); 0143 rowABTInserted.clear(); 0144 modelAboutToResetSpy.clear(); 0145 0146 // Test room 0147 QCOMPARE(w.data(w.index(2), InputCompleterModel::CompleterName).toString(), QStringLiteral("bal3")); 0148 QCOMPARE(w.data(w.index(2), InputCompleterModel::ChannelType).value<ChannelUserCompleter::ChannelUserCompleterType>(), 0149 ChannelUserCompleter::ChannelUserCompleterType::Room); 0150 QCOMPARE(w.data(w.index(2), InputCompleterModel::DisplayName).toString(), QStringLiteral("bal3")); 0151 0152 // Test user 0153 QCOMPARE(w.data(w.index(6), InputCompleterModel::CompleterName).toString(), QStringLiteral("bla.foo4")); 0154 QCOMPARE(w.data(w.index(6), InputCompleterModel::ChannelType).value<ChannelUserCompleter::ChannelUserCompleterType>(), 0155 ChannelUserCompleter::ChannelUserCompleterType::DirectChannel); 0156 // We use for user a channelid == channel name as we use it for opening direct channel 0157 QCOMPARE(w.data(w.index(6), InputCompleterModel::DisplayName).toString(), QStringLiteral("foo4")); 0158 0159 // Test without name/username ! 0160 0161 obj = loadFile(QStringLiteral("channelparentempty.json")); 0162 w.parseChannels(obj); 0163 QCOMPARE(w.rowCount(), 1); // "No found result" item 0164 QCOMPARE(rowInsertedSpy.count(), 1); 0165 QCOMPARE(rowABTInserted.count(), 1); 0166 QCOMPARE(modelAboutToResetSpy.count(), 1); 0167 } 0168 0169 void InputCompleterModelTest::shouldClearModel() 0170 { 0171 InputCompleterModel w(nullptr); 0172 QSignalSpy rowInsertedSpy(&w, &InputCompleterModel::rowsInserted); 0173 QSignalSpy rowABTInserted(&w, &InputCompleterModel::rowsAboutToBeInserted); 0174 QSignalSpy modelAboutToBeResetSpy(&w, &InputCompleterModel::modelAboutToBeReset); 0175 0176 QJsonObject obj = loadFile(QStringLiteral("channelparent.json")); 0177 InputCompleterModel::SearchInfo info; 0178 info.searchType = InputCompleterModel::SearchInfo::ChannelsAndUsers; 0179 w.setSearchInfo(std::move(info)); 0180 w.parseChannels(obj); 0181 QCOMPARE(w.rowCount(), 8); 0182 QCOMPARE(rowInsertedSpy.count(), 1); 0183 QCOMPARE(rowABTInserted.count(), 1); 0184 QCOMPARE(modelAboutToBeResetSpy.count(), 0); 0185 QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,7")); 0186 QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,7")); 0187 0188 rowInsertedSpy.clear(); 0189 rowABTInserted.clear(); 0190 modelAboutToBeResetSpy.clear(); 0191 0192 w.clear(); 0193 QCOMPARE(w.rowCount(), 0); 0194 QCOMPARE(rowInsertedSpy.count(), 0); 0195 QCOMPARE(rowABTInserted.count(), 0); 0196 QCOMPARE(modelAboutToBeResetSpy.count(), 1); 0197 } 0198 0199 #include "moc_inputcompletermodeltest.cpp"