From d8cd99992f1f09aa1ce51fb7611983b9d47c7d8b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 23 Apr 2014 15:40:26 +0200 Subject: Stabilize tst_qfiledialog. selectFile(): Ensure dialog is destroyed before the temporary file to prevent leak. selectFiles(): Introduce scopes to prevent co-existence of 2 instances of QFileDialog, which causes warnings from file watchers and occasional crashes in accessibility. Discovered while investigating: Task-number: QTBUG-38414 Change-Id: I94d2bb3e05538416286641cb08a88d8b3837a8a6 Reviewed-by: Frederik Gladhorn --- .../dialogs/qfiledialog/tst_qfiledialog.cpp | 59 ++++++++++++---------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'tests/auto/widgets') diff --git a/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp b/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp index d360a646f1..de8528216d 100644 --- a/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp +++ b/tests/auto/widgets/dialogs/qfiledialog/tst_qfiledialog.cpp @@ -857,36 +857,38 @@ void tst_QFiledialog::selectFile() { QFETCH(QString, file); QFETCH(int, count); - QNonNativeFileDialog fd; - QFileSystemModel *model = fd.findChild("qt_filesystem_model"); + QScopedPointer fd(new QNonNativeFileDialog); + QFileSystemModel *model = fd->findChild("qt_filesystem_model"); QVERIFY(model); - fd.setDirectory(QDir::currentPath()); + fd->setDirectory(QDir::currentPath()); // default value - QCOMPARE(fd.selectedFiles().count(), 1); + QCOMPARE(fd->selectedFiles().count(), 1); - QTemporaryFile tempFile(QDir::tempPath() + "/aXXXXXX"); - bool inTemp = (file == "temp"); - if (inTemp) { - tempFile.open(); - file = tempFile.fileName(); + QScopedPointer tempFile; + if (file == QLatin1String("temp")) { + tempFile.reset(new QTemporaryFile(QDir::tempPath() + QStringLiteral("/aXXXXXX"))); + QVERIFY(tempFile->open()); + file = tempFile->fileName(); } - fd.selectFile(file); - QCOMPARE(fd.selectedFiles().count(), count); - if (inTemp) { - QCOMPARE(model->index(fd.directory().path()), model->index(QDir::tempPath())); + fd->selectFile(file); + QCOMPARE(fd->selectedFiles().count(), count); + if (tempFile.isNull()) { + QCOMPARE(model->index(fd->directory().path()), model->index(QDir::currentPath())); } else { - QCOMPARE(model->index(fd.directory().path()), model->index(QDir::currentPath())); + QCOMPARE(model->index(fd->directory().path()), model->index(QDir::tempPath())); } + fd.reset(); // Ensure the file dialog let's go of the temporary file for "temp". } void tst_QFiledialog::selectFiles() { - QNonNativeFileDialog fd; - fd.setViewMode(QFileDialog::List); QTemporaryDir tempDir; QVERIFY(tempDir.isValid()); const QString tempPath = tempDir.path(); + { + QNonNativeFileDialog fd; + fd.setViewMode(QFileDialog::List); fd.setDirectory(tempPath); QSignalSpy spyCurrentChanged(&fd, SIGNAL(currentChanged(QString))); QSignalSpy spyDirectoryEntered(&fd, SIGNAL(directoryEntered(QString))); @@ -931,17 +933,20 @@ void tst_QFiledialog::selectFiles() QCOMPARE(spyFilesSelected.count(), 0); QCOMPARE(spyFilterSelected.count(), 0); - //If the selection is invalid then we fill the line edit but without the / - QNonNativeFileDialog * dialog = new QNonNativeFileDialog( 0, "Save" ); - dialog->setFileMode( QFileDialog::AnyFile ); - dialog->setAcceptMode( QFileDialog::AcceptSave ); - dialog->selectFile(tempPath + QStringLiteral("/blah")); - dialog->show(); - QVERIFY(QTest::qWaitForWindowExposed(dialog)); - QLineEdit *lineEdit = dialog->findChild("fileNameEdit"); - QVERIFY(lineEdit); - QCOMPARE(lineEdit->text(),QLatin1String("blah")); - delete dialog; + } + + { + //If the selection is invalid then we fill the line edit but without the / + QNonNativeFileDialog dialog( 0, "Save" ); + dialog.setFileMode( QFileDialog::AnyFile ); + dialog.setAcceptMode( QFileDialog::AcceptSave ); + dialog.selectFile(tempPath + QStringLiteral("/blah")); + dialog.show(); + QVERIFY(QTest::qWaitForWindowExposed(&dialog)); + QLineEdit *lineEdit = dialog.findChild("fileNameEdit"); + QVERIFY(lineEdit); + QCOMPARE(lineEdit->text(),QLatin1String("blah")); + } } void tst_QFiledialog::viewMode() -- cgit v1.2.3