summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgraphicsitem
diff options
context:
space:
mode:
authorYoann Lopes <yoann.lopes@nokia.com>2010-02-17 12:47:26 (GMT)
committerYoann Lopes <yoann.lopes@nokia.com>2010-02-17 12:47:26 (GMT)
commita7ef2d899d711d750238a8d69284da808188b407 (patch)
tree2c74fc117a83168641b13ce575af5766321c2eef /tests/auto/qgraphicsitem
parent100ea081145be48f7a091be5dee339255154b078 (diff)
downloadQt-a7ef2d899d711d750238a8d69284da808188b407.zip
Qt-a7ef2d899d711d750238a8d69284da808188b407.tar.gz
Qt-a7ef2d899d711d750238a8d69284da808188b407.tar.bz2
Fixes crash when destroying a QGraphicsItem.
When clearing the focus on an item, the focus was previously passed to the closest parent focus scope (if any). But the focus should go to the closest parent focus scope only if the item being cleared of the focus is itself a focus scope. This incorrect behavior leaded to invalid pointers in QGraphicsItem under specific circumstances, eventually leading to a crash when destroying the item. Auto-test included. Task-number: QT-2649 Reviewed-by: Andreas Aardal Hanssen
Diffstat (limited to 'tests/auto/qgraphicsitem')
-rw-r--r--tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
index 7c1b97e..269ec24 100644
--- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -438,6 +438,7 @@ private slots:
void QTBUG_6738_missingUpdateWithSetParent();
void QTBUG_7714_fullUpdateDiscardingOpacityUpdate2();
void QT_2653_fullUpdateDiscardingOpacityUpdate();
+ void QT_2649_focusScope();
private:
QList<QGraphicsItem *> paintedItems;
@@ -10002,5 +10003,61 @@ void tst_QGraphicsItem::QTBUG_7714_fullUpdateDiscardingOpacityUpdate2()
QTRY_COMPARE(view.repaints, 1);
}
+void tst_QGraphicsItem::QT_2649_focusScope()
+{
+ QGraphicsScene *scene = new QGraphicsScene;
+
+ QGraphicsRectItem *subFocusItem = new QGraphicsRectItem;
+ subFocusItem->setFlags(QGraphicsItem::ItemIsFocusable);
+ subFocusItem->setFocus();
+ QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
+
+ QGraphicsRectItem *scope = new QGraphicsRectItem;
+ scope->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsFocusScope);
+ scope->setFocus();
+ subFocusItem->setParentItem(scope);
+ QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
+ QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
+
+ QGraphicsRectItem *rootItem = new QGraphicsRectItem;
+ rootItem->setFlags(QGraphicsItem::ItemIsFocusable);
+ scope->setParentItem(rootItem);
+ QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
+ QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
+ QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
+
+ scene->addItem(rootItem);
+
+ QEvent windowActivate(QEvent::WindowActivate);
+ qApp->sendEvent(scene, &windowActivate);
+ scene->setFocus();
+
+ QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
+ QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
+ QVERIFY(subFocusItem->hasFocus());
+
+ //If we hide the focusScope, the entire subFocus chain should be cleared
+ scope->hide();
+
+ QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)0);
+ QCOMPARE(scope->focusItem(), (QGraphicsItem *)0);
+ QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)0);
+ QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
+ QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
+ QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
+ QVERIFY(!subFocusItem->hasFocus());
+
+ delete scene;
+}
+
QTEST_MAIN(tst_QGraphicsItem)
#include "tst_qgraphicsitem.moc"