File indexing completed on 2024-06-16 04:28:03

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "translatorwidgettest.h"
0008 #include "translator/widgets/translatorwidget.h"
0009 
0010 #include <QPushButton>
0011 
0012 #include <QComboBox>
0013 #include <QSignalSpy>
0014 #include <QStandardPaths>
0015 #include <QTest>
0016 #include <QToolButton>
0017 #include <qtestkeyboard.h>
0018 #include <qtestmouse.h>
0019 QTEST_MAIN(TranslatorWidgetTest)
0020 
0021 TranslatorWidgetTest::TranslatorWidgetTest()
0022 {
0023     QStandardPaths::setTestModeEnabled(true);
0024 }
0025 
0026 void TranslatorWidgetTest::shouldHaveDefaultValuesOnCreation()
0027 {
0028     TextTranslator::TranslatorWidget edit;
0029     auto from = edit.findChild<QComboBox *>(QStringLiteral("from"));
0030     auto to = edit.findChild<QComboBox *>(QStringLiteral("to"));
0031 
0032     auto inputtext = edit.findChild<TextTranslator::TranslatorTextEdit *>(QStringLiteral("inputtext"));
0033     auto translate = edit.findChild<QPushButton *>(QStringLiteral("translate-button"));
0034     auto clear = edit.findChild<QPushButton *>(QStringLiteral("clear-button"));
0035     auto invert = edit.findChild<QPushButton *>(QStringLiteral("invert-button"));
0036     auto configure = edit.findChild<QToolButton *>(QStringLiteral("configure_button"));
0037     auto close = edit.findChild<QToolButton *>(QStringLiteral("close-button"));
0038     QVERIFY(configure);
0039     QVERIFY(close);
0040     QVERIFY(invert);
0041     QVERIFY(clear);
0042     QVERIFY(translate);
0043     QVERIFY(from);
0044     QVERIFY(to);
0045     QCOMPARE(from->count() > 0, true);
0046     QCOMPARE(to->count() > 0, true);
0047     QVERIFY(inputtext);
0048     QCOMPARE(inputtext->toPlainText(), QString());
0049     QCOMPARE(translate->isEnabled(), false);
0050 }
0051 
0052 void TranslatorWidgetTest::shouldEnableTranslateButtonWhenTextToTranslateIsNotEmpty()
0053 {
0054     TextTranslator::TranslatorWidget edit;
0055 
0056     auto inputtext = edit.findChild<TextTranslator::TranslatorTextEdit *>(QStringLiteral("inputtext"));
0057     auto translate = edit.findChild<QPushButton *>(QStringLiteral("translate-button"));
0058     inputtext->setPlainText(QStringLiteral("Foo"));
0059     QCOMPARE(translate->isEnabled(), true);
0060 }
0061 
0062 void TranslatorWidgetTest::shouldDisableTranslateButtonAndClearTextWhenClickOnClearButton()
0063 {
0064     TextTranslator::TranslatorWidget edit;
0065     auto inputtext = edit.findChild<TextTranslator::TranslatorTextEdit *>(QStringLiteral("inputtext"));
0066     auto translate = edit.findChild<QPushButton *>(QStringLiteral("translate-button"));
0067     inputtext->setPlainText(QStringLiteral("Foo"));
0068     auto clear = edit.findChild<QPushButton *>(QStringLiteral("clear-button"));
0069     QTest::mouseClick(clear, Qt::LeftButton);
0070     QCOMPARE(inputtext->toPlainText(), QString());
0071     QCOMPARE(translate->isEnabled(), false);
0072 }
0073 
0074 void TranslatorWidgetTest::shouldInvertLanguageWhenClickOnInvertButton()
0075 {
0076     TextTranslator::TranslatorWidget edit;
0077     auto from = edit.findChild<QComboBox *>(QStringLiteral("from"));
0078     auto to = edit.findChild<QComboBox *>(QStringLiteral("to"));
0079 
0080     const int fromIndex = 5;
0081     const int toIndex = 7;
0082     from->setCurrentIndex(fromIndex);
0083     to->setCurrentIndex(toIndex);
0084     auto invert = edit.findChild<QPushButton *>(QStringLiteral("invert-button"));
0085     QTest::mouseClick(invert, Qt::LeftButton);
0086     const int newFromIndex = from->currentIndex();
0087     const int newToIndex = to->currentIndex();
0088     QCOMPARE(fromIndex != newFromIndex, true);
0089     QCOMPARE(toIndex != newToIndex, true);
0090 }
0091 
0092 void TranslatorWidgetTest::shouldHideWidgetWhenPressEscape()
0093 {
0094     TextTranslator::TranslatorWidget edit;
0095     edit.show();
0096     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0097     QTest::keyPress(&edit, Qt::Key_Escape);
0098     QCOMPARE(edit.isVisible(), false);
0099 }
0100 
0101 void TranslatorWidgetTest::shouldEmitTranslatorWasClosedSignalWhenCloseIt()
0102 {
0103     TextTranslator::TranslatorWidget edit;
0104     edit.show();
0105     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0106     QSignalSpy spy(&edit, &TextTranslator::TranslatorWidget::toolsWasClosed);
0107     QTest::keyClick(&edit, Qt::Key_Escape);
0108     QCOMPARE(spy.count(), 1);
0109 }
0110 
0111 #include "moc_translatorwidgettest.cpp"