tests/auto/qgraphicsproxywidget/tst_qgraphicsproxywidget.cpp
changeset 22 79de32ba3296
parent 19 fcece45ef507
child 30 5dc02b23752f
equal deleted inserted replaced
19:fcece45ef507 22:79de32ba3296
    41 
    41 
    42 
    42 
    43 #include <QtTest/QtTest>
    43 #include <QtTest/QtTest>
    44 #include <QtGui>
    44 #include <QtGui>
    45 #include "../../shared/util.h"
    45 #include "../../shared/util.h"
       
    46 #include <private/qgraphicsproxywidget_p.h>
    46 #include <private/qlayoutengine_p.h>    // qSmartMin functions...
    47 #include <private/qlayoutengine_p.h>    // qSmartMin functions...
    47 #if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
    48 #if defined(Q_WS_MAC) && !defined(QT_NO_STYLE_MAC)
    48 #include <QMacStyle>
    49 #include <QMacStyle>
    49 #endif
    50 #endif
    50 #ifdef Q_WS_X11
    51 #ifdef Q_WS_X11
   785     QGraphicsScene scene;
   786     QGraphicsScene scene;
   786     QGraphicsView view(&scene);
   787     QGraphicsView view(&scene);
   787     view.show();
   788     view.show();
   788     QApplication::setActiveWindow(&view);
   789     QApplication::setActiveWindow(&view);
   789     QTest::qWaitForWindowShown(&view);
   790     QTest::qWaitForWindowShown(&view);
   790     QApplication::processEvents();
       
   791     QTRY_COMPARE(QApplication::activeWindow(), &view);
   791     QTRY_COMPARE(QApplication::activeWindow(), &view);
   792     if (hasScene) {
   792     if (hasScene) {
   793         scene.addItem(proxy);
   793         scene.addItem(proxy);
   794         proxy->show();
   794         proxy->show();
   795 
   795 
   834     view.show();
   834     view.show();
   835     QApplication::setActiveWindow(&view);
   835     QApplication::setActiveWindow(&view);
   836     view.activateWindow();
   836     view.activateWindow();
   837     view.setFocus();
   837     view.setFocus();
   838     QTest::qWaitForWindowShown(&view);
   838     QTest::qWaitForWindowShown(&view);
   839     QApplication::processEvents();
       
   840     QTRY_VERIFY(view.isVisible());
   839     QTRY_VERIFY(view.isVisible());
   841     QTRY_COMPARE(QApplication::activeWindow(), &view);
   840     QTRY_COMPARE(QApplication::activeWindow(), &view);
   842 
   841 
   843     QWidget *widget = new QWidget;
   842     QWidget *widget = new QWidget;
   844     widget->setFocusPolicy(Qt::WheelFocus);
   843     widget->setFocusPolicy(Qt::WheelFocus);
   987     QTest::mouseMove(&view, QPoint(10, 10));
   986     QTest::mouseMove(&view, QPoint(10, 10));
   988     QTest::qWait(30);
   987     QTest::qWait(30);
   989 
   988 
   990     // in
   989     // in
   991     QTest::mouseMove(&view, QPoint(50, 50));
   990     QTest::mouseMove(&view, QPoint(50, 50));
   992     QTest::qWait(25);
       
   993     QTRY_COMPARE(widget->testAttribute(Qt::WA_UnderMouse), hasWidget ? true : false);
   991     QTRY_COMPARE(widget->testAttribute(Qt::WA_UnderMouse), hasWidget ? true : false);
   994     // ### this attribute isn't supported
   992     // ### this attribute isn't supported
   995     QCOMPARE(widget->enterCount, hasWidget ? 1 : 0);
   993     QCOMPARE(widget->enterCount, hasWidget ? 1 : 0);
   996     QCOMPARE(widget->hoverEnter, (hasWidget && hoverEnabled) ? 1 : 0);
   994     QCOMPARE(widget->hoverEnter, (hasWidget && hoverEnabled) ? 1 : 0);
   997     // does not work on all platforms
   995     // does not work on all platforms
   998     //QCOMPARE(widget->moveCount, 0);
   996     //QCOMPARE(widget->moveCount, 0);
   999 
   997 
  1000     // out
   998     // out
  1001     QTest::mouseMove(&view, QPoint(10, 10));
   999     QTest::mouseMove(&view, QPoint(10, 10));
  1002     QTest::qWait(25);
       
  1003     // QTRY_COMPARE(widget->testAttribute(Qt::WA_UnderMouse), false);
  1000     // QTRY_COMPARE(widget->testAttribute(Qt::WA_UnderMouse), false);
  1004     // ### this attribute isn't supported
  1001     // ### this attribute isn't supported
  1005     QCOMPARE(widget->leaveCount, hasWidget ? 1 : 0);
  1002     QTRY_COMPARE(widget->leaveCount, hasWidget ? 1 : 0);
  1006     QCOMPARE(widget->hoverLeave, (hasWidget && hoverEnabled) ? 1 : 0);
  1003     QTRY_COMPARE(widget->hoverLeave, (hasWidget && hoverEnabled) ? 1 : 0);
  1007     // does not work on all platforms
  1004     // does not work on all platforms
  1008     //QCOMPARE(widget->moveCount, 0);
  1005     //QCOMPARE(widget->moveCount, 0);
  1009 
  1006 
  1010     if (!hasWidget)
  1007     if (!hasWidget)
  1011         delete widget;
  1008         delete widget;
  1069     if (mouseDown)
  1066     if (mouseDown)
  1070         QTest::mousePress(view.viewport(), Qt::LeftButton);
  1067         QTest::mousePress(view.viewport(), Qt::LeftButton);
  1071 
  1068 
  1072     // move a little bit
  1069     // move a little bit
  1073     QTest::mouseMove(&view, QPoint(60, 60));
  1070     QTest::mouseMove(&view, QPoint(60, 60));
  1074     QTest::qWait(12);
       
  1075     QTRY_COMPARE(widget->hoverEnter, (hasWidget && hoverEnabled) ? 1 : 0);
  1071     QTRY_COMPARE(widget->hoverEnter, (hasWidget && hoverEnabled) ? 1 : 0);
  1076     QCOMPARE(widget->moveCount, (hasWidget && mouseTracking) || (hasWidget && mouseDown) ? 1 : 0);
  1072     QCOMPARE(widget->moveCount, (hasWidget && mouseTracking) || (hasWidget && mouseDown) ? 1 : 0);
  1077 
  1073 
  1078     if (!hasWidget)
  1074     if (!hasWidget)
  1079         delete widget;
  1075         delete widget;
  1095     QGraphicsView view(&scene);
  1091     QGraphicsView view(&scene);
  1096     view.show();
  1092     view.show();
  1097     view.viewport()->setFocus();
  1093     view.viewport()->setFocus();
  1098     QApplication::setActiveWindow(&view);
  1094     QApplication::setActiveWindow(&view);
  1099     QTest::qWaitForWindowShown(&view);
  1095     QTest::qWaitForWindowShown(&view);
  1100     QApplication::processEvents();
       
  1101     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1096     QTRY_COMPARE(QApplication::activeWindow(), &view);
  1102 
  1097 
  1103     SubQGraphicsProxyWidget *proxy = new SubQGraphicsProxyWidget;
  1098     SubQGraphicsProxyWidget *proxy = new SubQGraphicsProxyWidget;
  1104     proxy->setFlag(QGraphicsItem::ItemIsFocusable, true); // ### remove me!!!
  1099     proxy->setFlag(QGraphicsItem::ItemIsFocusable, true); // ### remove me!!!
  1105 
  1100 
  1303     QTest::qWait(30);
  1298     QTest::qWait(30);
  1304     proxy.setWidget(w);
  1299     proxy.setWidget(w);
  1305     scene.addItem(&proxy);
  1300     scene.addItem(&proxy);
  1306 
  1301 
  1307     //make sure we flush all the paint events
  1302     //make sure we flush all the paint events
  1308     QTest::qWait(30);
       
  1309     QTRY_VERIFY(proxy.paintCount > 1);
  1303     QTRY_VERIFY(proxy.paintCount > 1);
  1310     QTest::qWait(30);
  1304     QTest::qWait(30);
  1311     proxy.paintCount = 0;
  1305     proxy.paintCount = 0;
  1312 
  1306 
  1313     w->update();
  1307     w->update();
  1314     QTest::qWait(30);
       
  1315     QTRY_COMPARE(proxy.paintCount, 1); //the widget should have been painted now
  1308     QTRY_COMPARE(proxy.paintCount, 1); //the widget should have been painted now
  1316 }
  1309 }
  1317 
  1310 
  1318 
  1311 
  1319 void tst_QGraphicsProxyWidget::wheelEvent()
  1312 void tst_QGraphicsProxyWidget::wheelEvent()
  1502     widget->paintEventRegion = QRegion();
  1495     widget->paintEventRegion = QRegion();
  1503     widget->npaints = 0;
  1496     widget->npaints = 0;
  1504     view.paintEventRegion = QRegion();
  1497     view.paintEventRegion = QRegion();
  1505     view.npaints = 0;
  1498     view.npaints = 0;
  1506     QTimer::singleShot(0, widget, SLOT(updateScroll()));
  1499     QTimer::singleShot(0, widget, SLOT(updateScroll()));
  1507     QTest::qWait(50);
       
  1508     QTRY_COMPARE(view.npaints, 2);
  1500     QTRY_COMPARE(view.npaints, 2);
  1509     // QRect(0, 0, 200, 12) is the first update, expanded (-2, -2, 2, 2)
  1501     // QRect(0, 0, 200, 12) is the first update, expanded (-2, -2, 2, 2)
  1510     // QRect(0, 12, 102, 10) is the scroll update, expanded (-2, -2, 2, 2),
  1502     // QRect(0, 12, 102, 10) is the scroll update, expanded (-2, -2, 2, 2),
  1511     // intersected with the above update.
  1503     // intersected with the above update.
  1512     QCOMPARE(view.paintEventRegion.rects(),
  1504     QCOMPARE(view.paintEventRegion.rects(),
  2481     QApplication::processEvents();
  2473     QApplication::processEvents();
  2482 
  2474 
  2483     QTest::mousePress(view.viewport(), Qt::LeftButton, 0,
  2475     QTest::mousePress(view.viewport(), Qt::LeftButton, 0,
  2484 		      view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2476 		      view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2485 
  2477 
  2486     QTest::qWait(12);
  2478     QTRY_COMPARE(box->pos(), QPoint());
  2487 
       
  2488     QCOMPARE(box->pos(), QPoint());
       
  2489 
  2479 
  2490     QCOMPARE(proxy->childItems().count(), 1);
  2480     QCOMPARE(proxy->childItems().count(), 1);
  2491     QGraphicsProxyWidget *child = (QGraphicsProxyWidget*)(proxy->childItems())[0];
  2481     QGraphicsProxyWidget *child = (QGraphicsProxyWidget*)(proxy->childItems())[0];
  2492     QVERIFY(child->isWidget());
  2482     QVERIFY(child->isWidget());
  2493     QVERIFY(child->widget());
  2483     QVERIFY(child->widget());
  2496     opt.editable = box->isEditable();
  2486     opt.editable = box->isEditable();
  2497     if (box->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt))
  2487     if (box->style()->styleHint(QStyle::SH_ComboBox_Popup, &opt))
  2498         QSKIP("Does not work due to SH_Combobox_Popup", SkipAll);
  2488         QSKIP("Does not work due to SH_Combobox_Popup", SkipAll);
  2499     QCOMPARE(child->widget()->parent(), static_cast<QObject*>(box));
  2489     QCOMPARE(child->widget()->parent(), static_cast<QObject*>(box));
  2500 
  2490 
  2501     QTest::qWait(12);
       
  2502     QTRY_COMPARE(proxy->pos(), QPointF(box->pos()));
  2491     QTRY_COMPARE(proxy->pos(), QPointF(box->pos()));
  2503     QCOMPARE(child->x(), qreal(box->x()));
  2492     QCOMPARE(child->x(), qreal(box->x()));
  2504     QCOMPARE(child->y(), qreal(box->rect().bottom()));
  2493     QCOMPARE(child->y(), qreal(box->rect().bottom()));
  2505 #ifndef Q_OS_WIN
  2494 #ifndef Q_OS_WIN
  2506     // The popup's coordinates on Windows are in global space,
  2495     // The popup's coordinates on Windows are in global space,
  2581     QTRY_VERIFY(view.isActiveWindow());
  2570     QTRY_VERIFY(view.isActiveWindow());
  2582 
  2571 
  2583     // in
  2572     // in
  2584     QTest::mouseMove(view.viewport(), view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2573     QTest::mouseMove(view.viewport(), view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2585     sendMouseMove(view.viewport(), view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2574     sendMouseMove(view.viewport(), view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2586     QTest::qWait(12);
       
  2587     QTRY_COMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor);
  2575     QTRY_COMPARE(view.viewport()->cursor().shape(), Qt::IBeamCursor);
  2588 
  2576 
  2589     // out
  2577     // out
  2590     QTest::mouseMove(view.viewport(), QPoint(1, 1));
  2578     QTest::mouseMove(view.viewport(), QPoint(1, 1));
  2591     sendMouseMove(view.viewport(), QPoint(1, 1));
  2579     sendMouseMove(view.viewport(), QPoint(1, 1));
  2592     QTest::qWait(12);
       
  2593     QTRY_COMPARE(view.viewport()->cursor().shape(), Qt::ArrowCursor);
  2580     QTRY_COMPARE(view.viewport()->cursor().shape(), Qt::ArrowCursor);
  2594 #endif
  2581 #endif
  2595 }
  2582 }
  2596 
  2583 
  2597 void tst_QGraphicsProxyWidget::tooltip_basic()
  2584 void tst_QGraphicsProxyWidget::tooltip_basic()
  2598 {
  2585 {
  2599     // Confirm that mouse events are working properly by checking that
  2586     QString toolTip = "Qt rocks!";
  2600     // when moving the mouse over a label with a tooptip the tooltip appears
  2587     QString toolTip2 = "Qt rocks even more!";
  2601     QGraphicsScene scene;
  2588 
       
  2589     QPushButton *button = new QPushButton("button");
       
  2590     QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget;
       
  2591     QGraphicsProxyWidgetPrivate *proxyd = static_cast<QGraphicsProxyWidgetPrivate *>(QGraphicsItemPrivate::get(proxy));
       
  2592     proxy->setWidget(button);
       
  2593     proxyd->lastWidgetUnderMouse = button; // force widget under mouse
       
  2594 
       
  2595     QVERIFY(button->toolTip().isEmpty());
       
  2596     QVERIFY(proxy->toolTip().isEmpty());
       
  2597     // Check that setting the tooltip on the proxy also set it on the widget
       
  2598     proxy->setToolTip(toolTip);
       
  2599     QCOMPARE(proxy->toolTip(), toolTip);
       
  2600     QCOMPARE(button->toolTip(), toolTip);
       
  2601     // Check that setting the tooltip on the widget also set it on the proxy
       
  2602     button->setToolTip(toolTip2);
       
  2603     QCOMPARE(proxy->toolTip(), toolTip2);
       
  2604     QCOMPARE(button->toolTip(), toolTip2);
       
  2605 
       
  2606     QGraphicsScene scene;
       
  2607     scene.addItem(proxy);
       
  2608 
  2602     QGraphicsView view(&scene);
  2609     QGraphicsView view(&scene);
       
  2610     view.setFixedSize(200, 200);
  2603     view.show();
  2611     view.show();
  2604 
  2612     QTest::qWaitForWindowShown(&view);
  2605     QSKIP("Tooltips don't work yet", SkipAll);
  2613     {
  2606 
  2614         QHelpEvent helpEvent(QEvent::ToolTip, view.viewport()->rect().topLeft(),
  2607     SubQGraphicsProxyWidget *proxy = new SubQGraphicsProxyWidget;
  2615                              view.viewport()->mapToGlobal(view.viewport()->rect().topLeft()));
  2608     QLabel *widget = new QLabel;
  2616         QApplication::sendEvent(view.viewport(), &helpEvent);
  2609     widget->setText("If it ain't tested it's broken");
  2617         QTest::qWait(350);
  2610     widget->setToolTip("When in doubt, test");
  2618 
  2611     proxy->setWidget(widget);
  2619         bool foundView = false;
  2612     widget->show();
  2620         bool foundTipLabel = false;
  2613     scene.addItem(proxy);
  2621         foreach (QWidget *widget, QApplication::topLevelWidgets()) {
  2614     QTest::qWait(125);
  2622             if (widget == &view)
  2615 
  2623                 foundView = true;
  2616     // in
  2624             if (widget->inherits("QTipLabel"))
  2617     QTest::mouseMove(view.viewport(), view.mapFromScene(proxy->mapToScene(proxy->boundingRect().center())));
  2625                 foundTipLabel = true;
  2618     QTest::qWait(3000);
  2626         }
  2619 
  2627         QVERIFY(foundView);
  2620     QCOMPARE(proxy->childItems().count(), 1);
  2628         QVERIFY(!foundTipLabel);
  2621     QGraphicsProxyWidget *child = (QGraphicsProxyWidget*)(proxy->childItems())[0];
  2629     }
  2622     QVERIFY(child->isWidget());
  2630 
  2623     QVERIFY(child->widget());
  2631     {
  2624     QCOMPARE(child->widget()->parent(), static_cast<QObject*>(widget));
  2632         QHelpEvent helpEvent(QEvent::ToolTip, view.mapFromScene(proxy->boundingRect().center()),
  2625     QCOMPARE(child->widget()->x(), widget->x()); // ### ???
  2633                              view.viewport()->mapToGlobal(view.mapFromScene(proxy->boundingRect().center())));
  2626     QCOMPARE(child->widget()->y(), widget->y() + widget->height()); // ### ???
  2634         QApplication::sendEvent(view.viewport(), &helpEvent);
       
  2635         QTest::qWait(350);
       
  2636 
       
  2637         bool foundView = false;
       
  2638         bool foundTipLabel = false;
       
  2639         foreach (QWidget *widget, QApplication::topLevelWidgets()) {
       
  2640             if (widget == &view)
       
  2641                 foundView = true;
       
  2642             if (widget->inherits("QTipLabel"))
       
  2643                 foundTipLabel = true;
       
  2644         }
       
  2645         QVERIFY(foundView);
       
  2646         QVERIFY(foundTipLabel);
       
  2647     }
  2627 }
  2648 }
  2628 
  2649 
  2629 void tst_QGraphicsProxyWidget::childPos_data()
  2650 void tst_QGraphicsProxyWidget::childPos_data()
  2630 {
  2651 {
  2631     QTest::addColumn<bool>("moveCombo");
  2652     QTest::addColumn<bool>("moveCombo");
  3346     View view(&scene);
  3367     View view(&scene);
  3347     view.show();
  3368     view.show();
  3348 #ifdef Q_WS_X11
  3369 #ifdef Q_WS_X11
  3349     qt_x11_wait_for_window_manager(&view);
  3370     qt_x11_wait_for_window_manager(&view);
  3350 #endif
  3371 #endif
  3351     QTest::qWait(20);
       
  3352     QTRY_VERIFY(view.npaints > 0);
  3372     QTRY_VERIFY(view.npaints > 0);
  3353 
  3373 
  3354     const QRect itemDeviceBoundingRect = proxy->deviceTransform(view.viewportTransform())
  3374     const QRect itemDeviceBoundingRect = proxy->deviceTransform(view.viewportTransform())
  3355                                          .mapRect(proxy->boundingRect()).toRect();
  3375                                          .mapRect(proxy->boundingRect()).toRect();
  3356     const QRegion expectedRegion = itemDeviceBoundingRect.adjusted(-2, -2, 2, 2);
  3376     const QRegion expectedRegion = itemDeviceBoundingRect.adjusted(-2, -2, 2, 2);
  3359     view.paintEventRegion = QRegion();
  3379     view.paintEventRegion = QRegion();
  3360 
  3380 
  3361     // Update and hide.
  3381     // Update and hide.
  3362     proxy->update();
  3382     proxy->update();
  3363     proxy->hide();
  3383     proxy->hide();
  3364     QTest::qWait(50);
  3384     QTRY_COMPARE(view.npaints, 1);
  3365     QCOMPARE(view.npaints, 1);
       
  3366     QCOMPARE(view.paintEventRegion, expectedRegion);
  3385     QCOMPARE(view.paintEventRegion, expectedRegion);
  3367 
  3386 
  3368     proxy->show();
  3387     proxy->show();
  3369     QTest::qWait(50);
  3388     QTest::qWait(50);
  3370     view.npaints = 0;
  3389     view.npaints = 0;
  3371     view.paintEventRegion = QRegion();
  3390     view.paintEventRegion = QRegion();
  3372 
  3391 
  3373     // Update and delete.
  3392     // Update and delete.
  3374     proxy->update();
  3393     proxy->update();
  3375     delete proxy;
  3394     delete proxy;
  3376     QTest::qWait(50);
  3395     QTRY_COMPARE(view.npaints, 1);
  3377     QCOMPARE(view.npaints, 1);
       
  3378     QCOMPARE(view.paintEventRegion, expectedRegion);
  3396     QCOMPARE(view.paintEventRegion, expectedRegion);
  3379 }
  3397 }
  3380 
  3398 
  3381 class InputMethod_LineEdit : public QLineEdit
  3399 class InputMethod_LineEdit : public QLineEdit
  3382 {
  3400 {
  3437         view.show();
  3455         view.show();
  3438 #ifdef Q_WS_X11
  3456 #ifdef Q_WS_X11
  3439         qt_x11_wait_for_window_manager(&view);
  3457         qt_x11_wait_for_window_manager(&view);
  3440 #endif
  3458 #endif
  3441         QApplication::setActiveWindow(&view);
  3459         QApplication::setActiveWindow(&view);
  3442         QTest::qWait(25);
       
  3443         QTRY_COMPARE(QApplication::activeWindow(), &view);
  3460         QTRY_COMPARE(QApplication::activeWindow(), &view);
  3444 
  3461 
  3445         QVERIFY(!proxy->hasFocus());
  3462         QVERIFY(!proxy->hasFocus());
  3446         QVERIFY(!proxy->widget()->hasFocus());
  3463         QVERIFY(!proxy->widget()->hasFocus());
  3447 
  3464