diff options
Diffstat (limited to 'tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp')
-rw-r--r-- | tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp | 139 |
1 files changed, 111 insertions, 28 deletions
diff --git a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp index 897608bca8..b2e77bc935 100644 --- a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp +++ b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp @@ -1,11 +1,10 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <QtGui> #include <QtWidgets> #include <QtDebug> -#include <QPair> #include <QList> #include <QPointer> #include <QSignalSpy> @@ -16,6 +15,8 @@ #include <memory> +#include <QtWidgets/private/qapplication_p.h> + Q_DECLARE_METATYPE(QCompleter::CompletionMode) using namespace QTestPrivate; @@ -57,7 +58,7 @@ QString CsvCompleter::pathFromIndex(const QModelIndex &sourceIndex) const idx = parent.sibling(parent.row(), sourceIndex.column()); } while (idx.isValid()); - return list.count() == 1 ? list.constFirst() : list.join(QLatin1Char(',')); + return list.size() == 1 ? list.constFirst() : list.join(QLatin1Char(',')); } class tst_QCompleter : public QObject @@ -108,6 +109,8 @@ private slots: void dynamicSortOrder(); void disabledItems(); + void hideWidget(); + // task-specific tests below me void task178797_activatedOnReturn(); void task189564_omitNonSelectableItems(); @@ -121,6 +124,7 @@ private slots: void QTBUG_52028_tabAutoCompletes(); void QTBUG_51889_activatedSentTwice(); void showPopupInGraphicsView(); + void inheritedEventFilter(); private: void filter(bool assync = false); @@ -151,6 +155,16 @@ tst_QCompleter::~tst_QCompleter() delete completer; } +#ifdef Q_OS_ANDROID +static QString androidHomePath() +{ + const auto homePaths = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); + QDir dir = homePaths.isEmpty() ? QDir() : homePaths.first(); + dir.cdUp(); + return dir.path(); +} +#endif + void tst_QCompleter::setSourceModel(ModelType type) { QTreeWidgetItem *parent, *child; @@ -203,7 +217,13 @@ void tst_QCompleter::setSourceModel(ModelType type) completer->setCsvCompletion(false); { auto m = new QFileSystemModel(completer); +#ifdef Q_OS_ANDROID + // Android 11 and above doesn't allow accessing root filesystem as before, + // so let's opt int for the app's home. + m->setRootPath(androidHomePath()); +#else m->setRootPath("/"); +#endif completer->setModel(m); } completer->setCompletionColumn(0); @@ -590,7 +610,9 @@ void tst_QCompleter::fileSystemModel_data() // QTest::newRow("(/d)") << "/d" << "" << "Developer" << "/Developer"; #elif defined(Q_OS_ANDROID) QTest::newRow("()") << "" << "" << "/" << "/"; - QTest::newRow("(/et)") << "/et" << "" << "etc" << "/etc"; + const QString androidDir = androidHomePath(); + const QString tag = QStringLiteral("%1/fil").arg(androidDir); + QTest::newRow(tag.toUtf8().data()) << tag << "" << "files" << androidDir + "/files"; #else QTest::newRow("()") << "" << "" << "/" << "/"; #if !defined(Q_OS_AIX) && !defined(Q_OS_HPUX) && !defined(Q_OS_QNX) @@ -1040,7 +1062,6 @@ void tst_QCompleter::multipleWidgets() window.setWindowTitle(QLatin1String(QTest::currentTestFunction())); window.move(200, 200); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); QFocusEvent focusIn(QEvent::FocusIn); @@ -1052,7 +1073,6 @@ void tst_QCompleter::multipleWidgets() comboBox->setFocus(); comboBox->show(); window.activateWindow(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); QCOMPARE(QApplication::focusWidget(), comboBox); comboBox->lineEdit()->setText("it"); @@ -1087,7 +1107,6 @@ void tst_QCompleter::focusIn() window.move(200, 200); window.show(); window.activateWindow(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); auto comboBox = new QComboBox(&window); @@ -1158,13 +1177,39 @@ void tst_QCompleter::disabledItems() QAbstractItemView *view = lineEdit.completer()->popup(); QVERIFY(view->isVisible()); QTest::mouseClick(view->viewport(), Qt::LeftButton, {}, view->visualRect(view->model()->index(0, 0)).center()); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QVERIFY(view->isVisible()); QTest::mouseClick(view->viewport(), Qt::LeftButton, {}, view->visualRect(view->model()->index(1, 0)).center()); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QVERIFY(!view->isVisible()); } +void tst_QCompleter::hideWidget() +{ + // hiding the widget should hide/close the popup + QWidget w; + w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + w.setLayout(new QVBoxLayout); + + QLineEdit edit; + edit.setCompleter(new QCompleter({ "foo", "bar" })); + + w.layout()->addWidget(&edit); + + const auto pos = w.screen()->availableGeometry().topLeft() + QPoint(200, 200); + w.move(pos); + w.show(); + QApplicationPrivate::setActiveWindow(&w); + QVERIFY(QTest::qWaitForWindowActive(&w)); + + // activate the completer + QTest::keyClick(&edit, Qt::Key_F); + QVERIFY(edit.completer()->popup()); + QTRY_VERIFY(edit.completer()->popup()->isVisible()); + edit.hide(); + QVERIFY(!edit.completer()->popup()->isVisible()); +} + void tst_QCompleter::task178797_activatedOnReturn() { if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) @@ -1175,10 +1220,9 @@ void tst_QCompleter::task178797_activatedOnReturn() auto completer = new QCompleter({"foobar1", "foobar2"}, &ledit); ledit.setCompleter(completer); QSignalSpy spy(completer, QOverload<const QString &>::of(&QCompleter::activated)); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); ledit.move(200, 200); ledit.show(); - QApplication::setActiveWindow(&ledit); QVERIFY(QTest::qWaitForWindowActive(&ledit)); QTest::keyClick(&ledit, Qt::Key_F); QCoreApplication::processEvents(); @@ -1187,7 +1231,7 @@ void tst_QCompleter::task178797_activatedOnReturn() QCoreApplication::processEvents(); QTest::keyClick(QApplication::activePopupWidget(), Qt::Key_Return); QCoreApplication::processEvents(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } class task189564_StringListModel : public QStringListModel @@ -1262,14 +1306,13 @@ void tst_QCompleter::task246056_setCompletionPrefix() comboBox.addItem("a2"); comboBox.move(200, 200); comboBox.show(); - QApplication::setActiveWindow(&comboBox); QVERIFY(QTest::qWaitForWindowActive(&comboBox)); QSignalSpy spy(comboBox.completer(), QOverload<const QModelIndex &>::of(&QCompleter::activated)); QTest::keyPress(&comboBox, 'a'); QTest::keyPress(comboBox.completer()->popup(), Qt::Key_Down); QTest::keyPress(comboBox.completer()->popup(), Qt::Key_Down); QTest::keyPress(comboBox.completer()->popup(), Qt::Key_Enter); // don't crash! - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); const auto index = spy.at(0).constFirst().toModelIndex(); QVERIFY(!index.isValid()); } @@ -1328,7 +1371,6 @@ void tst_QCompleter::task250064_lostFocus() task250064_Widget widget; widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); widget.show(); - QApplication::setActiveWindow(&widget); QVERIFY(QTest::qWaitForWindowActive(&widget)); QTest::keyPress(widget.textEdit(), 'a'); Qt::FocusPolicy origPolicy = widget.textEdit()->focusPolicy(); @@ -1373,7 +1415,6 @@ void tst_QCompleter::task253125_lineEditCompletion() edit.move(200, 200); edit.show(); edit.setFocus(); - QApplication::setActiveWindow(&edit); QVERIFY(QTest::qWaitForWindowActive(&edit)); QTest::keyClick(&edit, 'i'); @@ -1536,7 +1577,6 @@ void tst_QCompleter::task247560_keyboardNavigation() edit.move(200, 200); edit.show(); edit.setFocus(); - QApplication::setActiveWindow(&edit); QVERIFY(QTest::qWaitForWindowActive(&edit)); QTest::keyClick(&edit, 'r'); @@ -1636,6 +1676,7 @@ void tst_QCompleter::QTBUG_14292_filesystem() // to pop up the completion list due to file changed signals. FileSystem fs; QFileSystemModel model; + QSignalSpy filesAddedSpy(&model, &QAbstractItemModel::rowsInserted); model.setRootPath(fs.path()); QVERIFY(fs.createDirectory(QLatin1String(testDir1))); @@ -1649,7 +1690,6 @@ void tst_QCompleter::QTBUG_14292_filesystem() edit.move(200, 200); edit.show(); - QApplication::setActiveWindow(&edit); QVERIFY(QTest::qWaitForWindowActive(&edit)); QCOMPARE(QApplication::activeWindow(), &edit); edit.setFocus(); @@ -1665,6 +1705,7 @@ void tst_QCompleter::QTBUG_14292_filesystem() QTRY_VERIFY(comp.popup()->isVisible()); QCOMPARE(comp.popup()->model()->rowCount(), 2); QApplication::processEvents(); + QCOMPARE(qApp->focusObject(), &edit); // for QTBUG_108522 QTest::keyClick(&edit, 'h'); QCOMPARE(comp.popup()->model()->rowCount(), 2); QTest::keyClick(&edit, 'e'); @@ -1672,13 +1713,16 @@ void tst_QCompleter::QTBUG_14292_filesystem() QTest::keyClick(&edit, 'r'); QTRY_VERIFY(!comp.popup()->isVisible()); QVERIFY(fs.createDirectory(QStringLiteral("hero"))); + if (!filesAddedSpy.wait()) + QSKIP("File system model didn't notify about new directory, skipping tests"); QTRY_VERIFY(comp.popup()->isVisible()); QCOMPARE(comp.popup()->model()->rowCount(), 1); QTest::keyClick(comp.popup(), Qt::Key_Escape); QTRY_VERIFY(!comp.popup()->isVisible()); QVERIFY(fs.createDirectory(QStringLiteral("nothingThere"))); //there is no reason creating a file should open a popup, it did in Qt 4.7.0 - QTest::qWait(60); + if (!filesAddedSpy.wait()) + QSKIP("File system model didn't notify about new file, skipping tests"); QVERIFY(!comp.popup()->isVisible()); QTest::keyClick(&edit, Qt::Key_Backspace); @@ -1690,13 +1734,13 @@ void tst_QCompleter::QTBUG_14292_filesystem() QWidget w; w.move(400, 200); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); QVERIFY(!edit.hasFocus() && !comp.popup()->hasFocus()); QVERIFY(fs.createDirectory(QStringLiteral("hemo"))); //there is no reason creating a file should open a popup, it did in Qt 4.7.0 - QTest::qWait(60); + if (!filesAddedSpy.wait()) + QSKIP("File system model didn't notify about new file, skipping tests"); QVERIFY(!comp.popup()->isVisible()); } @@ -1726,7 +1770,7 @@ void tst_QCompleter::QTBUG_52028_tabAutoCompletes() const auto pos = w.screen()->availableGeometry().topLeft() + QPoint(200,200); w.move(pos); w.show(); - QApplication::setActiveWindow(&w); + QApplicationPrivate::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); QSignalSpy activatedSpy(&cbox, &QComboBox::activated); @@ -1738,10 +1782,10 @@ void tst_QCompleter::QTBUG_52028_tabAutoCompletes() QTRY_VERIFY(cbox.completer()->popup()->isVisible()); QTest::keyClick(cbox.completer()->popup(), Qt::Key_Tab); QCOMPARE(cbox.completer()->currentCompletion(), QLatin1String("hux")); - QCOMPARE(activatedSpy.count(), 0); + QCOMPARE(activatedSpy.size(), 0); QEXPECT_FAIL("", "QTBUG-52028 will not be fixed today.", Abort); QCOMPARE(cbox.currentText(), QLatin1String("hux")); - QCOMPARE(activatedSpy.count(), 0); + QCOMPARE(activatedSpy.size(), 0); QVERIFY(!le->hasFocus()); } @@ -1770,7 +1814,6 @@ void tst_QCompleter::QTBUG_51889_activatedSentTwice() const auto pos = w.screen()->availableGeometry().topLeft() + QPoint(200,200); w.move(pos); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); QSignalSpy activatedSpy(&cbox, &QComboBox::activated); @@ -1782,7 +1825,7 @@ void tst_QCompleter::QTBUG_51889_activatedSentTwice() QTRY_VERIFY(cbox.completer()->popup()->isVisible()); QTest::keyClick(cbox.completer()->popup(), Qt::Key_Down); QTest::keyClick(cbox.completer()->popup(), Qt::Key_Return); - QTRY_COMPARE(activatedSpy.count(), 1); + QTRY_COMPARE(activatedSpy.size(), 1); // Navigate + enter activates only once (non-first item) cbox.lineEdit()->clear(); @@ -1792,7 +1835,7 @@ void tst_QCompleter::QTBUG_51889_activatedSentTwice() QTRY_VERIFY(cbox.completer()->popup()->isVisible()); QTest::keyClick(cbox.completer()->popup(), Qt::Key_Down); QTest::keyClick(cbox.completer()->popup(), Qt::Key_Return); - QTRY_COMPARE(activatedSpy.count(), 1); + QTRY_COMPARE(activatedSpy.size(), 1); // Full text + enter activates only once cbox.lineEdit()->clear(); @@ -1801,11 +1844,14 @@ void tst_QCompleter::QTBUG_51889_activatedSentTwice() QVERIFY(cbox.completer()->popup()); QTRY_VERIFY(cbox.completer()->popup()->isVisible()); QTest::keyClick(&cbox, Qt::Key_Return); - QTRY_COMPARE(activatedSpy.count(), 1); + QTRY_COMPARE(activatedSpy.size(), 1); } void tst_QCompleter::showPopupInGraphicsView() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: Skip this test, see also QTBUG-107186"); + QGraphicsView view; QGraphicsScene scene; view.setScene(&scene); @@ -1835,5 +1881,42 @@ void tst_QCompleter::showPopupInGraphicsView() QVERIFY(lineEdit.completer()->popup()->geometry().bottom() < lineEdit.mapToGlobal(QPoint(0, 0)).y()); } +void tst_QCompleter::inheritedEventFilter() +{ + class Completer : public QCompleter + { + public: + explicit Completer(QWidget *parent) : QCompleter(parent) + { + Q_ASSERT(parent); + setPopup(new QListView()); + popup()->installEventFilter(this); + } + + bool m_popupChildAdded = false; + + protected: + bool eventFilter(QObject *watched, QEvent *event) override + { + if (watched == popup() && event->type() == QEvent::ChildAdded) + m_popupChildAdded = true; + + return QCompleter::eventFilter(watched, event); + } + }; + + QComboBox comboBox; + comboBox.setEditable(true); + Completer *completer = new Completer(&comboBox); + comboBox.setCompleter(completer); + + // comboBox.show() must not crash with an infinite loop in the event filter + comboBox.show(); + QVERIFY(QTest::qWaitForWindowExposed(&comboBox)); + + // Since event orders are platform dependent, only the the ChildAdded event is checked. + QVERIFY(QTest::qWaitFor([completer](){return completer->m_popupChildAdded; })); +} + QTEST_MAIN(tst_QCompleter) #include "tst_qcompleter.moc" |