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