From a34e4a8946df5953c5b22d5c44cf1d80bc14bf93 Mon Sep 17 00:00:00 2001
From: Michael Hasselmann <michaelh@openismus.com>
Date: Tue, 8 Mar 2011 04:32:26 +0100
Subject: [PATCH 2/2] Extend ItemStopsClickFocusPropagation test to cover ItemStopsFocusHandling, too

The previous test was only testing one scenario, which didn't give a good idea
whether the flag was actually working as intended.
---
 tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp |   69 ++++++++++++++++++++----
 1 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
index dacb61e..168f75e 100644
--- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -447,7 +447,8 @@ private slots:
     void updateMicroFocus();
     void textItem_shortcuts();
     void scroll();
-    void stopClickFocusPropagation();
+    void focusHandling_data();
+    void focusHandling();
     void deviceCoordinateCache_simpleRotations();
 
     // task specific tests below me
@@ -10537,8 +10538,39 @@ void tst_QGraphicsItem::scroll()
     QCOMPARE(item2->lastExposedRect, expectedItem2Expose);
 }
 
-void tst_QGraphicsItem::stopClickFocusPropagation()
+Q_DECLARE_METATYPE(QGraphicsItem::GraphicsItemFlag);
+
+void tst_QGraphicsItem::focusHandling_data()
 {
+    QTest::addColumn<QGraphicsItem::GraphicsItemFlag>("focusFlag");
+    QTest::addColumn<bool>("useStickyFocus");
+    QTest::addColumn<int>("expectedFocusItem"); // 0: none, 1: focusableUnder, 2: itemWithFocus
+
+    QTest::newRow("Focus goes through.")
+        << static_cast<QGraphicsItem::GraphicsItemFlag>(0x0) << false << 1;
+
+    QTest::newRow("Focus goes through, even with sticky scene.")
+        << static_cast<QGraphicsItem::GraphicsItemFlag>(0x0) << true  << 1;
+
+    QTest::newRow("With ItemStopsClickFocusPropagation, we cannot focus the item beneath the flagged one (but can still focus-out).")
+        << QGraphicsItem::ItemStopsClickFocusPropagation << false << 0;
+
+    QTest::newRow("With ItemStopsClickFocusPropagation, we cannot focus the item beneath the flagged one (and cannot focus-out if scene is sticky).")
+        << QGraphicsItem::ItemStopsClickFocusPropagation << true << 2;
+
+    QTest::newRow("With ItemStopsFocusHandling, focus cannot be changed by presses.")
+        << QGraphicsItem::ItemStopsFocusHandling << false << 2;
+
+    QTest::newRow("With ItemStopsFocusHandling, focus cannot be changed by presses (even if scene is sticky).")
+        << QGraphicsItem::ItemStopsFocusHandling << true << 2;
+}
+
+void tst_QGraphicsItem::focusHandling()
+{
+    QFETCH(QGraphicsItem::GraphicsItemFlag, focusFlag);
+    QFETCH(bool, useStickyFocus);
+    QFETCH(int, expectedFocusItem);
+
     class MyItem : public QGraphicsRectItem
     {
     public:
@@ -10549,12 +10581,9 @@ void tst_QGraphicsItem::stopClickFocusPropagation()
         }
     };
 
-    QGraphicsScene scene(-50, -50, 400, 400);
-    scene.setStickyFocus(true);
-
     QGraphicsRectItem *noFocusOnTop = new MyItem;
+    noFocusOnTop->setFlag(QGraphicsItem::ItemIsFocusable, false);
     noFocusOnTop->setBrush(Qt::yellow);
-    noFocusOnTop->setFlag(QGraphicsItem::ItemStopsClickFocusPropagation);
 
     QGraphicsRectItem *focusableUnder = new MyItem;
     focusableUnder->setBrush(Qt::blue);
@@ -10566,9 +10595,13 @@ void tst_QGraphicsItem::stopClickFocusPropagation()
     itemWithFocus->setFlag(QGraphicsItem::ItemIsFocusable);
     itemWithFocus->setPos(250, 10);
 
+    QGraphicsScene scene(-50, -50, 400, 400);
     scene.addItem(noFocusOnTop);
     scene.addItem(focusableUnder);
     scene.addItem(itemWithFocus);
+    scene.setStickyFocus(useStickyFocus);
+
+    noFocusOnTop->setFlag(focusFlag);
     focusableUnder->stackBefore(noFocusOnTop);
     itemWithFocus->setFocus();
 
@@ -10580,14 +10613,28 @@ void tst_QGraphicsItem::stopClickFocusPropagation()
     QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&view));
     QVERIFY(itemWithFocus->hasFocus());
 
-    QPointF mousePressPoint = noFocusOnTop->mapToScene(QPointF());
-    mousePressPoint.rx() += 60;
-    mousePressPoint.ry() += 60;
+    const QPointF mousePressPoint = noFocusOnTop->mapToScene(noFocusOnTop->boundingRect().center());
     const QList<QGraphicsItem *> itemsAtMousePressPosition = scene.items(mousePressPoint);
-    QVERIFY(itemsAtMousePressPosition.contains(focusableUnder));
+    QVERIFY(itemsAtMousePressPosition.contains(noFocusOnTop));
 
     sendMousePress(&scene, mousePressPoint);
-    QVERIFY(itemWithFocus->hasFocus());
+
+    switch (expectedFocusItem) {
+    case 0:
+        QCOMPARE(scene.focusItem(), static_cast<QGraphicsRectItem *>(0));
+        break;
+    case 1:
+        QCOMPARE(scene.focusItem(), focusableUnder);
+        break;
+    case 2:
+        QCOMPARE(scene.focusItem(), itemWithFocus);
+        break;
+    }
+
+    // Sanity check - manually setting the focus must work regardless of our
+    // focus handling flags:
+    focusableUnder->setFocus();
+    QCOMPARE(scene.focusItem(), focusableUnder);
 }
 
 void tst_QGraphicsItem::deviceCoordinateCache_simpleRotations()
-- 
1.7.4.1

