tst_QSplitter::replaceWidget: explicit wait for events
Wait a little bit longer for the visibility and resize events. By directly waiting for the incoming events with QTRY_COMPARE we give the test some more time to deliver the expected events. Also move the check for the expectedResizeCount/expectedPaintCount before the geometry check for better debugging. Task-number: QTBUG-64639 Change-Id: I712e432aa8f8d35dd199adf88af3653009528b57 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
0901d4290f
commit
52f1692b84
@ -658,13 +658,34 @@ public:
|
|||||||
class EventCounterSpy : public QObject
|
class EventCounterSpy : public QObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
EventCounterSpy(QWidget *parentWidget) : QObject(parentWidget)
|
EventCounterSpy(QWidget *obj) : objectToWatch(obj)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
~EventCounterSpy()
|
||||||
|
{
|
||||||
|
removeEventFilter();
|
||||||
|
}
|
||||||
|
|
||||||
|
void installEventFilter()
|
||||||
|
{
|
||||||
|
if (needRemoveEventFilter)
|
||||||
|
return;
|
||||||
|
needRemoveEventFilter = true;
|
||||||
|
qApp->installEventFilter(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeEventFilter()
|
||||||
|
{
|
||||||
|
if (!needRemoveEventFilter)
|
||||||
|
return;
|
||||||
|
needRemoveEventFilter = false;
|
||||||
|
qApp->removeEventFilter(this);
|
||||||
|
}
|
||||||
|
|
||||||
bool eventFilter(QObject *watched, QEvent *event) override
|
bool eventFilter(QObject *watched, QEvent *event) override
|
||||||
{
|
{
|
||||||
// Watch for events in the parent widget and all its children
|
// Watch for events in the parent widget and all its children
|
||||||
if (watched == parent() || watched->parent() == parent()) {
|
if (watched == objectToWatch || watched->parent() == objectToWatch) {
|
||||||
if (event->type() == QEvent::Resize)
|
if (event->type() == QEvent::Resize)
|
||||||
resizeCount++;
|
resizeCount++;
|
||||||
else if (event->type() == QEvent::Paint)
|
else if (event->type() == QEvent::Paint)
|
||||||
@ -676,6 +697,8 @@ public:
|
|||||||
|
|
||||||
int resizeCount = 0;
|
int resizeCount = 0;
|
||||||
int paintCount = 0;
|
int paintCount = 0;
|
||||||
|
bool needRemoveEventFilter = false;
|
||||||
|
QObject *objectToWatch;
|
||||||
};
|
};
|
||||||
|
|
||||||
void tst_QSplitter::replaceWidget_data()
|
void tst_QSplitter::replaceWidget_data()
|
||||||
@ -733,31 +756,30 @@ void tst_QSplitter::replaceWidget()
|
|||||||
// to set a shorter label.
|
// to set a shorter label.
|
||||||
QLabel *newWidget = new QLabel(QLatin1String("<b>NEW</b>"));
|
QLabel *newWidget = new QLabel(QLatin1String("<b>NEW</b>"));
|
||||||
|
|
||||||
EventCounterSpy *ef = new EventCounterSpy(&sp);
|
EventCounterSpy ef(&sp);
|
||||||
qApp->installEventFilter(ef);
|
ef.installEventFilter();
|
||||||
const QWidget *res = sp.replaceWidget(index, newWidget);
|
const QWidget *res = sp.replaceWidget(index, newWidget);
|
||||||
QTest::qWait(100); // Give visibility and resizing some time
|
QTest::qWait(100); // Give visibility and resizing some time
|
||||||
qApp->removeEventFilter(ef);
|
|
||||||
|
|
||||||
// Check
|
// Check
|
||||||
if (index < 0 || index >= count) {
|
if (index < 0 || index >= count) {
|
||||||
QVERIFY(!res);
|
QVERIFY(!res);
|
||||||
QVERIFY(!newWidget->parentWidget());
|
QVERIFY(!newWidget->parentWidget());
|
||||||
QCOMPARE(ef->resizeCount, 0);
|
QCOMPARE(ef.resizeCount, 0);
|
||||||
QCOMPARE(ef->paintCount, 0);
|
QCOMPARE(ef.paintCount, 0);
|
||||||
} else {
|
} else {
|
||||||
QCOMPARE(res, oldWidget);
|
QCOMPARE(res, oldWidget);
|
||||||
QVERIFY(!res->parentWidget());
|
QVERIFY(!res->parentWidget());
|
||||||
QVERIFY(!res->isVisible());
|
QVERIFY(!res->isVisible());
|
||||||
|
const int expectedResizeCount = visible ? 1 : 0; // new widget only
|
||||||
|
const int expectedPaintCount = visible && !collapsed ? 2 : 0; // splitter and new widget
|
||||||
|
QTRY_COMPARE(ef.resizeCount, expectedResizeCount);
|
||||||
|
QTRY_COMPARE(ef.paintCount, expectedPaintCount);
|
||||||
QCOMPARE(newWidget->parentWidget(), &sp);
|
QCOMPARE(newWidget->parentWidget(), &sp);
|
||||||
QCOMPARE(newWidget->isVisible(), visible);
|
QCOMPARE(newWidget->isVisible(), visible);
|
||||||
if (visible && !collapsed)
|
if (visible && !collapsed)
|
||||||
QCOMPARE(newWidget->geometry(), oldGeom);
|
QCOMPARE(newWidget->geometry(), oldGeom);
|
||||||
QCOMPARE(newWidget->size().isEmpty(), !visible || collapsed);
|
QCOMPARE(newWidget->size().isEmpty(), !visible || collapsed);
|
||||||
const int expectedResizeCount = visible ? 1 : 0; // new widget only
|
|
||||||
const int expectedPaintCount = visible && !collapsed ? 2 : 0; // splitter and new widget
|
|
||||||
QCOMPARE(ef->resizeCount, expectedResizeCount);
|
|
||||||
QCOMPARE(ef->paintCount, expectedPaintCount);
|
|
||||||
delete res;
|
delete res;
|
||||||
}
|
}
|
||||||
QCOMPARE(sp.count(), count);
|
QCOMPARE(sp.count(), count);
|
||||||
@ -797,25 +819,24 @@ void tst_QSplitter::replaceWidgetWithSplitterChild()
|
|||||||
const QList<int> sizes = sp.sizes();
|
const QList<int> sizes = sp.sizes();
|
||||||
QWidget *sibling = srcIndex == -1 ? (new QLabel("<b>NEW</b>", &sp)) : sp.widget(srcIndex);
|
QWidget *sibling = srcIndex == -1 ? (new QLabel("<b>NEW</b>", &sp)) : sp.widget(srcIndex);
|
||||||
|
|
||||||
EventCounterSpy *ef = new EventCounterSpy(&sp);
|
EventCounterSpy ef(&sp);
|
||||||
qApp->installEventFilter(ef);
|
ef.installEventFilter();
|
||||||
const QWidget *res = sp.replaceWidget(dstIndex, sibling);
|
const QWidget *res = sp.replaceWidget(dstIndex, sibling);
|
||||||
QTest::qWait(100); // Give visibility and resizing some time
|
QTest::qWait(100); // Give visibility and resizing some time
|
||||||
qApp->removeEventFilter(ef);
|
|
||||||
|
|
||||||
QVERIFY(!res);
|
QVERIFY(!res);
|
||||||
if (srcIndex == -1) {
|
if (srcIndex == -1) {
|
||||||
// Create and replace before recalc. The sibling is scheduled to be
|
// Create and replace before recalc. The sibling is scheduled to be
|
||||||
// added after replaceWidget(), when QSplitter receives a child event.
|
// added after replaceWidget(), when QSplitter receives a child event.
|
||||||
QVERIFY(ef->resizeCount > 0);
|
QTRY_VERIFY(ef.resizeCount > 0);
|
||||||
QVERIFY(ef->paintCount > 0);
|
QTRY_VERIFY(ef.paintCount > 0);
|
||||||
QCOMPARE(sp.count(), count + 1);
|
QCOMPARE(sp.count(), count + 1);
|
||||||
QCOMPARE(sp.sizes().mid(0, count), sizes);
|
QCOMPARE(sp.sizes().mid(0, count), sizes);
|
||||||
QCOMPARE(sp.sizes().last(), sibling->width());
|
QCOMPARE(sp.sizes().last(), sibling->width());
|
||||||
} else {
|
} else {
|
||||||
// No-op for the rest
|
// No-op for the rest
|
||||||
QCOMPARE(ef->resizeCount, 0);
|
QCOMPARE(ef.resizeCount, 0);
|
||||||
QCOMPARE(ef->paintCount, 0);
|
QCOMPARE(ef.paintCount, 0);
|
||||||
QCOMPARE(sp.count(), count);
|
QCOMPARE(sp.count(), count);
|
||||||
QCOMPARE(sp.sizes(), sizes);
|
QCOMPARE(sp.sizes(), sizes);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user