Fix QWasmWindowStack::topWindow when the stack is empty

The method should by contract return nullptr when the stack is empty,
but it crashes in this case.

Also, unit-test the case.

Pick-to: 6.4
Change-Id: If64b71e761efd9a5cd5af407cd68cba7f8dbc8e2
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Mikolaj Boc 2022-09-12 10:43:07 +02:00
parent 0c1368c7e4
commit 91d7dcea11
2 changed files with 28 additions and 1 deletions

View File

@ -111,7 +111,7 @@ QWasmWindow *QWasmWasmWindowStack::rootWindow() const
QWasmWindow *QWasmWasmWindowStack::topWindow() const
{
return m_windowStack.last();
return m_windowStack.empty() ? nullptr : m_windowStack.last();
}
QWasmWasmWindowStack::StorageType::iterator QWasmWasmWindowStack::regularWindowsBegin()

View File

@ -36,6 +36,7 @@ private slots:
void lowering();
void removing();
void removingTheRoot();
void clearing();
private:
void onTopWindowChanged()
@ -241,5 +242,31 @@ void tst_QWasmWindowStack::removingTheRoot()
getWindowsFrontToBack(&stack).begin()));
}
void tst_QWasmWindowStack::clearing()
{
QWasmWasmWindowStack stack(m_mockCallback);
stack.pushWindow(&m_root);
stack.pushWindow(&m_window1);
// Window order: 1 R
clearCallbackCounter();
QCOMPARE(&m_window1, stack.topWindow());
m_onTopLevelChangedAction = [this, &stack]() { QVERIFY(stack.topWindow() == &m_root); };
stack.removeWindow(&m_window1);
// Window order: R
verifyTopWindowChangedCalled();
QCOMPARE(&m_root, stack.topWindow());
m_onTopLevelChangedAction = [&stack]() { QVERIFY(stack.topWindow() == nullptr); };
stack.removeWindow(&m_root);
// Window order: <empty>
verifyTopWindowChangedCalled();
QCOMPARE(nullptr, stack.topWindow());
QCOMPARE(0u, stack.size());
}
QTEST_MAIN(tst_QWasmWindowStack)
#include "tst_qwasmwindowstack.moc"