tests/auto/qfiledialog/tst_qfiledialog.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qcoreapplication.h>
       
    46 #include <qdebug.h>
       
    47 #include <qfiledialog.h>
       
    48 #include <qabstractitemdelegate.h>
       
    49 #include <qdirmodel.h>
       
    50 #include <qitemdelegate.h>
       
    51 #include <qlistview.h>
       
    52 #include <qcombobox.h>
       
    53 #include <qpushbutton.h>
       
    54 #include <qtoolbutton.h>
       
    55 #include <qtreeview.h>
       
    56 #include <qheaderview.h>
       
    57 #include <qcompleter.h>
       
    58 #include <qaction.h>
       
    59 #include <qdialogbuttonbox.h>
       
    60 #include <qsortfilterproxymodel.h>
       
    61 #include <qlineedit.h>
       
    62 #include <qlayout.h>
       
    63 #include "../../shared/util.h"
       
    64 #include "../../../src/gui/dialogs/qsidebar_p.h"
       
    65 #include "../../../src/gui/dialogs/qfilesystemmodel_p.h"
       
    66 #include "../../../src/gui/dialogs/qfiledialog_p.h"
       
    67 
       
    68 #include "../network-settings.h"
       
    69 
       
    70 //TESTED_CLASS=
       
    71 //TESTED_FILES=
       
    72 
       
    73 #if defined(Q_OS_SYMBIAN)
       
    74 # define STRINGIFY(x) #x
       
    75 # define TOSTRING(x) STRINGIFY(x)
       
    76 # define SRCDIR "C:/Private/" TOSTRING(SYMBIAN_SRCDIR_UID) "/"
       
    77 #endif
       
    78 
       
    79 class QNonNativeFileDialog : public QFileDialog
       
    80 {
       
    81     Q_OBJECT
       
    82 public:
       
    83     QNonNativeFileDialog(QWidget *parent = 0, const QString &caption = QString(), const QString &directory = QString(), const QString &filter = QString())
       
    84         : QFileDialog(parent, caption, directory, filter)
       
    85     {
       
    86         setOption(QFileDialog::DontUseNativeDialog, true);
       
    87     }
       
    88 };
       
    89 
       
    90 class tst_QFiledialog : public QObject
       
    91 {
       
    92 Q_OBJECT
       
    93 
       
    94 public:
       
    95     tst_QFiledialog();
       
    96     virtual ~tst_QFiledialog();
       
    97 
       
    98 public slots:
       
    99     void init();
       
   100     void cleanup();
       
   101 
       
   102 private slots:
       
   103     void currentChangedSignal();
       
   104     void directoryEnteredSignal();
       
   105     void filesSelectedSignal_data();
       
   106     void filesSelectedSignal();
       
   107     void filterSelectedSignal();
       
   108 
       
   109     void args();
       
   110     void directory();
       
   111     void completer_data();
       
   112     void completer();
       
   113     void completer_up();
       
   114     void acceptMode();
       
   115     void confirmOverwrite();
       
   116     void defaultSuffix();
       
   117     void fileMode();
       
   118     void filters();
       
   119     void history();
       
   120     void iconProvider();
       
   121     void isReadOnly();
       
   122     void itemDelegate();
       
   123     void labelText();
       
   124     void resolveSymlinks();
       
   125     void selectFile_data();
       
   126     void selectFile();
       
   127     void selectFiles();
       
   128     void selectFilter();
       
   129     void viewMode();
       
   130     void proxymodel();
       
   131     void setNameFilter();
       
   132     void focus();
       
   133     void caption();
       
   134     void historyBack();
       
   135     void historyForward();
       
   136     void disableSaveButton_data();
       
   137     void disableSaveButton();
       
   138     void saveButtonText_data();
       
   139     void saveButtonText();
       
   140     void clearLineEdit();
       
   141     void enableChooseButton();
       
   142     void hooks();
       
   143 
       
   144 private:
       
   145     QByteArray userSettings;
       
   146 };
       
   147 
       
   148 tst_QFiledialog::tst_QFiledialog()
       
   149 {
       
   150 }
       
   151 
       
   152 tst_QFiledialog::~tst_QFiledialog()
       
   153 {
       
   154 }
       
   155 
       
   156 void tst_QFiledialog::init()
       
   157 {
       
   158     // Save the developers settings so they don't get mad when their sidebar folders are gone.
       
   159     QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
   160     settings.beginGroup(QLatin1String("Qt"));
       
   161     userSettings = settings.value(QLatin1String("filedialog")).toByteArray();
       
   162     settings.remove(QLatin1String("filedialog"));
       
   163 
       
   164     // populate it with some default settings
       
   165     QNonNativeFileDialog fd;
       
   166 #if defined(Q_OS_WINCE)
       
   167     QTest::qWait(1000);
       
   168 #endif
       
   169 }
       
   170 
       
   171 void tst_QFiledialog::cleanup()
       
   172 {
       
   173     QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
       
   174     settings.beginGroup(QLatin1String("Qt"));
       
   175     settings.setValue(QLatin1String("filedialog"), userSettings);
       
   176 }
       
   177 
       
   178 class MyAbstractItemDelegate : public QAbstractItemDelegate
       
   179 {
       
   180 public:
       
   181     MyAbstractItemDelegate() : QAbstractItemDelegate() {};
       
   182     void paint(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const {}
       
   183     QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return QSize(); }
       
   184 };
       
   185 
       
   186 // emited any time the selection model emits current changed
       
   187 void tst_QFiledialog::currentChangedSignal()
       
   188 {
       
   189     QNonNativeFileDialog fd;
       
   190     fd.setViewMode(QFileDialog::List);
       
   191     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   192 
       
   193     QListView* listView = qFindChild<QListView*>(&fd, "listView");
       
   194     QVERIFY(listView);
       
   195     fd.setDirectory(QDir::root());
       
   196     QModelIndex root = listView->rootIndex();
       
   197     QTRY_COMPARE(listView->model()->rowCount(root) > 0, true);
       
   198 
       
   199     QModelIndex folder;
       
   200     for (int i = 0; i < listView->model()->rowCount(root); ++i) {
       
   201         folder = listView->model()->index(i, 0, root);
       
   202         if (listView->model()->hasChildren(folder))
       
   203             break;
       
   204     }
       
   205     QVERIFY(listView->model()->hasChildren(folder));
       
   206     listView->setCurrentIndex(folder);
       
   207 
       
   208     QCOMPARE(spyCurrentChanged.count(), 1);
       
   209 }
       
   210 
       
   211 // only emited from the views, sidebar, or lookin combo
       
   212 void tst_QFiledialog::directoryEnteredSignal()
       
   213 {
       
   214 #if defined QT_BUILD_INTERNAL
       
   215     QNonNativeFileDialog fd(0, "", QDir::root().path());
       
   216     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
   217     fd.show();
       
   218     QTRY_COMPARE(fd.isVisible(), true);
       
   219     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   220 
       
   221     // sidebar
       
   222     QSidebar *sidebar = qFindChild<QSidebar*>(&fd, "sidebar");
       
   223     sidebar->setCurrentIndex(sidebar->model()->index(1, 0));
       
   224     QTest::keyPress(sidebar->viewport(), Qt::Key_Return);
       
   225     QCOMPARE(spyDirectoryEntered.count(), 1);
       
   226     spyDirectoryEntered.clear();
       
   227 
       
   228     // lookInCombo
       
   229     QComboBox *comboBox = qFindChild<QComboBox*>(&fd, "lookInCombo");
       
   230     comboBox->showPopup();
       
   231     QVERIFY(comboBox->view()->model()->index(1, 0).isValid());
       
   232     comboBox->view()->setCurrentIndex(comboBox->view()->model()->index(1, 0));
       
   233     QTest::keyPress(comboBox->view()->viewport(), Qt::Key_Return);
       
   234     QCOMPARE(spyDirectoryEntered.count(), 1);
       
   235     spyDirectoryEntered.clear();
       
   236 
       
   237     // view
       
   238     /*
       
   239     // platform specific
       
   240     fd.setViewMode(QFileDialog::ViewMode(QFileDialog::List));
       
   241     QListView* listView = qFindChild<QListView*>(&fd, "listView");
       
   242     QVERIFY(listView);
       
   243     QModelIndex root = listView->rootIndex();
       
   244     QTRY_COMPARE(listView->model()->rowCount(root) > 0, true);
       
   245 
       
   246     QModelIndex folder;
       
   247     for (int i = 0; i < listView->model()->rowCount(root); ++i) {
       
   248         folder = listView->model()->index(i, 0, root);
       
   249         if (listView->model()->hasChildren(folder))
       
   250             break;
       
   251     }
       
   252     QVERIFY(listView->model()->hasChildren(folder));
       
   253     listView->setCurrentIndex(folder);
       
   254     QTRY_COMPARE((listView->indexAt(listView->visualRect(folder).center())), folder);
       
   255     QTest::mouseDClick(listView->viewport(), Qt::LeftButton, 0, listView->visualRect(folder).center());
       
   256     QTRY_COMPARE(spyDirectoryEntered.count(), 1);
       
   257     */
       
   258 #endif
       
   259 }
       
   260 
       
   261 Q_DECLARE_METATYPE(QFileDialog::FileMode)
       
   262 void tst_QFiledialog::filesSelectedSignal_data()
       
   263 {
       
   264     QTest::addColumn<QFileDialog::FileMode>("fileMode");
       
   265     QTest::newRow("any") << QFileDialog::AnyFile;
       
   266     QTest::newRow("existing") << QFileDialog::ExistingFile;
       
   267     QTest::newRow("directory") << QFileDialog::Directory;
       
   268     QTest::newRow("directoryOnly") << QFileDialog::DirectoryOnly;
       
   269     QTest::newRow("existingFiles") << QFileDialog::ExistingFiles;
       
   270 }
       
   271 
       
   272 // emited when the dialog closes with the selected files
       
   273 void tst_QFiledialog::filesSelectedSignal()
       
   274 {
       
   275     QNonNativeFileDialog fd;
       
   276     fd.setViewMode(QFileDialog::List);
       
   277     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
   278     QDir testDir(SRCDIR"/../../..");
       
   279     fd.setDirectory(testDir);
       
   280     QFETCH(QFileDialog::FileMode, fileMode);
       
   281     fd.setFileMode(fileMode);
       
   282     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   283 
       
   284     fd.show();
       
   285     QTest::qWait(500);
       
   286     QListView *listView = qFindChild<QListView*>(&fd, "listView");
       
   287     QVERIFY(listView);
       
   288 
       
   289     QModelIndex root = listView->rootIndex();
       
   290     QTRY_COMPARE(listView->model()->rowCount(root) > 0, true);
       
   291     QModelIndex file;
       
   292     for (int i = 0; i < listView->model()->rowCount(root); ++i) {
       
   293         file = listView->model()->index(i, 0, root);
       
   294         if (fileMode == QFileDialog::Directory || fileMode == QFileDialog::DirectoryOnly) {
       
   295             if (listView->model()->hasChildren(file))
       
   296                 break;
       
   297         } else {
       
   298             if (!listView->model()->hasChildren(file))
       
   299                 break;
       
   300         }
       
   301         file = QModelIndex();
       
   302     }
       
   303     QVERIFY(file.isValid());
       
   304     listView->selectionModel()->select(file, QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   305     listView->setCurrentIndex(file);
       
   306 
       
   307     QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&fd, "buttonBox");
       
   308     QPushButton *button = buttonBox->button(QDialogButtonBox::Open);
       
   309     QVERIFY(button);
       
   310     QVERIFY(button->isEnabled());
       
   311     button->animateClick();
       
   312     QTRY_COMPARE(fd.isVisible(), false);
       
   313     QCOMPARE(spyFilesSelected.count(), 1);
       
   314 }
       
   315 
       
   316 // only emited when the combo box is activated
       
   317 void tst_QFiledialog::filterSelectedSignal()
       
   318 {
       
   319     QNonNativeFileDialog fd;
       
   320     fd.setAcceptMode(QFileDialog::AcceptSave);
       
   321     fd.show();
       
   322     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   323 
       
   324     QStringList filterChoices;
       
   325     filterChoices << "Image files (*.png *.xpm *.jpg)"
       
   326                   << "Text files (*.txt)"
       
   327                   << "Any files (*.*)";
       
   328     fd.setFilters(filterChoices);
       
   329     QCOMPARE(fd.filters(), filterChoices);
       
   330 
       
   331     QComboBox *filters = qFindChild<QComboBox*>(&fd, "fileTypeCombo");
       
   332     QVERIFY(filters);
       
   333     QVERIFY(filters->view());
       
   334     QCOMPARE(filters->isVisible(), true);
       
   335 
       
   336     QTest::keyPress(filters, Qt::Key_Down);
       
   337 
       
   338     QCOMPARE(spyFilterSelected.count(), 1);
       
   339 }
       
   340 
       
   341 void tst_QFiledialog::args()
       
   342 {
       
   343     QWidget *parent = 0;
       
   344     QString caption = "caption";
       
   345     QString directory = QDir::tempPath();
       
   346     QString filter = "*.mp3";
       
   347     QNonNativeFileDialog fd(parent, caption, directory, filter);
       
   348     QCOMPARE(fd.parent(), (QObject *)parent);
       
   349     QCOMPARE(fd.windowTitle(), caption);
       
   350 #ifndef Q_OS_WIN
       
   351     QCOMPARE(fd.directory(), QDir(directory));
       
   352 #endif
       
   353     QCOMPARE(fd.filters(), QStringList(filter));
       
   354 }
       
   355 
       
   356 void tst_QFiledialog::directory()
       
   357 {
       
   358     QNonNativeFileDialog fd;
       
   359     fd.setViewMode(QFileDialog::List);
       
   360     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
   361     QVERIFY(model);
       
   362     fd.setDirectory(QDir::currentPath());
       
   363     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   364     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   365     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   366     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   367 
       
   368     QCOMPARE(QDir::current().absolutePath(), fd.directory().absolutePath());
       
   369     QDir temp = QDir::temp();
       
   370     QString tempPath = temp.absolutePath();
       
   371 #ifdef Q_OS_WIN
       
   372     // since the user can have lowercase temp dir, check that we are actually case-insensitive.
       
   373     tempPath = tempPath.toLower();
       
   374 #endif
       
   375     fd.setDirectory(tempPath);
       
   376 #ifndef Q_OS_WIN
       
   377     QCOMPARE(tempPath, fd.directory().absolutePath());
       
   378 #endif
       
   379     QCOMPARE(spyCurrentChanged.count(), 0);
       
   380     QCOMPARE(spyDirectoryEntered.count(), 0);
       
   381     QCOMPARE(spyFilesSelected.count(), 0);
       
   382     QCOMPARE(spyFilterSelected.count(), 0);
       
   383 
       
   384     // Check my way
       
   385     QList<QListView*> list = qFindChildren<QListView*>(&fd, "listView");
       
   386     QVERIFY(list.count() > 0);
       
   387 #ifdef Q_OS_WIN
       
   388     QCOMPARE(list.at(0)->rootIndex().data().toString().toLower(), temp.dirName().toLower());
       
   389 #else
       
   390     QCOMPARE(list.at(0)->rootIndex().data().toString(), temp.dirName());
       
   391 #endif
       
   392     QNonNativeFileDialog *dlg = new QNonNativeFileDialog(0, "", tempPath);
       
   393     QCOMPARE(model->index(tempPath), model->index(dlg->directory().absolutePath()));
       
   394     QCOMPARE(model->index(tempPath).data(QFileSystemModel::FileNameRole).toString(),
       
   395              model->index(dlg->directory().absolutePath()).data(QFileSystemModel::FileNameRole).toString());
       
   396     delete dlg;
       
   397     dlg = new QNonNativeFileDialog();
       
   398     QCOMPARE(model->index(tempPath), model->index(dlg->directory().absolutePath()));
       
   399     delete dlg;
       
   400 }
       
   401 
       
   402 void tst_QFiledialog::completer_data()
       
   403 {
       
   404     QTest::addColumn<QString>("startPath");
       
   405     QTest::addColumn<QString>("input");
       
   406     QTest::addColumn<int>("expected");
       
   407 
       
   408     QTest::newRow("r, 10")   << "" << "r"   << 10;
       
   409     QTest::newRow("x, 0")    << "" << "x"   << 0;
       
   410     QTest::newRow("../, -1") << "" << "../" << -1;
       
   411 
       
   412     QTest::newRow("goto root")     << QString()        << QDir::rootPath() << -1;
       
   413     QTest::newRow("start at root") << QDir::rootPath() << QString()        << -1;
       
   414 
       
   415     QDir root = QDir::root();
       
   416     QStringList list = root.entryList();
       
   417     QString folder;
       
   418     for (int i = 0; i < list.count(); ++i) {
       
   419         if (list.at(0) == QChar('.'))
       
   420             continue;
       
   421         QFileInfo info(QDir::rootPath() + list[i]);
       
   422         if (info.isDir()) {
       
   423             folder = QDir::rootPath() + list[i];
       
   424             break;
       
   425         }
       
   426     }
       
   427 
       
   428     QTest::newRow("start at one below root r") << folder << "r" << -1;
       
   429     QTest::newRow("start at one below root ../") << folder << "../" << -1;
       
   430 }
       
   431 
       
   432 void tst_QFiledialog::completer()
       
   433 {
       
   434     QFETCH(QString, input);
       
   435     QFETCH(QString, startPath);
       
   436     QFETCH(int, expected);
       
   437 
       
   438     QString tempPath = QDir::tempPath() + '/' + "QFileDialogTestDir";
       
   439     if (startPath.isEmpty())
       
   440         startPath = tempPath;
       
   441 
       
   442     startPath = QDir::cleanPath(startPath);
       
   443 
       
   444     // make temp dir and files
       
   445     {
       
   446         QDir cleanup(tempPath);
       
   447         QStringList x = cleanup.entryList();
       
   448         for (int i = 0; i < x.count(); ++i)
       
   449             QFile::remove(tempPath + '/' + x[i]);
       
   450         cleanup.rmdir(tempPath);
       
   451     }
       
   452     QDir tmp(QDir::tempPath());
       
   453     if (!tmp.exists(tempPath))
       
   454         QVERIFY(tmp.mkdir("QFileDialogTestDir"));
       
   455     QList<QTemporaryFile*> files;
       
   456     QT_TRY {
       
   457     for (int i = 0; i < 10; ++i) {
       
   458         QScopedPointer<QTemporaryFile> file(new QTemporaryFile(tempPath + "/rXXXXXX"));
       
   459         file->open();
       
   460         files.append(file.take());
       
   461     }
       
   462 
       
   463     // ### flesh this out more
       
   464     QNonNativeFileDialog fd(0,QString("Test it"),startPath);
       
   465     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
   466     fd.show();
       
   467     QVERIFY(fd.isVisible());
       
   468     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
   469     QVERIFY(model);
       
   470     QLineEdit *lineEdit = qFindChild<QLineEdit*>(&fd, "fileNameEdit");
       
   471     QVERIFY(lineEdit);
       
   472     QCompleter *completer = lineEdit->completer();
       
   473     QVERIFY(completer);
       
   474     QAbstractItemModel *cModel = completer->completionModel();
       
   475     QVERIFY(cModel);
       
   476 
       
   477     //wait a bit
       
   478     QTest::qWait(500);
       
   479 
       
   480     // path C:\depot\qt\examples\dialogs\standarddialogs
       
   481     // files
       
   482     //       [debug] [release] [tmp] dialog dialog main makefile makefile.debug makefile.release standarddialgos
       
   483     //
       
   484     // d      -> D:\ debug dialog.cpp dialog.h
       
   485     // ..\    -> ..\classwizard ..\configdialog ..\dialogs.pro
       
   486     // c      -> C:\ control panel
       
   487     // c:     -> C:\ (nothing more)
       
   488     // C:\    -> C:\, C:\_viminfo, ...
       
   489     // \      -> \_viminfo
       
   490     // c:\depot  -> 'nothing'
       
   491     // c:\depot\ -> C:\depot\devtools, C:\depot\dteske
       
   492     QCOMPARE(model->index(fd.directory().path()), model->index(startPath));
       
   493 
       
   494     if (input.isEmpty()) {
       
   495         QModelIndex r = model->index(model->rootPath());
       
   496         QVERIFY(model->rowCount(r) > 0);
       
   497         QModelIndex idx = model->index(0, 0, r);
       
   498         input = idx.data().toString().at(0);
       
   499     }
       
   500 
       
   501     // press 'keys' for the input
       
   502     for (int i = 0; i < input.count(); ++i)
       
   503         QTest::keyPress(lineEdit, input[i].toAscii());
       
   504 
       
   505     QStringList expectedFiles;
       
   506     if (expected == -1) {
       
   507         QString fullPath = startPath.isEmpty() ? tempPath : startPath;
       
   508         if (!fullPath.endsWith(QLatin1Char('/')))
       
   509             fullPath.append(QLatin1Char('/'));
       
   510         fullPath.append(input);
       
   511         bool inputStartsWithRootPath = false;
       
   512         if (input.startsWith(QDir::rootPath())) {
       
   513             fullPath = input;
       
   514             input.clear();
       
   515             inputStartsWithRootPath = true;
       
   516         }
       
   517 
       
   518         QFileInfo fi(fullPath);
       
   519         QDir x(fi.absolutePath());
       
   520         expectedFiles = x.entryList(model->filter());
       
   521         expected = 0;
       
   522         if (input.startsWith(".."))
       
   523             input.clear();
       
   524         for (int ii = 0; ii < expectedFiles.count(); ++ii) {
       
   525 #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
       
   526             if (expectedFiles.at(ii).startsWith(input,Qt::CaseInsensitive))
       
   527 #else
       
   528             if (expectedFiles.at(ii).startsWith(input))
       
   529 #endif
       
   530                 ++expected;
       
   531         }
       
   532 #if !defined(Q_OS_WIN)
       
   533         if (inputStartsWithRootPath)
       
   534             expected++;
       
   535 #endif
       
   536     }
       
   537 
       
   538     QTest::qWait(1000);
       
   539     if (cModel->rowCount() != expected) {
       
   540         for (int i = 0; i < cModel->rowCount(); ++i) {
       
   541             QString file = cModel->index(i, 0).data().toString();
       
   542             expectedFiles.removeAll(file);
       
   543         }
       
   544         //qDebug() << expectedFiles;
       
   545     }
       
   546 
       
   547 
       
   548     // ### FIXME: This will fail on Symbian on some tests and some environments until the file engine and QFileSystemModel
       
   549     // are fixed to properly capitalize paths, so that some folders are not duplicated in QFileSystemModel.
       
   550     QTRY_COMPARE(cModel->rowCount(), expected);
       
   551     } QT_CATCH(...) {
       
   552         qDeleteAll(files);
       
   553         QT_RETHROW;
       
   554     }
       
   555     qDeleteAll(files);
       
   556 }
       
   557 
       
   558 void tst_QFiledialog::completer_up()
       
   559 {
       
   560     QNonNativeFileDialog fd;
       
   561     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
   562     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   563     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   564     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   565     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   566 
       
   567     fd.show();
       
   568     QLineEdit *lineEdit = qFindChild<QLineEdit*>(&fd, "fileNameEdit");
       
   569     QVERIFY(lineEdit);
       
   570     QCOMPARE(spyFilesSelected.count(), 0);
       
   571     int depth = QDir::currentPath().split('/').count();
       
   572     for (int i = 0; i <= depth * 3 + 1; ++i) {
       
   573         lineEdit->insert("../");
       
   574         qApp->processEvents();
       
   575     }
       
   576     QCOMPARE(spyCurrentChanged.count(), 0);
       
   577     QCOMPARE(spyDirectoryEntered.count(), 0);
       
   578     QCOMPARE(spyFilesSelected.count(), 0);
       
   579     QCOMPARE(spyFilterSelected.count(), 0);
       
   580 }
       
   581 
       
   582 void tst_QFiledialog::acceptMode()
       
   583 {
       
   584     QNonNativeFileDialog fd;
       
   585     fd.show();
       
   586 
       
   587     QToolButton* newButton = qFindChild<QToolButton*>(&fd, "newFolderButton");
       
   588     QVERIFY(newButton);
       
   589 
       
   590     // default
       
   591     QCOMPARE(fd.acceptMode(), QFileDialog::AcceptOpen);
       
   592     QCOMPARE(newButton && newButton->isVisible(), true);
       
   593 
       
   594     //fd.setDetailsExpanded(true);
       
   595     fd.setAcceptMode(QFileDialog::AcceptSave);
       
   596     QCOMPARE(fd.acceptMode(), QFileDialog::AcceptSave);
       
   597     QCOMPARE(newButton->isVisible(), true);
       
   598 
       
   599     fd.setAcceptMode(QFileDialog::AcceptOpen);
       
   600     QCOMPARE(fd.acceptMode(), QFileDialog::AcceptOpen);
       
   601     QCOMPARE(newButton->isVisible(), true);
       
   602 }
       
   603 
       
   604 void tst_QFiledialog::confirmOverwrite()
       
   605 {
       
   606     QNonNativeFileDialog fd;
       
   607     QCOMPARE(fd.confirmOverwrite(), true);
       
   608     fd.setConfirmOverwrite(true);
       
   609     QCOMPARE(fd.confirmOverwrite(), true);
       
   610     fd.setConfirmOverwrite(false);
       
   611     QCOMPARE(fd.confirmOverwrite(), false);
       
   612     fd.setConfirmOverwrite(true);
       
   613     QCOMPARE(fd.confirmOverwrite(), true);
       
   614 }
       
   615 
       
   616 void tst_QFiledialog::defaultSuffix()
       
   617 {
       
   618     QNonNativeFileDialog fd;
       
   619     QCOMPARE(fd.defaultSuffix(), QString());
       
   620     fd.setDefaultSuffix("txt");
       
   621     QCOMPARE(fd.defaultSuffix(), QString("txt"));
       
   622     fd.setDefaultSuffix(QString());
       
   623     QCOMPARE(fd.defaultSuffix(), QString());
       
   624 }
       
   625 
       
   626 void tst_QFiledialog::fileMode()
       
   627 {
       
   628     QNonNativeFileDialog fd;
       
   629     QCOMPARE(fd.fileMode(), QFileDialog::AnyFile);
       
   630     fd.setFileMode(QFileDialog::ExistingFile);
       
   631     QCOMPARE(fd.fileMode(), QFileDialog::ExistingFile);
       
   632     fd.setFileMode(QFileDialog::Directory);
       
   633     QCOMPARE(fd.fileMode(), QFileDialog::Directory);
       
   634     fd.setFileMode(QFileDialog::DirectoryOnly);
       
   635     QCOMPARE(fd.fileMode(), QFileDialog::DirectoryOnly);
       
   636     fd.setFileMode(QFileDialog::ExistingFiles);
       
   637     QCOMPARE(fd.fileMode(), QFileDialog::ExistingFiles);
       
   638 }
       
   639 
       
   640 void tst_QFiledialog::caption()
       
   641 {
       
   642     QNonNativeFileDialog fd;
       
   643     fd.setWindowTitle("testing");
       
   644     fd.setFileMode(QFileDialog::Directory);
       
   645     QCOMPARE(fd.windowTitle(), QString("testing"));
       
   646 }
       
   647 
       
   648 void tst_QFiledialog::filters()
       
   649 {
       
   650     QNonNativeFileDialog fd;
       
   651     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
   652     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   653     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   654     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   655     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   656     QCOMPARE(fd.filters(), QStringList("All Files (*)"));
       
   657 
       
   658     // effects
       
   659     QList<QComboBox*> views = qFindChildren<QComboBox*>(&fd, "fileTypeCombo");
       
   660     QVERIFY(views.count() == 1);
       
   661     QCOMPARE(views.at(0)->isVisible(), false);
       
   662 
       
   663     QStringList filters;
       
   664     filters << "Image files (*.png *.xpm *.jpg)"
       
   665          << "Text files (*.txt)"
       
   666          << "Any files (*.*)";
       
   667     fd.setFilters(filters);
       
   668     QCOMPARE(views.at(0)->isVisible(), false);
       
   669     fd.show();
       
   670     fd.setAcceptMode(QFileDialog::AcceptSave);
       
   671     QCOMPARE(views.at(0)->isVisible(), true);
       
   672     QCOMPARE(fd.filters(), filters);
       
   673     fd.setFilter("Image files (*.png *.xpm *.jpg);;Text files (*.txt);;Any files (*.*)");
       
   674     QCOMPARE(fd.filters(), filters);
       
   675     QCOMPARE(spyCurrentChanged.count(), 0);
       
   676     QCOMPARE(spyDirectoryEntered.count(), 0);
       
   677     QCOMPARE(spyFilesSelected.count(), 0);
       
   678     QCOMPARE(spyFilterSelected.count(), 0);
       
   679 
       
   680     // setting shouldn't emit any signals
       
   681     for (int i = views.at(0)->currentIndex(); i < views.at(0)->count(); ++i)
       
   682         views.at(0)->setCurrentIndex(i);
       
   683     QCOMPARE(spyFilterSelected.count(), 0);
       
   684 
       
   685     //Let check if filters with whitespaces
       
   686     QNonNativeFileDialog fd2;
       
   687     QStringList expected;
       
   688     expected << "C++ Source Files(*.cpp)";
       
   689     expected << "Any(*.*)";
       
   690     fd2.setFilter("C++ Source Files(*.cpp);;Any(*.*)");
       
   691     QCOMPARE(expected, fd2.filters());
       
   692     fd2.setFilter("C++ Source Files(*.cpp) ;;Any(*.*)");
       
   693     QCOMPARE(expected, fd2.filters());
       
   694     fd2.setFilter("C++ Source Files(*.cpp);; Any(*.*)");
       
   695     QCOMPARE(expected, fd2.filters());
       
   696     fd2.setFilter(" C++ Source Files(*.cpp);; Any(*.*)");
       
   697     QCOMPARE(expected, fd2.filters());
       
   698     fd2.setFilter("C++ Source Files(*.cpp) ;; Any(*.*)");
       
   699     QCOMPARE(expected, fd2.filters());
       
   700 }
       
   701 
       
   702 void tst_QFiledialog::selectFilter()
       
   703 {
       
   704     QNonNativeFileDialog fd;
       
   705     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   706     QCOMPARE(fd.selectedFilter(), QString("All Files (*)"));
       
   707     QStringList filters;
       
   708     filters << "Image files (*.png *.xpm *.jpg)"
       
   709          << "Text files (*.txt)"
       
   710          << "Any files (*.*)";
       
   711     fd.setFilters(filters);
       
   712     QCOMPARE(fd.selectedFilter(), filters.at(0));
       
   713     fd.selectFilter(filters.at(1));
       
   714     QCOMPARE(fd.selectedFilter(), filters.at(1));
       
   715     fd.selectFilter(filters.at(2));
       
   716     QCOMPARE(fd.selectedFilter(), filters.at(2));
       
   717 
       
   718     fd.selectFilter("bob");
       
   719     QCOMPARE(fd.selectedFilter(), filters.at(2));
       
   720     fd.selectFilter("");
       
   721     QCOMPARE(fd.selectedFilter(), filters.at(2));
       
   722     QCOMPARE(spyFilterSelected.count(), 0);
       
   723 }
       
   724 
       
   725 void tst_QFiledialog::history()
       
   726 {
       
   727     QNonNativeFileDialog fd;
       
   728     fd.setViewMode(QFileDialog::List);
       
   729     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
   730     QVERIFY(model);
       
   731     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   732     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   733     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   734     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   735     QCOMPARE(model->index(fd.history().first()), model->index(QDir::toNativeSeparators(fd.directory().absolutePath())));
       
   736     fd.setDirectory(QDir::current().absolutePath());
       
   737     QStringList history;
       
   738     history << QDir::toNativeSeparators(QDir::current().absolutePath())
       
   739             << QDir::toNativeSeparators(QDir::home().absolutePath())
       
   740             << QDir::toNativeSeparators(QDir::temp().absolutePath());
       
   741     fd.setHistory(history);
       
   742     if (fd.history() != history) {
       
   743         qDebug() << fd.history() << history;
       
   744         // quick and dirty output for windows failure.
       
   745         QListView* list = qFindChild<QListView*>(&fd, "listView");
       
   746         QVERIFY(list);
       
   747         QModelIndex root = list->rootIndex();
       
   748         while (root.isValid()) {
       
   749             qDebug() << root.data();
       
   750             root = root.parent();
       
   751         }
       
   752     }
       
   753     QCOMPARE(fd.history(), history);
       
   754 
       
   755     QStringList badHistory;
       
   756     badHistory << "junk";
       
   757     fd.setHistory(badHistory);
       
   758     badHistory << QDir::toNativeSeparators(QDir::current().absolutePath());
       
   759     QCOMPARE(fd.history(), badHistory);
       
   760 
       
   761     QCOMPARE(spyCurrentChanged.count(), 0);
       
   762     QCOMPARE(spyDirectoryEntered.count(), 0);
       
   763     QCOMPARE(spyFilesSelected.count(), 0);
       
   764     QCOMPARE(spyFilterSelected.count(), 0);
       
   765 }
       
   766 
       
   767 void tst_QFiledialog::iconProvider()
       
   768 {
       
   769     QNonNativeFileDialog *fd = new QNonNativeFileDialog();
       
   770     QVERIFY(fd->iconProvider() != 0);
       
   771     QFileIconProvider *ip = new QFileIconProvider();
       
   772     fd->setIconProvider(ip);
       
   773     QCOMPARE(fd->iconProvider(), ip);
       
   774     delete fd;
       
   775     delete ip;
       
   776 }
       
   777 
       
   778 void tst_QFiledialog::isReadOnly()
       
   779 {
       
   780     QNonNativeFileDialog fd;
       
   781 
       
   782     QPushButton* newButton = qFindChild<QPushButton*>(&fd, "newFolderButton");
       
   783     QAction* renameAction = qFindChild<QAction*>(&fd, "qt_rename_action");
       
   784     QAction* deleteAction = qFindChild<QAction*>(&fd, "qt_delete_action");
       
   785 
       
   786     QCOMPARE(fd.isReadOnly(), false);
       
   787 
       
   788     // This is dependent upon the file/dir, find cross platform way to test
       
   789     //fd.setDirectory(QDir::home());
       
   790     //QCOMPARE(newButton && newButton->isEnabled(), true);
       
   791     //QCOMPARE(renameAction && renameAction->isEnabled(), true);
       
   792     //QCOMPARE(deleteAction && deleteAction->isEnabled(), true);
       
   793 
       
   794     fd.setReadOnly(true);
       
   795     QCOMPARE(fd.isReadOnly(), true);
       
   796 
       
   797     QCOMPARE(newButton && newButton->isEnabled(), false);
       
   798     QCOMPARE(renameAction && renameAction->isEnabled(), false);
       
   799     QCOMPARE(deleteAction && deleteAction->isEnabled(), false);
       
   800 }
       
   801 
       
   802 void tst_QFiledialog::itemDelegate()
       
   803 {
       
   804     QNonNativeFileDialog fd;
       
   805     QVERIFY(fd.itemDelegate() != 0);
       
   806     QItemDelegate *id = new QItemDelegate(&fd);
       
   807     fd.setItemDelegate(id);
       
   808     QCOMPARE(fd.itemDelegate(), (QAbstractItemDelegate *)id);
       
   809 }
       
   810 
       
   811 void tst_QFiledialog::labelText()
       
   812 {
       
   813     QNonNativeFileDialog fd;
       
   814     QDialogButtonBox buttonBox;
       
   815     QPushButton *cancelButton = buttonBox.addButton(QDialogButtonBox::Cancel);
       
   816     QCOMPARE(fd.labelText(QFileDialog::LookIn), QString("Look in:"));
       
   817     QCOMPARE(fd.labelText(QFileDialog::FileName), QString("File &name:"));
       
   818     QCOMPARE(fd.labelText(QFileDialog::FileType), QString("Files of type:"));
       
   819     QCOMPARE(fd.labelText(QFileDialog::Accept), QString("&Open")); ///### see task 241462
       
   820     QCOMPARE(fd.labelText(QFileDialog::Reject), cancelButton->text());
       
   821 
       
   822     fd.setLabelText(QFileDialog::LookIn, "1");
       
   823     QCOMPARE(fd.labelText(QFileDialog::LookIn), QString("1"));
       
   824     fd.setLabelText(QFileDialog::FileName, "2");
       
   825     QCOMPARE(fd.labelText(QFileDialog::FileName), QString("2"));
       
   826     fd.setLabelText(QFileDialog::FileType, "3");
       
   827     QCOMPARE(fd.labelText(QFileDialog::FileType), QString("3"));
       
   828     fd.setLabelText(QFileDialog::Accept, "4");
       
   829     QCOMPARE(fd.labelText(QFileDialog::Accept), QString("4"));
       
   830     fd.setLabelText(QFileDialog::Reject, "5");
       
   831     QCOMPARE(fd.labelText(QFileDialog::Reject), QString("5"));
       
   832 }
       
   833 
       
   834 void tst_QFiledialog::resolveSymlinks()
       
   835 {
       
   836     QNonNativeFileDialog fd;
       
   837 
       
   838     // default
       
   839     QCOMPARE(fd.resolveSymlinks(), true);
       
   840     fd.setResolveSymlinks(false);
       
   841     QCOMPARE(fd.resolveSymlinks(), false);
       
   842     fd.setResolveSymlinks(true);
       
   843     QCOMPARE(fd.resolveSymlinks(), true);
       
   844 
       
   845     // the file dialog doesn't do anything based upon this, just passes it to the model
       
   846     // the model should fully test it, don't test it here
       
   847 }
       
   848 
       
   849 void tst_QFiledialog::selectFile_data()
       
   850 {
       
   851     QTest::addColumn<QString>("file");
       
   852     QTest::addColumn<int>("count");
       
   853     QTest::newRow("null") << QString() << 1;
       
   854     QTest::newRow("file") << "foo" << 1;
       
   855     QTest::newRow("tmp") << "temp" << 1;
       
   856 }
       
   857 
       
   858 void tst_QFiledialog::selectFile()
       
   859 {
       
   860     QFETCH(QString, file);
       
   861     QFETCH(int, count);
       
   862     QNonNativeFileDialog fd;
       
   863     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
   864     QVERIFY(model);
       
   865     fd.setDirectory(QDir::currentPath());
       
   866     // default value
       
   867     QCOMPARE(fd.selectedFiles().count(), 1);
       
   868 
       
   869     QTemporaryFile tempFile(QDir::tempPath() + "/aXXXXXX");
       
   870     bool inTemp = (file == "temp");
       
   871     if (inTemp) {
       
   872         tempFile.open();
       
   873         file = tempFile.fileName();
       
   874     }
       
   875 
       
   876     fd.selectFile(file);
       
   877     QCOMPARE(fd.selectedFiles().count(), count);
       
   878     if (inTemp) {
       
   879         QCOMPARE(model->index(fd.directory().path()), model->index(QDir::tempPath()));
       
   880     } else {
       
   881         QCOMPARE(model->index(fd.directory().path()), model->index(QDir::currentPath()));
       
   882     }
       
   883 }
       
   884 
       
   885 void tst_QFiledialog::selectFiles()
       
   886 {
       
   887     QNonNativeFileDialog fd;
       
   888     fd.setViewMode(QFileDialog::List);
       
   889     QString tempPath = QDir::tempPath() + '/' + "QFileDialogTestDir4SelectFiles";
       
   890     QDir dir;
       
   891     QVERIFY(dir.mkpath(tempPath));
       
   892     fd.setDirectory(tempPath);
       
   893     QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
       
   894     QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
       
   895     QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
       
   896     QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
       
   897     fd.show();
       
   898     fd.setFileMode(QFileDialog::ExistingFiles);
       
   899 
       
   900     QString filesPath = fd.directory().absolutePath();
       
   901     for (int i=0; i < 5; ++i) {
       
   902         QFile file(filesPath + QString::fromLatin1("/qfiledialog_auto_test_not_pres_%1").arg(i));
       
   903         file.open(QIODevice::WriteOnly);
       
   904         file.resize(1024);
       
   905         file.flush();
       
   906         file.close();
       
   907     }
       
   908 
       
   909     // Get a list of files in the view and then get the corresponding index's
       
   910     QStringList list = fd.directory().entryList(QDir::Files);
       
   911     QModelIndexList toSelect;
       
   912     QVERIFY(list.count() > 1);
       
   913     QListView* listView = qFindChild<QListView*>(&fd, "listView");
       
   914     QVERIFY(listView);
       
   915     for (int i = 0; i < list.count(); ++i) {
       
   916         fd.selectFile(fd.directory().path() + "/" + list.at(i));
       
   917 #if defined(QT_MAC_USE_COCOA) || defined(Q_WS_WIN) || defined(Q_OS_SYMBIAN)
       
   918     QEXPECT_FAIL("", "This test does not work on Mac, Windows, or Symbian", Abort);
       
   919 #endif
       
   920         QTRY_VERIFY(!listView->selectionModel()->selectedRows().isEmpty());
       
   921         toSelect.append(listView->selectionModel()->selectedRows().last());
       
   922     }
       
   923     QCOMPARE(spyFilesSelected.count(), 0);
       
   924 
       
   925     listView->selectionModel()->clear();
       
   926     QCOMPARE(spyFilesSelected.count(), 0);
       
   927 
       
   928     // select the indexes
       
   929     for (int i = 0; i < toSelect.count(); ++i) {
       
   930         listView->selectionModel()->select(toSelect.at(i),
       
   931                 QItemSelectionModel::Select | QItemSelectionModel::Rows);
       
   932     }
       
   933     QCOMPARE(fd.selectedFiles().count(), toSelect.count());
       
   934     QCOMPARE(spyCurrentChanged.count(), 0);
       
   935     QCOMPARE(spyDirectoryEntered.count(), 0);
       
   936     QCOMPARE(spyFilesSelected.count(), 0);
       
   937     QCOMPARE(spyFilterSelected.count(), 0);
       
   938     for (int i=0; i < 5; ++i)
       
   939         QFile::remove(filesPath + QString::fromLatin1("/qfiledialog_auto_test_not_pres_%1").arg(i));
       
   940 
       
   941     //If the selection is invalid then we fill the line edit but without the /
       
   942     QNonNativeFileDialog * dialog = new QNonNativeFileDialog( 0, "Save" );
       
   943     dialog->setFileMode( QFileDialog::AnyFile );
       
   944     dialog->setAcceptMode( QFileDialog::AcceptSave );
       
   945     QString temporary = QDir::tempPath() + QLatin1String("/blah");
       
   946     dialog->selectFile(temporary);
       
   947     dialog->show();
       
   948     QTest::qWait(500);
       
   949     QLineEdit *lineEdit = qFindChild<QLineEdit*>(dialog, "fileNameEdit");
       
   950     QVERIFY(lineEdit);
       
   951     QCOMPARE(lineEdit->text(),QLatin1String("blah"));
       
   952 }
       
   953 
       
   954 void tst_QFiledialog::viewMode()
       
   955 {
       
   956     QNonNativeFileDialog fd;
       
   957     fd.setViewMode(QFileDialog::List);
       
   958     fd.show();
       
   959 
       
   960     // find widgets
       
   961     QList<QTreeView*> treeView = qFindChildren<QTreeView*>(&fd, "treeView");
       
   962     QCOMPARE(treeView.count(), 1);
       
   963     QList<QListView*> listView = qFindChildren<QListView*>(&fd, "listView");
       
   964     QCOMPARE(listView.count(), 1);
       
   965     QList<QToolButton*> listButton = qFindChildren<QToolButton*>(&fd, "listModeButton");
       
   966     QCOMPARE(listButton.count(), 1);
       
   967     QList<QToolButton*> treeButton = qFindChildren<QToolButton*>(&fd, "detailModeButton");
       
   968     QCOMPARE(treeButton.count(), 1);
       
   969 
       
   970     // default value
       
   971     QCOMPARE(fd.viewMode(), QFileDialog::List);
       
   972 
       
   973     // detail
       
   974     fd.setViewMode(QFileDialog::ViewMode(QFileDialog::Detail));
       
   975 
       
   976     QCOMPARE(QFileDialog::ViewMode(QFileDialog::Detail), fd.viewMode());
       
   977     QCOMPARE(listView.at(0)->isVisible(), false);
       
   978     QCOMPARE(listButton.at(0)->isDown(), false);
       
   979     QCOMPARE(treeView.at(0)->isVisible(), true);
       
   980     QCOMPARE(treeButton.at(0)->isDown(), true);
       
   981 
       
   982     // list
       
   983     fd.setViewMode(QFileDialog::ViewMode(QFileDialog::List));
       
   984 
       
   985     QCOMPARE(QFileDialog::ViewMode(QFileDialog::List), fd.viewMode());
       
   986     QCOMPARE(treeView.at(0)->isVisible(), false);
       
   987     QCOMPARE(treeButton.at(0)->isDown(), false);
       
   988     QCOMPARE(listView.at(0)->isVisible(), true);
       
   989     QCOMPARE(listButton.at(0)->isDown(), true);
       
   990 }
       
   991 
       
   992 void tst_QFiledialog::proxymodel()
       
   993 {
       
   994     QNonNativeFileDialog fd;
       
   995     QCOMPARE(fd.proxyModel(), (QAbstractProxyModel*)0);
       
   996 
       
   997     fd.setProxyModel(0);
       
   998     QCOMPARE(fd.proxyModel(), (QAbstractProxyModel*)0);
       
   999 
       
  1000     QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(&fd);
       
  1001     fd.setProxyModel(proxyModel);
       
  1002     QCOMPARE(fd.proxyModel(), (QAbstractProxyModel *)proxyModel);
       
  1003 
       
  1004     fd.setProxyModel(0);
       
  1005     QCOMPARE(fd.proxyModel(), (QAbstractProxyModel*)0);
       
  1006 }
       
  1007 
       
  1008 void tst_QFiledialog::setNameFilter()
       
  1009 {
       
  1010     QNonNativeFileDialog fd;
       
  1011     fd.setFilter(QString());
       
  1012     fd.setFilters(QStringList());
       
  1013 }
       
  1014 
       
  1015 void tst_QFiledialog::focus()
       
  1016 {
       
  1017     QNonNativeFileDialog fd;
       
  1018     fd.setDirectory(QDir::currentPath());
       
  1019     fd.show();
       
  1020     QApplication::setActiveWindow(&fd);
       
  1021     QTest::qWaitForWindowShown(&fd);
       
  1022     QTRY_COMPARE(fd.isVisible(), true);
       
  1023     QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget*>(&fd));
       
  1024     qApp->processEvents();
       
  1025 
       
  1026     // make sure the tests work with focus follows mouse
       
  1027     QCursor::setPos(fd.geometry().center());
       
  1028     QApplication::syncX();
       
  1029 
       
  1030     QList<QWidget*> treeView = qFindChildren<QWidget*>(&fd, "fileNameEdit");
       
  1031     QCOMPARE(treeView.count(), 1);
       
  1032     QVERIFY(treeView.at(0));
       
  1033     QTRY_COMPARE(treeView.at(0)->hasFocus(), true);
       
  1034     QCOMPARE(treeView.at(0)->hasFocus(), true);
       
  1035 }
       
  1036 
       
  1037 
       
  1038 void tst_QFiledialog::historyBack()
       
  1039 {
       
  1040     QNonNativeFileDialog fd;
       
  1041     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
  1042     QVERIFY(model);
       
  1043     QToolButton *backButton = qFindChild<QToolButton*>(&fd, "backButton");
       
  1044     QVERIFY(backButton);
       
  1045     QToolButton *forwardButton = qFindChild<QToolButton*>(&fd, "forwardButton");
       
  1046     QVERIFY(forwardButton);
       
  1047 
       
  1048     QSignalSpy spy(model, SIGNAL(rootPathChanged(const QString &)));
       
  1049 
       
  1050     QString home = fd.directory().absolutePath();
       
  1051     QString desktop = QDir::homePath();
       
  1052     QString temp = QDir::tempPath();
       
  1053 
       
  1054     QCOMPARE(backButton->isEnabled(), false);
       
  1055     QCOMPARE(forwardButton->isEnabled(), false);
       
  1056     fd.setDirectory(temp);
       
  1057     qApp->processEvents();
       
  1058     QCOMPARE(backButton->isEnabled(), true);
       
  1059     QCOMPARE(forwardButton->isEnabled(), false);
       
  1060     fd.setDirectory(desktop);
       
  1061     QCOMPARE(spy.count(), 2);
       
  1062 
       
  1063     backButton->click();
       
  1064     qApp->processEvents();
       
  1065     QCOMPARE(backButton->isEnabled(), true);
       
  1066     QCOMPARE(forwardButton->isEnabled(), true);
       
  1067     QCOMPARE(spy.count(), 3);
       
  1068     QString currentPath = qVariantValue<QString>(spy.last().first());
       
  1069     QCOMPARE(model->index(currentPath), model->index(temp));
       
  1070 
       
  1071     backButton->click();
       
  1072     currentPath = qVariantValue<QString>(spy.last().first());
       
  1073     QCOMPARE(currentPath, home);
       
  1074     QCOMPARE(backButton->isEnabled(), false);
       
  1075     QCOMPARE(forwardButton->isEnabled(), true);
       
  1076     QCOMPARE(spy.count(), 4);
       
  1077 
       
  1078     // nothing should change at this point
       
  1079     backButton->click();
       
  1080     QCOMPARE(spy.count(), 4);
       
  1081     QCOMPARE(backButton->isEnabled(), false);
       
  1082     QCOMPARE(forwardButton->isEnabled(), true);
       
  1083 }
       
  1084 
       
  1085 void tst_QFiledialog::historyForward()
       
  1086 {
       
  1087     QNonNativeFileDialog fd;
       
  1088     fd.setDirectory(QDir::currentPath());
       
  1089     QToolButton *backButton = qFindChild<QToolButton*>(&fd, "backButton");
       
  1090     QVERIFY(backButton);
       
  1091     QToolButton *forwardButton = qFindChild<QToolButton*>(&fd, "forwardButton");
       
  1092     QVERIFY(forwardButton);
       
  1093 
       
  1094     QFileSystemModel *model = qFindChild<QFileSystemModel*>(&fd, "qt_filesystem_model");
       
  1095     QVERIFY(model);
       
  1096     QSignalSpy spy(model, SIGNAL(rootPathChanged(const QString &)));
       
  1097 
       
  1098     QString home = fd.directory().absolutePath();
       
  1099     QString desktop = QDir::homePath();
       
  1100     QString temp = QDir::tempPath();
       
  1101 
       
  1102     fd.setDirectory(home);
       
  1103     fd.setDirectory(temp);
       
  1104     fd.setDirectory(desktop);
       
  1105 
       
  1106     backButton->click();
       
  1107     QCOMPARE(forwardButton->isEnabled(), true);
       
  1108     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(temp));
       
  1109 
       
  1110     forwardButton->click();
       
  1111     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(desktop));
       
  1112     QCOMPARE(backButton->isEnabled(), true);
       
  1113     QCOMPARE(forwardButton->isEnabled(), false);
       
  1114     QCOMPARE(spy.count(), 4);
       
  1115 
       
  1116     backButton->click();
       
  1117     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(temp));
       
  1118     QCOMPARE(backButton->isEnabled(), true);
       
  1119 
       
  1120     backButton->click();
       
  1121     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(home));
       
  1122     QCOMPARE(backButton->isEnabled(), false);
       
  1123     QCOMPARE(forwardButton->isEnabled(), true);
       
  1124     QCOMPARE(spy.count(), 6);
       
  1125 
       
  1126     forwardButton->click();
       
  1127     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(temp));
       
  1128     backButton->click();
       
  1129     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(home));
       
  1130     QCOMPARE(spy.count(), 8);
       
  1131 
       
  1132     forwardButton->click();
       
  1133     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(temp));
       
  1134     forwardButton->click();
       
  1135     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(desktop));
       
  1136 
       
  1137     backButton->click();
       
  1138     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(temp));
       
  1139     backButton->click();
       
  1140     QCOMPARE(model->index(qVariantValue<QString>(spy.last().first())), model->index(home));
       
  1141     fd.setDirectory(desktop);
       
  1142     QCOMPARE(forwardButton->isEnabled(), false);
       
  1143 }
       
  1144 
       
  1145 void tst_QFiledialog::disableSaveButton_data()
       
  1146 {
       
  1147     QTest::addColumn<QString>("path");
       
  1148     QTest::addColumn<bool>("isEnabled");
       
  1149 
       
  1150     QTest::newRow("valid path") << QDir::temp().absolutePath() + QDir::separator() + "qfiledialog.new_file" << true;
       
  1151     QTest::newRow("no path") << "" << false;
       
  1152 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) && !defined(Q_OS_OPENBSD)
       
  1153     QTest::newRow("too long path") << "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii" << false;
       
  1154 #endif
       
  1155     QTest::newRow("file") << "foo.html" << true;
       
  1156 }
       
  1157 
       
  1158 void tst_QFiledialog::disableSaveButton()
       
  1159 {
       
  1160     QFETCH(QString, path);
       
  1161     QFETCH(bool, isEnabled);
       
  1162 
       
  1163     QNonNativeFileDialog fd(0, "caption", path);
       
  1164     fd.setAcceptMode(QFileDialog::AcceptSave);
       
  1165     QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&fd, "buttonBox");
       
  1166     QPushButton *button = buttonBox->button(QDialogButtonBox::Save);
       
  1167     QVERIFY(button);
       
  1168     QCOMPARE(button->isEnabled(), isEnabled);
       
  1169 }
       
  1170 
       
  1171 void tst_QFiledialog::saveButtonText_data()
       
  1172 {
       
  1173     QTest::addColumn<QString>("path");
       
  1174     QTest::addColumn<QString>("label");
       
  1175     QTest::addColumn<QString>("caption");
       
  1176 
       
  1177     QTest::newRow("empty path") << "" << QString() << QFileDialog::tr("&Save");
       
  1178     QTest::newRow("file path") << "qfiledialog.new_file" << QString() << QFileDialog::tr("&Save");
       
  1179     QTest::newRow("dir") << QDir::temp().absolutePath() << QString() << QFileDialog::tr("&Open");
       
  1180     QTest::newRow("setTextLabel") << "qfiledialog.new_file" << "Mooo" << "Mooo";
       
  1181     QTest::newRow("dir & label") << QDir::temp().absolutePath() << "Poo" << QFileDialog::tr("&Open");
       
  1182 }
       
  1183 
       
  1184 void tst_QFiledialog::saveButtonText()
       
  1185 {
       
  1186     QFETCH(QString, path);
       
  1187     QFETCH(QString, label);
       
  1188     QFETCH(QString, caption);
       
  1189 
       
  1190     QNonNativeFileDialog fd(0, "auto test", QDir::temp().absolutePath());
       
  1191     fd.setAcceptMode(QFileDialog::AcceptSave);
       
  1192     if (!label.isNull())
       
  1193         fd.setLabelText(QFileDialog::Accept, label);
       
  1194     fd.setDirectory(QDir::temp());
       
  1195     fd.selectFile(path);
       
  1196     QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&fd, "buttonBox");
       
  1197     QVERIFY(buttonBox);
       
  1198     QPushButton *button = buttonBox->button(QDialogButtonBox::Save);
       
  1199     QVERIFY(button);
       
  1200     QCOMPARE(button->text(), caption);
       
  1201 }
       
  1202 
       
  1203 void tst_QFiledialog::clearLineEdit()
       
  1204 {
       
  1205     QNonNativeFileDialog fd(0, "caption", "foo");
       
  1206     fd.setViewMode(QFileDialog::List);
       
  1207     fd.setFileMode(QFileDialog::AnyFile);
       
  1208     fd.setOptions(QFileDialog::DontUseNativeDialog);
       
  1209     fd.show();
       
  1210 
       
  1211     //play it really safe by creating a directory
       
  1212     QDir::home().mkdir("_____aaaaaaaaaaaaaaaaaaaaaa");
       
  1213 
       
  1214     QLineEdit *lineEdit = qFindChild<QLineEdit*>(&fd, "fileNameEdit");
       
  1215     QVERIFY(lineEdit);
       
  1216     QVERIFY(lineEdit->text() == "foo");
       
  1217     fd.setDirectory(QDir::home());
       
  1218 
       
  1219     QListView* list = qFindChild<QListView*>(&fd, "listView");
       
  1220     QVERIFY(list);
       
  1221 
       
  1222     // saving a file the text shouldn't be cleared
       
  1223     fd.setDirectory(QDir::home());
       
  1224 
       
  1225     QTest::qWait(1000);
       
  1226 #ifdef QT_KEYPAD_NAVIGATION
       
  1227     list->setEditFocus(true);
       
  1228 #endif
       
  1229     QTest::keyClick(list, Qt::Key_Down);
       
  1230 #ifndef Q_WS_MAC
       
  1231     QTest::keyClick(list, Qt::Key_Return);
       
  1232 #else
       
  1233     QTest::keyClick(list, Qt::Key_O, Qt::ControlModifier);
       
  1234 #endif
       
  1235 
       
  1236     QTest::qWait(2000);
       
  1237     QVERIFY(fd.directory().absolutePath() != QDir::home().absolutePath());
       
  1238     QVERIFY(!lineEdit->text().isEmpty());
       
  1239 
       
  1240     // selecting a dir the text should be cleared so one can just hit ok
       
  1241     // and it selects that directory
       
  1242     fd.setFileMode(QNonNativeFileDialog::Directory);
       
  1243     fd.setDirectory(QDir::home());
       
  1244 
       
  1245     QTest::qWait(1000);
       
  1246     QTest::keyClick(list, Qt::Key_Down);
       
  1247 #ifndef Q_WS_MAC
       
  1248     QTest::keyClick(list, Qt::Key_Return);
       
  1249 #else
       
  1250     QTest::keyClick(list, Qt::Key_O, Qt::ControlModifier);
       
  1251 #endif
       
  1252 
       
  1253     QTest::qWait(2000);
       
  1254     QVERIFY(fd.directory().absolutePath() != QDir::home().absolutePath());
       
  1255     QVERIFY(lineEdit->text().isEmpty());
       
  1256 
       
  1257     //remove the dir
       
  1258     QDir::home().rmdir("_____aaaaaaaaaaaaaaaaaaaaaa");
       
  1259 }
       
  1260 
       
  1261 void tst_QFiledialog::enableChooseButton()
       
  1262 {
       
  1263     QNonNativeFileDialog fd;
       
  1264     fd.setFileMode(QFileDialog::Directory);
       
  1265     fd.show();
       
  1266     QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&fd, "buttonBox");
       
  1267     QPushButton *button = buttonBox->button(QDialogButtonBox::Open);
       
  1268     QVERIFY(button);
       
  1269     QCOMPARE(button->isEnabled(), true);
       
  1270 }
       
  1271 
       
  1272 QT_BEGIN_NAMESPACE
       
  1273 typedef QString (*_qt_filedialog_existing_directory_hook)(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options);
       
  1274 extern Q_GUI_EXPORT _qt_filedialog_existing_directory_hook qt_filedialog_existing_directory_hook;
       
  1275 QT_END_NAMESPACE
       
  1276 QString existing(QWidget *, const QString &, const QString &, QFileDialog::Options) {
       
  1277     return "dir";
       
  1278 }
       
  1279 
       
  1280 QT_BEGIN_NAMESPACE
       
  1281 typedef QString (*_qt_filedialog_open_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
       
  1282 extern Q_GUI_EXPORT _qt_filedialog_open_filename_hook qt_filedialog_open_filename_hook;
       
  1283 QT_END_NAMESPACE
       
  1284 QString openName(QWidget *, const QString &, const QString &, const QString &, QString *, QFileDialog::Options) {
       
  1285     return "openName";
       
  1286 }
       
  1287 
       
  1288 QT_BEGIN_NAMESPACE
       
  1289 typedef QStringList (*_qt_filedialog_open_filenames_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
       
  1290 extern Q_GUI_EXPORT _qt_filedialog_open_filenames_hook qt_filedialog_open_filenames_hook;
       
  1291 QT_END_NAMESPACE
       
  1292 QStringList openNames(QWidget *, const QString &, const QString &, const QString &, QString *, QFileDialog::Options) {
       
  1293     return QStringList("openNames");
       
  1294 }
       
  1295 
       
  1296 QT_BEGIN_NAMESPACE
       
  1297 typedef QString (*_qt_filedialog_save_filename_hook)(QWidget * parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options);
       
  1298 extern Q_GUI_EXPORT _qt_filedialog_save_filename_hook qt_filedialog_save_filename_hook;
       
  1299 QT_END_NAMESPACE
       
  1300 QString saveName(QWidget *, const QString &, const QString &, const QString &, QString *, QFileDialog::Options) {
       
  1301     return "saveName";
       
  1302 }
       
  1303 
       
  1304 
       
  1305 void tst_QFiledialog::hooks()
       
  1306 {
       
  1307     qt_filedialog_existing_directory_hook = &existing;
       
  1308     qt_filedialog_save_filename_hook = &saveName;
       
  1309     qt_filedialog_open_filename_hook = &openName;
       
  1310     qt_filedialog_open_filenames_hook = &openNames;
       
  1311 
       
  1312     QCOMPARE(QFileDialog::getExistingDirectory(), QString("dir"));
       
  1313     QCOMPARE(QFileDialog::getOpenFileName(), QString("openName"));
       
  1314     QCOMPARE(QFileDialog::getOpenFileNames(), QStringList("openNames"));
       
  1315     QCOMPARE(QFileDialog::getSaveFileName(), QString("saveName"));
       
  1316 }
       
  1317 
       
  1318 QTEST_MAIN(tst_QFiledialog)
       
  1319 #include "tst_qfiledialog.moc"