diff options
Diffstat (limited to 'tests/auto/widgets/util/qcompleter')
-rw-r--r-- | tests/auto/widgets/util/qcompleter/BLACKLIST | 4 | ||||
-rw-r--r-- | tests/auto/widgets/util/qcompleter/CMakeLists.txt | 15 | ||||
-rw-r--r-- | tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp | 218 |
3 files changed, 173 insertions, 64 deletions
diff --git a/tests/auto/widgets/util/qcompleter/BLACKLIST b/tests/auto/widgets/util/qcompleter/BLACKLIST index 367270fdf2..5487a1fe44 100644 --- a/tests/auto/widgets/util/qcompleter/BLACKLIST +++ b/tests/auto/widgets/util/qcompleter/BLACKLIST @@ -1,6 +1,2 @@ [QTBUG_14292_filesystem] -ubuntu-16.04 opensuse-leap -rhel-7.4 -rhel-6.6 -opensuse-42.3 diff --git a/tests/auto/widgets/util/qcompleter/CMakeLists.txt b/tests/auto/widgets/util/qcompleter/CMakeLists.txt index 5d4ff347a2..a96945771f 100644 --- a/tests/auto/widgets/util/qcompleter/CMakeLists.txt +++ b/tests/auto/widgets/util/qcompleter/CMakeLists.txt @@ -1,17 +1,22 @@ -# Generated from qcompleter.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qcompleter Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qcompleter LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qcompleter SOURCES tst_qcompleter.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Gui Qt::TestPrivate Qt::Widgets + Qt::WidgetsPrivate ) - -#### Keys ignored in scope 1:.:.:qcompleter.pro:<TRUE>: -# TEMPLATE = "app" diff --git a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp index 7de37875b2..b2e77bc935 100644 --- a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp +++ b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp @@ -1,36 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// 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> @@ -41,6 +15,8 @@ #include <memory> +#include <QtWidgets/private/qapplication_p.h> + Q_DECLARE_METATYPE(QCompleter::CompletionMode) using namespace QTestPrivate; @@ -82,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 @@ -110,6 +86,8 @@ private slots: void fileSystemModel_data(); void fileSystemModel(); + void fileDialog_data(); + void fileDialog(); void changingModel_data(); void changingModel(); @@ -131,6 +109,8 @@ private slots: void dynamicSortOrder(); void disabledItems(); + void hideWidget(); + // task-specific tests below me void task178797_activatedOnReturn(); void task189564_omitNonSelectableItems(); @@ -143,6 +123,8 @@ private slots: void QTBUG_14292_filesystem(); void QTBUG_52028_tabAutoCompletes(); void QTBUG_51889_activatedSentTwice(); + void showPopupInGraphicsView(); + void inheritedEventFilter(); private: void filter(bool assync = false); @@ -173,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; @@ -225,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); @@ -612,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) @@ -626,7 +626,23 @@ void tst_QCompleter::fileSystemModel_data() void tst_QCompleter::fileSystemModel() { - //QFileSystemModel is assync. + //QFileSystemModel is async. + filter(true); +} + +/*! + In the file dialog, the completer uses the EditRole. + See QTBUG-94799 +*/ +void tst_QCompleter::fileDialog_data() +{ + fileSystemModel_data(); + completer->setCompletionRole(Qt::EditRole); +} + +void tst_QCompleter::fileDialog() +{ + //QFileSystemModel is async. filter(true); } @@ -1046,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); @@ -1058,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"); @@ -1093,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); @@ -1164,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)) @@ -1181,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(); @@ -1193,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 @@ -1268,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()); } @@ -1334,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(); @@ -1379,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'); @@ -1542,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'); @@ -1642,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))); @@ -1655,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(); @@ -1671,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'); @@ -1678,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); @@ -1696,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()); } @@ -1732,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); @@ -1744,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()); } @@ -1776,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); @@ -1788,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(); @@ -1798,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(); @@ -1807,7 +1844,78 @@ 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); + + QLineEdit lineEdit; + lineEdit.setCompleter(new QCompleter({"alpha", "omega", "omicron", "zeta"})); + scene.addWidget(&lineEdit); + + view.move(view.screen()->availableGeometry().topLeft() + QPoint(10, 10)); + view.show(); + QVERIFY(QTest::qWaitForWindowExposed(&view)); + + // show popup under line edit + QTest::keyClick(&lineEdit, Qt::Key_A); + QVERIFY(lineEdit.completer()->popup()); + QVERIFY(lineEdit.completer()->popup()->isVisible()); + QCOMPARE(lineEdit.completer()->popup()->geometry().x(), lineEdit.mapToGlobal(QPoint(0, 0)).x()); + QVERIFY(lineEdit.completer()->popup()->geometry().top() >= (lineEdit.mapToGlobal(QPoint(0, lineEdit.height() - 1)).y() - 1)); + + // move widget to the bottom of screen + lineEdit.clear(); + int y = view.screen()->availableGeometry().height() - lineEdit.geometry().y(); + view.move(view.geometry().x(), y); + + // show popup above line edit + QTest::keyClick(&lineEdit, Qt::Key_A); + 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) |