File indexing completed on 2024-05-12 16:25:25

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 "inputtextmanagertest.h"
0008 #include "inputtextmanager.h"
0009 #include "model/inputcompletermodel.h"
0010 #include "rocketchataccount.h"
0011 #include <QSignalSpy>
0012 #include <QTest>
0013 QTEST_GUILESS_MAIN(InputTextManagerTest)
0014 
0015 InputTextManagerTest::InputTextManagerTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 void InputTextManagerTest::shouldHaveDefaultValue()
0021 {
0022     InputTextManager manager(nullptr, nullptr);
0023     QVERIFY(manager.inputCompleterModel());
0024     QCOMPARE(manager.inputCompleterModel()->rowCount(), 0);
0025 }
0026 
0027 void InputTextManagerTest::shouldReplaceWord_data()
0028 {
0029     QTest::addColumn<QString>("newword");
0030     QTest::addColumn<QString>("text");
0031     QTest::addColumn<int>("position");
0032     QTest::addColumn<QString>("result");
0033     QTest::addColumn<int>("expectedPosition");
0034     QTest::newRow("empty") << QString() << QString() << 5 << QString() << 5;
0035     QTest::newRow("replace1") << QStringLiteral("bla") << QStringLiteral("foo @d") << 5 << QStringLiteral("foo @bla") << 8;
0036     QTest::newRow("replace2") << QStringLiteral("bla") << QStringLiteral("foo @daaaa") << 5 << QStringLiteral("foo @bla") << 8;
0037     QTest::newRow("replace3") << QStringLiteral("bla") << QStringLiteral("@daaaa foo") << 1 << QStringLiteral("@bla foo") << 4;
0038     QTest::newRow("buildbot") << QStringLiteral("buildbot ") << QStringLiteral("@bu") << 3 << QStringLiteral("@buildbot ") << 10;
0039     QTest::newRow("trailing_space_already_there") << QStringLiteral("bla ") << QStringLiteral("@daaaa foo") << 1 << QStringLiteral("@bla foo") << 5;
0040 }
0041 
0042 void InputTextManagerTest::shouldReplaceWord()
0043 {
0044     QFETCH(QString, newword);
0045     QFETCH(QString, text);
0046     QFETCH(int, position);
0047     QFETCH(int, expectedPosition);
0048     QFETCH(QString, result);
0049 
0050     InputTextManager manager(nullptr, nullptr);
0051 
0052     // Widgets
0053     QCOMPARE(manager.applyCompletion(newword, text, &position), result);
0054     QCOMPARE(position, expectedPosition);
0055 }
0056 
0057 void InputTextManagerTest::shouldSearchWord_data()
0058 {
0059     QTest::addColumn<QString>("text");
0060     QTest::addColumn<int>("position");
0061     QTest::addColumn<QString>("result");
0062     QTest::newRow("empty") << QString() << 5 << QString();
0063     QTest::newRow("one_letter") << QStringLiteral("l") << 1 << QStringLiteral("l");
0064     QTest::newRow("at") << QStringLiteral("@") << 1 << QStringLiteral("@");
0065     QTest::newRow("at_letter_1") << QStringLiteral("@a") << 1 << QStringLiteral("@a");
0066     QTest::newRow("at_letter") << QStringLiteral("@a") << 2 << QStringLiteral("@a");
0067     QTest::newRow("at_word_space") << QStringLiteral("@abc ") << 5 << QString();
0068 }
0069 
0070 void InputTextManagerTest::shouldSearchWord()
0071 {
0072     QFETCH(QString, text);
0073     QFETCH(int, position);
0074     QFETCH(QString, result);
0075 
0076     InputTextManager manager(nullptr, nullptr);
0077     int start; // TODO test the output value
0078     QCOMPARE(manager.searchWord(text, position, start), result);
0079 }
0080 
0081 void InputTextManagerTest::shouldEmitCompletionRequestSignals()
0082 {
0083     RocketChatAccount account;
0084     InputTextManager manager(&account, nullptr);
0085     QSignalSpy typeChangedSpy(&manager, &InputTextManager::completionTypeChanged);
0086     QSignalSpy requestSpy(&manager, &InputTextManager::completionRequested);
0087 
0088     manager.setInputTextChanged(QString(), QStringLiteral("a @"), 3);
0089     QCOMPARE(typeChangedSpy.count(), 1);
0090     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::User);
0091     QCOMPARE(requestSpy.count(), 0); // We don't send signal when we have only "@" now.
0092     typeChangedSpy.clear();
0093     requestSpy.clear();
0094 
0095     manager.setInputTextChanged(QString(), QStringLiteral("a :"), 3);
0096     QCOMPARE(typeChangedSpy.count(), 1);
0097     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::Emoji);
0098     typeChangedSpy.clear();
0099     QCOMPARE(requestSpy.count(), 0); // emoji completion doesn't use this signal
0100     requestSpy.clear();
0101 
0102     manager.setInputTextChanged(QString(), QStringLiteral("a "), 2);
0103     QCOMPARE(typeChangedSpy.count(), 1);
0104     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::None);
0105     typeChangedSpy.clear();
0106 
0107     manager.setInputTextChanged(QString(), QStringLiteral("a #c"), 4);
0108     QCOMPARE(typeChangedSpy.count(), 1);
0109     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::Channel);
0110     QCOMPARE(requestSpy.count(), 1);
0111     QCOMPARE(requestSpy.at(0).at(0).toString(), QStringLiteral("c"));
0112     typeChangedSpy.clear();
0113     requestSpy.clear();
0114 
0115     manager.setInputTextChanged(QString(), QStringLiteral("hello @foo"), 10);
0116     QCOMPARE(typeChangedSpy.count(), 1);
0117     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::User);
0118     QCOMPARE(requestSpy.count(), 1);
0119     QCOMPARE(requestSpy.at(0).at(0).toString(), QStringLiteral("foo"));
0120     requestSpy.clear();
0121     typeChangedSpy.clear();
0122 
0123     manager.setInputTextChanged(QString(), QStringLiteral("@foo hello"), 4);
0124     QCOMPARE(typeChangedSpy.count(), 0); // User again
0125     QCOMPARE(requestSpy.count(), 1);
0126     QCOMPARE(requestSpy.at(0).at(0).toString(), QStringLiteral("foo"));
0127     requestSpy.clear();
0128     typeChangedSpy.clear();
0129 
0130     manager.setInputTextChanged(QString(), QStringLiteral("a :heart:"), 9);
0131     QCOMPARE(typeChangedSpy.count(), 1);
0132     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::Emoji);
0133     QCOMPARE(requestSpy.count(), 0);
0134     typeChangedSpy.clear();
0135 
0136     // If the cursor isn't at the end of the word, don't trigger completion
0137     manager.setInputTextChanged(QString(), QStringLiteral("@foo"), 2);
0138     QCOMPARE(typeChangedSpy.count(), 1);
0139     QCOMPARE(typeChangedSpy.at(0).at(0).value<InputTextManager::CompletionForType>(), InputTextManager::None);
0140     QCOMPARE(requestSpy.count(), 0);
0141     typeChangedSpy.clear();
0142 }
0143 
0144 #include "moc_inputtextmanagertest.cpp"