File indexing completed on 2024-09-22 04:18:54

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "textgotolinewidgettest.h"
0008 #include "widgets/textgotolinewidget.h"
0009 #include <QPushButton>
0010 #include <QSignalSpy>
0011 #include <QSpinBox>
0012 #include <QTest>
0013 #include <QToolButton>
0014 #include <qtestkeyboard.h>
0015 #include <qtestmouse.h>
0016 
0017 TextGoToLineWidgetTest::TextGoToLineWidgetTest(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 void TextGoToLineWidgetTest::shouldHaveDefaultValuesOnCreation()
0023 {
0024     TextCustomEditor::TextGoToLineWidget edit;
0025     edit.show();
0026     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0027     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0028     QVERIFY(line);
0029     QCOMPARE(line->minimum(), 1);
0030     auto gotolinebutton = edit.findChild<QPushButton *>(QStringLiteral("gotoline"));
0031     QVERIFY(gotolinebutton);
0032     auto closebutton = edit.findChild<QToolButton *>(QStringLiteral("closebutton"));
0033     QVERIFY(closebutton);
0034     QVERIFY(line->hasFocus());
0035 }
0036 
0037 void TextGoToLineWidgetTest::shouldEmitGoToLineSignalWhenPressOnButton()
0038 {
0039     TextCustomEditor::TextGoToLineWidget edit;
0040     auto gotolinebutton = edit.findChild<QPushButton *>(QStringLiteral("gotoline"));
0041     QSignalSpy spy(&edit, &TextCustomEditor::TextGoToLineWidget::moveToLine);
0042     QTest::mouseClick(gotolinebutton, Qt::LeftButton);
0043     QCOMPARE(spy.count(), 1);
0044 }
0045 
0046 void TextGoToLineWidgetTest::shouldEmitGoToLineSignalCorrectValueWhenPressOnButton()
0047 {
0048     TextCustomEditor::TextGoToLineWidget edit;
0049     auto gotolinebutton = edit.findChild<QPushButton *>(QStringLiteral("gotoline"));
0050     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0051     line->setValue(5);
0052     QCOMPARE(line->value(), 5);
0053     QSignalSpy spy(&edit, &TextCustomEditor::TextGoToLineWidget::moveToLine);
0054     QTest::mouseClick(gotolinebutton, Qt::LeftButton);
0055     QCOMPARE(spy.count(), 1);
0056     QCOMPARE(spy.at(0).at(0).toInt(), 5);
0057 }
0058 
0059 void TextGoToLineWidgetTest::shouldHideWidgetWhenClickOnCloseButton()
0060 {
0061     TextCustomEditor::TextGoToLineWidget edit;
0062     edit.show();
0063     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0064     QVERIFY(edit.isVisible());
0065     auto closebutton = edit.findChild<QToolButton *>(QStringLiteral("closebutton"));
0066     QTest::mouseClick(closebutton, Qt::LeftButton);
0067     QVERIFY(!edit.isVisible());
0068 }
0069 
0070 void TextGoToLineWidgetTest::shouldHideWidgetWhenPressEscape()
0071 {
0072     TextCustomEditor::TextGoToLineWidget edit;
0073     edit.show();
0074     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0075     QTest::keyPress(&edit, Qt::Key_Escape);
0076     QCOMPARE(edit.isVisible(), false);
0077 }
0078 
0079 void TextGoToLineWidgetTest::shouldEmitGoToLineSignalWhenSpinboxHasFocusAndWePressEnter()
0080 {
0081     TextCustomEditor::TextGoToLineWidget edit;
0082     edit.show();
0083     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0084     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0085     line->setFocus();
0086     QVERIFY(line->hasFocus());
0087     line->setValue(5);
0088     QSignalSpy spy(&edit, &TextCustomEditor::TextGoToLineWidget::moveToLine);
0089     QTest::keyPress(line, Qt::Key_Enter);
0090     QCOMPARE(spy.count(), 1);
0091     QCOMPARE(spy.at(0).at(0).toInt(), 5);
0092 }
0093 
0094 void TextGoToLineWidgetTest::shouldHasFocusEachTimeThatItShown()
0095 {
0096     TextCustomEditor::TextGoToLineWidget edit;
0097     edit.show();
0098     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0099     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0100     QVERIFY(line);
0101     QVERIFY(line->hasFocus());
0102     edit.hide();
0103     QVERIFY(!line->hasFocus());
0104     edit.show();
0105     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0106     // FIXME QVERIFY(line->hasFocus());
0107 }
0108 
0109 void TextGoToLineWidgetTest::shouldSetFocusWhenWeRecallGotToLine()
0110 {
0111     TextCustomEditor::TextGoToLineWidget edit;
0112     edit.show();
0113     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0114     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0115     QVERIFY(line->hasFocus());
0116     edit.setFocus();
0117     QVERIFY(!line->hasFocus());
0118     edit.goToLine();
0119     QVERIFY(line->hasFocus());
0120 }
0121 
0122 void TextGoToLineWidgetTest::shouldChangeMaximumValue()
0123 {
0124     TextCustomEditor::TextGoToLineWidget edit;
0125     edit.show();
0126     QVERIFY(QTest::qWaitForWindowExposed(&edit));
0127     auto line = edit.findChild<QSpinBox *>(QStringLiteral("line"));
0128 
0129     QCOMPARE(line->value(), 1);
0130     QCOMPARE(line->minimum(), 1);
0131 
0132     edit.setMaximumLineCount(50);
0133     QCOMPARE(line->value(), 1);
0134     QCOMPARE(line->minimum(), 1);
0135     QCOMPARE(line->maximum(), 50);
0136 
0137     edit.setMaximumLineCount(10);
0138     QCOMPARE(line->value(), 1);
0139     QCOMPARE(line->minimum(), 1);
0140     QCOMPARE(line->maximum(), 10);
0141 }
0142 
0143 QTEST_MAIN(TextGoToLineWidgetTest)
0144 
0145 #include "moc_textgotolinewidgettest.cpp"