Widget autotests: replace deprecated QWidget::repaint() calls

Replace QWidget::repaint() with update() + wait until the paint event is
received.

Task-number: QTBUG-80237
Change-Id: I57da7cd8fa119344484b849a88729bca7b48616c
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-11-23 20:29:48 +01:00
parent 64760504e7
commit e70fe301d9
6 changed files with 75 additions and 80 deletions

View File

@ -8297,20 +8297,14 @@ void tst_QGraphicsItem::sorting()
_paintedItems.clear(); _paintedItems.clear();
view.viewport()->repaint(); view.viewport()->update();
#if defined(Q_OS_MAC)
// There's no difference between repaint and update on the Mac,
// so we have to process events here to make sure we get the event.
QTest::qWait(100);
#endif
const GraphicsItems expected{grid[0][0], grid[0][1], grid[0][2], grid[0][3], const GraphicsItems expected{grid[0][0], grid[0][1], grid[0][2], grid[0][3],
grid[1][0], grid[1][1], grid[1][2], grid[1][3], grid[1][0], grid[1][1], grid[1][2], grid[1][3],
grid[2][0], grid[2][1], grid[2][2], grid[2][3], grid[2][0], grid[2][1], grid[2][2], grid[2][3],
grid[3][0], grid[3][1], grid[3][2], grid[3][3], grid[3][0], grid[3][1], grid[3][2], grid[3][3],
grid[4][0], grid[4][1], grid[4][2], grid[4][3], grid[4][0], grid[4][1], grid[4][2], grid[4][3],
item1, item2}; item1, item2};
QCOMPARE(_paintedItems, expected); QTRY_COMPARE(_paintedItems, expected);
} }
void tst_QGraphicsItem::itemHasNoContents() void tst_QGraphicsItem::itemHasNoContents()
@ -8337,13 +8331,7 @@ void tst_QGraphicsItem::itemHasNoContents()
_paintedItems.clear(); _paintedItems.clear();
view.viewport()->repaint(); view.viewport()->update();
#ifdef Q_OS_MAC
// There's no difference between update() and repaint() on the Mac,
// so we have to process events here to make sure we get the event.
QTest::qWait(10);
#endif
QTRY_COMPARE(_paintedItems, GraphicsItems{item2}); QTRY_COMPARE(_paintedItems, GraphicsItems{item2});
} }

View File

@ -358,13 +358,13 @@ void tst_QGraphicsView::renderHints()
QCOMPARE(item->hints, 0); QCOMPARE(item->hints, 0);
view.show(); view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view)); QVERIFY(QTest::qWaitForWindowExposed(&view));
view.repaint(); view.update();
QTRY_COMPARE(item->hints, view.renderHints()); QTRY_COMPARE(item->hints, view.renderHints());
view.setRenderHints(QPainter::Antialiasing); view.setRenderHints(QPainter::Antialiasing);
QCOMPARE(view.renderHints(), QPainter::Antialiasing); QCOMPARE(view.renderHints(), QPainter::Antialiasing);
view.repaint(); view.update();
QTRY_COMPARE(item->hints, view.renderHints()); QTRY_COMPARE(item->hints, view.renderHints());
} }
@ -2631,13 +2631,12 @@ void tst_QGraphicsView::optimizationFlags()
class MessUpPainterItem : public QGraphicsRectItem class MessUpPainterItem : public QGraphicsRectItem
{ {
public: public:
MessUpPainterItem(const QRectF &rect) : QGraphicsRectItem(rect), dirtyPainter(false) using QGraphicsRectItem::QGraphicsRectItem;
{ } bool dirtyPainter = false;
bool receivedPaintEvent = false;
bool dirtyPainter;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *w) void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *w)
{ {
receivedPaintEvent = true;
dirtyPainter = (painter->pen().color() != w->palette().color(w->foregroundRole())); dirtyPainter = (painter->pen().color() != w->palette().color(w->foregroundRole()));
painter->setPen(Qt::red); painter->setPen(Qt::red);
} }
@ -2675,18 +2674,22 @@ void tst_QGraphicsView::optimizationFlags_dontSavePainterState()
QGraphicsView view(&scene); QGraphicsView view(&scene);
view.show(); view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view)); QVERIFY(QTest::qWaitForWindowExposed(&view));
view.viewport()->repaint(); parent->receivedPaintEvent = false;
child->receivedPaintEvent = false;
view.viewport()->update();
QTRY_VERIFY(parent->receivedPaintEvent);
QTRY_VERIFY(child->receivedPaintEvent);
QVERIFY(!parent->dirtyPainter); QVERIFY(!parent->dirtyPainter);
QVERIFY(!child->dirtyPainter); QVERIFY(!child->dirtyPainter);
view.setOptimizationFlags(QGraphicsView::DontSavePainterState); view.setOptimizationFlags(QGraphicsView::DontSavePainterState);
view.viewport()->repaint(); parent->receivedPaintEvent = false;
child->receivedPaintEvent = false;
view.viewport()->update();
#ifdef Q_OS_MAC QTRY_VERIFY(parent->receivedPaintEvent);
// Repaint on OS X actually does require spinning the event loop. QTRY_VERIFY(child->receivedPaintEvent);
QTest::qWait(100);
#endif
QVERIFY(!parent->dirtyPainter); QVERIFY(!parent->dirtyPainter);
QVERIFY(child->dirtyPainter); QVERIFY(child->dirtyPainter);
@ -2753,7 +2756,7 @@ void tst_QGraphicsView::optimizationFlags_dontSavePainterState2()
QVERIFY(QTest::qWaitForWindowExposed(&view)); QVERIFY(QTest::qWaitForWindowExposed(&view));
// Make sure the view is repainted; otherwise the tests below will fail. // Make sure the view is repainted; otherwise the tests below will fail.
view.viewport()->repaint(); view.viewport()->update();
QTRY_VERIFY(view.painted); QTRY_VERIFY(view.painted);
// Make sure the painter's world transform is preserved after drawItems. // Make sure the painter's world transform is preserved after drawItems.
@ -4732,14 +4735,12 @@ void tst_QGraphicsView::QTBUG_5859_exposedRect()
QGraphicsView view(&scene); QGraphicsView view(&scene);
view.scale(4.15, 4.15); view.scale(4.15, 4.15);
view.showNormal(); view.showNormal();
qApp->setActiveWindow(&view); QApplication::setActiveWindow(&view);
QVERIFY(QTest::qWaitForWindowExposed(&view)); QVERIFY(QTest::qWaitForWindowExposed(&view));
QVERIFY(QTest::qWaitForWindowActive(&view)); QVERIFY(QTest::qWaitForWindowActive(&view));
view.viewport()->repaint(10,10,20,20); view.viewport()->update(10,10,20,20);
QApplication::processEvents(); QTRY_COMPARE(item.lastExposedRect, scene.lastBackgroundExposedRect);
QCOMPARE(item.lastExposedRect, scene.lastBackgroundExposedRect);
} }
#ifndef QT_NO_CURSOR #ifndef QT_NO_CURSOR

View File

@ -1186,7 +1186,7 @@ void tst_QGraphicsWidget::layoutDirection()
for (int i = 0; i < children.count(); ++i) { for (int i = 0; i < children.count(); ++i) {
QTRY_COMPARE(children[i]->layoutDirection(), layoutDirection); QTRY_COMPARE(children[i]->layoutDirection(), layoutDirection);
QTRY_COMPARE(children[i]->testAttribute(Qt::WA_SetLayoutDirection), false); QTRY_COMPARE(children[i]->testAttribute(Qt::WA_SetLayoutDirection), false);
view->repaint(); view->update();
QTRY_COMPARE(children[i]->m_painterLayoutDirection, layoutDirection); QTRY_COMPARE(children[i]->m_painterLayoutDirection, layoutDirection);
} }
} }

View File

@ -77,6 +77,30 @@ static void initStandardTreeModel(QStandardItemModel *model)
model->insertRow(2, item); model->insertRow(2, item);
} }
class TreeView : public QTreeView
{
Q_OBJECT
public:
using QTreeView::QTreeView;
using QTreeView::selectedIndexes;
void paintEvent(QPaintEvent *event) override
{
QTreeView::paintEvent(event);
wasPainted = true;
}
bool wasPainted = false;
public slots:
void handleSelectionChanged()
{
//let's select the last item
QModelIndex idx = model()->index(0, 0);
selectionModel()->select(QItemSelection(idx, idx), QItemSelectionModel::Select);
disconnect(selectionModel(), &QItemSelectionModel::selectionChanged,
this, &TreeView::handleSelectionChanged);
}
};
class tst_QTreeView : public QObject class tst_QTreeView : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -2980,7 +3004,7 @@ void tst_QTreeView::evilModel()
{ {
QFETCH(bool, visible); QFETCH(bool, visible);
// init // init
QTreeView view; TreeView view;
EvilModel model; EvilModel model;
view.setModel(&model); view.setModel(&model);
view.setVisible(visible); view.setVisible(visible);
@ -3018,7 +3042,7 @@ void tst_QTreeView::evilModel()
view.scrollTo(thirdLevel); view.scrollTo(thirdLevel);
model.change(); model.change();
view.repaint(); view.update(); // will not do anything since view is not visible
model.change(); model.change();
QTest::mouseDClick(view.viewport(), Qt::LeftButton); QTest::mouseDClick(view.viewport(), Qt::LeftButton);
@ -3175,7 +3199,7 @@ void tst_QTreeView::filterProxyModelCrash()
QSortFilterProxyModel proxy; QSortFilterProxyModel proxy;
proxy.setSourceModel(&model); proxy.setSourceModel(&model);
QTreeView view; TreeView view;
view.setModel(&proxy); view.setModel(&proxy);
view.show(); view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view)); QVERIFY(QTest::qWaitForWindowExposed(&view));
@ -3184,7 +3208,8 @@ void tst_QTreeView::filterProxyModelCrash()
QTest::qWait(20); QTest::qWait(20);
proxy.invalidate(); proxy.invalidate();
view.repaint(); //used to crash view.update(); //used to crash
QTRY_VERIFY(view.wasPainted);
} }
void tst_QTreeView::renderToPixmap_data() void tst_QTreeView::renderToPixmap_data()
@ -3652,10 +3677,7 @@ void tst_QTreeView::task220298_selectColumns()
} }
}; };
class TreeView : public QTreeView { TreeView view;
public:
using QTreeView::selectedIndexes;
} view;
Model model; Model model;
view.setModel(&model); view.setModel(&model);
view.show(); view.show();
@ -4004,20 +4026,6 @@ void tst_QTreeView::task254234_proxySort()
QCOMPARE(view.model()->data(view.model()->index(1, 1)).toString(), QString::fromLatin1("g")); QCOMPARE(view.model()->data(view.model()->index(1, 1)).toString(), QString::fromLatin1("g"));
} }
class TreeView : public QTreeView
{
Q_OBJECT
public slots:
void handleSelectionChanged()
{
//let's select the last item
QModelIndex idx = model()->index(0, 0);
selectionModel()->select(QItemSelection(idx, idx), QItemSelectionModel::Select);
disconnect(selectionModel(), &QItemSelectionModel::selectionChanged,
this, &TreeView::handleSelectionChanged);
}
};
void tst_QTreeView::task248022_changeSelection() void tst_QTreeView::task248022_changeSelection()
{ {
//we check that changing the selection between the mouse press and the mouse release //we check that changing the selection between the mouse press and the mouse release

View File

@ -8385,12 +8385,9 @@ void tst_QWidget::resizeInPaintEvent()
widget.resizeInPaintEvent = true; widget.resizeInPaintEvent = true;
// This will call resize in the paintEvent, which in turn will call // This will call resize in the paintEvent, which in turn will call
// invalidateBackingStore() and a new update request should be posted. // invalidateBackingStore() and a new update request should be posted.
widget.repaint(); // the resize triggers another update.
QCOMPARE(widget.numPaintEvents, 1); widget.update();
widget.numPaintEvents = 0; QTRY_COMPARE(widget.numPaintEvents, 2);
// Make sure the resize triggers another update.
QTRY_COMPARE(widget.numPaintEvents, 1);
} }
void tst_QWidget::opaqueChildren() void tst_QWidget::opaqueChildren()
@ -8559,8 +8556,8 @@ void tst_QWidget::immediateRepaintAfterInvalidateBackingStore()
// The entire widget is already dirty, but this time we want to update immediately // The entire widget is already dirty, but this time we want to update immediately
// by calling repaint(), and thus we have to repaint the widget and not wait for // by calling repaint(), and thus we have to repaint the widget and not wait for
// the UpdateRequest to be sent when we get back to the event loop. // the UpdateRequest to be sent when we get back to the event loop.
widget->repaint(); widget->update();
QCOMPARE(widget->numPaintEvents, 1); QTRY_COMPARE(widget->numPaintEvents, 1);
} }
#endif #endif
@ -9832,7 +9829,7 @@ public:
if (!static_cast<QWidgetPrivate*>(d_ptr.data())->maybeRepaintManager()) { if (!static_cast<QWidgetPrivate*>(d_ptr.data())->maybeRepaintManager()) {
static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->repaintManager.reset(new QWidgetRepaintManager(this)); static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->repaintManager.reset(new QWidgetRepaintManager(this));
static_cast<QWidgetPrivate*>(d_ptr.data())->invalidateBackingStore(this->rect()); static_cast<QWidgetPrivate*>(d_ptr.data())->invalidateBackingStore(this->rect());
repaint(); update();
} }
} }
}; };
@ -9855,7 +9852,7 @@ void tst_QWidget::scrollWithoutBackingStore()
scrollable.scroll(-25,-25); scrollable.scroll(-25,-25);
QCOMPARE(child.pos(),QPoint(25,25)); QCOMPARE(child.pos(),QPoint(25,25));
scrollable.enableBackingStore(); scrollable.enableBackingStore();
QCOMPARE(child.pos(),QPoint(25,25)); QTRY_COMPARE(child.pos(),QPoint(25,25));
} }
#endif #endif

View File

@ -77,24 +77,22 @@ using namespace QTestPrivate;
class StyleOptionTestStyle : public QCommonStyle class StyleOptionTestStyle : public QCommonStyle
{ {
private:
bool readOnly;
public: public:
inline StyleOptionTestStyle() : QCommonStyle(), readOnly(false) bool readOnly = false;
{ mutable bool wasDrawn = false;
}
inline void setReadOnly(bool readOnly) using QCommonStyle::QCommonStyle;
void setReadOnly(bool readOnly)
{ {
this->readOnly = readOnly; this->readOnly = readOnly;
} }
inline void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *, void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *,
const QWidget *) const const QWidget *) const override
{ {
switch (pe) { switch (pe) {
case PE_PanelLineEdit: case PE_PanelLineEdit:
wasDrawn = true;
if (readOnly) if (readOnly)
QVERIFY(opt->state & QStyle::State_ReadOnly); QVERIFY(opt->state & QStyle::State_ReadOnly);
else else
@ -3271,19 +3269,22 @@ void tst_QLineEdit::readOnlyStyleOption()
QLineEdit *testWidget = ensureTestWidget(); QLineEdit *testWidget = ensureTestWidget();
bool wasReadOnly = testWidget->isReadOnly(); bool wasReadOnly = testWidget->isReadOnly();
QStyle *oldStyle = testWidget->style(); QStyle *oldStyle = testWidget->style();
testWidget->show();
QTRY_VERIFY(QTest::qWaitForWindowExposed(testWidget));
StyleOptionTestStyle myStyle; StyleOptionTestStyle myStyle;
testWidget->setStyle(&myStyle); testWidget->setStyle(&myStyle);
myStyle.setReadOnly(true); myStyle.setReadOnly(true);
testWidget->setReadOnly(true); testWidget->setReadOnly(true);
testWidget->repaint(); testWidget->update();
qApp->processEvents(); QTRY_VERIFY(myStyle.wasDrawn);
myStyle.wasDrawn = false;
testWidget->setReadOnly(false); testWidget->setReadOnly(false);
myStyle.setReadOnly(false); myStyle.setReadOnly(false);
testWidget->repaint(); testWidget->update();
qApp->processEvents(); QTRY_VERIFY(myStyle.wasDrawn);
testWidget->setReadOnly(wasReadOnly); testWidget->setReadOnly(wasReadOnly);
testWidget->setStyle(oldStyle); testWidget->setStyle(oldStyle);