From c535df0f286826c5575ec00c7e4c3f811a73918b Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 16 Nov 2009 15:54:38 +0100 Subject: Fix tst_QGraphicsView::inputMethodSensitivity autotest The problem was that the scene was not notified it lost the focus when it was disconnected from the view (with setScene) So when it got back the focus, nothing was updated because the scene thought it still had the focus. Reviewed-by: jasplin --- src/gui/graphicsview/qgraphicsview.cpp | 2 + tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp | 62 +++++++++++++++++++++--- tests/auto/qgraphicsview/tst_qgraphicsview.cpp | 9 ++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/gui/graphicsview/qgraphicsview.cpp b/src/gui/graphicsview/qgraphicsview.cpp index 3ef311c..3f9f443 100644 --- a/src/gui/graphicsview/qgraphicsview.cpp +++ b/src/gui/graphicsview/qgraphicsview.cpp @@ -1517,6 +1517,8 @@ void QGraphicsView::setScene(QGraphicsScene *scene) QEvent windowDeactivate(QEvent::WindowDeactivate); QApplication::sendEvent(d->scene, &windowDeactivate); } + if(hasFocus()) + d->scene->clearFocus(); } // Assign the new scene and update the contents (scrollbars, etc.)). diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp index 9a561eb..a49c2c1 100644 --- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp @@ -3983,20 +3983,28 @@ void tst_QGraphicsScene::isActive() QVERIFY(!scene1.isActive()); //it is hidden; QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view1->show(); QVERIFY(scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view2->hide(); QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); toplevel1.hide(); - QTest::qWait(12); + QTest::qWait(50); QTRY_VERIFY(!scene1.isActive()); QTRY_VERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); toplevel1.show(); QApplication::setActiveWindow(&toplevel1); @@ -4005,14 +4013,21 @@ void tst_QGraphicsScene::isActive() QTRY_VERIFY(scene1.isActive()); QTRY_VERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view2->show(); QVERIFY(scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); } QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); + { QWidget toplevel2; @@ -4025,6 +4040,8 @@ void tst_QGraphicsScene::isActive() QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); toplevel2.show(); QApplication::setActiveWindow(&toplevel2); @@ -4033,66 +4050,89 @@ void tst_QGraphicsScene::isActive() QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view2->setScene(&scene2); QVERIFY(scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view1->setScene(&scene2); QVERIFY(!scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view1->hide(); QVERIFY(!scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view1->setScene(&scene1); QVERIFY(!scene1.isActive()); QVERIFY(scene2.isActive()); - - view1->show(); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view1->show(); QVERIFY(scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); view2->hide(); QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); QGraphicsView topLevelView; topLevelView.show(); QApplication::setActiveWindow(&topLevelView); + topLevelView.setFocus(); QTest::qWaitForWindowShown(&topLevelView); QTRY_COMPARE(QApplication::activeWindow(), static_cast(&topLevelView)); QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); topLevelView.setScene(&scene1); QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view2->show(); QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view1->hide(); QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); QApplication::setActiveWindow(&toplevel2); QTRY_COMPARE(QApplication::activeWindow(), &toplevel2); QVERIFY(!scene1.isActive()); QVERIFY(scene2.isActive()); - - + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); } QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); { QWidget toplevel3; @@ -4104,6 +4144,9 @@ void tst_QGraphicsScene::isActive() QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); + toplevel3.show(); QApplication::setActiveWindow(&toplevel3); @@ -4112,23 +4155,30 @@ void tst_QGraphicsScene::isActive() QVERIFY(scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); layout->addWidget(view2); QApplication::processEvents(); QVERIFY(scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); view1->setParent(0); QVERIFY(!scene1.isActive()); QVERIFY(scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(scene2.hasFocus()); delete view1; } QVERIFY(!scene1.isActive()); QVERIFY(!scene2.isActive()); + QVERIFY(!scene1.hasFocus()); + QVERIFY(!scene2.hasFocus()); } - QTEST_MAIN(tst_QGraphicsScene) #include "tst_qgraphicsscene.moc" diff --git a/tests/auto/qgraphicsview/tst_qgraphicsview.cpp b/tests/auto/qgraphicsview/tst_qgraphicsview.cpp index f07453c..1ff06c2 100644 --- a/tests/auto/qgraphicsview/tst_qgraphicsview.cpp +++ b/tests/auto/qgraphicsview/tst_qgraphicsview.cpp @@ -3600,6 +3600,7 @@ void tst_QGraphicsView::inputMethodSensitivity() item->setFlag(QGraphicsItem::ItemIsFocusable); scene.addItem(item); scene.setFocusItem(item); + QCOMPARE(scene.focusItem(), item); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false); @@ -3614,27 +3615,35 @@ void tst_QGraphicsView::inputMethodSensitivity() scene.addItem(item2); scene.setFocusItem(item2); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); + QCOMPARE(scene.focusItem(), item2); scene.setFocusItem(item); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); + QCOMPARE(scene.focusItem(), item); view.setScene(0); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); + QCOMPARE(scene.focusItem(), item); view.setScene(&scene); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); + QCOMPARE(scene.focusItem(), item); scene.setFocusItem(item2); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); + QCOMPARE(scene.focusItem(), item2); view.setScene(0); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); + QCOMPARE(scene.focusItem(), item2); scene.setFocusItem(item); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false); + QCOMPARE(scene.focusItem(), item); view.setScene(&scene); QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true); + QCOMPARE(scene.focusItem(), item); } class InputContextTester : public QInputContext -- cgit v0.12