File indexing completed on 2024-04-28 15:31:53

0001 /*
0002     SPDX-FileCopyrightText: 2017 Montel Laurent <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "kpasswordlineedittest.h"
0008 #include "kpasswordlineedit.h"
0009 #include <QAction>
0010 #include <QHBoxLayout>
0011 #include <QLineEdit>
0012 #include <QSignalSpy>
0013 #include <QTest>
0014 
0015 PasswordLineEditTest::PasswordLineEditTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 void PasswordLineEditTest::shouldHaveDefaultValue()
0021 {
0022     KPasswordLineEdit lineEdit;
0023     QVERIFY(lineEdit.password().isEmpty());
0024 
0025     QHBoxLayout *mainLayout = lineEdit.findChild<QHBoxLayout *>(QStringLiteral("mainlayout"));
0026     QVERIFY(mainLayout);
0027     QCOMPARE(mainLayout->contentsMargins(), QMargins(0, 0, 0, 0));
0028 
0029     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
0030     QVERIFY(edit);
0031     QVERIFY(edit->text().isEmpty());
0032     QCOMPARE(edit->echoMode(), QLineEdit::Password);
0033 
0034     QVERIFY(lineEdit.toggleEchoModeAction());
0035     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
0036 }
0037 
0038 void PasswordLineEditTest::shouldShowTogglePassword()
0039 {
0040     KPasswordLineEdit lineEdit;
0041     lineEdit.show();
0042     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
0043 
0044     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
0045     edit->setText(QStringLiteral("FOO"));
0046     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
0047 
0048     edit->clear();
0049     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
0050 }
0051 
0052 void PasswordLineEditTest::shouldNotShowToggleWhenSetPassword()
0053 {
0054     KPasswordLineEdit lineEdit;
0055     lineEdit.show();
0056     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
0057     lineEdit.setPassword(QStringLiteral("foo"));
0058     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
0059 }
0060 
0061 void PasswordLineEditTest::shouldShowRevealPassword()
0062 {
0063     KPasswordLineEdit lineEdit;
0064     lineEdit.show();
0065     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
0066 
0067     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
0068     edit->setText(QStringLiteral("FOO"));
0069     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
0070 
0071     lineEdit.setRevealPasswordAvailable(false);
0072     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
0073 
0074     lineEdit.setRevealPasswordAvailable(true);
0075     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
0076 
0077     edit->clear();
0078     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
0079 }
0080 
0081 void PasswordLineEditTest::shouldEmitSignalPasswordChanged()
0082 {
0083     KPasswordLineEdit lineEdit;
0084     lineEdit.show();
0085     QSignalSpy spy(&lineEdit, &KPasswordLineEdit::passwordChanged);
0086     lineEdit.setPassword(QStringLiteral("foo"));
0087     QCOMPARE(spy.count(), 1);
0088 }
0089 
0090 QTEST_MAIN(PasswordLineEditTest)
0091 
0092 #include "moc_kpasswordlineedittest.cpp"