summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp')
-rw-r--r--tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp198
1 files changed, 144 insertions, 54 deletions
diff --git a/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp b/tests/auto/widgets/util/qcompleter/tst_qcompleter.cpp
index 51a75573a4..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
@@ -133,6 +109,8 @@ private slots:
void dynamicSortOrder();
void disabledItems();
+ void hideWidget();
+
// task-specific tests below me
void task178797_activatedOnReturn();
void task189564_omitNonSelectableItems();
@@ -145,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);
@@ -175,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;
@@ -227,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);
@@ -614,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)
@@ -1064,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);
@@ -1076,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");
@@ -1111,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);
@@ -1182,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))
@@ -1199,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();
@@ -1211,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
@@ -1286,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());
}
@@ -1352,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();
@@ -1397,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');
@@ -1560,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');
@@ -1660,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)));
@@ -1673,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();
@@ -1689,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');
@@ -1696,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);
@@ -1714,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());
}
@@ -1750,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);
@@ -1762,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());
}
@@ -1794,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);
@@ -1806,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();
@@ -1816,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();
@@ -1825,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)