File indexing completed on 2024-04-28 15:32:18

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2007 Olivier Goffart <ogoffart at kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <QApplication>
0009 #include <QMap>
0010 
0011 #include <kpassworddialog.h>
0012 
0013 #include <iostream>
0014 
0015 // We can't depend on i18n in this test, but still -- show good practice (other than the line below) :-)
0016 #define i18n QString::fromLatin1
0017 
0018 int main(int argc, char *argv[])
0019 {
0020     QApplication::setApplicationName(QStringLiteral("KNewPasswordDialogTest"));
0021     QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
0022     QApplication a(argc, argv);
0023 
0024     // step 1  simple password
0025     {
0026         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowKeepPassword);
0027         dlg.setPrompt(
0028             i18n("This is a long prompt line. It is important it to be long so we can test the dialog does not get broken because of multiline labels. Please "
0029                  "enter a password:"));
0030         dlg.addCommentLine(i18n("This is a rather large left comment line"),
0031                            i18n("Right part of the comment line has to be long too so be test the layouting works really ok. Please visit http://www.kde.org"));
0032 
0033         if (dlg.exec()) {
0034             std::cout << "Entered password: " << qPrintable(dlg.password()) << std::endl;
0035         } else {
0036             std::cout << "No password" << std::endl;
0037             return -1;
0038         }
0039     }
0040 
0041     // step 2 readonly username
0042     {
0043         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowUsernameLine | KPasswordDialog::UsernameReadOnly);
0044         dlg.setPrompt(i18n("Enter a password for the test"));
0045         dlg.setUsername(QStringLiteral("konqui"));
0046         dlg.addCommentLine(i18n("Site"), i18n("http://www.kde.org"));
0047 
0048         if (dlg.exec()) {
0049             std::cout << "Entered password: " << qPrintable(dlg.password()) << std::endl;
0050         } else {
0051             std::cout << "No password" << std::endl;
0052             return -1;
0053         }
0054     }
0055 
0056     // step 3 with some username preset
0057     {
0058         KPasswordDialog dlg(nullptr, KPasswordDialog::ShowUsernameLine);
0059         dlg.setPrompt(i18n("Enter a password for the test"));
0060         QMap<QString, QString> logins;
0061         logins.insert(QStringLiteral("konqui"), QStringLiteral("foo"));
0062         logins.insert(QStringLiteral("watson"), QStringLiteral("bar"));
0063         logins.insert(QStringLiteral("ogoffart"), QString());
0064 
0065         dlg.setKnownLogins(logins);
0066 
0067         if (dlg.exec()) {
0068             std::cout << "Entered password: " << qPrintable(dlg.password()) << " for username " << qPrintable(dlg.username()) << std::endl;
0069         } else {
0070             std::cout << "No password" << std::endl;
0071             return -1;
0072         }
0073     }
0074 
0075     return 0;
0076 }