1 /*
2     SPDX-FileCopyrightText: 2017 Montel Laurent <montel@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "kpasswordlineedittest.h"
8 #include "kpasswordlineedit.h"
9 #include <QAction>
10 #include <QHBoxLayout>
11 #include <QLineEdit>
12 #include <QSignalSpy>
13 #include <QTest>
14 
PasswordLineEditTest(QObject * parent)15 PasswordLineEditTest::PasswordLineEditTest(QObject *parent)
16     : QObject(parent)
17 {
18 }
19 
shouldHaveDefaultValue()20 void PasswordLineEditTest::shouldHaveDefaultValue()
21 {
22     KPasswordLineEdit lineEdit;
23     QVERIFY(lineEdit.password().isEmpty());
24 
25     QHBoxLayout *mainLayout = lineEdit.findChild<QHBoxLayout *>(QStringLiteral("mainlayout"));
26     QVERIFY(mainLayout);
27     QCOMPARE(mainLayout->contentsMargins(), QMargins(0, 0, 0, 0));
28 
29     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
30     QVERIFY(edit);
31     QVERIFY(edit->text().isEmpty());
32     QCOMPARE(edit->echoMode(), QLineEdit::Password);
33 
34     QVERIFY(lineEdit.toggleEchoModeAction());
35     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
36 }
37 
shouldShowTogglePassword()38 void PasswordLineEditTest::shouldShowTogglePassword()
39 {
40     KPasswordLineEdit lineEdit;
41     lineEdit.show();
42     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
43 
44     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
45     edit->setText(QStringLiteral("FOO"));
46     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
47 
48     edit->clear();
49     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
50 }
51 
shouldNotShowToggleWhenSetPassword()52 void PasswordLineEditTest::shouldNotShowToggleWhenSetPassword()
53 {
54     KPasswordLineEdit lineEdit;
55     lineEdit.show();
56     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
57     lineEdit.setPassword(QStringLiteral("foo"));
58     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
59 }
60 
shouldShowRevealPassword()61 void PasswordLineEditTest::shouldShowRevealPassword()
62 {
63     KPasswordLineEdit lineEdit;
64     lineEdit.show();
65     QVERIFY(QTest::qWaitForWindowExposed(&lineEdit));
66 
67     QLineEdit *edit = lineEdit.findChild<QLineEdit *>(QStringLiteral("passwordlineedit"));
68     edit->setText(QStringLiteral("FOO"));
69     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
70 
71     lineEdit.setRevealPasswordAvailable(false);
72     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
73 
74     lineEdit.setRevealPasswordAvailable(true);
75     QVERIFY(lineEdit.toggleEchoModeAction()->isVisible());
76 
77     edit->clear();
78     QVERIFY(!lineEdit.toggleEchoModeAction()->isVisible());
79 }
80 
shouldEmitSignalPasswordChanged()81 void PasswordLineEditTest::shouldEmitSignalPasswordChanged()
82 {
83     KPasswordLineEdit lineEdit;
84     lineEdit.show();
85     QSignalSpy spy(&lineEdit, &KPasswordLineEdit::passwordChanged);
86     lineEdit.setPassword(QStringLiteral("foo"));
87     QCOMPARE(spy.count(), 1);
88 }
89 
90 QTEST_MAIN(PasswordLineEditTest)
91