From: BM
Date:   Tue Oct 9 13:33:48 2007 +0100
Precedes: v4.6.0-beta1

p4i integration
Integrate 279490 from 4.3 to main:
AutoTest: Add new test to cover all of the file dialog signals
Task:     179902
RevBy:    JAS

[git-p4: depot-paths = "//depot/qt/main/": change = 279491]


diff --git a/tests/auto/qfiledialog/tst_qfiledialog.cpp b/tests/auto/qfiledialog/tst_qfiledialog.cpp
--- a/tests/auto/qfiledialog/tst_qfiledialog.cpp
+++ b/tests/auto/qfiledialog/tst_qfiledialog.cpp
@@ -24,6 +24,7 @@
 #include <qdialogbuttonbox.h>
 #include <qsortfilterproxymodel.h>
 #include <qlineedit.h>
+#include "../../../src/gui/dialogs/qsidebar_p.h"
 
 // Will try to wait for the condition while allowing event processing
 // for a maximum of 5 seconds.
@@ -63,6 +64,12 @@ public slots:
     void cleanup();
 
 private slots:
+    void currentChangedSignal();
+    void directoryEnteredSignal();
+    void filesSelectedSignal_data();
+    void filesSelectedSignal();
+    void filterSelectedSignal();
+
     void args();
     void directory();
     void completor();
@@ -137,6 +144,142 @@ public:
     QSize sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const { return QSize(); }
 };
 
+// emited any time the selection model emits current changed
+void tst_QFiledialog::currentChangedSignal()
+{
+    QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+
+    QListView* listView = fd.findChild<QListView*>("listView");
+    QVERIFY(listView);
+    fd.setDirectory(QDir::root());
+    QModelIndex root = listView->rootIndex();
+    WAIT_FOR_CONDITION(listView->model()->rowCount(root) > 0, true);
+
+    QModelIndex folder;
+    for (int i = 0; i < listView->model()->rowCount(root); ++i) {
+        folder = listView->model()->index(i, 0, root);
+        if (listView->model()->hasChildren(folder))
+            break;
+    }
+    QVERIFY(listView->model()->hasChildren(folder));
+    listView->setCurrentIndex(folder);
+
+    QCOMPARE(spyCurrentChanged.count(), 1);
+}
+
+// only emited from the views, sidebar, or lookin combo
+void tst_QFiledialog::directoryEnteredSignal()
+{
+    QFileDialog fd;
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+
+    // view
+    QListView* listView = fd.findChild<QListView*>("listView");
+    QVERIFY(listView);
+    fd.setDirectory(QDir::root());
+    QModelIndex root = listView->rootIndex();
+    WAIT_FOR_CONDITION(listView->model()->rowCount(root) > 0, true);
+
+    QModelIndex folder;
+    for (int i = 0; i < listView->model()->rowCount(root); ++i) {
+        folder = listView->model()->index(i, 0, root);
+        if (listView->model()->hasChildren(folder))
+            break;
+    }
+    QVERIFY(listView->model()->hasChildren(folder));
+    listView->setCurrentIndex(folder);
+    QTest::keyPress(listView->viewport(), Qt::Key_Return);
+    QCOMPARE(spyDirectoryEntered.count(), 1);
+    spyDirectoryEntered.clear();
+
+    // sidebar
+    QSidebar *sidebar = fd.findChild<QSidebar*>("sidebar");
+    sidebar->setCurrentIndex(sidebar->model()->index(1, 0));
+    QTest::keyPress(sidebar->viewport(), Qt::Key_Return);
+    QCOMPARE(spyDirectoryEntered.count(), 1);
+    spyDirectoryEntered.clear();
+
+
+    // lookInCombo
+    QComboBox *comboBox = fd.findChild<QComboBox*>("lookInCombo");
+    comboBox->view()->setCurrentIndex(comboBox->view()->model()->index(1, 0));
+    QTest::keyPress(comboBox->view()->viewport(), Qt::Key_Return);
+    QCOMPARE(spyDirectoryEntered.count(), 1);
+}
+
+Q_DECLARE_METATYPE(QFileDialog::FileMode)
+void tst_QFiledialog::filesSelectedSignal_data()
+{
+    QTest::addColumn<QFileDialog::FileMode>("fileMode");
+    QTest::newRow("any") << QFileDialog::AnyFile;
+    QTest::newRow("existing") << QFileDialog::ExistingFile;
+    QTest::newRow("directory") << QFileDialog::Directory;
+    QTest::newRow("directoryOnly") << QFileDialog::DirectoryOnly;
+    QTest::newRow("existingFiles") << QFileDialog::ExistingFiles;
+}
+
+// emited when the dialog closes with the selected files
+void tst_QFiledialog::filesSelectedSignal()
+{
+    QFileDialog fd;
+    QFETCH(QFileDialog::FileMode, fileMode);
+    fd.setFileMode(fileMode);
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+
+    fd.show();
+    QListView *listView = fd.findChild<QListView*>("listView");
+    QVERIFY(listView);
+    QModelIndex root = listView->rootIndex();
+    WAIT_FOR_CONDITION(listView->model()->rowCount(root) > 0, true);
+    QModelIndex file;
+    for (int i = 0; i < listView->model()->rowCount(root); ++i) {
+        file = listView->model()->index(i, 0, root);
+        if (fileMode == QFileDialog::Directory || fileMode == QFileDialog::DirectoryOnly)
+            if (listView->model()->hasChildren(file))
+                break;
+        else if (!listView->model()->hasChildren(file))
+            break;
+    }
+    QVERIFY(file.isValid());
+    listView->selectionModel()->select(file, QItemSelectionModel::Select | QItemSelectionModel::Rows);
+    listView->setCurrentIndex(file);
+
+    QDialogButtonBox *buttonBox = fd.findChild<QDialogButtonBox*>("buttonBox");
+    QPushButton *button = buttonBox->button(QDialogButtonBox::Open);
+    QVERIFY(button);
+    QVERIFY(button->isEnabled());
+    button->animateClick();
+    WAIT_FOR_CONDITION(fd.isVisible(), false);
+    QCOMPARE(spyFilesSelected.count(), 1);
+}
+
+// only emited when the combo box is activated
+void tst_QFiledialog::filterSelectedSignal()
+{
+    QFileDialog fd;
+    fd.setAcceptMode(QFileDialog::AcceptSave);
+    fd.show();
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
+
+    QStringList filterChoices;
+    filterChoices << "Image files (*.png *.xpm *.jpg)"
+                  << "Text files (*.txt)"
+                  << "Any files (*.*)";
+    fd.setFilters(filterChoices);
+    QCOMPARE(fd.filters(), filterChoices);
+
+    QComboBox *filters = fd.findChild<QComboBox*>("fileTypeCombo");
+    QVERIFY(filters);
+    QVERIFY(filters->view());
+    QCOMPARE(filters->isVisible(), true);
+
+    filters->view()->setCurrentIndex(filters->view()->model()->index(1, 0));
+    QTest::keyPress(filters->view()->viewport(), Qt::Key_Return);
+
+    QCOMPARE(spyFilterSelected.count(), 1);
+}
+
 void tst_QFiledialog::args()
 {
     QWidget *parent = 0;
@@ -153,10 +296,19 @@ void tst_QFiledialog::args()
 void tst_QFiledialog::directory()
 {
     QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
+
     QCOMPARE(QDir::current().absolutePath(), fd.directory().absolutePath());
     QDir temp = QDir::temp();
     fd.setDirectory(temp);
     QCOMPARE(temp.absolutePath(), fd.directory().absolutePath());
+    QCOMPARE(spyCurrentChanged.count(), 0);
+    QCOMPARE(spyDirectoryEntered.count(), 0);
+    QCOMPARE(spyFilesSelected.count(), 0);
+    QCOMPARE(spyFilterSelected.count(), 0);
 
     // Check my way
     QList<QListView*> list = fd.findChildren<QListView*>("listView");
@@ -168,14 +320,24 @@ void tst_QFiledialog::completor()
 {
     // ### flesh this out more
     QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
+
     fd.show();
     QLineEdit *lineEdit = fd.findChild<QLineEdit*>("fileNameEdit");
     QVERIFY(lineEdit);
+    QCOMPARE(spyFilesSelected.count(), 0);
     int depth = QDir::currentPath().split('/').count();
     for (int i = 0; i <= depth * 3 + 1; ++i) {
         lineEdit->insert("../");
         qApp->processEvents();
     }
+    QCOMPARE(spyCurrentChanged.count(), 0);
+    QCOMPARE(spyDirectoryEntered.count(), 0);
+    QCOMPARE(spyFilesSelected.count(), 0);
+    QCOMPARE(spyFilterSelected.count(), 0);
 }
 
 void tst_QFiledialog::acceptMode()
@@ -247,6 +409,10 @@ void tst_QFiledialog::caption()
 void tst_QFiledialog::filters()
 {
     QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
     QCOMPARE(fd.filters(), QStringList("All Files (*)"));
 
     // effects
@@ -266,11 +432,21 @@ void tst_QFiledialog::filters()
     QCOMPARE(fd.filters(), filters);
     fd.setFilter("Image files (*.png *.xpm *.jpg);;Text files (*.txt);;Any files (*.*)");
     QCOMPARE(fd.filters(), filters);
+    QCOMPARE(spyCurrentChanged.count(), 0);
+    QCOMPARE(spyDirectoryEntered.count(), 0);
+    QCOMPARE(spyFilesSelected.count(), 0);
+    QCOMPARE(spyFilterSelected.count(), 0);
+
+    // setting shouldn't emit any signals
+    for (int i = views.at(0)->currentIndex(); i < views.at(0)->count(); ++i)
+        views.at(0)->setCurrentIndex(i);
+    QCOMPARE(spyFilterSelected.count(), 0);
 }
 
 void tst_QFiledialog::selectFilter()
 {
     QFileDialog fd;
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
     QCOMPARE(fd.selectedFilter(), QString("All Files (*)"));
     QStringList filters;
     filters << "Image files (*.png *.xpm *.jpg)"
@@ -287,11 +463,16 @@ void tst_QFiledialog::selectFilter()
     QCOMPARE(fd.selectedFilter(), filters.at(2));
     fd.selectFilter("");
     QCOMPARE(fd.selectedFilter(), filters.at(2));
+    QCOMPARE(spyFilterSelected.count(), 0);
 }
 
 void tst_QFiledialog::history()
 {
     QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
     QCOMPARE(fd.history(), QStringList(QDir::toNativeSeparators(QDir::current().absolutePath())));
     QStringList history;
     history << QDir::toNativeSeparators(QDir::current().absolutePath())
@@ -316,6 +497,11 @@ void tst_QFiledialog::history()
     fd.setHistory(badHistory);
     badHistory << QDir::toNativeSeparators(QDir::current().absolutePath());
     QCOMPARE(fd.history(), badHistory);
+
+    QCOMPARE(spyCurrentChanged.count(), 0);
+    QCOMPARE(spyDirectoryEntered.count(), 0);
+    QCOMPARE(spyFilesSelected.count(), 0);
+    QCOMPARE(spyFilterSelected.count(), 0);
 }
 
 void tst_QFiledialog::iconProvider()
@@ -421,6 +607,10 @@ void tst_QFiledialog::selectFile()
 void tst_QFiledialog::selectFiles()
 {
     QFileDialog fd;
+    QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(const QString &)));
+    QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(const QString &)));
+    QSignalSpy spyFilesSelected(&fd, SIGNAL(filesSelected(const QStringList &)));
+    QSignalSpy spyFilterSelected(&fd, SIGNAL(filterSelected(const QString &)));
     fd.show();
     fd.setFileMode(QFileDialog::ExistingFiles);
 
@@ -438,8 +628,10 @@ void tst_QFiledialog::selectFiles()
         TRY_VERIFY(!listView->selectionModel()->selectedRows().isEmpty());
         toSelect.append(listView->selectionModel()->selectedRows().last());
     }
+    QCOMPARE(spyFilesSelected.count(), 0);
 
     listView->selectionModel()->clear();
+    QCOMPARE(spyFilesSelected.count(), 0);
 
     // select the indexes
     for (int i = 0; i < toSelect.count(); ++i) {
@@ -447,6 +639,10 @@ void tst_QFiledialog::selectFiles()
                 QItemSelectionModel::Select | QItemSelectionModel::Rows);
     }
     QCOMPARE(fd.selectedFiles().count(), toSelect.count());
+    QCOMPARE(spyCurrentChanged.count(), 0);
+    QCOMPARE(spyDirectoryEntered.count(), 0);
+    QCOMPARE(spyFilesSelected.count(), 0);
+    QCOMPARE(spyFilterSelected.count(), 0);
 }
 
 void tst_QFiledialog::viewMode()
