diff options
author | Andrew den Exter <andrew.den.exter@jollamobile.com> | 2013-02-25 17:01:16 +1000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-02-27 01:35:58 +0100 |
commit | c520a69f06317fb90d37324bf284ef9614cb5dbf (patch) | |
tree | 5dfddec914698dd0630bb50d73b92f3c1e94d902 /tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp | |
parent | e8ca72d484c0e56f030e4742bb5f92b6f0555146 (diff) |
Improve emission of FocusScope focusChanged signals.
Emit activeFocusChanged up the focus tree when an item receives a focus
event, rather than when the focusItem changes which happens before
internal state is sufficiently updated for hasFocus and hasActiveFocus
to return the correct values from within a changed signal handler.
There are some limitions to this. First the signals are not emitted
reliably when the scene is not active which makes sense for activeFocus
but not for focus. The second is in some instances the focus and
activeFocus can update unnecessarily while the focus chain sorts itself
out.
QDeclarativeItem tests by Andreas Aardal Hanssen <andreas@hanssen.name>
Task-number: QTBUG-28288
Task-number: QTBUG-25644
Change-Id: Ib3d17c42754c15a08b34c3388f50b45cc1d2a831
Reviewed-by: Andreas Aardal Hanssen <andreas@hanssen.name>
Reviewed-by: Alan Alpert <aalpert@rim.com>
Diffstat (limited to 'tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp')
-rw-r--r-- | tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp | 179 |
1 files changed, 177 insertions, 2 deletions
diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index 0f61bb7e..0251d74d 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -86,6 +86,11 @@ private slots: void qtbug_16871(); void qtbug_21045(); void hasActiveFocusAfterClear(); + void setAndClearFocusOfItem(); + void setAndClearFocusScopeFocus(); + void setFocusThenSetFocusItem0(); + void setFocusFocusItem0ThenHasActiveFocus(); + private: QDeclarativeEngine engine; }; @@ -1043,6 +1048,7 @@ void tst_QDeclarativeItem::propertyChanges() QCOMPARE(parentItem->childrenRect(), childrenRectArguments.at(0).toRectF()); QCOMPARE(item->hasActiveFocus(), true); + QCOMPARE(item->hasFocus(), true); QCOMPARE(focusSpy.count(),1); QList<QVariant> focusArguments = focusSpy.first(); QVERIFY(focusArguments.count() == 1); @@ -1249,8 +1255,9 @@ void tst_QDeclarativeItem::qtbug_21045() void tst_QDeclarativeItem::hasActiveFocusAfterClear() { QGraphicsScene scene; - QGraphicsView view(&scene); - view.show(); + scene.setFocus(); + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); QDeclarativeEngine engine; QDeclarativeComponent qmlComponent(&engine); @@ -1273,6 +1280,174 @@ void tst_QDeclarativeItem::hasActiveFocusAfterClear() QVERIFY(!createdItem->hasActiveFocus()); } +void tst_QDeclarativeItem::setAndClearFocusOfItem() +{ + QGraphicsScene scene; + scene.setFocus(); + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); + + QDeclarativeEngine engine(&scene); + QDeclarativeComponent qmlComponent(&engine); + qmlComponent.setData( + "import QtQuick 1.1;" + "Item { id: root; " + "signal focusChangedTo(bool value);" + "signal activeFocusChangedTo(bool value);" + "onFocusChanged: root.focusChangedTo(focus);" + "onActiveFocusChanged: root.activeFocusChangedTo(activeFocus);" + "}", QUrl("test")); + + QGraphicsItem *createdItem = qobject_cast<QGraphicsItem*>(qmlComponent.create(engine.rootContext())); + QVERIFY(createdItem); + scene.addItem(createdItem); + + QSignalSpy focusChangedSpy(createdItem->toGraphicsObject(), SIGNAL(focusChangedTo(bool))); + QSignalSpy activeFocusChangedSpy(createdItem->toGraphicsObject(), SIGNAL(activeFocusChangedTo(bool))); + + QVERIFY(!createdItem->hasFocus()); + createdItem->toGraphicsObject()->setFocus(); + QCOMPARE(focusChangedSpy.count(), 1); + QCOMPARE(activeFocusChangedSpy.count(), 1); + QCOMPARE(focusChangedSpy.takeFirst().first().toBool(), true); + QCOMPARE(activeFocusChangedSpy.takeFirst().first().toBool(), true); + + createdItem->toGraphicsObject()->clearFocus(); + QCOMPARE(focusChangedSpy.count(), 1); + QCOMPARE(activeFocusChangedSpy.count(), 1); + QCOMPARE(focusChangedSpy.takeFirst().first().toBool(), false); + QCOMPARE(activeFocusChangedSpy.takeFirst().first().toBool(), false); +} + +void tst_QDeclarativeItem::setAndClearFocusScopeFocus() +{ + //graphicsview init + QGraphicsScene scene; + scene.setFocus(); + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); + + //declarative init + QDeclarativeEngine engine(&scene); + QDeclarativeComponent qmlComponent(&engine); + + qmlComponent.setData( + "\nimport QtQuick 1.1" + "\nFocusScope {" + "\n id: root" + "\n signal listActiveFocusChangedTo(bool value)" + "\n signal topRectActiveFocusChangedTo(bool value)" + "\n Rectangle {" + "\n id: topRect" + "\n focus: true" + "\n onActiveFocusChanged: root.topRectActiveFocusChangedTo(topRect.activeFocus)" + "\n }" + "\n FocusScope {" + "\n objectName: \"focusScope\"" + "\n onActiveFocusChanged: root.listActiveFocusChangedTo(activeFocus)" + "\n }" + "\n Rectangle { objectName: \"bottom\" }" + "}", QUrl("")); + + QGraphicsItem *createdItem = qobject_cast<QGraphicsItem*>(qmlComponent.create(engine.rootContext())); + QVERIFY(createdItem); + scene.addItem(createdItem); + + QDeclarativeItem *focusScope = createdItem->toGraphicsObject()->findChild<QDeclarativeItem*>("focusScope"); + QDeclarativeItem *bottomRect = createdItem->toGraphicsObject()->findChild<QDeclarativeItem*>("bottom"); + + QSignalSpy focusScopeSpy(createdItem->toGraphicsObject(), SIGNAL(listActiveFocusChangedTo(bool))); + QSignalSpy topRectFocusSpy(createdItem->toGraphicsObject(), SIGNAL(topRectActiveFocusChangedTo(bool))); + + //#1: root gets activefocus, and in turn the top rectangle + createdItem->setFocus(); + + //#2 + focusScope->setFocus(true); + + //#3 + bottomRect->setFocus(true); + + QCOMPARE(topRectFocusSpy.count(), 2); + QCOMPARE(focusScopeSpy.count(), 2); + + QCOMPARE(topRectFocusSpy.takeFirst().first().toBool(), true); //from #1 + QCOMPARE(topRectFocusSpy.takeFirst().first().toBool(), false); //from #2 + + QCOMPARE(focusScopeSpy.takeFirst().first().toBool(), true); //from #2 + QCOMPARE(focusScopeSpy.takeFirst().first().toBool(), false); //from #3 +} + +void tst_QDeclarativeItem::setFocusThenSetFocusItem0() +{ + //graphicsview init + QGraphicsScene scene; + scene.setFocus(); + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); + + //declarative init + QDeclarativeEngine engine(&scene); + QDeclarativeComponent qmlComponent(&engine); + qmlComponent.setData( + "import QtQuick 1.1;" + "Item {" + "signal focusChangedTo(bool value);" + "signal activeFocusChangedTo(bool value);" + "onFocusChanged: focusChangedTo(focus);" + "onActiveFocusChanged: activeFocusChangedTo(activeFocus);" + "}", QUrl("")); + + QGraphicsItem *createdItem = qobject_cast<QGraphicsItem*>(qmlComponent.create(engine.rootContext())); + QVERIFY(createdItem); + + scene.addItem(createdItem); + + QSignalSpy focusChangedSpy(createdItem->toGraphicsObject(), SIGNAL(focusChangedTo(bool))); + QSignalSpy activeFocusChangedSpy(createdItem->toGraphicsObject(), SIGNAL(activeFocusChangedTo(bool))); + + createdItem->toGraphicsObject()->setFocus(); + QVERIFY(!focusChangedSpy.isEmpty()); + QVERIFY(!activeFocusChangedSpy.isEmpty()); + QCOMPARE(focusChangedSpy.takeFirst().first().toBool(), true); + QCOMPARE(activeFocusChangedSpy.takeFirst().first().toBool(), true); + + scene.setFocusItem(0); + QVERIFY(!focusChangedSpy.isEmpty()); + QVERIFY(!activeFocusChangedSpy.isEmpty()); + QCOMPARE(focusChangedSpy.takeFirst().first().toBool(), false); + QCOMPARE(activeFocusChangedSpy.takeFirst().first().toBool(), false); + QVERIFY(activeFocusChangedSpy.isEmpty()); +} + +void tst_QDeclarativeItem::setFocusFocusItem0ThenHasActiveFocus() +{ + QGraphicsScene scene; + scene.setFocus(); + QEvent event(QEvent::WindowActivate); + qApp->sendEvent(&scene, &event); + + QDeclarativeEngine engine(&scene); + QDeclarativeComponent qmlComponent(&engine); + qmlComponent.setData( + "import QtQuick 1.1;" + "TextInput {" + "width: 100; height: 100;" + "Rectangle { anchors.fill: parent; color: \"yellow\"; z: parent.z - 1 }" + "}", QUrl("")); + QDeclarativeItem *createdItem = qobject_cast<QDeclarativeItem*>(qmlComponent.create(engine.rootContext())); + QVERIFY(createdItem != 0); + + scene.addItem(createdItem); + + createdItem->QGraphicsItem::setFocus(); + QCoreApplication::processEvents(); + scene.setFocusItem(0); + QCoreApplication::processEvents(); + + QVERIFY(!createdItem->hasActiveFocus()); +} + QTEST_MAIN(tst_QDeclarativeItem) #include "tst_qdeclarativeitem.moc" |