diff options
Diffstat (limited to 'tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp')
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp | 131 |
1 files changed, 130 insertions, 1 deletions
diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index f4edeb2..ecc813e 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -55,7 +55,9 @@ public: tst_QDeclarativeItem(); private slots: + void initTestCase(); void keys(); + void keysProcessingOrder(); void keyNavigation(); void smooth(); void clip(); @@ -79,8 +81,11 @@ private: class KeysTestObject : public QObject { Q_OBJECT + + Q_PROPERTY(bool processLast READ processLast NOTIFY processLastChanged) + public: - KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0) {} + KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0), mLast(false) {} void reset() { mKey = 0; @@ -89,6 +94,14 @@ public: mForwardedKey = 0; } + bool processLast() const { return mLast; } + void setProcessLast(bool b) { + if (b != mLast) { + mLast = b; + emit processLastChanged(); + } + } + public slots: void keyPress(int key, QString text, int modifiers) { mKey = key; @@ -104,20 +117,73 @@ public slots: mForwardedKey = key; } +signals: + void processLastChanged(); + public: int mKey; QString mText; int mModifiers; int mForwardedKey; + bool mLast; private: }; +class KeyTestItem : public QDeclarativeItem +{ + Q_OBJECT +public: + KeyTestItem(QDeclarativeItem *parent=0) : QDeclarativeItem(parent), mKey(0) {} + +protected: + void keyPressEvent(QKeyEvent *e) { + keyPressPreHandler(e); + if (e->isAccepted()) + return; + + mKey = e->key(); + + if (e->key() == Qt::Key_A) + e->accept(); + else + e->ignore(); + + if (!e->isAccepted()) + QDeclarativeItem::keyPressEvent(e); + } + + void keyReleaseEvent(QKeyEvent *e) { + keyReleasePreHandler(e); + + if (e->isAccepted()) + return; + + if (e->key() == Qt::Key_B) + e->accept(); + else + e->ignore(); + + if (!e->isAccepted()) + QDeclarativeItem::keyReleaseEvent(e); + } + +public: + int mKey; +}; + +QML_DECLARE_TYPE(KeyTestItem); + tst_QDeclarativeItem::tst_QDeclarativeItem() { } +void tst_QDeclarativeItem::initTestCase() +{ + qmlRegisterType<KeyTestItem>("Test",1,0,"KeyTestItem"); +} + void tst_QDeclarativeItem::keys() { QDeclarativeView *canvas = new QDeclarativeView(0); @@ -214,6 +280,69 @@ void tst_QDeclarativeItem::keys() QCOMPARE(testObject->mKey, 0); QVERIFY(!key.isAccepted()); + canvas->rootContext()->setContextProperty("enableKeyHanding", QVariant(true)); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier, "", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_Return)); + QVERIFY(key.isAccepted()); + + delete canvas; + delete testObject; +} + +void tst_QDeclarativeItem::keysProcessingOrder() +{ + QDeclarativeView *canvas = new QDeclarativeView(0); + canvas->setFixedSize(240,320); + + KeysTestObject *testObject = new KeysTestObject; + canvas->rootContext()->setContextProperty("keysTestObject", testObject); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/keyspriority.qml")); + canvas->show(); + qApp->processEvents(); + + KeyTestItem *testItem = qobject_cast<KeyTestItem*>(canvas->rootObject()); + QVERIFY(testItem); + + QEvent wa(QEvent::WindowActivate); + QApplication::sendEvent(canvas, &wa); + QFocusEvent fe(QEvent::FocusIn); + QApplication::sendEvent(canvas, &fe); + + QKeyEvent key(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, "A", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_A)); + QCOMPARE(testObject->mText, QLatin1String("A")); + QVERIFY(testObject->mModifiers == Qt::NoModifier); + QVERIFY(key.isAccepted()); + + testObject->reset(); + + testObject->setProcessLast(true); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, "A", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, 0); + QVERIFY(key.isAccepted()); + + testObject->reset(); + + key = QKeyEvent(QEvent::KeyPress, Qt::Key_B, Qt::NoModifier, "B", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_B)); + QCOMPARE(testObject->mText, QLatin1String("B")); + QVERIFY(testObject->mModifiers == Qt::NoModifier); + QVERIFY(!key.isAccepted()); + + testObject->reset(); + + key = QKeyEvent(QEvent::KeyRelease, Qt::Key_B, Qt::NoModifier, "B", false, 1); + QApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, 0); + QVERIFY(key.isAccepted()); + delete canvas; delete testObject; } |