tests/auto/qgraphicswidget/tst_qgraphicswidget.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
   109     void fontChangedEvent();
   109     void fontChangedEvent();
   110     void fontPropagationWidgetItemWidget();
   110     void fontPropagationWidgetItemWidget();
   111     void fontPropagationSceneChange();
   111     void fontPropagationSceneChange();
   112     void geometry_data();
   112     void geometry_data();
   113     void geometry();
   113     void geometry();
       
   114     void width();
       
   115     void height();
   114     void getContentsMargins_data();
   116     void getContentsMargins_data();
   115     void getContentsMargins();
   117     void getContentsMargins();
   116     void initStyleOption_data();
   118     void initStyleOption_data();
   117     void initStyleOption();
   119     void initStyleOption();
   118     void layout_data();
   120     void layout_data();
   161     void widgetSendsGeometryChanges();
   163     void widgetSendsGeometryChanges();
   162     void respectHFW();
   164     void respectHFW();
   163     void addChildInpolishEvent();
   165     void addChildInpolishEvent();
   164     void polishEvent();
   166     void polishEvent();
   165     void polishEvent2();
   167     void polishEvent2();
       
   168     void initialShow();
       
   169     void initialShow2();
   166 
   170 
   167     // Task fixes
   171     // Task fixes
   168     void task236127_bspTreeIndexFails();
   172     void task236127_bspTreeIndexFails();
   169     void task243004_setStyleCrash();
   173     void task243004_setStyleCrash();
   170     void task250119_shortcutContext();
   174     void task250119_shortcutContext();
   457     }
   461     }
   458     if (childCount > 0) {
   462     if (childCount > 0) {
   459         QFETCH(int, childWithFocus);
   463         QFETCH(int, childWithFocus);
   460         SubQGraphicsWidget *widget = children[childWithFocus];
   464         SubQGraphicsWidget *widget = children[childWithFocus];
   461         widget->setFocus();
   465         widget->setFocus();
   462         QVERIFY(widget->hasFocus());
   466         QTRY_VERIFY(widget->hasFocus());
   463         QCOMPARE(parent->focusWidget(), static_cast<QGraphicsWidget*>(widget));
   467         QCOMPARE(parent->focusWidget(), static_cast<QGraphicsWidget*>(widget));
   464     }
   468     }
   465 }
   469 }
   466 
   470 
   467 void tst_QGraphicsWidget::focusWidget2()
   471 void tst_QGraphicsWidget::focusWidget2()
   474     EventSpy focusInSpy(widget, QEvent::FocusIn);
   478     EventSpy focusInSpy(widget, QEvent::FocusIn);
   475     EventSpy focusOutSpy(widget, QEvent::FocusOut);
   479     EventSpy focusOutSpy(widget, QEvent::FocusOut);
   476 
   480 
   477     scene.addItem(widget);
   481     scene.addItem(widget);
   478 
   482 
   479     QVERIFY(!widget->hasFocus());
   483     QTRY_VERIFY(!widget->hasFocus());
   480     widget->setFocusPolicy(Qt::StrongFocus);
   484     widget->setFocusPolicy(Qt::StrongFocus);
   481     QVERIFY(!widget->hasFocus());
   485     QTRY_VERIFY(!widget->hasFocus());
   482 
   486 
   483     QGraphicsWidget *subWidget = new QGraphicsWidget(widget);
   487     QGraphicsWidget *subWidget = new QGraphicsWidget(widget);
   484     QVERIFY(!subWidget->hasFocus());
   488     QTRY_VERIFY(!subWidget->hasFocus());
   485 
   489 
   486     scene.setFocus();
   490     scene.setFocus();
   487 
   491 
   488     QVERIFY(!widget->hasFocus());
   492     QTRY_VERIFY(!widget->hasFocus());
   489     QVERIFY(!subWidget->hasFocus());
   493     QTRY_VERIFY(!subWidget->hasFocus());
   490 
   494 
   491     widget->setFocus();
   495     widget->setFocus();
   492 
   496 
   493     QVERIFY(widget->hasFocus());
   497     QTRY_VERIFY(widget->hasFocus());
   494     QCOMPARE(focusInSpy.count(), 1);
   498     QTRY_COMPARE(focusInSpy.count(), 1);
   495     QVERIFY(!subWidget->hasFocus());
   499     QTRY_VERIFY(!subWidget->hasFocus());
   496 
   500 
   497     QGraphicsWidget *otherSubWidget = new QGraphicsWidget;
   501     QGraphicsWidget *otherSubWidget = new QGraphicsWidget;
   498     EventSpy otherFocusInSpy(otherSubWidget, QEvent::FocusIn);
   502     EventSpy otherFocusInSpy(otherSubWidget, QEvent::FocusIn);
   499     EventSpy otherFocusOutSpy(otherSubWidget, QEvent::FocusOut);
   503     EventSpy otherFocusOutSpy(otherSubWidget, QEvent::FocusOut);
   500 
   504 
   501     otherSubWidget->setFocusPolicy(Qt::StrongFocus);
   505     otherSubWidget->setFocusPolicy(Qt::StrongFocus);
   502     otherSubWidget->setParentItem(widget);
   506     otherSubWidget->setParentItem(widget);
   503 
   507 
   504     QVERIFY(widget->hasFocus());
   508     QTRY_VERIFY(widget->hasFocus());
   505     QCOMPARE(scene.focusItem(), (QGraphicsItem *)widget);
   509     QCOMPARE(scene.focusItem(), (QGraphicsItem *)widget);
   506     QVERIFY(!subWidget->hasFocus());
   510     QTRY_VERIFY(!subWidget->hasFocus());
   507     QVERIFY(!otherSubWidget->hasFocus());
   511     QTRY_VERIFY(!otherSubWidget->hasFocus());
   508 
   512 
   509     widget->hide();
   513     widget->hide();
   510     QVERIFY(!widget->hasFocus()); // lose but still has subfocus
   514     QTRY_VERIFY(!widget->hasFocus()); // lose but still has subfocus
   511     QCOMPARE(focusInSpy.count(), 1);
   515     QCOMPARE(focusInSpy.count(), 1);
   512     QCOMPARE(focusOutSpy.count(), 1);
   516     QCOMPARE(focusOutSpy.count(), 1);
   513 
   517 
   514     widget->show();
   518     widget->show();
   515     QVERIFY(!widget->hasFocus()); // no regain
   519     QTRY_VERIFY(!widget->hasFocus()); // no regain
   516     QCOMPARE(focusInSpy.count(), 1);
   520     QCOMPARE(focusInSpy.count(), 1);
   517     QCOMPARE(focusOutSpy.count(), 1);
   521     QCOMPARE(focusOutSpy.count(), 1);
   518 
   522 
   519     widget->hide();
   523     widget->hide();
   520 
   524 
   521     // try to setup subFocus on item that can't take focus
   525     // try to setup subFocus on item that can't take focus
   522     subWidget->setFocus();
   526     subWidget->setFocus();
   523     QVERIFY(!subWidget->hasFocus());
   527     QTRY_VERIFY(!subWidget->hasFocus());
   524     QVERIFY(!scene.focusItem()); // but isn't the scene's focus item
   528     QVERIFY(!scene.focusItem()); // but isn't the scene's focus item
   525 
   529 
   526     // try to setup subFocus on item that can take focus
   530     // try to setup subFocus on item that can take focus
   527     otherSubWidget->setFocus();
   531     otherSubWidget->setFocus();
   528     QVERIFY(!otherSubWidget->hasFocus());
   532     QTRY_VERIFY(!otherSubWidget->hasFocus());
   529     QCOMPARE(widget->focusWidget(), otherSubWidget);
   533     QCOMPARE(widget->focusWidget(), otherSubWidget);
   530     QVERIFY(!scene.focusItem()); // but isn't the scene's focus item
   534     QVERIFY(!scene.focusItem()); // but isn't the scene's focus item
   531 
   535 
   532     widget->show();
   536     widget->show();
   533 
   537 
   534     QCOMPARE(scene.focusItem(), (QGraphicsItem *)otherSubWidget); // but isn't the scene's focus item
   538     QTRY_COMPARE(scene.focusItem(), (QGraphicsItem *)otherSubWidget); // but isn't the scene's focus item
   535     QCOMPARE(otherFocusInSpy.count(), 1);
   539     QCOMPARE(otherFocusInSpy.count(), 1);
   536     QCOMPARE(otherFocusOutSpy.count(), 0);
   540     QCOMPARE(otherFocusOutSpy.count(), 0);
   537 
   541 
   538     delete otherSubWidget;
   542     delete otherSubWidget;
   539 
   543 
   540     QCOMPARE(otherFocusOutSpy.count(), 1);
   544     QTRY_COMPARE(otherFocusOutSpy.count(), 1);
   541     QVERIFY(!scene.focusItem());
   545     QVERIFY(!scene.focusItem());
   542     QVERIFY(!widget->focusWidget());
   546     QVERIFY(!widget->focusWidget());
   543 }
   547 }
   544 
   548 
   545 Q_DECLARE_METATYPE(Qt::FocusPolicy)
   549 Q_DECLARE_METATYPE(Qt::FocusPolicy)
   574     QEvent windowActivate(QEvent::WindowActivate);
   578     QEvent windowActivate(QEvent::WindowActivate);
   575     qApp->sendEvent(&scene, &windowActivate);
   579     qApp->sendEvent(&scene, &windowActivate);
   576 
   580 
   577     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
   581     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
   578     scene.addItem(widget);
   582     scene.addItem(widget);
   579     QCOMPARE(Qt::NoFocus, widget->focusPolicy());
   583     QTRY_COMPARE(Qt::NoFocus, widget->focusPolicy());
   580 
   584 
   581     QFETCH(Qt::FocusPolicy, focusPolicy1);
   585     QFETCH(Qt::FocusPolicy, focusPolicy1);
   582     widget->setFocusPolicy(focusPolicy1);
   586     widget->setFocusPolicy(focusPolicy1);
   583     QCOMPARE(widget->focusPolicy(), focusPolicy1);
   587     QTRY_COMPARE(widget->focusPolicy(), focusPolicy1);
   584     bool isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
   588     bool isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
   585     bool wasFocusable = isFocusable;
   589     bool wasFocusable = isFocusable;
   586     QVERIFY(isFocusable == (focusPolicy1 != Qt::NoFocus));
   590     QTRY_VERIFY(isFocusable == (focusPolicy1 != Qt::NoFocus));
   587     widget->setFocus();
   591     widget->setFocus();
   588     QCOMPARE(widget->hasFocus(), isFocusable);
   592     QTRY_COMPARE(widget->hasFocus(), isFocusable);
   589 
   593 
   590     QFETCH(Qt::FocusPolicy, focusPolicy2);
   594     QFETCH(Qt::FocusPolicy, focusPolicy2);
   591     widget->setFocusPolicy(focusPolicy2);
   595     widget->setFocusPolicy(focusPolicy2);
   592     QCOMPARE(widget->focusPolicy(), focusPolicy2);
   596     QCOMPARE(widget->focusPolicy(), focusPolicy2);
   593     isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
   597     isFocusable = widget->flags() & QGraphicsItem::ItemIsFocusable;
   686     QCOMPARE(scene.font(), appFont);
   690     QCOMPARE(scene.font(), appFont);
   687     QCOMPARE(root->font(), appFont);
   691     QCOMPARE(root->font(), appFont);
   688 
   692 
   689     EventSpy rootSpyFont(root, QEvent::FontChange);
   693     EventSpy rootSpyFont(root, QEvent::FontChange);
   690     EventSpy rootSpyPolish(root, QEvent::Polish);
   694     EventSpy rootSpyPolish(root, QEvent::Polish);
   691     QCOMPARE(rootSpyFont.count(), 0);
   695     QTRY_COMPARE(rootSpyFont.count(), 0);
   692     QApplication::processEvents(); //The polish event is sent
   696     QTRY_COMPARE(rootSpyPolish.count(), 1);
   693     QCOMPARE(rootSpyPolish.count(), 1);
       
   694     QApplication::processEvents(); //Process events to see if we get the font change event
       
   695     //The font is still the same so no fontChangeEvent
   697     //The font is still the same so no fontChangeEvent
   696     QCOMPARE(rootSpyFont.count(), 0);
   698     QTRY_COMPARE(rootSpyFont.count(), 0);
   697 
   699 
   698     QFont font;
   700     QFont font;
   699     font.setPointSize(43);
   701     font.setPointSize(43);
   700     root->setFont(font);
   702     root->setFont(font);
   701     QApplication::processEvents(); //Process events to get the font change event
       
   702     //The font changed
   703     //The font changed
   703     QCOMPARE(rootSpyFont.count(), 1);
   704     QTRY_COMPARE(rootSpyFont.count(), 1);
   704 
   705 
   705     //then roll back to the default one.
   706     //then roll back to the default one.
   706     root->setFont(appFont);
   707     root->setFont(appFont);
   707     QApplication::processEvents(); //Process events to get the font change event
       
   708     //The font changed
   708     //The font changed
   709     QCOMPARE(rootSpyFont.count(), 2);
   709     QTRY_COMPARE(rootSpyFont.count(), 2);
   710 }
   710 }
   711 
   711 
   712 void tst_QGraphicsWidget::fontPropagationWidgetItemWidget()
   712 void tst_QGraphicsWidget::fontPropagationWidgetItemWidget()
   713 {
   713 {
   714     QGraphicsWidget *widget = new QGraphicsWidget;
   714     QGraphicsWidget *widget = new QGraphicsWidget;
   765 // QRectF geometry() const public
   765 // QRectF geometry() const public
   766 void tst_QGraphicsWidget::geometry()
   766 void tst_QGraphicsWidget::geometry()
   767 {
   767 {
   768     SubQGraphicsWidget widget;
   768     SubQGraphicsWidget widget;
   769     QCOMPARE(widget.geometry(), QRectF(widget.pos(), widget.size()));
   769     QCOMPARE(widget.geometry(), QRectF(widget.pos(), widget.size()));
   770 
   770     QSignalSpy spy(&widget, SIGNAL(geometryChanged()));
   771     QFETCH(QPointF, pos);
   771     QFETCH(QPointF, pos);
   772     QFETCH(QSizeF, size);
   772     QFETCH(QSizeF, size);
   773     widget.setPos(pos);
   773     widget.setPos(pos);
   774     widget.resize(size);
   774     widget.resize(size);
       
   775     if (!size.isNull())
       
   776         QCOMPARE(spy.count(), 1);
   775     QCOMPARE(widget.geometry(), QRectF(pos, size));
   777     QCOMPARE(widget.geometry(), QRectF(pos, size));
       
   778 }
       
   779 
       
   780 void tst_QGraphicsWidget::width()
       
   781 {
       
   782     QGraphicsWidget w;
       
   783     QCOMPARE(w.property("width").toReal(), qreal(0));
       
   784     QSignalSpy spy(&w, SIGNAL(widthChanged()));
       
   785     w.setProperty("width", qreal(50));
       
   786     QCOMPARE(w.property("width").toReal(), qreal(50));
       
   787     QCOMPARE(spy.count(), 1);
       
   788     //calling old school setGeometry should work too
       
   789     w.setGeometry(0, 0, 200, 200);
       
   790     QCOMPARE(spy.count(), 2);
       
   791 }
       
   792 
       
   793 void tst_QGraphicsWidget::height()
       
   794 {
       
   795     QGraphicsWidget w;
       
   796     QCOMPARE(w.property("height").toReal(), qreal(0));
       
   797     QSignalSpy spy(&w, SIGNAL(heightChanged()));
       
   798     w.setProperty("height", qreal(50));
       
   799     QCOMPARE(w.property("height").toReal(), qreal(50));
       
   800     QCOMPARE(spy.count(), 1);
       
   801     //calling old school setGeometry should work too
       
   802     w.setGeometry(0, 0, 200, 200);
       
   803     QCOMPARE(spy.count(), 2);
   776 }
   804 }
   777 
   805 
   778 void tst_QGraphicsWidget::getContentsMargins_data()
   806 void tst_QGraphicsWidget::getContentsMargins_data()
   779 {
   807 {
   780     QTest::addColumn<qreal>("left");
   808     QTest::addColumn<qreal>("left");
   838     view.show();
   866     view.show();
   839 #ifdef Q_WS_X11
   867 #ifdef Q_WS_X11
   840     qt_x11_wait_for_window_manager(&view);
   868     qt_x11_wait_for_window_manager(&view);
   841 #endif
   869 #endif
   842     QApplication::setActiveWindow(&view);
   870     QApplication::setActiveWindow(&view);
   843     QTest::qWait(25);
       
   844     QTRY_COMPARE(QApplication::activeWindow(), &view);
   871     QTRY_COMPARE(QApplication::activeWindow(), &view);
   845 
   872 
   846     view.setAlignment(Qt::AlignTop | Qt::AlignLeft);
   873     view.setAlignment(Qt::AlignTop | Qt::AlignLeft);
   847     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
   874     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
   848     widget->setAcceptsHoverEvents(true);
   875     widget->setAcceptsHoverEvents(true);
   863     QFETCH(bool, underMouse);
   890     QFETCH(bool, underMouse);
   864     if (underMouse) {
   891     if (underMouse) {
   865         view.resize(300, 300);
   892         view.resize(300, 300);
   866         view.show();
   893         view.show();
   867         QTest::qWaitForWindowShown(&view);
   894         QTest::qWaitForWindowShown(&view);
   868         QTest::qWait(20);
       
   869         sendMouseMove(view.viewport(), view.mapFromScene(widget->mapToScene(widget->boundingRect().center())));
   895         sendMouseMove(view.viewport(), view.mapFromScene(widget->mapToScene(widget->boundingRect().center())));
   870     }
   896     }
   871 
   897 
   872     QFETCH(QPalette, palette);
   898     QFETCH(QPalette, palette);
   873     widget->setPalette(palette);
   899     widget->setPalette(palette);
   916         layout->addItem(item);
   942         layout->addItem(item);
   917         children.append(item);
   943         children.append(item);
   918     }
   944     }
   919     widget.setLayout(layout);
   945     widget.setLayout(layout);
   920 
   946 
   921     QTest::qWait(25);
   947     QTRY_COMPARE(widget.layout(), static_cast<QGraphicsLayout*>(layout));
   922 
       
   923     QCOMPARE(widget.layout(), static_cast<QGraphicsLayout*>(layout));
       
   924     for (int i = 0; i < children.count(); ++i) {
   948     for (int i = 0; i < children.count(); ++i) {
   925         SubQGraphicsWidget *item = children[i];
   949         SubQGraphicsWidget *item = children[i];
   926         QCOMPARE(item->parentWidget(), (QGraphicsWidget *)&widget);
   950         QCOMPARE(item->parentWidget(), (QGraphicsWidget *)&widget);
   927         QVERIFY(item->geometry() != QRectF(0, 0, -1, -1));
   951         QVERIFY(item->geometry() != QRectF(0, 0, -1, -1));
   928     }
   952     }
   958     widget.setLayoutDirection(layoutDirection);
   982     widget.setLayoutDirection(layoutDirection);
   959     QCOMPARE(widget.testAttribute(Qt::WA_SetLayoutDirection), true);
   983     QCOMPARE(widget.testAttribute(Qt::WA_SetLayoutDirection), true);
   960     view->show();
   984     view->show();
   961     QTest::qWaitForWindowShown(view);
   985     QTest::qWaitForWindowShown(view);
   962     for (int i = 0; i < children.count(); ++i) {
   986     for (int i = 0; i < children.count(); ++i) {
   963         QCOMPARE(children[i]->layoutDirection(), layoutDirection);
   987         QTRY_COMPARE(children[i]->layoutDirection(), layoutDirection);
   964         QCOMPARE(children[i]->testAttribute(Qt::WA_SetLayoutDirection), false);
   988         QTRY_COMPARE(children[i]->testAttribute(Qt::WA_SetLayoutDirection), false);
   965         view->repaint();
   989         view->repaint();
   966         QApplication::processEvents();
       
   967         QTRY_COMPARE(children[i]->m_painterLayoutDirection, layoutDirection);
   990         QTRY_COMPARE(children[i]->m_painterLayoutDirection, layoutDirection);
   968     }
   991     }
   969     delete view;
   992     delete view;
   970 }
   993 }
   971 
   994 
  1174     view.show();
  1197     view.show();
  1175 #ifdef Q_WS_X11
  1198 #ifdef Q_WS_X11
  1176     qt_x11_wait_for_window_manager(&view);
  1199     qt_x11_wait_for_window_manager(&view);
  1177 #endif
  1200 #endif
  1178     QApplication::setActiveWindow(&view);
  1201     QApplication::setActiveWindow(&view);
  1179     QTest::qWait(25);
       
  1180     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1202     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1181 
  1203 
  1182     QGraphicsWidget *lastItem = 0;
  1204     QGraphicsWidget *lastItem = 0;
  1183     QTest::ignoreMessage(QtWarningMsg, "QGraphicsWidget::setTabOrder(0, 0) is undefined");
  1205     QTest::ignoreMessage(QtWarningMsg, "QGraphicsWidget::setTabOrder(0, 0) is undefined");
  1184     QGraphicsWidget::setTabOrder(0, 0);
  1206     QGraphicsWidget::setTabOrder(0, 0);
  1195     }
  1217     }
  1196 
  1218 
  1197     if (!children.isEmpty()) {
  1219     if (!children.isEmpty()) {
  1198         QGraphicsWidget *first = children.first();
  1220         QGraphicsWidget *first = children.first();
  1199         view.viewport()->setFocus();
  1221         view.viewport()->setFocus();
  1200         QApplication::processEvents();
       
  1201         QTRY_VERIFY(view.viewport()->hasFocus());
  1222         QTRY_VERIFY(view.viewport()->hasFocus());
  1202         first->setFocus();
  1223         first->setFocus();
  1203         QVERIFY(first->hasFocus());
  1224         QVERIFY(first->hasFocus());
  1204         QVERIFY(scene.hasFocus());
  1225         QVERIFY(scene.hasFocus());
  1205         QVERIFY(view.viewport()->hasFocus());
  1226         QVERIFY(view.viewport()->hasFocus());
  1456         window->show();
  1477         window->show();
  1457         QApplication::setActiveWindow(window);
  1478         QApplication::setActiveWindow(window);
  1458         QTest::qWaitForWindowShown(window);
  1479         QTest::qWaitForWindowShown(window);
  1459 
  1480 
  1460         lineEdit->setFocus();
  1481         lineEdit->setFocus();
  1461         QTest::qWait(25);
       
  1462         QTRY_VERIFY(lineEdit->hasFocus());
  1482         QTRY_VERIFY(lineEdit->hasFocus());
  1463         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1483         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1464         QTest::qWait(25);
       
  1465         QTRY_VERIFY(w1_1->hasFocus());
  1484         QTRY_VERIFY(w1_1->hasFocus());
  1466         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1485         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1467         QTest::qWait(25);
       
  1468         QTRY_VERIFY(w1_2->hasFocus());
  1486         QTRY_VERIFY(w1_2->hasFocus());
  1469 
  1487 
  1470         // remove the tabFocusFirst and insert new item
  1488         // remove the tabFocusFirst and insert new item
  1471         delete w1_1;            // calls _q_removeItemLater
  1489         delete w1_1;            // calls _q_removeItemLater
  1472         QTest::qWait(25);
       
  1473         SubQGraphicsWidget *w1_3 = new SubQGraphicsWidget;
  1490         SubQGraphicsWidget *w1_3 = new SubQGraphicsWidget;
  1474         w1_3->setFocusPolicy(Qt::StrongFocus);
  1491         w1_3->setFocusPolicy(Qt::StrongFocus);
  1475         w1_3->setData(0, "w1_3");
  1492         w1_3->setData(0, "w1_3");
  1476         w1_3->setGeometry(50,0,25, 25);
  1493         w1_3->setGeometry(50,0,25, 25);
  1477         scene.addItem(w1_3);
  1494         scene.addItem(w1_3);
  1478         QTRY_VERIFY(w1_2->hasFocus());
  1495         QTRY_VERIFY(w1_2->hasFocus());
  1479         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
  1496         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
  1480         QTest::qWait(25);
       
  1481         QTRY_VERIFY(lineEdit->hasFocus());
  1497         QTRY_VERIFY(lineEdit->hasFocus());
  1482         // tabFocusFirst should now point to w1_2
  1498         // tabFocusFirst should now point to w1_2
  1483         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1499         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1484         QTest::qWait(25);
       
  1485         QTRY_VERIFY(w1_2->hasFocus());
  1500         QTRY_VERIFY(w1_2->hasFocus());
  1486         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1501         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1487         QTest::qWait(25);
       
  1488         QTRY_VERIFY(w1_3->hasFocus());
  1502         QTRY_VERIFY(w1_3->hasFocus());
  1489         scene.removeItem(w1_2);   // does not call _q_removeItemLater
  1503         scene.removeItem(w1_2);   // does not call _q_removeItemLater
  1490         delete w1_2;            // calls _q_removeItemLater
  1504         delete w1_2;            // calls _q_removeItemLater
  1491 
  1505 
  1492         SubQGraphicsWidget *w1_4 = new SubQGraphicsWidget;
  1506         SubQGraphicsWidget *w1_4 = new SubQGraphicsWidget;
  1493         w1_4->setFocusPolicy(Qt::StrongFocus);
  1507         w1_4->setFocusPolicy(Qt::StrongFocus);
  1494         w1_4->setData(0, "w1_4");
  1508         w1_4->setData(0, "w1_4");
  1495         w1_4->setGeometry(75,0,25, 25);
  1509         w1_4->setGeometry(75,0,25, 25);
  1496         scene.addItem(w1_4);
  1510         scene.addItem(w1_4);
  1497         QTRY_VERIFY(w1_3->hasFocus());
  1511         QTRY_VERIFY(w1_3->hasFocus());
  1498         QTest::qWait(25);
       
  1499         QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
  1512         QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
  1500         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
  1513         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Backtab);
  1501         QTest::qWait(25);
       
  1502         QTRY_VERIFY(lineEdit->hasFocus());
  1514         QTRY_VERIFY(lineEdit->hasFocus());
  1503         // tabFocusFirst should now point to w1_3
  1515         // tabFocusFirst should now point to w1_3
  1504         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1516         QTest::keyPress(QApplication::focusWidget(), Qt::Key_Tab);
  1505         QTest::qWait(25);
       
  1506         QTRY_VERIFY(w1_3->hasFocus());
  1517         QTRY_VERIFY(w1_3->hasFocus());
  1507         QTest::qWait(25);
       
  1508         QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
  1518         QTRY_VERIFY(compareFocusChain(view, QList<QGraphicsItem*>() << w1_3 << w1_4));
  1509         delete window;
  1519         delete window;
  1510     }
  1520     }
  1511 }
  1521 }
  1512 
  1522 
  1517     view.show();
  1527     view.show();
  1518 #ifdef Q_WS_X11
  1528 #ifdef Q_WS_X11
  1519     qt_x11_wait_for_window_manager(&view);
  1529     qt_x11_wait_for_window_manager(&view);
  1520 #endif
  1530 #endif
  1521     QApplication::setActiveWindow(&view);
  1531     QApplication::setActiveWindow(&view);
  1522     QTest::qWait(25);
       
  1523     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1532     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1524 
  1533 
  1525     // delete item in focus chain with no focus and verify chain
  1534     // delete item in focus chain with no focus and verify chain
  1526     SubQGraphicsWidget *parent = new SubQGraphicsWidget(0, Qt::Window);
  1535     SubQGraphicsWidget *parent = new SubQGraphicsWidget(0, Qt::Window);
  1527     SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
  1536     SubQGraphicsWidget *w = new SubQGraphicsWidget(0, Qt::Window);
  1751 
  1760 
  1752     widget->setMinimumSize(min);
  1761     widget->setMinimumSize(min);
  1753     widget->setPreferredSize(pref);
  1762     widget->setPreferredSize(pref);
  1754     widget->setMaximumSize(max);
  1763     widget->setMaximumSize(max);
  1755 
  1764 
  1756     QApplication::processEvents();
       
  1757 
       
  1758     for (i = 0; i < compareInstructions.count(); ++i) {
  1765     for (i = 0; i < compareInstructions.count(); ++i) {
  1759         Inst input = compareInstructions.at(i);
  1766         Inst input = compareInstructions.at(i);
  1760         switch (input.first) {
  1767         switch (input.first) {
  1761             case MinimumSize:
  1768             case MinimumSize:
  1762                 QCOMPARE(widget->minimumSize(), input.second.toSizeF());
  1769                 QTRY_COMPARE(widget->minimumSize(), input.second.toSizeF());
  1763                 break;
  1770                 break;
  1764             case PreferredSize:
  1771             case PreferredSize:
  1765                 QCOMPARE(widget->preferredSize(), input.second.toSizeF());
  1772                 QTRY_COMPARE(widget->preferredSize(), input.second.toSizeF());
  1766                 break;
  1773                 break;
  1767             case MaximumSize:
  1774             case MaximumSize:
  1768                 QCOMPARE(widget->maximumSize(), input.second.toSizeF());
  1775                 QTRY_COMPARE(widget->maximumSize(), input.second.toSizeF());
  1769                 break;
  1776                 break;
  1770             case Size:
  1777             case Size:
  1771                 QCOMPARE(widget->size(), input.second.toSizeF());
  1778                 QTRY_COMPARE(widget->size(), input.second.toSizeF());
  1772                 break;
  1779                 break;
  1773             case MinimumWidth:
  1780             case MinimumWidth:
  1774                 QCOMPARE(widget->minimumWidth(), qreal(input.second.toDouble()));
  1781                 QTRY_COMPARE(widget->minimumWidth(), qreal(input.second.toDouble()));
  1775                 break;
  1782                 break;
  1776             case PreferredWidth:
  1783             case PreferredWidth:
  1777                 QCOMPARE(widget->preferredWidth(), qreal(input.second.toDouble()));
  1784                 QTRY_COMPARE(widget->preferredWidth(), qreal(input.second.toDouble()));
  1778                 break;
  1785                 break;
  1779             case MaximumWidth:
  1786             case MaximumWidth:
  1780                 QCOMPARE(widget->maximumWidth(), qreal(input.second.toDouble()));
  1787                 QTRY_COMPARE(widget->maximumWidth(), qreal(input.second.toDouble()));
  1781                 break;
  1788                 break;
  1782             default:
  1789             default:
  1783                 qWarning("instruction not implemented");
  1790                 qWarning("instruction not implemented");
  1784                 break;
  1791                 break;
  1785         }
  1792         }
  1830     QGraphicsView view(&scene);
  1837     QGraphicsView view(&scene);
  1831     view.show();
  1838     view.show();
  1832 #ifdef Q_WS_X11
  1839 #ifdef Q_WS_X11
  1833     qt_x11_wait_for_window_manager(&view);
  1840     qt_x11_wait_for_window_manager(&view);
  1834 #endif
  1841 #endif
  1835     QTest::qWait(100);
  1842 
  1836 
  1843     QTRY_VERIFY(!scene.itemAt(25, 25));
  1837     QVERIFY(!scene.itemAt(25, 25));
       
  1838     widget->setGeometry(0, 112, 360, 528);
  1844     widget->setGeometry(0, 112, 360, 528);
  1839     QCOMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
  1845     QTRY_COMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
  1840     widget2->setGeometry(0, 573, 360, 67);
  1846     widget2->setGeometry(0, 573, 360, 67);
  1841     QCOMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
  1847     QTRY_COMPARE(scene.itemAt(15, 120), (QGraphicsItem *)widget);
  1842     QCOMPARE(scene.itemAt(50, 585), (QGraphicsItem *)widget2);
  1848     QTRY_COMPARE(scene.itemAt(50, 585), (QGraphicsItem *)widget2);
  1843 }
  1849 }
  1844 
  1850 
  1845 void tst_QGraphicsWidget::defaultSize()
  1851 void tst_QGraphicsWidget::defaultSize()
  1846 {
  1852 {
  1847     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
  1853     SubQGraphicsWidget *widget = new SubQGraphicsWidget;
  1852     QGraphicsView view(&scene);
  1858     QGraphicsView view(&scene);
  1853     view.show();
  1859     view.show();
  1854 #ifdef Q_WS_X11
  1860 #ifdef Q_WS_X11
  1855     qt_x11_wait_for_window_manager(&view);
  1861     qt_x11_wait_for_window_manager(&view);
  1856 #endif
  1862 #endif
  1857     QTest::qWait(50);
       
  1858     QSizeF initialSize = widget->size();
  1863     QSizeF initialSize = widget->size();
  1859 
  1864 
  1860     widget->resize(initialSize);
  1865     widget->resize(initialSize);
  1861     QCOMPARE(widget->geometry().size(), initialSize);
  1866     QCOMPARE(widget->geometry().size(), initialSize);
  1862     widget->setVisible(false);
  1867     widget->setVisible(false);
  1863     widget->setMinimumSize(10, 10);
  1868     widget->setMinimumSize(10, 10);
  1864     widget->setPreferredSize(60, 60);
  1869     widget->setPreferredSize(60, 60);
  1865     widget->setMaximumSize(110, 110);
  1870     widget->setMaximumSize(110, 110);
  1866     widget->setVisible(true);
  1871     widget->setVisible(true);
  1867     QTest::qWait(50);
       
  1868     // should still have its size set to initialsize
  1872     // should still have its size set to initialsize
  1869     QCOMPARE(widget->geometry().size(), initialSize);
  1873     QTRY_COMPARE(widget->geometry().size(), initialSize);
  1870 
  1874 
  1871 }
  1875 }
  1872 
  1876 
  1873 void tst_QGraphicsWidget::explicitMouseGrabber()
  1877 void tst_QGraphicsWidget::explicitMouseGrabber()
  1874 {
  1878 {
  2386     QGraphicsScene scene(0, 0, 300, 300);
  2390     QGraphicsScene scene(0, 0, 300, 300);
  2387     QGraphicsView view(&scene);
  2391     QGraphicsView view(&scene);
  2388     scene.addItem(widget);
  2392     scene.addItem(widget);
  2389     view.show();
  2393     view.show();
  2390     QTest::qWaitForWindowShown(&view);
  2394     QTest::qWaitForWindowShown(&view);
  2391     QTest::qWait(500);
       
  2392 }
  2395 }
  2393 
  2396 
  2394 class ProxyStyle : public QCommonStyle
  2397 class ProxyStyle : public QCommonStyle
  2395 {
  2398 {
  2396 public:
  2399 public:
  2475     QGraphicsScene scene;
  2478     QGraphicsScene scene;
  2476     QGraphicsView view;
  2479     QGraphicsView view;
  2477     view.setScene(&scene);
  2480     view.setScene(&scene);
  2478     view.show();
  2481     view.show();
  2479     QApplication::setActiveWindow(&view);
  2482     QApplication::setActiveWindow(&view);
  2480     QTest::qWait(25);
       
  2481     QTRY_COMPARE(QApplication::activeWindow(), &view);
  2483     QTRY_COMPARE(QApplication::activeWindow(), &view);
  2482 
  2484 
  2483 
  2485 
  2484     // *** Event: ***
  2486     // *** Event: ***
  2485 
  2487 
  2730     view->show();
  2732     view->show();
  2731     window->setGeometry(0, 0, 70, 70);
  2733     window->setGeometry(0, 0, 70, 70);
  2732     QTest::qWaitForWindowShown(view);
  2734     QTest::qWaitForWindowShown(view);
  2733 
  2735 
  2734     {   // here we go - simulate a interactive resize of the window
  2736     {   // here we go - simulate a interactive resize of the window
  2735         QTest::qWait(100);
       
  2736         QTest::mouseMove(view, view->mapFromScene(71, 71)); // bottom right corner
  2737         QTest::mouseMove(view, view->mapFromScene(71, 71)); // bottom right corner
  2737         QTest::qWait(100);
       
  2738 
  2738 
  2739         QTest::mousePress(view->viewport(), Qt::LeftButton, 0, view->mapFromScene(71, 71), 200);
  2739         QTest::mousePress(view->viewport(), Qt::LeftButton, 0, view->mapFromScene(71, 71), 200);
  2740         view->grabMouse();
  2740         view->grabMouse();
  2741         // move both mouse cursor and set correct event in order to emulate resize
  2741         // move both mouse cursor and set correct event in order to emulate resize
  2742         QTest::mouseMove(view->viewport(), view->mapFromScene(60, 30), 200);
  2742         QTest::mouseMove(view->viewport(), view->mapFromScene(60, 30), 200);
  2746                       Qt::LeftButton,
  2746                       Qt::LeftButton,
  2747                       Qt::NoModifier);
  2747                       Qt::NoModifier);
  2748         QApplication::sendEvent(view->viewport(), &e);
  2748         QApplication::sendEvent(view->viewport(), &e);
  2749         view->releaseMouse();
  2749         view->releaseMouse();
  2750     }
  2750     }
  2751     QTest::qWait(100);
       
  2752     const QSizeF winSize = window->size();
  2751     const QSizeF winSize = window->size();
  2753     qreal minHFW = window->effectiveSizeHint(Qt::MinimumSize, QSizeF(winSize.width(), -1)).height();
  2752     qreal minHFW = window->effectiveSizeHint(Qt::MinimumSize, QSizeF(winSize.width(), -1)).height();
  2754     QVERIFY(qAbs(minHFW - winSize.height()) < 1);
  2753     QTRY_VERIFY(qAbs(minHFW - winSize.height()) < 1);
  2755 #endif
  2754 #endif
  2756 }
  2755 }
  2757 
  2756 
  2758 class PolishWidget : public QGraphicsWidget
  2757 class PolishWidget : public QGraphicsWidget
  2759 {
  2758 {
  2801 
  2800 
  2802     QGraphicsView view(&scene);
  2801     QGraphicsView view(&scene);
  2803     view.resize(200, 200);
  2802     view.resize(200, 200);
  2804     view.show();
  2803     view.show();
  2805     QTest::qWaitForWindowShown(&view);
  2804     QTest::qWaitForWindowShown(&view);
  2806     QCOMPARE(PolishWidget::numberOfPolish, 2);
  2805     QTRY_COMPARE(PolishWidget::numberOfPolish, 2);
  2807 }
  2806 }
  2808 
  2807 
  2809 void tst_QGraphicsWidget::polishEvent()
  2808 void tst_QGraphicsWidget::polishEvent()
  2810 {
  2809 {
  2811     class MyGraphicsWidget : public QGraphicsWidget
  2810     class MyGraphicsWidget : public QGraphicsWidget
  2848     widget->hide();
  2847     widget->hide();
  2849     scene.addItem(widget);
  2848     scene.addItem(widget);
  2850 
  2849 
  2851     widget->events.clear();
  2850     widget->events.clear();
  2852 
  2851 
  2853     QApplication::processEvents();
       
  2854 
       
  2855     // Make sure the item got polish event.
  2852     // Make sure the item got polish event.
  2856     QVERIFY(widget->events.contains(QEvent::Polish));
  2853     QTRY_VERIFY(widget->events.contains(QEvent::Polish));
       
  2854 }
       
  2855 
       
  2856 void tst_QGraphicsWidget::initialShow()
       
  2857 {
       
  2858     class MyGraphicsWidget : public QGraphicsWidget
       
  2859     { public:
       
  2860         MyGraphicsWidget() : repaints(0) {}
       
  2861         int repaints;
       
  2862         void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget*) { ++repaints; }
       
  2863         void polishEvent() { update(); }
       
  2864     };
       
  2865 
       
  2866     QGraphicsScene scene;
       
  2867     MyGraphicsWidget *widget = new MyGraphicsWidget;
       
  2868 
       
  2869     QGraphicsView view(&scene);
       
  2870     view.show();
       
  2871     QTest::qWaitForWindowShown(&view);
       
  2872 
       
  2873     scene.addItem(widget);
       
  2874 
       
  2875     QTRY_COMPARE(widget->repaints, 1);
       
  2876 }
       
  2877 
       
  2878 void tst_QGraphicsWidget::initialShow2()
       
  2879 {
       
  2880     class MyGraphicsWidget : public QGraphicsWidget
       
  2881     { public:
       
  2882         MyGraphicsWidget() : repaints(0) {}
       
  2883         int repaints;
       
  2884         void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget*) { ++repaints; }
       
  2885         void polishEvent() { update(); }
       
  2886     };
       
  2887 
       
  2888     // Don't let paint events triggered by the windowing system
       
  2889     // influence our test case. We're only interested in knowing
       
  2890     // whether a QGraphicsWidget generates an additional repaint
       
  2891     // on the inital show. Hence create a dummy scenario to find out
       
  2892     // how many repaints we should expect.
       
  2893     QGraphicsScene dummyScene(0, 0, 200, 200);
       
  2894     dummyScene.addItem(new QGraphicsRectItem(0, 0, 100, 100));
       
  2895 
       
  2896     QGraphicsView *dummyView = new QGraphicsView(&dummyScene);
       
  2897     dummyView->setWindowFlags(Qt::X11BypassWindowManagerHint);
       
  2898     EventSpy paintSpy(dummyView->viewport(), QEvent::Paint);
       
  2899     dummyView->show();
       
  2900     QTest::qWaitForWindowShown(dummyView);
       
  2901     const int expectedRepaintCount = paintSpy.count();
       
  2902     delete dummyView;
       
  2903     dummyView = 0;
       
  2904 
       
  2905     MyGraphicsWidget *widget = new MyGraphicsWidget;
       
  2906     widget->resize(100, 100);
       
  2907 
       
  2908     QGraphicsScene scene(0, 0, 200, 200);
       
  2909     scene.addItem(widget);
       
  2910 
       
  2911     QGraphicsView view(&scene);
       
  2912     view.setWindowFlags(Qt::X11BypassWindowManagerHint);
       
  2913     view.show();
       
  2914     QTest::qWaitForWindowShown(&view);
       
  2915 
       
  2916     QTRY_COMPARE(widget->repaints, expectedRepaintCount);
  2857 }
  2917 }
  2858 
  2918 
  2859 void tst_QGraphicsWidget::QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems()
  2919 void tst_QGraphicsWidget::QT_BUG_6544_tabFocusFirstUnsetWhenRemovingItems()
  2860 {
  2920 {
  2861     QGraphicsScene scene;
  2921     QGraphicsScene scene;