lineeditwithautocorrectiontest.cpp 3.26 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2014-2020 Laurent Montel <montel@kde.org>
3

4
  SPDX-License-Identifier: GPL-2.0-or-later
5 6 7
*/

#include "lineeditwithautocorrectiontest.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include <PimCommon/LineEditWithAutoCorrection>
9
#include "../autocorrection.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include <QTest>
11 12 13 14 15 16 17 18
#include <qtestkeyboard.h>

LineEditWithAutocorrectionTest::LineEditWithAutocorrectionTest()
{
}

void LineEditWithAutocorrectionTest::shouldNotAutocorrectWhenDisabled()
{
Laurent Montel's avatar
Laurent Montel committed
19
    PimCommon::LineEditWithAutoCorrection lineedit(nullptr, QStringLiteral("lineeditwithautocorrecttestrc"));
20 21 22 23 24 25
    QHash<QString, QString> entries;
    const QString originalWord = QStringLiteral("FOOFOO");
    const QString replaceWord = QStringLiteral("BLABLA");
    entries.insert(originalWord, replaceWord);
    lineedit.autocorrection()->setAutocorrectEntries(entries);
    lineedit.show();
26
    QVERIFY(QTest::qWaitForWindowExposed(&lineedit));
27 28 29 30 31 32 33
    QTest::keyClicks(&lineedit, originalWord);
    QTest::keyClick(&lineedit, ' ');
    QCOMPARE(lineedit.toPlainText(), QString(originalWord + QLatin1Char(' ')));
}

void LineEditWithAutocorrectionTest::shouldReplaceWordWhenExactText()
{
Laurent Montel's avatar
Laurent Montel committed
34
    PimCommon::LineEditWithAutoCorrection lineedit(nullptr, QStringLiteral("lineeditwithautocorrecttestrc"));
35 36 37 38 39 40 41 42
    const QString originalWord = QStringLiteral("FOOFOO");
    const QString replaceWord = QStringLiteral("BLABLA");
    QHash<QString, QString> entries;
    entries.insert(originalWord, replaceWord);
    lineedit.autocorrection()->setAutocorrectEntries(entries);
    lineedit.autocorrection()->setEnabledAutoCorrection(true);
    lineedit.autocorrection()->setAdvancedAutocorrect(true);
    lineedit.show();
43
    QVERIFY(QTest::qWaitForWindowExposed(&lineedit));
44 45 46 47 48 49 50
    QTest::keyClicks(&lineedit, originalWord);
    QTest::keyClick(&lineedit, ' ');
    QCOMPARE(lineedit.toPlainText(), QString(replaceWord + QLatin1Char(' ')));
}

void LineEditWithAutocorrectionTest::shouldNotReplaceWordWhenInexactText()
{
Laurent Montel's avatar
Laurent Montel committed
51
    PimCommon::LineEditWithAutoCorrection lineedit(nullptr, QStringLiteral("lineeditwithautocorrecttestrc"));
52 53 54 55 56 57 58 59 60
    const QString originalWord = QStringLiteral("FOOFOO");
    const QString replaceWord = QStringLiteral("BLABLA");
    QHash<QString, QString> entries;
    entries.insert(originalWord, replaceWord);
    lineedit.autocorrection()->setAutocorrectEntries(entries);
    lineedit.autocorrection()->setEnabledAutoCorrection(true);
    lineedit.autocorrection()->setAdvancedAutocorrect(true);
    lineedit.show();
    const QString nonExactText = QStringLiteral("BLIBLI");
61
    QVERIFY(QTest::qWaitForWindowExposed(&lineedit));
62 63 64 65 66 67 68
    QTest::keyClicks(&lineedit, nonExactText);
    QTest::keyClick(&lineedit, ' ');
    QCOMPARE(lineedit.toPlainText(), QString(nonExactText + QLatin1Char(' ')));
}

void LineEditWithAutocorrectionTest::shouldNotAddTwoSpace()
{
Laurent Montel's avatar
Laurent Montel committed
69
    PimCommon::LineEditWithAutoCorrection lineedit(nullptr, QStringLiteral("lineeditwithautocorrecttestrc"));
70 71 72 73
    const QString originalWord = QStringLiteral("FOOFOO ");
    lineedit.autocorrection()->setSingleSpaces(true);
    lineedit.autocorrection()->setEnabledAutoCorrection(true);
    lineedit.show();
74
    QVERIFY(QTest::qWaitForWindowExposed(&lineedit));
75 76 77 78 79 80
    QTest::keyClicks(&lineedit, originalWord);
    QTest::keyClick(&lineedit, ' ');
    QCOMPARE(lineedit.toPlainText(), originalWord);
}

QTEST_MAIN(LineEditWithAutocorrectionTest)