From 0866e48ae25f53eb9b27b0b86d333700f912459f Mon Sep 17 00:00:00 2001 From: Ivan Komissarov Date: Sat, 14 Apr 2012 12:14:22 +0400 Subject: Fixed applying invalid data from QLineEdit to model Task-number: QTBUG-2216 Change-Id: I5d1c18aadb380667dedc2bb9f9b7e3dd8178a24c Reviewed-by: Friedemann Kleint Reviewed-by: Stephen Kelly Reviewed-by: David Faure --- .../itemviews/qitemdelegate/tst_qitemdelegate.cpp | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+) (limited to 'tests/auto/widgets/itemviews') diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp index d47eebe03a..538cc7bb4c 100644 --- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp @@ -232,6 +232,8 @@ private slots: void enterKey_data(); void enterKey(); void comboBox(); + void testLineEditValidation_data(); + void testLineEditValidation(); void task257859_finalizeEdit(); void QTBUG4435_keepSelectionOnCheck(); @@ -1413,6 +1415,135 @@ void tst_QItemDelegate::comboBox() QCOMPARE(data.toBool(), false); } +void tst_QItemDelegate::testLineEditValidation_data() +{ + QTest::addColumn("key"); + + QTest::newRow("enter") << int(Qt::Key_Enter); + QTest::newRow("return") << int(Qt::Key_Return); + QTest::newRow("tab") << int(Qt::Key_Tab); + QTest::newRow("backtab") << int(Qt::Key_Backtab); + QTest::newRow("escape") << int(Qt::Key_Escape); +} + +void tst_QItemDelegate::testLineEditValidation() +{ + QFETCH(int, key); + + struct TestDelegate : public QItemDelegate + { + virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const + { + Q_UNUSED(option); + Q_UNUSED(index); + + QLineEdit *editor = new QLineEdit(parent); + QRegularExpression re("\\w+,\\w+"); // two words separated by a comma + editor->setValidator(new QRegularExpressionValidator(re, editor)); + editor->setObjectName(QStringLiteral("TheEditor")); + return editor; + } + } delegate; + + QStandardItemModel model; + // need a couple of dummy items to test tab and back tab + model.appendRow(new QStandardItem(QStringLiteral("dummy"))); + QStandardItem *item = new QStandardItem(QStringLiteral("abc,def")); + model.appendRow(item); + model.appendRow(new QStandardItem(QStringLiteral("dummy"))); + + QListView view; + view.setModel(&model); + view.setItemDelegate(&delegate); + view.show(); + view.setFocus(); + QApplication::setActiveWindow(&view); + QVERIFY(QTest::qWaitForWindowActive(&view)); + + QList lineEditors; + QPointer editor; + QPersistentModelIndex index = model.indexFromItem(item); + + view.setCurrentIndex(index); + view.edit(index); + QTest::qWait(30); + + lineEditors = view.findChildren(QStringLiteral("TheEditor")); + QCOMPARE(lineEditors.count(), 1); + editor = lineEditors.at(0); + editor->clear(); + + // first try to set a valid text + QTest::keyClicks(editor, QStringLiteral("foo,bar")); + QTest::qWait(30); + + // close the editor + QTest::keyClick(editor, Qt::Key(key)); + QTest::qWait(30); + + QVERIFY(editor.isNull()); + if (key != Qt::Key_Escape) + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("foo,bar")); + else + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("abc,def")); + + // now an invalid (but partially matching) text + view.setCurrentIndex(index); + view.edit(index); + QTest::qWait(30); + + lineEditors = view.findChildren(QStringLiteral("TheEditor")); + QCOMPARE(lineEditors.count(), 1); + editor = lineEditors.at(0); + editor->clear(); + + // edit + QTest::keyClicks(editor, QStringLiteral("foobar")); + QTest::qWait(30); + + // try to close the editor + QTest::keyClick(editor, Qt::Key(key)); + QTest::qWait(30); + + if (key != Qt::Key_Escape) { + QVERIFY(!editor.isNull()); + QCOMPARE(qApp->focusWidget(), editor.data()); + QCOMPARE(editor->text(), QStringLiteral("foobar")); + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("foo,bar")); + } else { + QVERIFY(editor.isNull()); + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("abc,def")); + } + + // reset the view to forcibly close the editor + view.reset(); + QTest::qWait(30); + + // set a valid text again + view.setCurrentIndex(index); + view.edit(index); + QTest::qWait(30); + + lineEditors = view.findChildren(QStringLiteral("TheEditor")); + QCOMPARE(lineEditors.count(), 1); + editor = lineEditors.at(0); + editor->clear(); + + // set a valid text + QTest::keyClicks(editor, QStringLiteral("gender,bender")); + QTest::qWait(30); + + // close the editor + QTest::keyClick(editor, Qt::Key(key)); + QTest::qWait(30); + + QVERIFY(editor.isNull()); + if (key != Qt::Key_Escape) + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("gender,bender")); + else + QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("abc,def")); +} + // ### _not_ covered: -- cgit v1.2.3 From 074975055d7091c7bbfe0a0e1ad2c0900359955b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simo=20F=C3=A4lt?= Date: Fri, 10 May 2013 13:08:10 +0300 Subject: Disabling some concurrency in widget tests There has been frequent failures with CI, especially with OSX 10.7. These seems to work ok in CI node when ran alone but CI is running those in parallel with some other tests, and this is causing errors, most likely due to lost focus. Change-Id: Ic4151099c27a4b5d91778c9b13e88d2d661e8a0d Reviewed-by: Sergio Ahumada Reviewed-by: Friedemann Kleint --- tests/auto/widgets/itemviews/qtableview/qtableview.pro | 1 - 1 file changed, 1 deletion(-) (limited to 'tests/auto/widgets/itemviews') diff --git a/tests/auto/widgets/itemviews/qtableview/qtableview.pro b/tests/auto/widgets/itemviews/qtableview/qtableview.pro index 41016ff984..0814af77fb 100644 --- a/tests/auto/widgets/itemviews/qtableview/qtableview.pro +++ b/tests/auto/widgets/itemviews/qtableview/qtableview.pro @@ -1,5 +1,4 @@ CONFIG += testcase -CONFIG += parallel_test TARGET = tst_qtableview QT += widgets widgets-private testlib QT += core-private gui-private -- cgit v1.2.3