File indexing completed on 2024-04-21 14:56:11

0001 /**
0002  * This file is part of the KDE libraries
0003  * Copyright 2008 Rafael Fernández López <ereslibre@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include <QApplication>
0022 #include <kfiledialog.h>
0023 #include <kmessagebox.h>
0024 
0025 int main(int argc, char **argv)
0026 {
0027     QApplication app(argc, argv);
0028 
0029     // Test for: saved location keyword.
0030     //     - Should return to the starting directory last used for this test.
0031     //     - Should have no entered file name.
0032     KFileDialog keywordDlg(QUrl("kfiledialog:///testKeyword"), QString("*.*|"), nullptr);
0033     keywordDlg.setMode(KFile::Files);
0034     keywordDlg.setWindowTitle(QString("Test for keyword with no file name"));
0035     keywordDlg.exec();
0036 
0037     // Test for: saved location keyword with file name.
0038     //     - Should return to the starting directory last used for this test.
0039     //     - Should enter the file name 'new.file'.
0040     KFileDialog keywordDlg2(QUrl("kfiledialog:///testKeyword/new.file"), QString("*.*|"), nullptr);
0041     keywordDlg2.setMode(KFile::Files);
0042     keywordDlg2.setWindowTitle(QString("Test for keyword and file name"));
0043     keywordDlg2.exec();
0044 
0045     // bug 173137
0046     KFileDialog dlg(QUrl(QString()), QString("*.*|"), nullptr);
0047     dlg.setMode(KFile::Files | KFile::Directory);
0048     dlg.setWindowTitle(QString("Test for bug 173137"));
0049     dlg.exec();
0050     QList<QUrl> selectedUrls = dlg.selectedUrls();
0051     if (selectedUrls.count()) {
0052         QString str("The listed files and folders below were asked to be opened:\n");
0053         foreach (const QUrl &filename, selectedUrls) {
0054             str += QString("\n%1").arg(filename.toString());
0055         }
0056         KMessageBox::information(nullptr, str, "Dialog for bug #173137 accepted");
0057     } else {
0058         KMessageBox::information(nullptr, QString("Dialog for bug #173137 cancelled"));
0059     }
0060     // end bug 173137
0061 
0062     // Note: when I talk about 'filename' I mean also with path. For instance, a filename on this
0063     // context is 'foo.txt', but also '/home/foo/bar/bar.txt'.
0064 
0065     // Test for: getOpenFileName.
0066     //     - Should return the selected file (if any).
0067     //     - Should return an empty string if 'Cancel' was pressed.
0068     //     - Should NOT return a non existing filename. If a non existing filename was given to it,
0069     //       it should inform the user about it, so we always get an empty string or an existing
0070     //       filename.
0071     QString getOpenFileName = KFileDialog::getOpenFileName(QUrl(), QString(), nullptr,
0072                               QString("Test getOpenFileName"));
0073 
0074     if (!getOpenFileName.isEmpty()) {
0075         KMessageBox::information(nullptr, QString("\"%1\" file was opened").arg(getOpenFileName), "Dialog for 'getOpenFileName' accepted");
0076     } else {
0077         KMessageBox::information(nullptr, QString("Dialog for 'getOpenFileName' cancelled"));
0078     }
0079 
0080     // Test for: getOpenFileNames.
0081     //     - Should return the selected files (if any).
0082     //     - Should return an empty list of strings if 'Cancel' was pressed.
0083     //     - Should NOT return a non existing filename in the list. If a non existing filename was
0084     //       given to it, it should inform the user about it, so we always get an empty string or an
0085     //       existing list of filenames.
0086     QStringList getOpenFileNames = KFileDialog::getOpenFileNames(QUrl(), QString(), nullptr,
0087                                    QString("Test getOpenFileNames"));
0088     if (getOpenFileNames.count()) {
0089         QString str("The listed files below were asked to be opened:\n");
0090         foreach (const QString &filename, getOpenFileNames) {
0091             str += QString("\n%1").arg(filename);
0092         }
0093         KMessageBox::information(nullptr, str, "Dialog for 'getOpenFileNames' accepted");
0094     } else {
0095         KMessageBox::information(nullptr, QString("Dialog for 'getOpenFileNames' cancelled"));
0096     }
0097 
0098     // Test for: getOpenUrl.
0099     //     - Is a convenience method for getOpenFileName, that returns a QUrl object instead of a
0100     //       QString object.
0101     //     - From the previous point it is expectable that its behavior should be the same as
0102     //       getOpenFileName.
0103     QUrl getOpenUrl = KFileDialog::getOpenUrl(QUrl(), QString(), nullptr,
0104                       QString("Test getOpenUrl"));
0105     if (getOpenUrl.isValid()) {
0106         KMessageBox::information(nullptr, QString("\"%1\" file was opened").arg(getOpenUrl.url()), "Dialog for 'getOpenUrl' accepted");
0107     } else {
0108         KMessageBox::information(nullptr, QString("Dialog for 'getOpenUrl' cancelled"));
0109     }
0110 
0111     // Test for: getOpenUrls.
0112     //     - Is a convenience method for getOpenFileNames, that returns a QList<QUrl> object instead
0113     //       of a QStringList object.
0114     //     - From the previous point it is expectable that its behavior should be the same as
0115     //       getOpenFileNames.
0116     QList<QUrl> getOpenUrls = KFileDialog::getOpenUrls(QUrl(), QString(), nullptr,
0117                               QString("Test getOpenUrls"));
0118     if (getOpenUrls.count()) {
0119         QString str("The listed files below were asked to be opened:\n");
0120         foreach (const QUrl &filename, getOpenUrls) {
0121             str += QString("\n%1").arg(filename.url());
0122         }
0123         KMessageBox::information(nullptr, str, "Dialog for 'getOpenUrls' accepted");
0124     } else {
0125         KMessageBox::information(nullptr, QString("Dialog for 'getOpenUrls' cancelled"));
0126     }
0127 
0128     // Test for: getSaveFileName.
0129     //     - Should return the selected file (if any).
0130     //     - Should return an empty string if 'Cancel' was pressed.
0131     //     - Don't care about existing or non existing filenames.
0132     QString getSaveFileName = KFileDialog::getSaveFileName(QUrl(), QString(), nullptr,
0133                               QString("Test getSaveFileName"));
0134     if (!getSaveFileName.isEmpty()) {
0135         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveFileName), "Dialog for 'getSaveFileName' accepted");
0136     } else {
0137         KMessageBox::information(nullptr, QString("Dialog for 'getSaveFileName' cancelled"));
0138     }
0139 
0140     // Tests for bug 194900
0141     //     - Should enter the specified directory with the file preselected.
0142     getSaveFileName = KFileDialog::getSaveFileName(QUrl::fromLocalFile("/usr/share/X11/rgb.txt"),
0143                       QString(), nullptr,
0144                       QString("Test bug 194900 getSaveFileName with file preselected"));
0145     if (!getSaveFileName.isEmpty()) {
0146         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveFileName), "Dialog for 'getSaveFileName' accepted");
0147     } else {
0148         KMessageBox::information(nullptr, QString("Dialog for 'getSaveFileName' cancelled"));
0149     }
0150     //     - Should enter the specified directory with no file preselected.
0151     getSaveFileName = KFileDialog::getSaveFileName(QUrl::fromLocalFile("/usr/share/X11"),
0152                       QString(), nullptr,
0153                       QString("Test bug 194900 getSaveFileName with no file preselected"));
0154     if (!getSaveFileName.isEmpty()) {
0155         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveFileName), "Dialog for 'getSaveFileName' accepted");
0156     } else {
0157         KMessageBox::information(nullptr, QString("Dialog for 'getSaveFileName' cancelled"));
0158     }
0159 
0160     // Test for: getSaveUrl.
0161     //     - Is a convenience method for getSaveFileName, that returns a QUrl object instead of a
0162     //       QString object.
0163     //     - From the previous point it is expectable that its behavior should be the same as
0164     //       getSaveFileName.
0165     QUrl getSaveUrl = KFileDialog::getSaveUrl(QUrl(), QString(), nullptr,
0166                       QString("Test getSaveUrl"));
0167     if (getSaveUrl.isValid()) {
0168         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveUrl.url()), "Dialog for 'getSaveUrl' accepted");
0169     } else {
0170         KMessageBox::information(nullptr, QString("Dialog for 'getSaveUrl' cancelled"));
0171     }
0172 
0173     // Tests for bug 194900
0174     //     - Should enter the specified directory with the file preselected.
0175     getSaveUrl = KFileDialog::getSaveUrl(QUrl::fromLocalFile("/usr/share/X11/rgb.txt"),
0176                                          QString(), nullptr,
0177                                          QString("Test bug 194900 getSaveUrl with file preselected"));
0178     if (getSaveUrl.isValid()) {
0179         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveUrl.url()), "Dialog for 'getSaveUrl' accepted");
0180     } else {
0181         KMessageBox::information(nullptr, QString("Dialog for 'getSaveUrl' cancelled"));
0182     }
0183     //     - Should enter the specified directory with no file preselected.
0184     getSaveUrl = KFileDialog::getSaveUrl(QUrl::fromLocalFile("/usr/share/X11/"),
0185                                          QString(), nullptr,
0186                                          QString("Test bug 194900 getSaveUrl with no file preselected"));
0187     if (getSaveUrl.isValid()) {
0188         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getSaveUrl.url()), "Dialog for 'getSaveUrl' accepted");
0189     } else {
0190         KMessageBox::information(nullptr, QString("Dialog for 'getSaveUrl' cancelled"));
0191     }
0192 
0193     // Test for: getImageOpenUrl.
0194     //     - Is the same as getOpenUrl but showing inline previews.
0195     QUrl getImageOpenUrl = KFileDialog::getImageOpenUrl(QUrl(), nullptr,
0196                            QString("Test getImageOpenUrl"));
0197     if (getImageOpenUrl.isValid()) {
0198         KMessageBox::information(nullptr, QString("\"%1\" file was asked to be saved").arg(getImageOpenUrl.url()), "Dialog for 'getImageOpenUrl' accepted");
0199     } else {
0200         KMessageBox::information(nullptr, QString("Dialog for 'getImageOpenUrl' cancelled"));
0201     }
0202 
0203     return 0;
0204 }